Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
L = [[int(i) for i in input().split()], [-int(i) for i in input().split()]]
c = 10**15
for i in range(2):
A = L[i]
if A[0] != 0:
ans = 0
S = A[0]
f = A[0]//abs(A[0])
else:
ans = 1
S = 1
f = 1
for a in A[1:]:
S += a
if S == 0:
ans += 1
S = -f
else:
if S/abs(S) != f*(-1):
ans += abs(S)+1
S = -f
f *= -1
c = min(ans, c)
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<long long> S;
vector<long long> A;
int j;
bool is_plus;
long long ans = 0;
long long sum = 0;
cin >> n;
S.push_back(0);
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
A.push_back(a);
}
for (j = 0; j < n; j++) {
if (abs(A[j])) {
break;
}
}
if (j == n) {
cout << A.size() * 2 - 1 << endl;
return 0;
}
if (j) {
ans += (j + 1) * 2 - 1;
sum = (A[j] > 0) ? -1 : 1;
} else {
sum = 0;
ans = 0;
}
for (int i = j; i < n; i++) {
if (!i) {
sum = A[i];
continue;
}
bool is_plus = sum > 0;
sum += A[i];
if (sum == 0) {
ans += 1;
sum = is_plus ? -1 : 1;
} else if (is_plus == (sum > 0)) {
ans += abs(sum) + 1;
sum = is_plus ? -1 : 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long int L = 1e5 + 5;
vector<long long int> a(L);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int cur = a[0];
long long int ans = 0;
if (cur == 0) {
ans++;
a[0]++;
}
for (long long int i = 1; i < n; i++) {
if (cur * (cur + a[i]) >= 0) {
if (cur + a[i] == 0) {
if (cur < 0) {
a[i]++;
} else {
a[i]--;
}
ans++;
} else {
if (cur < 0) {
long long int temp = a[i];
a[i] = 1 - cur;
ans += abs(temp - a[i]);
} else {
long long int temp = a[i];
a[i] = -1 - cur;
ans += abs(temp - a[i]);
}
}
}
cur += a[i];
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
seq = [int(x) for x in input().split()]
# first : positive case
a_sum = seq[0]
op = 0
if a_sum > 0:
pass
else:
a_sum = 1
op += abs(a_sum) + 1
for a in seq[1:]:
tmp = a_sum + a
if tmp * a_sum < 0:
a_sum = tmp
elif a_sum < 0:
#diff = 1 - a_sum - a
a_sum = 1
op += abs(tmp) + 1
elif a_sum > 0:
#diff = -1 - a_sum - a
a_sum = -1
op += abs(tmp) + 1
op1 = op
# first : negative case
a_sum = seq[0]
op = 0
if a_sum < 0:
pass
else:
a_sum = -1
op += abs(a_sum) + 1
for a in seq[1:]:
tmp = a_sum + a
if tmp * a_sum < 0:
a_sum = tmp
elif a_sum < 0:
#diff = 1 - a_sum - a
a_sum = 1
op += abs(tmp) + 1
elif a_sum > 0:
#diff = -1 - a_sum - a
a_sum = -1
op += abs(tmp) + 1
op2 = op
print(min(op1, op2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
vector<long long> v(N);
bool check = false;
for (int i = 0; i < N; i++) {
cin >> v[i];
if (v[0] < 0) check = true;
if (check) v[i] = -v[i];
}
vector<long long> a(N), b(N);
int cntA = 0, cntB = 0;
if (v[0] == 0) {
a[0] = 1;
b[0] = -1;
cntA++;
cntB++;
} else {
a[0] = v[0];
b[0] = -1;
cntB += v[0] + 1;
}
for (int i = 1; i < N; i++) {
long long tmp_a = a[i - 1] + v[i];
if (i % 2 == 0 && tmp_a <= 0) {
a[i] = 1;
cntA += abs(tmp_a) + 1;
} else if (i % 2 == 1 && tmp_a >= 0) {
a[i] = -1;
cntA += tmp_a + 1;
} else {
a[i] = tmp_a;
}
long long tmp_b = b[i - 1] + v[i];
if (i % 2 == 0 && tmp_b >= 0) {
b[i] = -1;
cntB += tmp_b + 1;
} else if (i % 2 == 1 && tmp_b <= 0) {
b[i] = 1;
cntB += abs(tmp_b) + 1;
} else {
b[i] = tmp_b;
}
}
cout << min(cntA, cntB) << 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;
void solve() {
int n, total, total2, cnt, cnt2;
total2 = total = cnt2 = cnt = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
int ai;
cin >> ai;
total2 += ai;
total += ai;
if (i % 2 == 0) {
if (total <= 0) {
cnt += -total + 1;
total = 1;
}
if (total2 >= 0) {
cnt2 += total2 + 1;
total2 = -1;
}
} else {
if (total >= 0) {
cnt += total + 1;
total = -1;
}
if (total2 <= 0) {
cnt2 += -total2 + 1;
total2 = 1;
}
}
}
cout << min(cnt, cnt2);
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int i, a, n, num;
long long int sum = 0, bsum = 0, ans = 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++;
} while (num >= 0);
sum = -1;
}
if (sum == 0) {
ans++;
sum = -1;
}
}
if (bsum < 0) {
if (sum < 0) {
num = sum;
do {
num++;
ans++;
} while (num <= 0);
sum = 1;
}
if (sum == 0) {
ans++;
sum = 1;
}
}
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
# define INF (1 < <29)
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD;
#define MODADDSET(d) if ((d) >= MOD) d -= MOD;
#define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD;
//defines
#define FILE_IO freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
#define sc1(a,type) type a; cin>>a;
#define sc2(a,b,type) type a,b; cin>>a>>b;
#define sc3(a, b, c,type) type a,b,c; cin>>a>>b>>c;
#define sc4(a, b, c, d,type) type a ,b,c,d; cin>>a>>b>>c>>d;
#define nl cout<<"\n";
#define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin(); v != (c).rend(); ++v)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
#define re(i,b) for(int i=0;i<int(b);i++)
#define re1(i,b) for(int i=1;i<=int(b);i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(),c.rend()
#define mpresent(container, element) (container.find(element) != container.end()) //for map,set..etc (returns true/false value)
#define vpresent(container, element) (find(all(container),element) != container.end()) //for vectors,strings,list,deque (returns true/false value)
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ins insert
#define F first
#define S second
#define clr clear()
#define sz(x) ((int)x.size())
#define dt distance
#define test(t) int t; cin>>t; while(t--)
#define csb(i) __builtin_popcount(i)
#define csbll(i) __builtin_popcountll(i)
#define clz(x) __builtin_clz(x)
#define clzl(x) __builtin_clzl(x)
#define cp(x) __builtin_parity(x)
#define adv(v,num) advance(v,num)//used for lists and other structures that use iterators,when you can't access elements randomly ( iterator moves num positions)
#define mod 1000000007
#define MAX_ARR 1000000
#define v2d(rowsize,colsize,type,name) vector<vector<type>> name(rowsize,vector<type>(colsize));
#define digits_in(i) (ll)log10(i)+1 // gives no of digits in a number
#define sqr(x) (x)*(x)
//does not apply for i==0 , add an excetion contition for n==0 ( cust return count 1 for that inseted of using this function)
//typedef
typedef string str;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<str> vs;
typedef vector<char> vc;
typedef pair<int,int> pii;
typedef pair<str,int> psi;
typedef pair<int,str> pis;
typedef vector<pii> vii;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef map<int,str> mis;
typedef unordered_map<int,int> umii;
typedef unordered_map<str,int> umsi;
typedef unordered_map<int,str> umis;
typedef unordered_map<str,str> umss;
typedef unordered_map<char,int> umci;
typedef set<str> ss;
typedef set<int> si;
typedef unordered_set<str> uss;
typedef unordered_set<int> usi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define debug(args...)
#endif
int main(){fastio
#ifndef ONLINE_JUDGE
FILE_IO
#endif
vi v;
test(t){
int temp;cin>>temp;
v.pb(temp);
}
int ct=0;
re(i,sz(v)-1){
debug(v[i] ,v[i]+v[i+1]);
if( (v[i]<0 && v[i]+v[i+1]<0) || (v[i]>0 && v[i]+v[i+1]>0 || v[i]+v[i+1]==0) ){
if( v[i]>0 && v[i]+v[i+1]>0){
ct+=v[i]+v[i+1]+1;
}
else if(v[i]<0 && v[i]+v[i+1]<0 ){
ct+=abs(v[i]+v[i+1])+1;
}
else{
ct+=1;
}
v[i+1]= v[i]>0?-1:1;
}
else{
v[i+1]+=v[i];
}
debug(ct);
}
cout<<ct;
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;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long prevArraySum = a[0];
long long currentArraySum = a[0];
long long res = 0;
if (a[0] == 0) {
res = 1;
prevArraySum = 1;
currentArraySum = 1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
long long res1 = res;
res = 1;
prevArraySum = -1;
currentArraySum = -1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
res = min(res, res1);
} else {
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
A = a[0]
if A != 0:
ans = 0
for i in range(1, n):
nextA = A + a[i]
if (A > 0 and nextA < 0) or (A < 0 and nextA > 0):
A = nextA
elif nextA == 0 and A > 0:
ans += 1
A = -1
elif nextA == 0 and A < 0:
ans += 1
A = 1
elif A > 0:
ans += abs(nextA) + 1
A = -1
else:
ans += abs(nextA) + 1
A = 1
print(ans)
sys.exit()
# ans1 = 1
# A1 = 1
# for i in range(1, n):
# nextA = A1 + a[i]
# if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
# A1 = nextA
# elif nextA == 0 and A1 > 0:
# ans1 += 1
# A1 = -1
# elif nextA == 0 and A1 < 0:
# ans1 += 1
# A1 = 1
# elif A1 > 0:
# ans1 += abs(nextA) + 1
# A1 = -1
# else:
# ans1 += abs(nextA) + 1
# A1 = 1
# ans2 = 1
# A2 = -1
# for i in range(1, n):
# nextA = A2 + a[i]
# if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
# A2 = nextA
# elif nextA == 0 and A2 > 0:
# ans2 += 1
# A2 = -1
# elif nextA == 0 and A2 < 0:
# ans2 += 1
# A2 = 1
# elif A2 > 0:
# ans2 += abs(nextA) + 1
# A2 = -1
# else:
# ans2 += abs(nextA) + 1
# A2 = 1
# print(min(ans1, ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a.at(i);
}
int ans1 = 0, ans2 = 0, sum = 0;
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (i % 2 == 0 and sum <= 0) {
ans1 += -sum + 1;
sum = 1;
} else if (i % 2 == 1 and sum >= 0) {
ans1 += sum + 1;
sum = -1;
}
}
sum = 0;
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (i % 2 == 0 and sum >= 0) {
ans2 += sum + 1;
sum = -1;
} else if (i % 2 == 1 and sum <= 0) {
ans2 += -sum + 1;
sum = 1;
}
}
int ans = min(ans1, ans2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
using namespace std;
bool cmp_P(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return a.second < b.second;
}
int main() {
long long int tmp = 0, n, sum = 0, v, res = 0;
cin >> n;
vector<long long int> a(n + 1);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
if (a[0] != 0) {
v = abs(a[0]) / a[0];
sum = a[0];
for (int i = 1; i < n; i++) {
if (v == -1) {
if (a[i] + sum <= 0) {
res += abs(1 - a[i] - sum);
sum = 1;
} else {
sum += a[i];
}
} else {
if (a[i] + sum >= 0) {
res += abs(-1 - a[i] - sum);
sum = -1;
} else {
sum += a[i];
}
}
v = -v;
}
} else {
v = 1;
tmp++;
sum = 1;
for (int i = 1; i < n; i++) {
if (v == -1) {
if (a[i] + sum <= 0) {
tmp += abs(1 - a[i] - sum);
sum = 1;
} else {
sum += a[i];
}
} else {
if (a[i] + sum >= 0) {
tmp += abs(-1 - a[i] - sum);
sum = -1;
} else {
sum += a[i];
}
}
v = -v;
}
v = 1;
res++;
sum = 1;
for (int i = 1; i < n; i++) {
if (v == -1) {
if (a[i] + sum <= 0) {
res += abs(1 - a[i] - sum);
sum = 1;
} else {
sum += a[i];
}
} else {
if (a[i] + sum >= 0) {
res += abs(-1 - a[i] - sum);
sum = -1;
} else {
sum += a[i];
}
}
v = -v;
}
res = min(res, tmp);
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
def solve(n):
ans = 0
cusm = []
if n == -1 and a[0] > 0:
cusm.append(-1)
ans += abs(-1 - a[0])
elif n == -1 and a[0] < 0:
cusm.append(1)
ans += abs(1 - a[0])
else:
cusm.append(a[0])
for i, ai in enumerate(a):
if i == 0: continue
if (cusm[-1] + ai) * cusm[-1] < 0: #符号が逆なら
cusm.append(cusm[-1] + ai)
elif cusm[-1] > 0: #操作の必要があって前が正なら
ans += abs(-1 - (cusm[-1] + ai))
cusm.append(-1)
else: #操作の必要があって前が負なら
ans += abs(1 - (cusm[-1] + ai))
cusm.append(1)
return ans
print(min(solve(1),solve(-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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n; cin >> n;
vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i];
vector<int> a1; a1 = a;
long long int sum1 = 0, ans1 = ;
for (int i = 1; i <= n; i++) {
sum1 += a1[i];
if (i % 2 == 1 && sum1 <= 0) {
int plus = 1 - sum1;
sum1 = 1;
ans1 += plus;
continue;
} if (i % 2 == 0 && sum1 >= 0) {
int minus = 1 + sum1;
sum1 = -1;
ans1 += minus;
}
}
vector<int> a2; a2 = a;
long long int sum2 = 0, ans2 = 0;
for (int i = 1; i <= n; i++) {
sum2 += a2[i];
if (i % 2 == 1 && sum2 >= 0) {
int minus = 1 + sum2;
sum2 = -1;
ans2 += minus;
continue;
} else if (i % 2 == 0 && sum2 <= 0) {
int plus = 1 - sum2;
sum2 = 1;
ans2 += plus;
}
}
cout << min(ans1, ans2) << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN |
use std::io::*;
use std::str::FromStr;
pub fn read<T: FromStr>() -> T {
let stdin = stdin();
let stdin = stdin.lock();
let token: String = stdin
.bytes()
.map(|c| c.expect("failed to read char") as char)
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect();
token.parse().ok().expect("failed to parse token")
}
use std::cmp::{max, min};
use std::collections::BTreeMap;
fn main() {
let n = read::<i64>();
let mut vec_a = vec![];
for i in 0..n {
vec_a.push(read::<i64>());
}
let mut prev_sum = vec_a[0];
let mut ans = 0;
if prev_sum != 0 {
for i in 1..vec_a.len() {
let b = vec_a[i as usize];
if 0 < prev_sum {
if 0 <= prev_sum + b {
ans += (1 + prev_sum).abs() + b;
prev_sum = -1;
} else {
prev_sum += b;
}
} else if prev_sum < 0 {
if prev_sum + b <= 0 {
ans += (1 - prev_sum).abs() - b;
prev_sum = 1;
} else {
prev_sum += b;
}
}
}
} else {
prev_sum = 1;
ans = 1;
for i in 1..vec_a.len() {
let b = vec_a[i as usize];
if 0 < prev_sum {
if 0 <= prev_sum + b {
ans += (1 + prev_sum).abs() + b;
prev_sum = -1;
} else {
prev_sum += b;
}
} else if prev_sum < 0 {
if prev_sum + b <= 0 {
ans += (1 - prev_sum).abs() - b;
prev_sum = 1;
} else {
prev_sum += b;
}
}
}
let plus_min = ans;
prev_sum = -1;
ans = 1;
for i in 1..vec_a.len() {
let b = vec_a[i as usize];
if 0 < prev_sum {
if 0 <= prev_sum + b {
ans += (1 + prev_sum).abs() + b;
prev_sum = -1;
} else {
prev_sum += b;
}
} else if prev_sum < 0 {
if prev_sum + b <= 0 {
ans += (1 - prev_sum).abs() - b;
prev_sum = 1;
} else {
prev_sum += b;
}
}
}
let minus_min = ans;
ans = min(plus_min, minus_min)
}
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;
signed main() {
int N;
cin >> N;
vector<long long> A(N);
for (int i = 0, i_len = (N); i < i_len; ++i) cin >> A[i];
long long ans = LLONG_MAX;
int sig[2] = {1, -1};
for (int j = 0, j_len = (2); j < j_len; ++j) {
long long sum = 0;
long long count = 0;
for (int i = 0, i_len = (N); i < i_len; ++i) {
sum += A[i];
if (sig[i ^ j & 1] * sum >= 0) {
count += llabs(sum) + 1;
sum = -sig[i ^ j & 1];
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
vector<long long int> v;
int solve(int first_pon) {
long long int pon = first_pon, sum = 0, result = 0;
for (int i = 0; (i) < (n); i++) {
sum += v[i];
if (pon > 0 && sum >= 0) {
result += sum + 1;
sum = -1;
} else if (pon < 0 && sum <= 0) {
result += (-sum) + 1;
sum = 1;
}
if (sum > 0) {
pon = 1;
} else {
pon = -1;
}
}
return result;
}
int main() {
cin >> n;
v = vector<long long int>(n);
for (int i = 0; (i) < (n); i++) {
cin >> v[i];
}
cout << min(solve(1), solve(-1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = [int(_) for _ in input().split()]
ans = 0
zeroflag = True
zerocnt = 0
tmp = 0
for i in a:
if zeroflag and i == 0:
zerocnt += 1
elif zeroflag and i != 0:
zeroflag = False
if zerocnt != 0 and abs(i) == 1:
ans += 1
tmp += np.sign(i)*2
elif zerocnt != 0:
tmp = i - np.sign(i)
else:
tmp = i
else:
if np.sign(tmp+i) == np.sign(tmp) or np.sign(tmp+i) == 0:
ans += abs((tmp+i) + np.sign(tmp))
tmp = -np.sign(tmp)
else:
tmp += i
if zerocnt != 0:
ans += 2*zerocnt - 1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(a) for a in input().split()]
def f(times, prefix_sum):
for i in range(1, n):
if prefix_sum < 0:
if prefix_sum + a[i] > 0:
prefix_sum += a[i]
else:
times += 1 - (prefix_sum + a[i])
prefix_sum = 1
elif prefix_sum > 0:
if prefix_sum + a[i] < 0:
prefix_sum += a[i]
else:
times += abs(-1 - (prefix_sum + a[i]))
prefix_sum = -1
return times
t1 = 0
p1 = a[0]
t2 = 0
if a[0] > 0:
t2 += abs(-1 - a[0])
p2 = -1
elif a[0] < 0:
t2 += 1 - a[0]
p2 = 1
else: # a[0] == 0
t1 = 1
p1 = -1
t2 = 1
p2 = 1
print(min(f(t1, p1), t2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
a = gets.split.map &:to_i
count = 0
temp = 0
a.each{|ai|
dif = 0
unless (temp + ai) * temp < 0 || temp == 0
dif = (temp + ai).abs+1
count += dif
ai += dif * (-temp/temp)
end
temp += ai
}
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 = [int(i) for i in input().split()]
def pura(a):
A = a[:]
ans = 0
check = 0
if A[0] <= 0:
ans += 1 + abs(A[0])
A[0] = 1
check += A[0]
for i in range(1, N):
if i % 2 != 0:
if check + A[i] >= 0:
ans += abs(A[i] + 1 + check)
A[i] = -1 + check
else:
if check + A[i] <= 0:
ans += abs(A[i] - (1 + check))
A[i] = 1 - check
check += A[i]
return ans
def mai(a):
A = a[:]
ans = 0
check = 0
if A[0] >= 0:
ans +=1 + abs(A[0])
A[0] = -1
check += A[0]
for i in range(1, N):
if i % 2 != 0:
if check + A[i] <= 0:
ans += abs(A[i] - (1 - check))
A[i] = 1 - check
else:
if check + A[i] >= 0:
ans += abs(A[i] +( 1 + check))
A[i] = -1 - check
check += A[i]
return ans
print(min(pura(a), mai(a))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(void) {
ll n;
cin >> n;
vector<ll> a(n);
for (auto& it : a) cin >> it;
ll total = 0;
ll ans1 = 0;
for (ll i = 0; i < n; i++) {
if (total > 0) {
if (total + a[i] >= 0) {
ans1 += abs(-total - 1 - a[i]);
a[i] = -total - 1;
}
} else {
if (total + a[i] <= 0) {
ans1 += abs(-total + 1 - a[i]);
a[i] = -total + 1;
}
}
total += a[i];
}
total = 0;
ll ans2 = 0;
for (ll i = 0; i < n; i++) {
if (total >= 0) {
if (total + a[i] >= 0) {
ans2 += abs(-total - 1 - a[i]);
a[i] = -total - 1;
}
} else {
if (total + a[i] <= 0) {
ans2 += abs(-total + 1 - a[i]);
a[i] = -total + 1;
}
}
total += a[i];
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), es(n), os(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
es[0] = os[0] = a[0];
int ecnt = 0, ocnt = 0;
for (int i = 0; i < n; i++) {
if (i != 0) es[i] = es[i - 1] + a[i];
if (i % 2 == 0 && es[i] <= 0) {
ecnt += abs(1 - es[i]);
es[i] = 1;
} else if (i % 2 != 0 && es[i] >= 0) {
ecnt += abs(-1 - es[i]);
es[i] = -1;
}
}
for (int i = 0; i < n; i++) {
if (i != 0) os[i] = os[i - 1] + a[i];
cout << os[i] << " ";
if (i % 2 == 0 && os[i] >= 0) {
ocnt += abs(-1 - os[i]);
os[i] = -1;
} else if (i % 2 != 0 && os[i] <= 0) {
ocnt += abs(1 - os[i]);
os[i] = 1;
}
}
cout << (ecnt < ocnt ? ecnt : ocnt) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
if (a < b) a = b;
}
signed main() {
int n;
cin >> n;
long long ans = 0;
long long sum = 0;
bool fl = 0;
for (int i = 0; i < (n); i++) {
int a;
cin >> a;
if (i == 0) {
if (a == 0) {
fl = 1;
break;
}
sum += a;
continue;
}
if (0 < sum) {
if (0 < sum + a) {
ans += sum + a + 1;
sum = -1;
} else if (sum + a == 0) {
ans++;
sum = -1;
} else {
sum += a;
}
} else {
if (0 > sum + a) {
ans += abs(sum + a) + 1;
sum = 1;
} else if (sum + a == 0) {
ans++;
sum = 1;
} else {
sum += a;
}
}
}
if (fl) {
long long sum1 = 1, sum2 = -1;
ans++;
int ans2 = 1;
for (int i = 0; i < (n - 1); i++) {
int a;
cin >> a;
if (0 < sum1) {
if (0 < sum1 + a) {
ans += sum1 + a + 1;
sum1 = -1;
} else if (sum1 + a == 0) {
ans++;
sum1 = -1;
} else {
sum1 += a;
}
} else {
if (0 > sum1 + a) {
ans += abs(sum1 + a) + 1;
sum1 = 1;
} else if (sum1 + a == 0) {
ans++;
sum1 = 1;
} else {
sum1 += a;
}
}
if (0 < sum2) {
if (0 < sum2 + a) {
ans2 += sum2 + a + 1;
sum2 = -1;
} else if (sum2 + a == 0) {
ans2++;
sum2 = -1;
} else {
sum2 += a;
}
} else {
if (0 > sum2 + a) {
ans2 += abs(sum2 + a) + 1;
sum2 = 1;
} else if (sum2 + a == 0) {
ans2++;
sum2 = 1;
} else {
sum2 += a;
}
}
}
chmax(ans, ans2);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sum = a[0]
count = 0
for i in range(1, n) :
if sum == 0 :
count += 1
if a[i] > 0 :
sum = -1
elif a[i] < 0 :
sum = 1
temp = sum
sum += a[i]
if temp > 0 and sum > 0 :
count += sum + 1
sum = -1
elif temp < 0 and sum < 0 :
count += -sum + 1
sum = 1
if sum == 0 :
count += 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 | #include <bits/stdc++.h>
int main(void) {
int j = 1, n, a, i, sum = 0, ope = 0;
scanf("%d%d", &n, &a);
if (a < 0) j = 0;
sum += a;
for (i = 0; i < n - 1; i++) {
scanf("%d", &a);
sum += a;
if (sum >= 0 && j % 2 == 1) {
ope += sum + 1;
sum = -1;
} else if (sum <= 0 && j % 2 == 0) {
ope += 1 - sum;
sum = 1;
}
j++;
}
printf("%d\n", ope);
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;
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
int sum = 0;
int plus = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (sum + a[i] < 1) {
plus += 1 - (sum + a[i]);
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] > -1) {
plus += 1 + (sum + a[i]);
sum = -1;
} else
sum += a[i];
}
}
sum = 0;
int minus = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 1) {
minus += 1 - (sum + a[i]);
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] > -1) {
minus += 1 + (sum + a[i]);
sum = -1;
} else
sum += a[i];
}
}
cout << min(plus, minus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
vector<int> cumulative;
void reCalc(int index, int value) {
for (int i = index; i < n; ++i) cumulative[i] -= value;
}
int main() {
cin >> n;
vector<int> input;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
input.push_back(a);
}
auto itr = input.begin();
for (int i = 0; i < n; ++i)
cumulative.push_back(accumulate(itr, itr + i + 1, 0));
int ans = 0;
bool isPositive = cumulative[0] > 0;
for (int i = 1; i < n; ++i) {
if (isPositive) {
if (cumulative[i] > 0) {
ans += cumulative[i] + 1;
reCalc(i, cumulative[i] + 1);
} else if (cumulative[i] == 0) {
++ans;
reCalc(i, 1);
}
} else {
if (cumulative[i] < 0) {
int diff = (0 - cumulative[i]) + 1;
ans += diff;
reCalc(i, -diff);
} else if (cumulative[i] == 0) {
++ans;
reCalc(i, -1);
}
}
isPositive = cumulative[i] > 0;
}
if (accumulate(cumulative.begin(), cumulative.end(), 0) == 0)
cout << 1 << endl;
else
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define VIEW(x) do {cerr << #x << ": "; for(auto i : x) cerr << i << " "; cerr << endl;} while(0)
#define ALL(x) (x).begin(),(x).end()
template<class T>bool umax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool umin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
template<typename A,size_t N,typename T> void FILL(A (&array)[N],const T &val){fill((T*)array,(T*)(array+N),val);}
template<typename T> void FILL(vector<T> &v, const T &x) {fill(v.begin(), v.end(), x);}
template<typename T> void FILL(vector<vector<T>> &v, const T &x) {for(auto &i:v)fill(i.begin(), i.end(), x);}
int main() {
int n; cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
ll ans_p = 0;
ll sum_p = 0;
rep(i, n) {
if(i%2) { // -
if(sum_p + a[i] < 0) {
sum_p += a[i];
}
else {
ans_p += (sum_p+a[i]) + 1;
sum_p = -1;
}
}
else { // +
if(sum_p + a[i] > 0) {
sum_p += a[i];
}
else {
ans_p += abs(sum_p+a[i]) + 1;
sum_p = 1;
}
}
}
ll ans_m = 0;
ll sum_m = 0;
rep(i, n) {
if(!(i%2)) { // +
if(sum_m + a[i] < 0) {
sum_m += a[i];
}
else {
ans_m += (sum_m+a[i]) + 1;
sum_m = -1;
}
}
else { // -
if(sum_m + a[i] > 0) {
sum_m += a[i];
}
else {
ans_m += abs(sum_m+a[i]) + 1;
sum_m = 1;
}
}
}
cout << min(ans_m, ans_p) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class A>
void pr(A a) {
cout << (a) << endl;
}
template <class A, class B>
void pr(A a, B b) {
cout << a << " ";
pr(b);
}
template <class A, class B, class C>
void pr(A a, B b, C c) {
cout << a << " ";
pr(b, c);
}
template <class A, class B, class C, class D>
void pr(A a, B b, C c, D d) {
cout << a << " ";
pr(b, c, d);
}
template <class T>
inline bool chmin(T& a, T b) {
return a > b ? a = b, true : false;
}
template <class T>
inline bool chmax(T& a, T b) {
return a < b ? a = b, true : false;
}
struct BIT {
vector<long long> bit;
int n;
BIT(int n) : n(n), bit(vector<long long>(n + 1, 0)) {}
long long sum(int i) {
long long s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x) {
while (i <= n) {
bit[i] += (long long)x;
i += i & -i;
}
}
int lower_bound(long long x) {
long long ret = 0;
long long k = 1;
while (2 * k <= n) k <<= 1;
for (; k > 0; k >>= 1) {
if (ret + k < n && bit[ret + k] < x) {
x -= bit[ret + k];
ret += k;
}
}
return ret + 1;
}
int upper_bound(long long x) {
long long ret = 0;
long long k = 1;
while (2 * k <= n) k <<= 1;
for (; k > 0; k >>= 1) {
if (ret + k < n && bit[ret + k] <= x) {
x -= bit[ret + k];
ret += k;
}
}
return ret + 1;
}
long long between(int i, int j) {
if (i > j) return 0;
return sum(j) - sum(i - 1);
}
};
int main(void) {
int n;
cin >> n;
BIT a(n);
BIT b(n);
for (int i = 0; i < n; i++) {
long long ai;
cin >> ai;
a.add(i + 1, ai);
b.add(i + 1, ai);
}
long long ans = 0;
for (int i = 0; i < int(n); ++i) {
auto a_sum = a.sum(i + 1);
if ((i & 1) == 0) {
if (a_sum > 0) {
a.add(i + 1, -(abs(a_sum) + 1));
ans += abs(a_sum) + 1;
} else if (a_sum == 0) {
a.add(i + 1, -1);
ans += 1;
}
} else {
if (a_sum < 0) {
a.add(i + 1, (abs(a_sum) + 1));
ans += abs(a_sum) + 1;
} else if (a_sum == 0) {
a.add(i + 1, 1);
ans += 1;
}
}
}
long long ans1 = 0;
for (int i = 0; i < int(n); ++i) {
auto b_sum = b.sum(i + 1);
if ((i & 1) == 1) {
if (b_sum > 0) {
b.add(i + 1, -(abs(b_sum) + 1));
ans1 += abs(b_sum) + 1;
} else if (b_sum == 0) {
b.add(i + 1, 1);
ans += 1;
}
} else {
if (b_sum < 0) {
b.add(i + 1, (abs(b_sum) + 1));
ans1 += abs(b_sum) + 1;
} else if (b_sum == 0) {
b.add(i + 1, -1);
ans += 1;
}
}
}
pr(min(ans, ans1));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Inf = 1e9;
const double EPS = 1e-9;
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int bitCount(long bits) {
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int ans = Inf;
int cnt = 0;
vector<int> a(n), b(n);
for (int i = 0; i < (int)n; ++i) {
cin >> a[i];
b[i] = a[i];
}
int sum = a[0];
for (int i = 1; i < (int)n; ++i) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
int diff = sum + 1;
cnt += diff;
sum = -1;
} else if (i % 2 == 0 && sum <= 0) {
int diff = 1 - sum;
cnt += diff;
sum = 1;
}
}
ans = min(ans, cnt);
cnt = 0;
sum = b[0];
for (int i = 1; i < (int)n; ++i) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
int diff = sum + 1;
cnt += diff;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
int diff = 1 - sum;
cnt += diff;
sum = 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 | cpp | #include <bits/stdc++.h>
using namespace std;
long long solve(long long *a, int n) {
long long count = 0;
long long calc = 0;
int state, pstate;
if (a[0] < 0) state = -1;
if (a[0] > 0) state = 1;
for (int i = 1; i < n; i++) {
pstate = state;
int tmp = a[i] + calc;
if (tmp < 0) state = -1;
if (tmp == 0) state = 0;
if (tmp > 0) state = 1;
if (pstate == state) {
if (state == -1) {
count += 1 - tmp;
calc += 1 - tmp;
state = 1;
} else if (state == 1) {
count += tmp + 1;
calc += -1 - tmp;
state = -1;
}
}
if (state == 0) {
if (pstate == -1) {
count += 1;
calc += 1;
state = 1;
} else if (pstate == 1) {
count += 1;
calc += -1;
state = -1;
}
}
}
return count;
}
int main() {
int n;
long long ans;
long long *a;
cin >> n;
a = new long long[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) a[i] = a[i - 1] + a[i];
if (a[0] == 0) {
long long bs, cs;
long long *b = new long long[n];
long long *c = new long long[n];
for (int i = 0; i < n; i++) b[i] = a[i] + 1;
for (int i = 0; i < n; i++) c[i] = a[i] - 1;
bs = solve(b, n);
cs = solve(c, n);
ans = bs < cs ? bs : cs;
} else
ans = solve(a, n);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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;
l[i] += ((p > 0) ? -1 : 1) * c;
}
}
System.out.println(count);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
num = 0
for i in range(len(a_s)):
a = a_s[i]
new_sum_i = sum_i + a
# print(f"a:{a}, tmp_sum_i:{new_sum_i}")
if (new_sum_i == 0):
if sum_i == 0:
new_sum_i = 1 if a_s[i+1] == 0 else np.sign(a_s[i+1])*(-1)
else:
new_sum_i = np.sign(sum_i)*(-1)
adjusted_a = new_sum_i - sum_i
num += abs(max(adjusted_a, a) - min(adjusted_a, a))
sum_i = new_sum_i
elif new_sum_i * sum_i > 0:
new_sum_i = np.sign(new_sum_i)*-1
adjusted_a = new_sum_i - sum_i
num += abs(max(adjusted_a, a) - min(adjusted_a, a))
sum_i = new_sum_i
else:
sum_i = new_sum_i
# print(f"a:{a}, new_sum_i:{sum_i}, num:{num}")
# print()
print(num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int pos = 0;
int neg = 0;
int tmpsum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
tmpsum = tmpsum + s[i];
if (tmpsum <= 0) {
pos = pos + 1 - tmpsum;
tmpsum = 1;
}
} else {
tmpsum = tmpsum + s[i];
if (tmpsum >= 0) {
pos = pos + tmpsum + 1;
tmpsum = -1;
}
}
}
tmpsum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
tmpsum = tmpsum + s[i];
if (tmpsum >= 0) {
neg = neg + 1 + tmpsum;
tmpsum = -1;
}
} else {
tmpsum = tmpsum + s[i];
if (tmpsum <= 0) {
neg = neg - tmpsum + 1;
tmpsum = 1;
}
}
}
if (pos > neg) {
cout << neg;
} else {
cout << pos;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int INF = 2000000000;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long ans = 0;
long long wa;
if (a[0] != 0) {
wa = a[0];
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa < 0)
continue;
else {
ans += wa + 1;
wa = -1;
}
} else {
wa += a[i];
if (wa > 0)
continue;
else {
ans += 1 - wa;
wa = 1;
}
}
}
cout << ans << endl;
} else {
long long ans1 = 0, ans2 = 0;
wa = 1;
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa < 0)
continue;
else {
ans1 += wa + 1;
wa = -1;
}
} else {
wa += a[i];
if (wa > 0)
continue;
else {
ans1 += 1 - wa;
wa = 1;
}
}
}
wa = -1;
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa < 0)
continue;
else {
ans2 += wa + 1;
wa = -1;
}
} else {
wa += a[i];
if (wa > 0)
continue;
else {
ans2 += 1 - wa;
wa = 1;
}
}
}
if (ans1 < ans2)
cout << ans1 << endl;
else
cout << ans2 << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
int main() {
int n;
cin >> n;
vector<ll> a(n);
REP(i,n) {
ll b;
cin >> b;
a[i] = b;
}
vector<ll> a2(a);
// a[0] > 0
ll res1 = 0, sum1 = 0;
REP(i, n) {
if(i%2 == 0) {
if (sum1 + a[i] <= 0) {
ll dif = -(sum1 + a[i]) + 1;
res1 += dif;
a[i] += dif;
}
sum1 += a[i];
} else {
if (sum1 + a[i] >= 0) {
ll dif = (sum1 + a[i]) + 1;
res1 += dif;
a[i] -= dif;
}
sum1 += a[i];
}
}
ll res2 = 0, sum2 = 0;
REP(i, n) {
if(i%2 == 1) {
if (sum2 + a2[i] <= 0) {
ll dif = -(sum2 + a2[i]) + 1;
res2 += dif;
a2[i] += dif;
}
sum2 += a2[i];
} else {
if (sum2 + a2[i] >= 0) {
ll dif = (sum2 + a2[i]) + 1;
res2 += dif;
a2[i] -= dif;
}
sum2 += a2m[i];
}
}
cout << min(res1, res2) << 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() {
int64_t n;
cin >> n;
int64_t count = 0;
int64_t Acount = 0;
int64_t Bcount = 0;
int64_t sum = 0;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
int Ah = 0;
int Bh = 0;
int64_t sumA = 0;
int64_t sumB = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
Acount++;
for (int i = 0; i < n - 1; i++) {
sumA += a.at(i);
Ah = 0;
Bh = 0;
for (;;) {
sumB = sumA + a.at(i + 1);
if (sumA > 0)
Ah = 1;
else
Ah = -1;
if (sumB > 0)
Bh = 1;
else if (sumB < 0)
Bh = -1;
else
Bh = 0;
if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1))
break;
else if (Ah == 1 && Bh != -1) {
a.at(i + 1) -= abs(sumB) + 1;
Acount += abs(sumB) + 1;
break;
} else if (Ah == -1 && Bh != 1) {
a.at(i + 1) += abs(sumB) + 1;
Acount += abs(sumB) + 1;
break;
}
}
}
a.at(0) = -1;
Bcount++;
for (int i = 0; i < n - 1; i++) {
sumA += a.at(i);
Ah = 0;
Bh = 0;
for (;;) {
sumB = sumA + a.at(i + 1);
if (sumA > 0)
Ah = 1;
else
Ah = -1;
if (sumB > 0)
Bh = 1;
else if (sumB < 0)
Bh = -1;
else
Bh = 0;
if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1))
break;
else if (Ah == 1 && Bh != -1) {
a.at(i + 1) -= abs(sumB) + 1;
Bcount += abs(sumB) + 1;
break;
} else if (Ah == -1 && Bh != 1) {
a.at(i + 1) += abs(sumB) + 1;
Bcount += abs(sumB) + 1;
break;
}
}
}
if (Acount > Bcount)
cout << Bcount << endl;
else
cout << Acount << endl;
} else {
for (int i = 0; i < n - 1; i++) {
sumA += a.at(i);
Ah = 0;
Bh = 0;
for (;;) {
sumB = sumA + a.at(i + 1);
if (sumA > 0)
Ah = 1;
else
Ah = -1;
if (sumB > 0)
Bh = 1;
else if (sumB < 0)
Bh = -1;
else
Bh = 0;
if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1))
break;
else if (Ah == 1 && Bh != -1) {
a.at(i + 1) -= abs(sumB) + 1;
count += abs(sumB) + 1;
break;
} else if (Ah == -1 && Bh != 1) {
a.at(i + 1) += abs(sumB) + 1;
count += abs(sumB) + 1;
break;
}
}
}
cout << count << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
if a[0] == 0:
flag = 1
a[0] = 1
ans += 1
elif a[0] > 0:
flag = 1
else:
flag = -1
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
dp[i] = dp[i - 1] + a[i]
if dp[i] == 0:
dp[i] = flag * -1
ans += 1
elif flag == 1 and dp[i] > 0:
ans += abs(-1 - dp[i])
dp[i] = -1
elif flag == -1 and dp[i] < 0:
ans += abs(1 - dp[i])
dp[i] = 1
flag *= -1
print(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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int min_ans = INT_MAX;
for (int mod = 0; mod < 2; ++mod) {
int ans = 0;
int sum = 0;
for (int i = 0; i < n; ++i) {
int sign = ((i % 2) == mod) * -2 + 1;
sum += a[i];
if (sign * sum <= 0) {
int diff = sign - sum;
sum += diff;
ans += abs(diff);
}
}
min_ans = min(min_ans, ans);
}
cout << min_ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n;
long long rp(long long *a, long long sum, long long cnt) {
for (int j = 1; j < n; j++) {
if (sum > 0 && sum + a[j] < 0) {
sum += a[j];
continue;
}
if (sum < 0 && sum + a[j] > 0) {
sum += a[j];
continue;
}
if (sum < 0 && sum + a[j] <= 0) {
long long dt = 1 - (sum + a[j]);
cnt += dt;
a[j] += dt;
sum += a[j];
continue;
}
if (sum > 0 && sum + a[j] >= 0) {
long long dt = (sum + a[j]) - (-1);
cnt += dt;
a[j] -= dt;
sum += a[j];
continue;
}
}
return cnt;
}
int main() {
cin >> n;
long long a[int(1e5) + 5];
long long b[int(1e5) + 5];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
long long cnt = 0;
long long sum = 0;
long long result;
if (a[0] > 0) {
cnt += a[0] + 1;
sum = -1;
long long ans1 = rp(a, sum, cnt);
sum = b[0];
cnt = 0;
long long ans2 = rp(b, sum, cnt);
result = min(ans1, ans2);
} else {
sum = 1;
cnt = a[0] + 1;
long long ans1 = rp(a, sum, cnt);
sum = b[0];
cnt = 0;
long long ans2 = rp(b, sum, cnt);
result = min(ans1, ans2);
}
cout << result << 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 k(n, a, p):
x = 0
c = 0
for i in range(n):
s = x + a[i]
if (s > 0) == p: x = s
else:
c += abs(s) + 1
if p: x = 1
else: x = -1
p = not p
return c
n = int(input())
a = [int(x) for x in input().split()]
print(min(k(n, a, True), k(n, a, False)))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long table[100005];
int main() {
long long n, ans;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> table[i];
}
ans = 0;
if (table[0] >= 0) {
for (int i = 0; i < n; i++) {
if (i > 0) table[i] += table[i - 1];
if (i % 2 == 0 && table[i] <= 0) {
ans += 1 - table[i];
table[i] = 1;
}
if (i % 2 == 1 && table[i] >= 0) {
ans += table[i] + 1;
table[i] = -1;
}
}
} else {
for (int i = 0; i < n; i++) {
if (i > 0) table[i] += table[i - 1];
if (i % 2 == 0 && table[i] >= 0) {
ans += table[i] + 1;
table[i] = -1;
}
if (i % 2 == 1 && table[i] <= 0) {
ans += 1 - table[i];
table[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;
static const long long maxLL = (long long)1 << 62;
long long a[100001] = {};
long long s[100001] = {};
int main() {
long long n;
cin >> n;
long long cnt = 0;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
if (i > 1) {
if (s[i] == 0) {
s[i] = s[i - 1] * -1;
cnt++;
} else if (i > 1 && s[i - 1] * s[i] > 0) {
cnt += abs(s[i]) + 1;
if (s[i] > 0)
s[i] -= abs(s[i]) + 1;
else if (s[i] < 0)
s[i] += abs(s[i]) + 1;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, no, ans;
int main() {
cin >> n;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
if (i == 0) {
no = a;
} else {
if (no > 0) {
no += a;
if (no >= 0) {
ans += no + 1;
no = -1;
}
} else {
no += a;
if (no <= 0) {
ans += no * -1 + 1;
no = 1;
}
}
}
cout << no << " " << ans << 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 | python3 | n = int(input())
a = list(map(int,input().split()))
A = a[0]
for i in range(1,n):
num = abs(A)+1
if A*(A+a[i])<0:
A += a[i]
elif A+a[i]==0:
if A>0:
A += a[i]-num
ans += num
else:
A += a[i]+num
ans += num
elif A*(A+a[i])>0:
if A>0:
A += a[i]-num
ans += num
else:
A += a[i]+num
ans += num
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
private static final int mod =(int)1e9+7;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
long sum=a[0];
long operations=0;
if(a.length==1) {
if(a[0]!=0) {
System.out.println(0);
}else {
System.out.println(1);
}
}else {
if(sum==0) {
if(a.length>=2&&sum+a[1]>0)
sum--;
else
sum++;
operations++;
}
for(int i=1;i<n;i++) {
if(sum>0) {
if(sum+a[i]<0) {
sum+=a[i];
}else {
if(sum+a[i]==0) {
sum+=a[i]-1;
operations++;
}else {
long req=(long)-1-1l*sum;
sum=-1;
operations+=(-1l*req+a[i]);
}
}
}else {
if(sum+a[i]>0) {
sum+=a[i];
}else {
if(sum+a[i]==0) {
sum+=a[i]+1;
operations++;
}else {
long req=(long)1+-1l*sum;
sum=1;
operations+=(req-a[i]);
}
}
}
}
System.out.println(operations);
}
}
static boolean vis[]=new boolean[10001];
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
static int f(int arr[], int n)
{
int result = n;
int max=-1;
int ans=0;
for (int element: arr){
if(vis[element]==false)
result = gcd(n, element);
if(result>max) {
max=result;
ans=element;
}
}
return ans;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import std.stdio, std.conv, std.algorithm, std.range, std.array, std.string, std.uni, std.bigint, std.math; void main() { auto n = readln.chomp.to!uint; auto an = readln.split.to!(int[]); auto sum = 0; auto cnt = 0; foreach (a; an) { if (sum != 0 && (sum + a) * sum >= 0) { auto na = -sum - sgn(sum); cnt += abs(na - a); a = na; }_ sum += a; } writeln(cnt); } |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def func(a, n, x, a0)
res=x
sum=a0
(1...n).each do |i|
t=sum+a[i]
if t==0 || (sum>0)==(t>0)
t=sum>0 ? -1 :1
res+=(t-sum-a[i]).abs
end
sum=t
end
res
end
n, *a=`dd`.split.map &:to_i
puts (a[0]==0 ? [func(a, n, 1, 1), func(a, n, 1, -1)].min : func(a, n, 0, a[0]))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
A = ()
A_append = A.append
cnt = 0
for i in range(n-1):
A_append(a[i])
x = sum(A) + a[i+1]
if sum(A) > 0 and x > 0:
y = abs(x)+1
cnt += y
a[i+1] -= y
elif sum(A) < 0 and x < 0:
y = abs(sum(A) - a[i+1])-1
cnt += y
a[i+1] += y
if sum(a) == 0:
cnt += 1
print(cnt)
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long a, sum1 = 0, sum2 = 0, ans1 = 0, ans2 = 0;
for (int i = 1; i <= N; i++) {
cin >> a;
sum1 += a;
sum2 += a;
if (i % 2 != 0) {
if (sum1 < 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
ans2 += 1 + sum2;
sum2 = -1;
}
} else {
if (sum1 >= 0) {
ans1 += 1 + sum1;
sum1 = -1;
}
if (sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
}
}
cout << 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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
# define INF (1 < <29)
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD;
#define MODADDSET(d) if ((d) >= MOD) d -= MOD;
#define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD;
//defines
#define FILE_IO freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
#define sc1(a,type) type a; cin>>a;
#define sc2(a,b,type) type a,b; cin>>a>>b;
#define sc3(a, b, c,type) type a,b,c; cin>>a>>b>>c;
#define sc4(a, b, c, d,type) type a ,b,c,d; cin>>a>>b>>c>>d;
#define nl cout<<"\n";
#define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin(); v != (c).rend(); ++v)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
#define re(i,b) for(int i=0;i<int(b);i++)
#define re1(i,b) for(int i=1;i<=int(b);i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(),c.rend()
#define mpresent(container, element) (container.find(element) != container.end()) //for map,set..etc (returns true/false value)
#define vpresent(container, element) (find(all(container),element) != container.end()) //for vectors,strings,list,deque (returns true/false value)
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ins insert
#define F first
#define S second
#define clr clear()
#define sz(x) ((int)x.size())
#define dt distance
#define test(t) int t; cin>>t; while(t--)
#define csb(i) __builtin_popcount(i)
#define csbll(i) __builtin_popcountll(i)
#define clz(x) __builtin_clz(x)
#define clzl(x) __builtin_clzl(x)
#define cp(x) __builtin_parity(x)
#define adv(v,num) advance(v,num)//used for lists and other structures that use iterators,when you can't access elements randomly ( iterator moves num positions)
#define mod 1000000007
#define MAX_ARR 1000000
#define v2d(rowsize,colsize,type,name) vector<vector<type>> name(rowsize,vector<type>(colsize));
#define digits_in(i) (ll)log10(i)+1 // gives no of digits in a number
#define sqr(x) (x)*(x)
//does not apply for i==0 , add an excetion contition for n==0 ( cust return count 1 for that inseted of using this function)
//typedef
typedef string str;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<str> vs;
typedef vector<char> vc;
typedef pair<int,int> pii;
typedef pair<str,int> psi;
typedef pair<int,str> pis;
typedef vector<pii> vii;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef map<int,str> mis;
typedef unordered_map<int,int> umii;
typedef unordered_map<str,int> umsi;
typedef unordered_map<int,str> umis;
typedef unordered_map<str,str> umss;
typedef unordered_map<char,int> umci;
typedef set<str> ss;
typedef set<int> si;
typedef unordered_set<str> uss;
typedef unordered_set<int> usi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// #ifndef ONLINE_JUDGE
// #include "debug.h"
// #else
// #define debug(args...)
// #endif
int main(){fastio
// #ifndef ONLINE_JUDGE
// FILE_IO
// #endif
vi v;
test(t){
int temp;cin>>temp;
v.pb(temp);
}
int ct=0;
re(i,sz(v)-1){
// debug(v[i] ,v[i]+v[i+1]);
if( (v[i]<0 && v[i]+v[i+1]<0) || (v[i]>0 && v[i]+v[i+1]>0 || v[i]+v[i+1]==0) ){
if( v[i]>0 && v[i]+v[i+1]>0){
ct+=v[i]+v[i+1]+1;
}
else if(v[i]<0 && v[i]+v[i+1]<0 ){
ct+=abs(v[i]+v[i+1])+1;
}
else{
ct+=1;
}
v[i+1]= v[i]>0?-1:1;
}
else{
v[i+1]+=v[i];
}
// debug(ct);
}
cout<<ct;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (auto &i : a) cin >> i;
int64_t sum = 0;
int64_t cnt = 0;
int sign;
for (int i = 0; i < N; i++) {
if (a.at(i) != 0) {
sign = a.at(i) / abs(a.at(i));
break;
}
if (i == N - 1) {
cout << 2 * N - 1 << endl;
return 0;
}
}
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (sign * sum <= 0) {
cnt += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<ll> v(n), s(n, 0);
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
s[i] = sum;
}
ll c = 0;
ll ans = 0;
for (int i = 1; i < n; i++) {
s[i] += c;
if (s[i - 1] * s[i] > 0) {
ans += abs(s[i]);
c -= s[i];
s[i] -= s[i];
}
if (s[i] == 0) {
if (s[i - 1] < 0) s[i]++, c++, ans++;
if (s[i - 1] > 0) s[i]--, c--, ans++;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int sum = a.at(0);
int count_eve = 0;
int count_odd = 0;
if (a.at(0) <= 0) {
count_eve += -sum + 1;
sum = 1;
}
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
if (sum + a.at(i + 1) < 0) {
sum += a.at(i + 1);
} else {
count_eve += sum + a.at(i + 1) + 1;
sum = -1;
}
} else if (i % 2 == 1) {
if (sum + a.at(i + 1) > 0) {
sum += a.at(i + 1);
} else {
count_eve += -sum - a.at(i + 1) + 1;
sum = 1;
}
}
}
sum = a.at(0);
if (a.at(0) >= 0) {
count_odd += sum + 1;
sum = -1;
}
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
if (sum + a.at(i + 1) > 0) {
sum += a.at(i + 1);
} else {
count_odd += -sum - a.at(i + 1) + 1;
sum = 1;
}
} else if (i % 2 == 1) {
if (sum + a.at(i + 1) < 0) {
sum += a.at(i + 1);
} else {
count_odd += sum + a.at(i + 1) + 1;
sum = -1;
}
}
}
cout << min(count_eve, count_odd);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long a[100006];
long long ans1 = 0;
long long ans2 = 0;
long long sum = 0;
int i;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lld", &a[i]);
sum = a[0];
for (i = 1; i < n; i++) {
if (sum > 0) {
if (sum + a[i] >= 0) {
ans1 += a[i] + sum + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] <= 0) {
ans1 += -sum + 1 - a[i];
sum = 1;
} else {
sum += a[i];
}
}
}
sum = -a[0];
for (i = 1; i < n; i++) {
if (sum > 0) {
if (sum + a[i] >= 0) {
ans2 += a[i] + sum + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] <= 0) {
ans2 += -sum + 1 - a[i];
sum = 1;
} else {
sum += a[i];
}
}
}
printf("%lld\n", min(ans1, ans2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int store[100007];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &store[i]);
int cnt = 0;
int sum = store[1];
for (int i = 2; i <= n; i++) {
int tmp = store[i] + sum;
if (tmp)
if ((tmp < 0 && sum > 0) || (tmp > 0 && sum < 0)) {
sum = tmp;
continue;
}
int need;
if (sum > 0) {
need = -1 - sum, sum = -1;
if (!need) need = -2 - sum, sum = -2;
} else {
need = abs(1 - sum), sum = 1;
if (!need) need = abs(2 - sum), sum = 2;
}
cnt += abs(store[i] - need);
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0
if s0p<=0:
s0p+=(abs(s0p)+1)
countp+=abs(s0p)
if s0n>=0:
s0n-=(abs(s0n)+1)
countn+=abs(s0n)
for i in range(1,n):
s1 = s0p+a[i]
if s0p*s1>=0:
if s1>0:
a[i]-=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1<0:
a[i]+=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1==0:
if s0p>0:
a[i]-=1
countp+=1
elif s0p<0:
a[i]+=1
countp+=1
s0p += a[i]
for i in range(1,n):
s1 = s0n+b[i]
if s0n*s1>=0:
if s1>0:
b[i]-=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1<0:
b[i]+=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1==0:
if s0n>0:
b[i]-=1
countn+=1
elif s0n<0:
b[i]+=1
countn+=1
s0n += b[i]
print(countp if countp<=countn else(countn))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 lli = long long int;
using ulli = unsigned long long int;
vector<lli> N, rN;
lli in, n, d = 0, dp, pm;
ulli ans = 0;
int main() {
cin >> n;
for (lli l = 0; l < n; l++) {
cin >> in;
if (l == 0) {
N.push_back(in);
} else {
N.push_back(N[l - 1] + in);
}
}
for (lli l = 1; l < (lli)N.size(); l++) {
dp = d;
{};
{};
if (N[l - 1] + dp < 0) {
if (N[l] + dp < 0) {
d += 1 - N[l] - dp;
ans += 1 - N[l] - dp;
} else if (N[l] + dp == 0) {
d += 1;
ans += 1;
}
} else if (N[l - 1] + dp > 0) {
if (N[l] + dp > 0) {
d -= N[l] + dp + 1;
ans += N[l] + dp + 1;
} else if (N[l] + dp == 0) {
d -= 1;
ans += 1;
}
} else {
for (lli m = l - 1; m < (lli)N.size(); m++) {
if (N[m] + dp > 0) {
pm = (m - l) % 2;
break;
} else if (N[m] + dp < 0) {
pm = (m - l + 1) % 2;
break;
}
if (m == (lli)N.size() - 1) {
pm = (m + 1) % 2;
break;
}
}
if (pm == 1) {
d += 1;
ans += 1;
} else if (pm == 0) {
d -= 1;
ans += 1;
}
dp = d;
if (N[l] + dp < 0) {
d += 1 - N[l] - dp;
ans += 1 - N[l] - dp;
} else if (N[l] + dp == 0) {
d += 1;
ans += 1;
} else if (N[l] + dp > 0) {
d -= N[l] + dp + 1;
ans += N[l] + dp + 1;
} else if (N[l] + dp == 0) {
d -= 1;
ans += 1;
}
}
{};
{};
{};
{};
{};
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
long n, i, a, sum1, ans1, sum2, ans2;
bool sign1, sign2;
inline void solve() {
cin >> (n);
for ((i) = (0); (i) < (n); (i)++) {
cin >> (a);
if (i == 0) {
sum1 = a;
sum2 = a;
if (a < 0) {
sign1 = false;
sign2 = true;
} else {
sign1 = true;
sign2 = false;
}
} else {
if (sign1) {
if (sum1 + a >= 0) {
ans1 += sum1 + a + 1;
sum1 = -1;
} else {
sum1 += a;
}
sign1 = false;
} else {
if (sum1 + a <= 0) {
ans1 += abs(sum1 + a) + 1;
sum1 = 1;
} else {
sum1 += a;
}
sign1 = true;
}
if (sign2) {
if (sum2 + a >= 0) {
ans2 += sum2 + a + 1;
sum2 = -1;
} else {
sum2 += a;
}
sign2 = false;
} else {
if (sum2 + a <= 0) {
ans2 += abs(sum2 + a) + 1;
sum2 = 1;
} else {
sum2 += a;
}
sign2 = true;
}
}
}
cout << min(ans1, ans2) << endl;
}
int main(int argc, char** argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
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 | python3 | n = int(input())
a = list(map(int, input().split()))
ansl = []
s = a[0]
ans = 0
if s <= 0:
s = 1
ans += abs(s) + 1
for i in range(n-1):
if s > 0:
s += a[i+1]
if s >= 0:
ans += abs(s) + 1
s = -1
"""
print(i)
print('s > 0')
print(ans)
print(s)
"""
elif s < 0:
s += a[i+1]
if s <= 0:
ans += abs(s) + 1
s = 1
"""
print(i)
print('s < 0')
print(ans)
print(s)
"""
ansl.append(ans)
s = a[0]
ans = 0
if s >= 0:
s = -1
ans += abs(s) + 1
for i in range(n-1):
if s > 0:
s += a[i+1]
if s >= 0:
ans += abs(s) + 1
s = -1
"""
print(i)
print('s > 0')
print(ans)
print(s)
"""
elif s < 0:
s += a[i+1]
if s <= 0:
ans += abs(s) + 1
s = 1
"""
print(i)
print('s < 0')
print(ans)
print(s)
"""
ansl.append(ans)
print(min(ansl))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | # main
n = gets.to_i
ary = gets.split(' ').map(&:to_i)
sum = ary[0]
cnt = 0
if ary[0] == 0
if ary[1] > 1
sum = -1
else
sum = 1
end
cnt = 1
end
(1...n).each{ |i|
if sum < 0
sum += ary[i]
if sum <= 0
cnt += 1-sum
sum = 1
end
else
sum += ary[i]
if sum >= 0
cnt += sum+1 # sum-(-1)
sum = -1
end
end
}
puts cnt |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
for (int i = 0; i < n; i++) {
a[i] *= -1;
}
}
int64_t ans = 0;
int64_t sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
a = list(map(int, input().split()))
b = a[:]
# 無変更チェック
counter_1=0
S=int(a[0])
if a[0]!=0:
for i in range(1,n):
if S<0 and S+int(a[i])<=0:
break
elif S>0 and S+int(a[i])>=0:
break
S+=int(a[i])
if i==n-1:
print(counter_1)
sys.exit()
# a[0]を1に変えた場合の計算
counter_1=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
if S<0 and S+int(b[i])<=0:
counter_1+=-S-int(b[i])+1
b[i]=-S+1
elif S>0 and S+int(b[i])>=0:
counter_1+=S+int(b[i])+1
b[i]=-S-1
S+=int(b[i])
# a[0]を-1に変えた場合の計算
counter_2=abs(int(a[0])+1)
a[0]=-1
S=a[0]
for i in range(1,n):
if S<0 and S+int(a[i])<=0:
counter_2+=-S-int(a[i])+1
a[i]=-S+1
elif S>0 and S+int(a[i])>=0:
counter_2+=S+int(a[i])+1
a[i]=-S-1
S+=int(a[i])
print(min(counter_1,counter_2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
long sum = a[0];
long x = 0;
if(a[0] == 0) {
if(a[1] >= 0) {
sum = -1;
x++;
} else {
sum = 1;
x++;
}
}
long count = 0;
for(int i = 1; i < n; i++) {
sum += a[i];
if((sum - a[i]) * a[i] < 0) {
if(Math.abs(a[i]) - Math.abs(sum - a[i]) < 0) {
if(sum - a[i] < 0) {
count = Math.abs(sum - a[i]) - Math.abs(a[i]) + 1;
} else {
count = -1 * (Math.abs(sum - a[i]) - Math.abs(a[i]) + 1);
}
sum -= a[i];
a[i] += count;
sum += a[i];
x += Math.abs(count);
} else if(Math.abs(a[i]) - Math.abs(sum - a[i]) == 0) {
if(a[i] > 0) {
count = 1;
} else {
count = -1;
}
sum -= a[i];
a[i] += count;
sum += a[i];
x += Math.abs(count);
}
} else if((sum - a[i]) * a[i] > 0) {
if(a[i] < 0) {
count = -a[i] + 1;
} else if(a[i] > 0) {
count = -a[i] - 1;
} else {
if(sum < 0) {
count = sum + 1;
} else {
count = -sum - 1;
}
}
sum -= a[i];
a[i] += count;
sum += a[i];
x += Math.abs(count);
} else {
if(sum - a[i] < 0) {
count = -(sum - a[i]) + 1;
} else {
count = -(sum - a[i]) - 1;
}
sum -= a[i];
a[i] += count;
sum += a[i];
x += Math.abs(count);
}
}
System.out.println(x);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 nums[n];
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
long long current = nums[0];
long long res = 0;
bool current_positive, val_positive = false;
if (current >= 0)
current_positive = true;
else
current_positive = false;
for (int i = 1; i < n; i++) {
long long val = nums[i];
if (val >= 0)
val_positive = true;
else
val_positive = false;
if (current_positive) {
if (val_positive) {
res += ((current + val) + 1);
current = -1;
} else {
if ((current + val) >= 0) {
current += val;
res += (current + 1);
current = -1;
} else {
current += val;
}
}
current_positive = false;
} else {
if (val_positive) {
if ((current + val) <= 0) {
current += val;
res += (abs(current) + 1);
current = 1;
} else {
current += val;
}
} else {
res += ((current + val) + 1);
current = 1;
}
current_positive = true;
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
var n int
fmt.Scan(&n)
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
const (
unknown = 0 + iota
plus
minus
)
preSign := unknown
sum, operationNum := 0, 0
for i := 0; i < n; i++ {
sc.Scan()
a, _ := strconv.Atoi(sc.Text())
sum = sum + a
if sum != 0 {
if preSign == unknown {
if sum > 0 {
preSign = plus
} else {
preSign = minus
}
} else if preSign == plus {
if sum > 0 {
operationNum += (sum + 1)
sum = -1
}
preSign = minus
} else { // preSign == minus
if sum < 0 {
operationNum -= (sum - 1)
sum = 1
}
preSign = plus
}
} else {
if preSign == plus {
operationNum++
sum = -1
preSign = minus
} else {
operationNum++
sum = 1
preSign = plus
}
}
}
fmt.Printf("%d\n", operationNum)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 modpow(long long int a, int n) {
if (n == 0) return 1;
if (n == 1) return a;
long long int wk = modpow(a, n / 2);
long long int ret = 0;
if (n % 2 == 0) {
ret = 1;
} else {
ret = a;
}
return (((ret * wk) % 1000000007) * wk) % 1000000007;
}
long long int gcd(long long int a, long long int b) {
long long int tmp;
if (a < b) {
tmp = a;
a = b;
b = tmp;
}
long long int r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
long long int lcm(long long int a, long long int b) {
long long int wk_int;
wk_int = (a * b) % 1000000007;
long long int ret;
ret = (wk_int * modpow(gcd(a, b), 1000000007 - 2)) % 1000000007;
return ret;
}
int main() {
int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
bool plusmode = true;
long long int ans = LLONG_MAX;
long long int wk_ans = 0;
long long int sum = 0;
for (int i = 0; i < n; i++) {
plusmode = !plusmode;
sum += a[i];
if (plusmode) {
if (sum <= 0) {
wk_ans += abs(sum) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
wk_ans += abs(sum) + 1;
sum = -1;
}
}
}
if (wk_ans < ans) ans = wk_ans;
plusmode = false;
wk_ans = 0;
sum = 0;
for (int i = 0; i < n; i++) {
plusmode = !plusmode;
sum += a[i];
if (plusmode) {
if (sum <= 0) {
wk_ans += abs(sum) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
wk_ans += abs(sum) + 1;
sum = -1;
}
}
}
if (wk_ans < ans) ans = wk_ans;
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e9;
const long long MOD = (long long)1e9 + 7;
const long long MAX = 510000;
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
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;
}
int main() {
long long N, sum, ans = 0;
cin >> N;
long long A[N], B[N];
bool f = false;
for (long long i = 0; i < N; i++) {
cin >> A[i];
B[i] = A[i];
}
if (A[0] == 0) {
A[0] = 1;
B[0] = -1;
f = true;
}
sum = A[0];
for (long long i = 1; i < N; i++) {
if (sum * (sum + A[i]) >= 0) {
if (sum > 0) {
ans += abs(sum * (-1) - 1 - A[i]);
sum = -1;
} else {
ans += abs(sum * (-1) + 1 - A[i]);
sum = 1;
}
} else
sum += A[i];
}
if (f) {
long long sumb = B[0], ansb = 0;
for (long long i = 1; i < N; i++) {
if (sumb * (sumb + B[i]) >= 0) {
if (sumb > 0) {
ansb += abs(sumb * (-1) - 1 - B[i]);
sumb = -1;
} else {
ansb += abs(sumb * (-1) + 1 - B[i]);
sumb = 1;
}
} else
sumb += B[i];
}
ans = min(ans, ansb);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long N;
int main() {
cin >> N;
vector<long long> a(N);
for (int i = 0; i < (N); ++i) cin >> a[i];
vector<long long> sum_a(N);
sum_a[0] = a[0];
for (int i = 1; i < N; i++) {
sum_a[i] += sum_a[i - 1] + a[i];
}
int counter = 0;
int ans1 = 0;
for (int i = 0; i < N; i++) {
int diff = 0;
if (i % 2 == 0) {
if (counter + sum_a[i] <= 0) {
diff = abs(counter + sum_a[i]) + 1;
}
} else {
if (counter + sum_a[i] >= 0) {
diff = abs(counter + sum_a[i]) + 1;
}
}
if (i % 2 == 0) {
counter += diff;
} else {
counter -= diff;
}
ans1 += diff;
}
int counter2 = 0;
int ans2 = 0;
for (int i = 0; i < N; i++) {
int diff2 = 0;
if (i % 2 == 1) {
if (counter2 + sum_a[i] <= 0) {
diff2 = abs(counter2 + sum_a[i]) + 1;
}
} else {
if (counter2 + sum_a[i] >= 0) {
diff2 = abs(counter2 + sum_a[i]) + 1;
}
}
if (i % 2 == 1) {
counter2 += diff2;
} else {
counter2 -= diff2;
}
ans2 += diff2;
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int ans = 0;
vector<int> vec(100000);
vector<int> rui(100000);
for (int i = (int)(0); i < (int)(N); ++i) {
cin >> vec[i];
}
rui[0] = vec[0];
for (int i = 1; i < N; i++) {
rui[i] = rui[i - 1] + vec[i];
}
int G = vec[0];
int X = 0;
if (G >= 0) {
for (int i = (int)(0); i < (int)(N); ++i) {
if (i % 2 == 0) {
if (rui[i] <= 0) {
X = abs(1 - rui[i]);
ans += X;
}
} else {
if (rui[i] >= 0) {
X = abs(-1 - rui[i]) * -1;
ans += abs(X);
}
}
for (int j = i; j < N; j++) {
rui[j] += X;
}
}
} else {
for (int i = (int)(0); i < (int)(N); ++i) {
X = 0;
if (i % 2 == 0) {
if (rui[i] >= 0) {
X = abs(-1 - rui[i]) * -1;
ans += abs(X);
}
} else {
if (rui[i] <= 0) {
X = abs(1 - rui[i]);
ans += abs(X);
}
}
for (int j = i; j < N; j++) {
rui[j] += X;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
num = a[0]
result = 0
r = 1
if num == 0:
for i in range(1, n):
if a[i] > 0:
num = a[i] - 1
result = 1 + (i - 1) * 2
if num == 0:
num += 1
result += 1
r = i + 1
break
elif a[i] < 0:
num = a[i] + 1
result = 1 + (i - 1) * 2
if num == 0:
num -= 1
result += 1
r = i + 1
break
if i == n - 1 and a[i] == 0:
r = n
result = n * 2 - 1
for i in range(r, n):
if num > 0:
if num + a[i] >= 0:
result += num + a[i] + 1
num = -1
else:
num += a[i]
else:
if num + a[i] <= 0:
result -= num + a[i] - 1
num = 1
else:
num += a[i]
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()))
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;
const long long mod = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long n;
cin >> n;
long long a[n];
long long ans = 0, num = 0;
bool maki = false;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i) {
if ((num < 0 && num + a[i] > 0) || (num > 0 && num + a[i] < 0)) {
num += a[i];
} else {
if (num < 0) {
ans += 1 - (num + a[i]);
num = 1;
} else {
ans += 1 + (num + a[i]);
num = -1;
}
}
} else {
if (a[0] == 0) {
num = -1;
ans = 1;
maki = true;
} else {
num = a[i];
}
}
}
long long a1 = num = 0;
if (maki) {
for (int i = 0; i < n; i++) {
if (i) {
if ((num < 0 && num + a[i] > 0) || (num > 0 && num + a[i] < 0)) {
num += a[i];
} else {
if (num < 0) {
a1 += 1 - (num + a[i]);
num = 1;
} else {
a1 += 1 + (num + a[i]);
num = -1;
}
}
} else {
num = a1 = 1;
}
}
ans = min(ans, a1);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000], b[100001];
int main() {
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[i] += a[i];
b[i + 1] = b[i];
}
long long sum = 0, sum2 = 0;
if (b[0] == 0) {
long long i = 0;
while (b[i] == 0 && i < n) i++;
if (i % 2 == 0) {
b[0] = 1;
sum2 = 1;
} else {
b[0] = -1;
sum2 = -1;
}
sum++;
}
for (long long i = 1; i < n; i++) {
b[i] += sum2;
if (b[i] == 0) {
if (b[i - 1] > 0) {
sum2--;
sum++;
b[i] = -1;
} else {
sum2++;
sum++;
b[i] = 1;
}
} else {
if (b[i - 1] > 0 && b[i] > 0) {
sum2 -= (b[i] + 1);
sum += (b[i] + 1);
b[i] = -1;
} else if (b[i] < 0 && b[i - 1] < 0) {
sum2 += (0 - b[i] + 1);
sum += (0 - b[i] + 1);
b[i] = 1;
}
}
}
cout << sum << endl;
cin >> 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 | import numpy as np
N = int(input())
a_s = input().split()
for i in range(N):
a_s[i] = int(a_s[i])
a_s = np.array(a_s)
def get_sign(x):
if x>0:
return +1
elif x<0:
return -1
else:
return 0
ans = 0
for i,a in enumerate(a_s):
if i==0:
S = a
if S == 0:
ans += 1
S = get_sign(a_s[1])*(-1)
else:
S = S0 + a
if get_sign(S0) == get_sign(S):
ans += abs(get_sign(S)*(-1) - S)
S = get_sign(S)*(-1)
elif get_sign(S)==0:
ans += 1
if i<N-1:
S = get_sign(a_s[i+1])*(-1)
elif i==N-1:
pass
S0 = 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;
int main() {
int N, sum_p = 0, sum_n = 0, count_p = 0, count_n = 0;
cin >> N;
int A[N];
for (int i = 0; i < N; ++i) cin >> A[i];
for (int i = 0; i < N; ++i) {
sum_n += A[i];
if (i % 2 == 0) {
if (sum_n <= 0) {
count_n += 1 - sum_n;
sum_n = 1;
}
} else {
if (sum_n >= 0) {
count_n += sum_n + 1;
sum_n = -1;
}
}
}
for (int i = 0; i < N; ++i) {
sum_p += A[i];
if (i % 2 == 1) {
if (sum_p <= 0) {
count_p += 1 - sum_p;
sum_p = 1;
}
} else {
if (sum_p >= 0) {
count_p += sum_p + 1;
sum_p = -1;
}
}
}
int ans = min(count_p, count_n);
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
long long a1;
cin >> a1;
long long sum = a1;
if (a1 >= 0) {
for (int i = 0; i < n - 1; i++) {
long long a;
cin >> a;
sum += a;
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count++;
}
} else {
while (sum <= 0) {
sum++;
count++;
}
}
}
} else {
for (int i = 0; i < n - 1; i++) {
long long a;
cin >> a;
sum += a;
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count++;
}
} else {
while (sum <= 0) {
sum++;
count++;
}
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
struct edge {
int to, cost;
};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
long long int c(long long int a, long long int b, long long int m) {
long long int ans = 1;
for (long long int i = 0; i < b; i++) {
ans *= a - i;
ans %= m;
}
for (long long int i = 1; i <= b; i++) {
ans *= modinv(i, m);
ans %= m;
}
return ans;
}
void dijkdtra(int s, int v, vector<int>& d, vector<vector<edge>>& G) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
d[s] = 0;
que.push(pair<int, int>(0, s));
while (!que.empty()) {
pair<int, int> p = que.top();
que.pop();
int V = p.second;
if (d[V] < p.first) continue;
for (int i = 0; i < G[V].size(); i++) {
edge e = G[V][i];
if (d[e.to] > d[V] + e.cost) {
d[e.to] = d[V] + e.cost;
que.push(pair<int, int>(d[e.to], e.to));
}
}
}
}
long long int binary_search(vector<int>& s, long long int a) {
long long int l = -1;
long long int r = (int)s.size();
while (r - l > 1) {
long long int mid = l + (r - l) / 2;
if (s[mid] >= a)
r = mid;
else
l = mid;
}
return r;
}
int k(long long n) {
int x = 0;
while (n) {
x += n % 10;
n /= 10;
}
return x;
}
long long max(long long x, long long y) {
if (x < y) return y;
return x;
}
int main() {
long long int n, ans = 10000000000000000;
cin >> n;
vector<long long int> a(n), t(n), s(n);
for (int i = (0); i < (n); i++) {
cin >> a[i];
t[i] = a[i];
s[i] = a[i];
}
long long int w = a[0];
if (w <= 0) {
w = 1;
}
for (int i = (1); i < (n); i++) {
if (i % 2 == 0) {
if (abs(w) >= a[i]) {
a[i] = abs(w) + 1;
}
w += a[i];
} else {
if (w >= abs(a[i])) {
a[i] = -1 * (w + 1);
}
w += a[i];
}
}
w = t[0];
if (w >= 0) {
w = -1;
}
for (int i = (1); i < (n); i++) {
if (i % 2 == 1) {
if (abs(w) >= t[i]) {
t[i] = abs(w) + 1;
}
w += t[i];
} else {
if (w >= abs(t[i])) {
t[i] = -1 * (w + 1);
}
w += t[i];
}
}
long long int cost1 = 0, cost2 = 0;
for (int i = (0); i < (n); i++) {
cost1 += abs(s[i] - a[i]);
cost2 += abs(s[i] - t[i]);
}
ans = min(cost1, cost2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int ans = 0;
int sum = a.at(0);
if (a.at(0) > 0) {
for (int i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
} else if (a.at(0) < 0) {
for (int i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
} else if (a.at(0) == 0) {
ans++;
sum++;
for (int i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
a[i] += a[i-1]
a_1 = a.copy()
ans = 0
for i in range(n):
if i%2==0 and a_1[i]<=0:
tmp = -a_1[i]-1
for j in range(i,n):
a_1[j] += tmp
ans += abs(tmp)
elif i%2==1 and a_1[i]>=0:
tmp = -a_1[i]+1
for j in range(i,n):
a_1[j] += tmp
ans += abs(tmp)
ans_2 = 0
for i in range(n):
if i%2==1 and a[i]<=0:
tmp = -a[i]+1
for j in range(i,n):
a[j] += tmp
ans_2 += abs(tmp)
elif i%2==0 and a[i]>=0:
tmp = -a[i]-1
for j in range(i,n):
a[j] += tmp
ans_2 += abs(tmp)
print(min(ans,ans_2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
if f == "+":
if a[i] + sum(a[:i]) > 0:
c = -1 - sum(a[:i])
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + sum(a[:i]) == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + sum(a[:i]) < 0:
c = 1 - sum(a[:i])
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + sum(a[:i]) == 0:
a[i] += 1
ans += 1
f = "+"
print(a)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a.at(i);
}
long long ans = 0;
long long sum = a.at(0);
if (a.at(0) > 0) {
for (long long i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
} else if (a.at(0) < 0) {
for (long long i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
} else if (a.at(0) == 0) {
ans++;
sum++;
for (long long i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long n;
long long data[100001];
long long solve() {
long long res = 0;
long long part = 0;
part += data[0];
bool isP = part > 0 ? true : false;
for (int i = 1; i < n; i++) {
part += data[i];
if (isP) {
if (part > 0) {
res += part + 1;
part = -1;
} else if (part == 0) {
res++;
part = -1;
}
isP = false;
} else {
if (part < 0) {
res += 1 - part;
part = 1;
} else if (part == 0) {
res++;
part = 1;
}
isP = true;
}
}
return res;
}
int main() {
cin >> n;
long long ans = 0;
for (int i = 0; i < n; i++) cin >> data[i];
ans = solve();
for (int i = 0; i < n; i++) data[i] *= -1;
cout << min(ans, solve()) << 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 + 1 長の array を返す
def cumulative_sum(array)
tmp = [0]
previous = 0
array.each { |num| tmp << previous += num }
tmp
end
def ans(cumulative_sums, previous_plus, ans, added)
cumulative_sums.each do |sum|
if previous_plus && sum + added >= 0
ans += (sum + added).abs + 1
added -= (sum + added).abs + 1
elsif !previous_plus && sum + added <= 0
ans += (sum + added).abs + 1
added += (sum + added).abs + 1
end
previous_plus = !previous_plus
end
ans
end
n = gets.to_i
nums = gets.split.map(&:to_i)
cumulative_sums = cumulative_sum(nums)
cumulative_sums.delete_at(0)
first = cumulative_sums.delete_at(0)
answers = []
if first > 0
answers << ans(cumulative_sums, true, 0, 0)
elsif first < 0
answers << ans(cumulative_sums, false, 0, 0)
else
answers << ans(cumulative_sums, true, 1, 1)
answers << ans(cumulative_sums, false, 1, -1)
end
puts answers.min
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll sum = a[0];
ull ans = 0;
for (int i = 1; i < n; i++) {
if (sum < 0) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(1 - sum - a[i]);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += abs(-1 - sum - a[i]);
sum = -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 mod = 1e9 + 7;
void _IOS() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
}
int sx, sy, tx, ty;
struct threeElements {
int _1st, _2nd, _3rd;
};
vector<vector<int>> adj(10);
long long v[200009];
int n;
int solve(int x) {
int ans = 0, sum = x;
for (int i = 2; i <= n; i++) {
int u = v[i] + sum;
if (sum < 0) {
if (u <= 0) {
ans += abs(u) + 1;
u = 1;
}
} else {
if (u >= 0) {
ans += u + 1;
u = -1;
}
}
sum = u;
}
return ans;
}
int main() {
_IOS();
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v[i] = x;
}
if (v[1] == 0) {
cout << min(solve(1), solve(-1)) + 1;
} else {
long long ans1 = solve(v[1]);
long long ans2 = solve(-v[1]);
cout << min(ans1, ans2);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
ll r = 1;
while (e > 0) {
if (e & 1) r = (r % m) * (b % m) % m;
b = (b % m) * (b % m) % m;
e >>= 1;
}
return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
template <typename T>
void ppp(T n) {
cout << n << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[n];
for (int i = 0; i < (n); ++i) {
cin >> a[i];
}
int s[n + 1];
s[0] = 0;
for (int i = 0; i < (n); ++i) {
s[i + 1] = s[i] + a[i];
}
ll ans1 = 0, ans2 = 0, diff1 = 0, diff2 = 0;
for (int i = 1; i <= n; ++i) {
if (i % 2) {
if (s[i] + diff1 <= 0) {
ll tmp = abs((s[i] + diff1) - 1);
ans1 += tmp;
diff1 += tmp;
}
if (s[i] + diff2 >= 0) {
ll tmp = abs((s[i] + diff2) + 1);
ans2 += tmp;
diff2 -= tmp;
}
} else {
if (s[i] + diff1 >= 0) {
ll tmp = abs((s[i] + diff1) + 1);
ans1 += tmp;
diff1 -= tmp;
}
if (s[i] + diff2 <= 0) {
ll tmp = abs((s[i] + diff2) - 1);
ans2 += tmp;
diff2 += tmp;
}
}
}
ppp(min(ans1, ans2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using Int = long long;
Int INF = 1 << 30;
Int large0(std::vector<Int> a, Int n) {
Int ans = 0;
Int sum = a[0];
for (Int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
return ans;
}
Int small0(std::vector<Int> a, Int n) {
Int ans = 0;
Int sum = a[0];
for (Int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
if (i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
return ans;
}
int main() {
Int n;
std::cin >> n;
std::vector<Int> a(n);
for (Int i = 0; i < n; i++) std::cin >> a[i];
Int ans = 0;
Int sum = a[0];
if (a[0] > 0) {
std::cout << large0(a, n) << std::endl;
}
if (a[0] < 0) {
std::cout << small0(a, n) << std::endl;
}
if (a[0] == 0) {
Int res1 = 0;
Int res2 = 0;
a[0] = 1;
res1 = large0(a, n);
a[0] = -1;
res2 = small0(a, n);
std::cout << std::min(res1, res2) + 1 << std::endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100100];
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0, sum = a[0];
int sign = (a[0] > 0 ? 1 : -1);
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum == 0) {
sum += -1 * sign;
ans++;
} else if (sign > 0 && sum > 0) {
long long x = sum + 1;
sum -= x;
ans += x;
} else if (sign < 0 && sum < 0) {
long long y = abs(sum) + 1;
sum += y;
ans += y;
}
sign *= -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>
int inf = 1000000007;
using namespace std;
int main() {
int64_t n;
cin >> n;
vector<int64_t> data(n);
int64_t ans = 0;
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
int64_t sum = data.at(0);
int64_t sump = sum;
for (int i = 1; i < n; i++) {
sump += data.at(i);
if (sum * sump >= 0) {
int c = sump;
if (c < 0) c *= -1;
c++;
ans += c;
if (sump > 0) {
data.at(i) -= c;
sump -= c;
} else {
data.at(i) += c;
sump += c;
}
}
sum += data.at(i);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[100010];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
unsigned long long sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int tmp = -1;
int flagg = 0;
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
tmp = i;
flagg = 1;
break;
}
}
if (!flagg) {
printf("%d\n", (n - 1) * 2 + 1);
continue;
}
if (tmp != 0 && a[tmp] > 0) {
if (tmp % 2)
a[0] = -1;
else
a[0] = 1;
sum++;
} else if (tmp != 0 && a[tmp] < 0) {
if (tmp % 2)
a[0] = 1;
else
a[0] = -1;
sum++;
}
unsigned long long oo = a[0], flag;
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (int i = 1; i < n; i++) {
oo += a[i];
if (flag == 1) {
if (oo >= 0) {
sum += oo + 1;
oo = -1;
}
flag = -1;
} else if (flag == -1) {
if (oo <= 0) {
sum += 0 - oo + 1;
oo = 1;
}
flag = 1;
}
}
printf("%lld\n", sum);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD7 = 1000000007;
const long long MOD9 = 1000000009;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
vector<long long> vec(N);
for (long long i = 0; i < N; i++) cin >> vec[i];
long long res, partial, distance_0;
vector<long long> res_vec;
bool flag_before;
for (long long n = 0; n < 2; ++n) {
res = 0, partial = vec[0];
flag_before = (n == 0) ? partial > 0 : partial < 0;
for (long long i = 1; i < N; ++i) {
partial += vec[i];
distance_0 = abs(partial) + 1;
if (flag_before) {
if (partial >= 0) {
res += distance_0;
partial -= distance_0;
}
} else {
if (partial <= 0) {
res += distance_0;
partial += distance_0;
}
}
flag_before = !flag_before;
}
res_vec.push_back(res);
}
cout << *min_element(((res_vec)).begin(), ((res_vec)).end()) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
int arr[MAX], n;
int status(int a) {
if (a < 0)
return 1;
else if (a > 0)
return 0;
else
return 2;
}
long long int solve() {
long long int cnt = 0;
long long int sum = arr[0], f = 0;
if (arr[0] < 0)
f = 1;
else
f = 0;
for (int i = 1; i < n; i++) {
f ^= 1;
int add = arr[i];
if (status(arr[i]) != f) add = 0, cnt += abs(arr[i]);
sum += add;
if (status(sum) != f) {
cnt += abs(sum) + 1;
if (f)
sum = -1;
else
sum = 1;
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (!arr[0]) {
arr[0] = 1;
long long int x = solve() + 1;
arr[0] = -1;
long long int y = solve() + 1;
cout << min(x, y) << endl;
} else {
long long int x, y;
x = solve();
arr[0] *= -1;
y = solve() + (abs(arr[0] * 2));
cout << min(x, y) << 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>
long body(std::vector<long>& a) {
long ans = 0;
std::vector<long> s(a.size());
s.at(0) = a.at(0);
for (unsigned long i = 1; i < a.size(); i++) {
s.at(i) = s.at(i - 1) + a.at(i);
}
long diff = 0;
for (unsigned long i = 1; i < s.size(); i++) {
s.at(i) += diff;
long n = 0;
if (s.at(i - 1) > 0 && s.at(i) >= 0) {
n = s.at(i) + 1;
ans += n;
diff -= n;
s.at(i) += diff;
} else if (s.at(i - 1) < 0 && s.at(i) <= 0) {
n = -s.at(i) + 1;
ans += n;
diff += n;
s.at(i) += diff;
}
}
return ans;
}
int main(int argc, char** argv) {
long n;
std::cin >> n;
std::vector<long> a(n);
for (long i = 0; i < n; i++) {
std::cin >> a.at(i);
}
long ans;
if (a.at(0) != 0) {
ans = body(a);
} else {
a.at(0) = -1;
long ans_a = body(a);
a.at(0) = 1;
long ans_b = body(a);
ans = std::min(ans_a, ans_b);
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
int N, a[MAX_N + 5];
int solve(int next) {
int s = 0, cnt = 0;
for (int i = 0; i < N; i++) {
s += a[i];
if (next == 1 && s <= 0) {
cnt += next - s;
s = 1;
} else if (next == -1 && s >= 0) {
cnt += s - next;
s = -1;
}
next *= -1;
}
return cnt;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) cin >> a[i];
cout << min(solve(1), solve(-1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # vim: fileencoding=utf-8
def main():
n = int(input())
a = list(map(int, input().split()))
ans = 0
cursol = a[0]
if cursol == 0:
if a[1] >= 0:
cursol = -1
else:
cursol = 1
ans += 1
for i in a[1:]:
t = cursol + i
if cursol > 0:
if t >= 0:
ans += t + 1
cursol = -1
else:
cursol = t
elif cursol < 0:
if t <= 0:
ans += abs(t) + 1
cursol = 1
else:
cursol = t
print(ans)
if __name__ == "__main__":
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = [int(x) for x in input().split()]
flag = True if A[0] > 0 else False
count = 0
for i in range(n):
if i == 0:
continue
sum_for_i = sum(A[:i])
sum_for_next = sum(A[:i+1])
if (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
continue
else:
#print("needs to be changed: A[{}] ({})".format(i, A[i]))
while not (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
if (sum_for_i < 0):
A[i] += 1
count += 1
else:
A[i] -= 1
count += 1
sum_for_i = sum(A[:i])
sum_for_next = sum(A[:i+1])
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def solve():
n = int(input())
a = list(map(int, input().split()))
i = 0
sum = 0
ans = 0
for i in range(n-1):
sum += a[i]
if sum > 0 and sum+a[i+1] > 0:
tmp = -1 - sum
ans += abs(tmp - a[i+1])
a[i+1] = tmp
elif sum < 0 and sum+a[i+1] < 0:
tmp = 1 - sum
ans += abs(tmp - a[i+1])
a[i+1] = tmp
print(ans)
# print(a)
if __name__ == "__main__":
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = 0
f += a[0]
if f <= 0:
f = 1
ans1 += 1 - a[0]
for i in range(1, N):
if f * (f + a[i]) < 0:
f += a[i]
continue
ans1 += abs(f + a[i]) + 1
if f > 0:
f = -1
else:
f = 1
f = 0
f += a[0]
if f >= 0:
f = -1
ans2 += 1 + a[i]
for i in range(1, N):
if f * (f + a[i]) < 0:
f += a[i]
continue
ans2 += abs(f + a[i]) + 1
if f > 0:
f = -1
else:
f = 1
print(min(ans1, ans2)) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.