Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
int INF = 1 << 30;
long long INFL = 1LL << 60;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (long long i = 0; i < (n); i++) cin >> a.at(i);
int sum1 = 0;
int ans1 = 0;
int sum2 = 0;
int ans2 = 0;
for (long long i = 0; i < (n); i++) {
sum1 += a.at(i);
if (i % 2 == 0) {
if (sum1 <= 0) {
ans1 += sum1 * (-1) + 1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
}
}
for (long long i = 0; i < (n); i++) {
sum2 += a.at(i);
if (i % 2 == 1) {
if (sum2 <= 0) {
ans2 += sum2 * (-1) + 1;
sum2 = 1;
}
} else {
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long N;
cin >> N;
long a[N];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long sum = 0;
bool check = false;
long ans = 0;
if (a[0] != 0) {
for (int i = 0; i < N; i++) {
sum += a[i];
if (i == 0 && sum == 0 && a[1] > 0) {
check = false;
sum = -1;
ans++;
} else if (i == 0 && sum == 0 && a[1] < 0) {
check = true;
sum = 1;
ans++;
} else if (i == 0 && sum > 0) {
check = true;
} else if (i == 0 && sum < 0) {
check = false;
} else if (sum <= 0 && !check) {
ans += 1 - sum;
sum = 1;
check = true;
} else if (sum >= 0 && check) {
ans += sum + 1;
sum = -1;
check = false;
} else if (check) {
check = false;
} else if (!check) {
check = true;
}
}
}
cout << 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 | java | import java.util.*;
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 = new long[n+1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i-1] + a[i-1];
}
long count1 = count(n, true, sum, 0 );
long count2 = count(n, false, sum, 0);
System.out.println(Math.min(count1, count2));
}
private static long count(int n , boolean pastPlus, long[] sum, long carry){
long count2 = 0;
for (int i = 1; i <= n; i++) {
long cur = sum[i] + carry;
if (pastPlus && cur >= 0) {
// minus nisinaito
count2 += cur + 1;
carry = - cur - 1;
}
if (!pastPlus && cur <= 0) {
count2 += -cur + 1;
carry = -cur + 1;
}
pastPlus = !pastPlus;
}
return count2;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | //ヘッダー
#include<bits/stdc++.h>
using namespace std;
//型定義
typedef long long ll;
//定数
const int INF=1e+9;
const int MOD=1e+9+7;
//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)
// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値
int main(){
ll n;
cin>>n;
vector<ll> a(n);
ll sum=0;
ll ans=0;
REP(i,n) cin>>a[i];
REP(i,n){
ll tmp=sum;
sum+=a[i];
if(tmp>0&&sum>0){
ans+=(sum+1);
sum=-1;
}else if(tmp<0&&sum<0){
ans+=abs(sum-1);
sum=1;
}else if(sum==0){
ans++;
if(tmp<0){
sum++;
}else{
sum--;
}
}
}
ll ans2=0;
sum=0;
REP(i,n){
ll tmp=sum;
sum-=a[i];
ans2+=abs(a[i])+1;
if(tmp>0&&sum>0){
ans2+=(sum+1);
sum=-1;
}else if(tmp<0&&sum<0){
ans2+=abs(sum-1);
sum=1;
}else if(sum==0){
ans2++;
if(tmp<0){
sum++;
}else{
sum--;
}
}
}
ans=min(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 | java | import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Sequence solver = new Sequence();
solver.readInput();
solver.solve();
solver.writeOutput();
}
static class Sequence {
private int n;
private long a[];
private long output;
private Scanner scanner;
public Sequence() {
this.scanner = new Scanner(System.in);
}
public void readInput() {
n = Integer.parseInt(scanner.next());
a = new long[n];
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(scanner.next());
}
}
private int count(boolean sign) {
int count=0;
long sum=0;
for(int i=0; i<n; i++) {
sum += a[i];
if((i%2==0) == sign) {
// a[i]までの合計を正にするとき
if(sum<=0) {
count += 1-sum;
sum = 1;
}
} else {
// a[i]までの合計を負にするとき
if(0<=sum) {
count += 1+sum;
sum = -1;
}
}
}
return count;
}
public void solve() {
int plus = count(true);
int minus = count(false);
output = Math.min(plus,minus);
}
public void writeOutput() {
System.out.println(output);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> rev_a = a;
int result = 0;
bool isPlus = a[0] > 0 ? true : false;
int sum = a[0];
for (int i = 1; i < n; i++) {
int temp_sum = sum + a[i];
if (isPlus) {
if (temp_sum >= 0) {
result += temp_sum + 1;
a[i] -= temp_sum + 1;
}
} else {
if (temp_sum <= 0) {
result += -temp_sum + 1;
a[i] += -temp_sum + 1;
}
}
isPlus = !isPlus;
sum += a[i];
}
sum = 0;
int rev_result = 0;
isPlus = a[0] > 0 ? true : false;
if (isPlus) {
rev_result += a[0] + 1;
a[0] -= a[0] + 1;
isPlus = !isPlus;
}
for (int i = 1; i < n; i++) {
int temp_sum = sum + rev_a[i];
if (isPlus) {
if (temp_sum >= 0) {
rev_result += temp_sum + 1;
rev_a[i] -= temp_sum + 1;
}
} else {
if (temp_sum <= 0) {
rev_result += -temp_sum + 1;
rev_a[i] += -temp_sum + 1;
}
}
isPlus = !isPlus;
sum += rev_a[i];
}
if (rev_result < result)
cout << rev_result << endl;
else
cout << result << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
class ParseError {};
template <typename T>
class Zip {
vector<T> d;
bool flag;
void init() {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
long long getNum(T x) {
if (flag) init();
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
long long size() {
if (flag) init();
return (long long)d.size();
}
};
long long N, M, K, a, b, c, d, e, H, W, L, T;
long long x, y, z;
long long A[2000004] = {};
long long B[2000004] = {};
long long C[2000004] = {};
long long D[1000006] = {};
long long E[1000006] = {};
bool f;
string S[200000];
string SS;
set<long long> sll;
pair<long long, long long> bufpl;
vector<long long> vl[200005];
vector<long long> vll;
vector<long long> v;
vector<pair<long long, long long>> vpl;
vector<string> vs;
set<long long> llset;
set<string> Sset;
multiset<long long> llmset;
queue<long long> ql;
multiset<pair<long long, long long>> plmset;
typedef struct ST {
long long first;
long long second;
long long cost;
bool operator<(const ST& another) const { return cost < another.cost; };
bool operator>(const ST& another) const { return cost > another.cost; };
} ST;
priority_queue<ST, vector<ST>, greater<ST>> qst;
long long modinv(long long aa, long long mm) {
long long bb = mm, uu = 1, vv = 0;
while (bb) {
long long tt = aa / bb;
aa -= tt * bb;
swap(aa, bb);
uu -= tt * vv;
swap(uu, vv);
}
uu %= mm;
if (uu < 0) uu += mm;
return uu;
}
long long zettai(long long aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
float zettai(float aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
class UnionFind {
public:
vector<long long> pairent;
vector<long long> depth;
vector<long long> size;
UnionFind(long long Amount)
: pairent(Amount, 1), depth(Amount, 1), size(Amount, 1) {
for (long long i = 0; i < Amount; i++) {
pairent[i] = i;
}
}
long long FindPairent(long long x) {
if (pairent[x] == x)
return x;
else
return pairent[x] = FindPairent(pairent[x]);
}
long long Merge(long long x, long long y) {
x = FindPairent(x);
y = FindPairent(y);
if (x != y) {
if (depth[x] > depth[y]) {
pairent[y] = pairent[x];
return size[x] += size[y];
} else {
pairent[x] = pairent[y];
if (depth[x] == depth[y]) {
depth[y]++;
}
return size[y] += size[x];
}
} else {
return -1;
}
}
bool IsSame(long long x, long long y) {
if (FindPairent(x) == FindPairent(y))
return true;
else
return false;
}
long long GetSize(long long x) {
x = FindPairent(x);
return size[x];
}
};
struct Edge {
long long a, b, cost;
bool operator<(const Edge& other) const { return cost < other.cost; }
};
struct Graph {
long long n;
vector<Edge> es;
};
class Kruskal {
Graph origin_G;
Graph MST;
long long total_cost = 0;
public:
void Solve() {
UnionFind uf = UnionFind(MST.n);
for (long long i = 0; i < origin_G.es.size(); i++) {
long long a = origin_G.es[i].a;
long long b = origin_G.es[i].b;
long long cost = origin_G.es[i].cost;
if (!uf.IsSame(a, b)) {
uf.Merge(a, b);
MST.es.push_back(origin_G.es[i]);
total_cost += cost;
}
}
}
Kruskal(Graph graph) {
origin_G = graph;
MST = graph;
MST.es.clear();
sort(origin_G.es.begin(), origin_G.es.end());
}
long long GetMinCost() { return total_cost; }
};
long long RepeatSquaring(long long N, long long P, long long M) {
if (P == 0) return 1;
if (P % 2 == 0) {
long long t = RepeatSquaring(N, P / 2, M) % M;
return t * t % M;
}
return N * RepeatSquaring(N, P - 1, M) % M;
}
long long GCD(long long a, long long b) {
if (a % b == 0)
return b;
else
return GCD(b, a % b);
}
long long Min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
long long Max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long Sum(long long a, long long b) { return a + b; }
template <typename T>
class SegmentTree {
long long n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(long long num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num) n *= 2;
node.resize(2 * n - 1);
fill(node.begin(), node.end(), init);
outValue = out;
initValue = init;
}
void valueChange(long long num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(long long a, long long b, long long l = 0, long long r = -1,
long long k = 0) {
if (r == -1) r = n;
if (a <= l && r <= b) return node[k];
if (b <= l || r <= a) return outValue;
long long mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
class Combination {
vector<long long> factorial;
vector<long long> factorial_inv;
long long mod;
long long max_n;
public:
void Init(long long init_max_n, long long init_mod) {
max_n = init_max_n;
mod = init_mod;
factorial.resize(max_n + 1);
factorial[0] = 1;
for (long long i = 1; i < factorial.size(); i++) {
factorial[i] = factorial[i - 1] * i;
factorial[i] %= mod;
}
factorial_inv.resize(max_n + 1);
factorial_inv[0] = 1;
for (long long i = 1; i < factorial_inv.size(); i++) {
factorial_inv[i] = factorial_inv[i - 1] * modinv(i, mod);
factorial_inv[i] %= mod;
}
}
long long GetComb(long long n, long long k) {
long long comb = factorial[n];
comb *= factorial_inv[k];
comb %= mod;
comb *= factorial_inv[n - k];
comb %= mod;
return comb;
}
long long GetH(long long n, long long k) {
long long comb = factorial[n + k - 1];
comb *= factorial_inv[n];
comb %= mod;
comb *= factorial_inv[k - 1];
comb %= mod;
return comb;
}
};
class Tree {
long long node_N;
vector<long long> node;
vector<vector<pair<long long, long long>>> pass;
long long diameter = -1;
vector<long long> dist_Diamieter[2];
pair<long long, long long> maxDist_Num;
public:
void Init(long long node_Num) {
node_N = node_Num;
node.resize(node_N + 1);
pass.resize(node_N + 1);
dist_Diamieter[0].resize(node_N + 1);
for (long long i = 0; i < node_N + 1; i++) dist_Diamieter[0][i] = -1;
dist_Diamieter[1].resize(node_N + 1);
for (long long i = 0; i < node_N + 1; i++) dist_Diamieter[1][i] = -1;
}
void AddEdge(long long a, long long b, long long dist) {
bufpl.first = b;
bufpl.second = dist;
pass[a].push_back(bufpl);
bufpl.first = a;
pass[b].push_back(bufpl);
}
void DFS(long long step, long long now, long long dist) {
dist_Diamieter[step][now] = dist;
if (dist_Diamieter[step][now] > maxDist_Num.first) {
maxDist_Num.first = dist_Diamieter[step][now];
maxDist_Num.second = now;
}
for (long long i = 0; i < pass[now].size(); i++) {
long long next_node = pass[now][i].first;
if (dist_Diamieter[step][next_node] == -1) {
DFS(step, next_node, dist + pass[now][i].second);
}
}
}
long long GetDiameter(long long min_node_num) {
if (diameter >= 0)
return diameter;
else {
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(0, min_node_num, 0ll);
long long step2_start = maxDist_Num.second;
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(1, step2_start, 0ll);
diameter = maxDist_Num.first;
return diameter;
}
}
long long GetDistFromMinNode(long long num) { return dist_Diamieter[0][num]; }
};
void Nibu(long long node, long long co) {
C[node] = co % 2;
D[co % 2]++;
for (long long i = 0; i < vl[node].size(); i++) {
long long next = vl[node][i];
if (C[next] == -1) {
Nibu(next, co + 1);
}
}
}
int main() {
cin >> N;
for (long long i = 0; i < N; i++) {
cin >> A[i];
if (i == 0)
B[i] = A[i];
else
B[i] = B[i - 1] + A[i];
}
long long ruiseki = 0;
long long ans = 0;
long long ans2 = 0;
if (B[0] == 0) {
for (long long i = 0; i < N; i++) C[i] = B[i];
B[0] = -1;
C[0] = 1;
ruiseki = 1;
ans2 = 1;
for (long long i = 1; i < N; i++) {
if (C[i - 1] + ruiseki < 0) {
if (C[i] + ruiseki <= 0) {
ans2 += 1 - (C[i] + ruiseki);
ruiseki += 1 - (C[i] + ruiseki);
}
} else {
if (C[i] + ruiseki >= 0) {
ans2 += (C[i] + ruiseki) - (-1);
ruiseki -= (C[i] + ruiseki) - (-1);
}
}
}
ruiseki = -1;
ans = 1;
} else
ans2 = 8223372036854775807ll;
for (long long i = 1; i < N; i++) {
if (B[i - 1] + ruiseki < 0) {
if (B[i] + ruiseki <= 0) {
ans += 1 - (B[i] + ruiseki);
ruiseki += 1 - (B[i] + ruiseki);
}
} else {
if (B[i] + ruiseki >= 0) {
ans += (B[i] + ruiseki) - (-1);
ruiseki -= (B[i] + ruiseki) - (-1);
}
}
}
cout << min(ans, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
set<string> c;
map<ll, ll> mp;
const ll inf = 100000000000000000;
const ll mod = 1000000007;
const ll mod2 = 998244353;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
int main() {
ll n;
cin >> n;
vector<ll> a(n), sums(n + 1, 0);
for (int i = 0; i < n; i++) cin >> a.at(i);
for (int i = 0; i < n; i++) sums.at(i + 1) = a.at(i) + sums.at(i);
ll ans = 0, res = 0, tmp = 0;
vector<ll> sum = sums;
if (sum.at(1) == 0) {
sum.at(1) += 1;
tmp += 1;
}
for (int i = 2; i < n + 1; i++) {
if ((sum.at(i) + tmp > 0 && sum.at(i - 1) < 0) ||
(sum.at(i) + tmp < 0 && sum.at(i - 1) > 0))
continue;
if (sum.at(i) == 0) {
if (sum.at(i - 1) > 0) {
sum.at(i) = -1;
tmp--;
} else {
sum.at(i) = 1;
tmp++;
}
ans++;
} else if (sum.at(i - 1) < 0) {
sum.at(i) += abs(sum.at(i)) + 1;
ans += abs(sum.at(i)) + 1;
tmp += abs(sum.at(i)) + 1;
} else {
sum.at(i) -= sum.at(i) - 1;
ans += sum.at(i) + 1;
tmp -= sum.at(i) - 1;
}
}
sum = sums;
tmp = 0;
res += abs(sum.at(1)) + 1;
if (sum.at(1) < 0) {
sum.at(1) = 1;
tmp += res;
} else {
sum.at(1) = -1;
tmp -= res;
}
for (int i = 2; i < n + 1; i++) {
if ((sum.at(i) + tmp > 0 && sum.at(i - 1) < 0) ||
(sum.at(i) + tmp < 0 && sum.at(i - 1) > 0))
continue;
if (sum.at(i) == 0) {
if (sum.at(i - 1) > 0) {
sum.at(i) = -1;
tmp--;
} else {
sum.at(i) = 1;
tmp++;
}
res++;
} else if (sum.at(i - 1) < 0) {
sum.at(i) += abs(sum.at(i)) + 1;
res += abs(sum.at(i)) + 1;
tmp += abs(sum.at(i)) + 1;
} else {
sum.at(i) -= sum.at(i) - 1;
res += sum.at(i) + 1;
tmp -= sum.at(i) - 1;
}
}
cout << min(ans, res) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int chk(long a[], int N, bool t) {
long total = 0;
long ops = 0;
for (int i = 0; i < N; i++) {
total += a[i];
if (t == true && (total < 1)) {
ops += (1 - total);
total = 1;
} else if (t == false && (total > -1)) {
ops += (total + 1);
total = -1;
}
t = !t;
}
return ops;
}
int main() {
long N;
cin >> N;
long a[100001];
for (long i = 0; i < N; i++) {
cin >> a[i];
}
printf("%d\n", min(chk(a, N, true), chk(a, N, false)));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, a[];
double count = 0;
double sum = 0, presum;
n = sc.nextInt();
a = new int[n];
for(int i = 0; i < n; ++i)a[i] = sc.nextInt();
sc.close();
presum = -1.0 * a[0];
for(int i: a) {
sum += (double)i;
if(sum * presum > 0) {
double tmp = Math.abs(sum) + 1;
if(presum > 0)sum -= tmp;
else sum += tmp;
count += tmp;
}
if(sum == 0) {
if(presum > 0)sum--;
else sum++;
++count;
}
presum = sum;
}
System.out.printf("%.0f\n", count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().execute();
}
public void execute() {
Scanner sc = new Scanner(System.in);
final int n = sc.nextInt();
int ops = 0;
long[] sums = new long[n + 1];
for (int i = 1; i <= n; i++) {
int ai = sc.nextInt();
if (i > 1 && sums[i - 1] == 0) {
sums[i - 1] = (ai > 0) ? -1 : 1;
}
sums[i] = sums[i - 1] + ai;
if (sums[i] == 0) {
ops++;
} else if (i > 0 && sums[i - 1] * sums[i] > 0) {
if (sums[i] > 0) {
ops += sums[i] + 1;
sums[i] = -1;
} else if (sums[i] < 0) {
ops += 1 - sums[i];
sums[i] = 1;
}
}
}
System.out.println(ops);
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::vector<int> vec(n);
for (auto&& e : vec) std::cin >> e;
auto solve = [&](bool positive) {
int ret = 0;
int sum = 0;
for (int i = 0; i < vec.size(); ++i) {
sum += vec[i];
if (sum == 0 || positive != sum > 0) {
ret += std::abs(sum) + 1;
sum = (positive ? 1 : -1);
}
positive = !positive;
}
return ret;
};
std::cout << std::min(solve(true), solve(false)) << 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 INF = 1e9;
const int mod = 1e9 + 7;
int main() {
int n;
cin >> n;
long long sum;
cin >> sum;
long long ans = 0;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
if (sum > 0) {
if (sum + a >= 0) {
ans += llabs(sum + a) + 1;
sum = -1;
} else
sum += a;
} else {
if (sum + a <= 0) {
ans += llabs(sum + a) + 1;
sum = 1;
} else
sum += a;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n, x = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
for i in range(1, n):
if l[i] + l[i-1] > x:
ans += (l[i] + l[i-1]) - x
l[i] -= (l[i] + l[i-1]) - x
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;
using ll = long long;
int main() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) cin >> a[i];
ll ans = 4e18;
if (a[0] < 0)
for (int i = 0; i < n; i++) a[i] *= -1;
ll sum = a[0], now = 0;
for (int i = 0; i < n - 1; i++) {
if (i % 2) {
if (sum + a[i + 1] < 0) {
now += 1 - (sum + a[i + 1]);
sum = 1;
} else {
sum += a[i + 1];
}
} else {
if (sum + a[i + 1] >= 0) {
now += sum + a[i + 1] + 1;
sum = -1;
} else {
sum += a[i + 1];
}
}
}
ans = min(ans, now);
sum = -1, now = a[0] + 1;
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
if (sum + a[i + 1] <= 0) {
sum = 1;
now += 1 - a[i + 1];
} else {
sum += a[i + 1];
}
} else {
if (sum + a[i + 1] >= 0) {
sum = -1;
now += a[i + 1] + 1;
} else {
sum += a[i + 1];
}
}
}
ans = min(ans, now);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
vector<long long> b(N), c(N);
long long count1 = 0, count2 = 0;
if (a[0] <= 0) {
b[0] = 1;
count1 += 1 - a[0];
if (a[0] == 0) {
c[0] = -1;
count2++;
} else {
c[0] = a[0];
}
}
if (a[0] >= 0) {
c[0] = -1;
count2 += a[0] + 1;
if (a[0] == 0) {
b[0] = 1;
count1++;
} else {
b[0] = a[0];
}
}
for (int i = 1; i < N; i++) {
if (b[i - 1] < 0 && b[i - 1] + a[i] <= 0) {
count1 += 1 - (b[i - 1] + a[i]);
b[i] = 1;
} else if (b[i - 1] > 0 && b[i - 1] + a[i] >= 0) {
count1 += (b[i - 1] + a[i]) + 1;
b[i] = -1;
} else {
b[i] = b[i - 1] + a[i];
}
if (c[i - 1] < 0 && c[i - 1] + a[i] <= 0) {
count2 += 1 - (c[i - 1] + a[i]);
c[i] = 1;
} else if (c[i - 1] > 0 && c[i - 1] + a[i] >= 0) {
count2 += (c[i - 1] + a[i]) + 1;
c[i] = -1;
} else {
c[i] = c[i - 1] + a[i];
}
}
long long ans = min(count1, count2);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
long long int a[n];
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int oddcount = 0, evencount = 0;
long long int oddsum = 0, evensum = 0;
bool oddplus = true, evenplus = false;
for (int i = 0; i < (n); i++) {
oddsum += a[i];
evensum += a[i];
if (oddplus && oddsum <= 0) {
oddcount += 1 - oddsum;
oddsum = 1;
} else if (!oddplus && oddsum >= 0) {
oddcount += 1 + oddsum;
oddsum = -1;
}
if (evenplus && evensum <= 0) {
evencount += 1 - evensum;
evensum = 1;
} else if (!evenplus && evensum >= 0) {
evencount += 1 + evensum;
evensum = -1;
}
oddplus = !oddplus;
evenplus = !evenplus;
}
cout << fmin(oddcount, evencount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> A(n, 0);
for (int i = 0; i < n; i++) cin >> A[i];
int cnt = 0, acm = 0, ans = 0;
for (int i = 0; i < n; i++) {
acm += A[i];
if (i % 2) {
if (acm > 0)
;
else {
cnt += abs(acm) + 1;
acm = 1;
}
} else {
if (acm < 0)
;
else {
cnt += abs(acm) + 1;
acm = -1;
}
}
}
ans = cnt;
cnt = 0;
acm = 0;
for (int i = 0; i < n; i++) {
acm += A[i];
if ((i + 1) % 2) {
if (acm > 0)
;
else {
cnt += abs(acm) + 1;
acm = 1;
}
} else {
if (acm < 0)
;
else {
cnt += abs(acm) + 1;
acm = -1;
}
}
}
ans = min(ans, cnt);
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ttl = a[0]
cst = 0
if a[0]>0:
flg = 1
elif a[0]<0:
flg = -1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst += memo
flg *= -1
ttl = a[0]
cst2 = 0
print(min(cst,cst2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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() {
const int N = 100000;
int n;
long long a[N];
long long sum = 0, ans = 0;
bool sign1 = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0];
if (sum >= 0)
sign1 = true;
else
sign1 = false;
for (int i = 1; i < n; i++) {
bool sign2 = true;
sum += a[i];
if (sum >= 0)
sign2 = true;
else
sign2 = false;
if (sign1 != sign2) {
if (sum == 0) {
ans++;
if (sign1) {
sum = -1;
sign1 = false;
} else {
sum = 1;
sign1 = true;
}
} else {
sign1 = sign2;
}
} else {
ans += abs(sum) + 1;
if (sign2)
sum = -1;
else
sum = 1;
sign1 = !sign2;
}
}
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 n;
int a[100000], c[2];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int C = -1; C <= 1; C += 2) {
int sign = C;
for (int s = 0, i = 0; i < n; i++, sign *= -1) {
s += a[i];
if (sign * s <= 0) {
c[(C + 1) / 2] += abs(s) + 1;
s = sign;
}
}
}
cout << min(c[0], c[1]);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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;
}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double, long long int>& firstElof,
const pair<double, long long int>& secondElof) {
return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, long long int>& firstElof,
const pair<double, long long int>& secondElof) {
return firstElof.second < secondElof.second;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct edge {
long long int from, to, cost;
};
long long int gcd(long long int a, long long int b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
int main() {
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int x = a[0], ans1 = 0, ans2 = 0;
if (x < 0) {
ans1 += abs(x) + 1;
x = 1;
}
for (long long int i = 1; i < n; i++) {
x += a[i];
if (i % 2) {
if (x > 0) {
ans1 += x + 1;
x = -1;
}
} else {
if (x < 0) {
ans1 += abs(x) + 1;
x = 1;
}
}
}
x = a[0];
if (x > 0) {
ans2 += x + 1;
x = -1;
}
for (long long int i = 1; i < n; i++) {
x += a[i];
if (i % 2 == 0) {
if (x > 0) {
ans2 += x + 1;
x = -1;
}
} else {
if (x < 0) {
ans2 += abs(x) + 1;
x = 1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int sum=0;
int count=0;
for(int i=0;i<n-1;i++){
sum+=a[i];
if(sum>0){
if(sum+a[i+1]>=0){
count+=sum+a[i+1]+1;
a[i+1]-=sum+a[i+1]+1;
}
}else if(sum<0){
if(sum+a[i+1]<=0){
count+=sum+a[i+1]+1;
a[i+1]-=sum+a[i+1]+1;
}
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def count_loop(n, old_state, a_list, flag):
count = 0
for i in range(1, int(n)):
num_state = old_state + int(a_list[i])
if flag == 1:
if num_state == 0:
count += 1
old_state = -1
flag = -1
elif num_state > 0:
count += num_state + 1
old_state = -1
flag = -1
else:
old_state = num_state
flag = -1
elif flag == -1:
if num_state == 0:
count += 1
old_state = 1
flag = 1
elif num_state < 0:
count += abs(num_state) + 1
old_state = 1
flag = 1
else:
old_state = num_state
flag = 1
else:
if num_state == 0:
count += 1
flag = 0
elif num_state > 0:
old_state = num_state
flag = 1
else:
old_state = num_state
flag = -1
return count
if __name__ == "__main__":
n = input()
a = input()
a_list = a.split(" ")
old_state = int(a_list[0])
count = 0
c1 = count_loop(n,old_state,a_list,1)
c2 = count_loop(n,old_state,a_list,-1)
c3 = count_loop(n,old_state,a_list,0)+1
if c1 <= c2 and c1 <= c3:
print(c1)
elif c2 <= c1 and c2 <= c3:
print(c2)
else:
print(c3)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int(i) = 0; (i) < (n); ++(i)) {
cin >> a[i];
}
long long ans;
long long sum[n];
bool plusSumFlg;
ans = 0;
sum[0] = a[0];
if (sum[0] == 0) {
if (a[1] >= 0) {
sum[0]--;
ans++;
} else {
sum[0]++;
ans++;
}
}
plusSumFlg = sum[0] > 0 ? true : false;
for (int i = 1; i < n; ++i) {
sum[i] = sum[i - 1] + a[i];
if (plusSumFlg) {
plusSumFlg = false;
if (sum[i] < 0) {
continue;
} else if (sum[i] > 0) {
ans += sum[i] + 1;
sum[i] = -1;
} else {
sum[i]++;
ans++;
}
} else {
plusSumFlg = true;
if (sum[i] > 0) {
continue;
} else if (sum[i] < 0) {
ans += -sum[i] + 1;
sum[i] = 1;
} else {
sum[i]++;
ans++;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | <?php
$n = trim(fgets(STDIN));
$a = explode(' ',trim(fgets(STDIN)));
$x = $a[0];
$sign_p = ['p'=>-1,'m'=>1];
$sign = [];
if ($a[0] > 0){
$sign = ['p','m'];
} else {
$sign = ['m','p'];
}
$ans = 0;
for($i=1; $i<$n; $i++){
$bx = $x;
$x = $x + $a[$i];
if ($x == 0){
$ans++;
$x = $sign_p[$sign[0]];
$sign = array_reverse($sign);
} else if (($sign[0]=='p' && $x>0) || ($sign[0]=='m' && $x<0)){
$d = $sign_p[$sign[0]] - $bx;
$ans += abs($d-$a[$i]);
$x = $sign_p[$sign[0]];
$sign = array_reverse($sign);
} else {
$sign = array_reverse($sign);
}
}
echo $ans;
?> |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> ads(n);
long long sum = 0;
for (int i = 0; i < n; ++i) {
long long a;
cin >> a;
ads[i] = a + sum;
sum += a;
}
long long ans1 = 0;
long long sum1 = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (ads[i] + sum1 <= 0) {
ans1 += abs(1 - ads[i] - sum1);
sum1 += 1 - ads[i];
}
} else {
if (ads[i] + sum1 >= 0) {
ans1 += abs(-1 - ads[i] - sum1);
sum1 += -1 - ads[i];
}
}
}
long long ans2 = 0;
long long sum2 = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 1) {
if (ads[i] + sum2 <= 0) {
ans2 += abs(1 - ads[i] - sum2);
sum2 += 1 - ads[i];
}
} else {
if (ads[i] + sum2 >= 0) {
ans2 += abs(-1 - ads[i] - sum2);
sum2 += -1 - ads[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 | UNKNOWN | using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Program {
static List<long> rep;
static void Main(string[] args){
//入力を受け取る
var N = long.Parse(Console.ReadLine());
var A = Console.ReadLine().Split().Select(a => long.Parse(a)).ToArray();
long ans = 0;
long sum = A[0];
for(int i =1 ;i <N; i++){
if(sum > 0){
if(sum*(-1) <= A[i]){
ans += (long) Math.Abs(A[i]-(sum*(-1)-1));
A[i] = sum*(-1)-1;
}
sum += A[i];
}else{
if(sum*(-1) >= A[i]){
ans += (long) Math.Abs(A[i]-(sum*(-1)+1));
A[i] = sum*(-1)+1;
}
sum += A[i];
}
}
Console.WriteLine(ans);
}
static int LowerBound(long num){
var l = 0;
var r = rep.Count()-1;
while(l <= r){
var mid = l+(r-l)/2;
if(rep[mid] < num){
l = mid+1;
}else{
r = mid-1;
}
}
return l;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
s = 0
num = 0
sign = "+" if A[0] >= 0 else "-"
for a in A:
s += a
if sign == "+":
if s > 0:
pass
else:
num += abs(s) + 1
s = 1
sign = "-"
continue
if sign == "-":
if s < 0:
pass
else:
num += s + 1
s = -1
sign = "+"
continue
if A[0] != 0:
print(num)
exit()
s = 0
num2 = 0
sign = "-"
for a in A:
s += a
if sign == "+":
if s > 0:
pass
else:
num2 += abs(s) + 1
s = 1
sign = "-"
continue
if sign == "-":
if s < 0:
pass
else:
num2 += s + 1
s = -1
sign = "+"
continue
print(min(num, num2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
long long n, ai, sum = 0, ans = 0;
int sign;
scanf("%lld", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &ai);
if (i == 0) {
if (ai >= 0) sign = 1;
if (ai < 0) sign = -1;
}
sum += ai;
if ((i % 2 == 0 && sign == 1) || (i % 2 == 1 && sign == -1)) {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
if ((i % 2 == 1 && sign == 1) || (i % 2 == 0 && sign == -1)) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
printf("%lld\n", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
lint n, p = 0, q = 0, r = 0, s = 0;
cin >> n;
v1i v(n), w(n), a(n), b(n);
for (int i = 0; i < n; ++i) cin >> v[i];
w[0] = v[0];
for (int i = 0; i < n - 1; ++i) w[i + 1] = w[i] + v[i + 1];
for (int i = 0; i < n; ++i) {
a[i] = w[i];
a[i] += r;
if (i % 2 == 0) {
if (w[i] < 0) {
p += 1 - w[i];
r += 1 - w[i];
}
} else {
if (w[i] > 0) {
p += w[i] + 1;
r -= w[i] + 1;
}
}
}
for (int i = 0; i < n; ++i) {
b[i] = w[i];
b[i] += s;
if (i % 2 == 1) {
if (w[i] < 0) {
q += 1 - w[i];
s += 1 - w[i];
}
} else {
if (w[i] > 0) {
q += w[i] + 1;
s -= w[i] + 1;
}
}
}
cout << min(p, q) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>;
using PII = pair<int_fast32_t, int_fast32_t>;
template <class T>
using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
signed main() {
int START_TIME = clock();
cin.tie(nullptr);
ios::sync_with_stdio(false);
i32 n;
cin >> n;
vi a(n);
for (int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++) cin >> a[i];
i32 ans = 0, sum = a[0];
for (i32 i = 1; i < n; ++i) {
sum += a[i];
if (i & 1) {
if (sum == 0) {
++ans;
sum = 1;
continue;
}
if (sum > 0) continue;
ans += 1 - sum;
sum = 1;
} else {
if (sum == 0) {
++ans;
sum = -1;
continue;
}
if (sum < 0) continue;
ans += sum + 1;
sum = -1;
}
}
i32 ans2 = 0, sum2 = a[0];
for (i32 i = 1; i < n; ++i) {
sum2 += a[i];
if (!(i & 1)) {
if (sum2 == 0) {
++ans2;
sum2 = -1;
continue;
}
if (sum2 > 0) continue;
ans2 += 1 - sum2;
sum2 = 1;
} else {
if (sum2 == 0) {
++ans2;
sum2 = 1;
continue;
}
if (sum2 < 0) continue;
ans2 += sum2 + 1;
sum2 = -1;
}
}
cout << min(ans, ans2) << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strings"
"strconv"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 64*1024*1024), 64*1024*1024)
sc.Scan()
n, _ := strconv.Atoi(sc.Text())
sc.Scan()
aArr := strings.Split(sc.Text(), " ")
a := make([]int, n)
for i := 0; i < n; i++ {
a[i], _ = strconv.Atoi(aArr[i])
}
cnt := 0
sum := a[0]
for i := 1; i < n; i++ {
if (sum+a[i])*sum < 0 {
sum += a[i]
continue
} else if sum+a[i] < 0 {
cnt += 1-(sum+a[i])
sum = 1
} else {
cnt += 1+(sum+a[i])
sum = -1
}
}
fmt.Println(cnt)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll solve(vector<ll>& v, int first) {
ll cost = abs(first - v[0]);
ll sum = first;
for (int i = 1; i < v.size(); ++i) {
ll after = sum + v[i];
if (after * sum < 0) {
sum = after;
} else {
cost += abs(after) + 1;
sum = sum > 0 ? -1 : 1;
}
}
return cost;
}
int main() {
int N;
cin >> N;
vector<ll> v(N);
for (int i = 0; i < N; i++) cin >> v[i];
ll ret = LONG_MAX;
if (v[0] == 0) {
ret = min(ret, solve(v, 1));
ret = min(ret, solve(v, -1));
} else {
ret = min(ret, solve(v, v[0]));
ret = min(ret, solve(v, v[0] > 0 ? -1 : 1));
}
cout << ret << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) cin >> a[i];
ll before = 1, tmp = 0, sum = a[0];
if (a[0] / llabs(a[0]) != 1) {
tmp += 1 - a[0];
sum = 1;
}
for (int i = 1; i < n; i++) {
sum += a[i];
before *= -1;
if (sum == 0) {
sum += before;
tmp++;
}
if (before != (sum / llabs(sum))) {
tmp += llabs(before) + llabs(sum);
sum = before;
}
}
ll ans = tmp;
before = -1, tmp = 0, sum = a[0];
if (a[0] / llabs(a[0]) != -1) {
tmp += 1 + a[0];
sum = -1;
}
for (int i = 1; i < n; i++) {
sum += a[i];
before *= -1;
if (sum == 0) {
sum += before;
tmp++;
}
if (before != (sum / llabs(sum))) {
tmp += llabs(before) + llabs(sum);
sum = before;
}
}
ans = min(ans, tmp);
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 | 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 | UNKNOWN | fun main(args: Array<String>) {
val N = nextInt()
val A = listOfLong()
val B = A.toMutableList()
var ans = 0L
var sum = A[0]
var sign = A[0].sign()
for (n in 1 until N) {
val now = sum + A[n]
when (now.sign()) {
1 ->
if (sign != -1) {
ans += -(-1 - now)
sum = -1
} else sum = now
-1 ->
if (sign != 1) {
ans += (1 - now)
sum = 1
} else sum = now
0 ->
if (sign == 1) {
ans += 1
sum = -1
} else {
ans += 1
sum = 1
}
}
sign = sum.sign()
}
println(ans)
}
fun Long.sign() = if (this > 0) 1 else if (this < 0) -1 else 0
fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)
fun listOfLong() = listOfString().map(String::toLong)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (int i = 0; i < ((int)v.size()); ++i) {
if (i) os << ",";
os << v[i];
}
os << "}";
return os;
}
template <typename T, size_t S>
void printArray(const T (&array)[S]) {
for (auto val : array) std::cout << val << ", ";
std::cout << "\n";
}
const int mod = 1e9 + 7;
const int inf = 1e9 + 5;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
int n;
std::cin >> n;
vi a(n);
for (int i = 0; i < (n); ++i) std::cin >> a[i];
ll mp = 0, pm = 0, tot = 0;
bool plus = true;
for (int i = 0; i < (n); ++i) {
tot += a[i];
if (plus && tot < 1)
pm += (-1 * tot) + 1, tot = 1;
else if (!plus && tot > 0)
pm += tot + 1, tot = -1;
plus ^= 1;
}
plus = false;
tot = 0;
for (int i = 0; i < (n); ++i) {
tot += a[i];
if (plus && tot < 1)
mp += (-1 * tot) + 1, tot = 1;
else if (!plus && tot > 0)
mp += tot + 1, tot = -1;
plus ^= 1;
}
std::cout << min(pm, mp) << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int c1 = 0, s1 = 0, c2 = 0, s2 = 0;
for (int i = 1; i <= n; i++) {
s1 += a[i];
s2 += a[i];
if (i % 2 == 0) {
if (s1 <= 0) {
c1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
c2 += s2 + 1;
s2 = -1;
}
} else {
if (s2 <= 0) {
c2 += 1 - s2;
s2 = 1;
}
if (s1 >= 0) {
c1 += s1 + 1;
s1 = -1;
}
}
}
cout << min(c1, c2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count1 = 0;
int count2 = 0;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count1++;
}
} else {
while (sum <= 0) {
sum++;
count1++;
}
}
}
} else {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count1++;
}
} else {
while (sum <= 0) {
sum++;
count1++;
}
}
}
}
sum = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count2++;
}
} else {
while (sum <= 0) {
sum++;
count2++;
}
}
}
} else {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count2++;
}
} else {
while (sum <= 0) {
sum++;
count2++;
}
}
}
}
if (count1 >= count2)
cout << count2 << endl;
else
cout << count1 << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long gcm(long long a, long long b);
long long lcm(long long a, long long b);
long long fac(long long a);
int main() {
long long n;
cin >> n;
long long nowsum = 0;
long long ans = 0;
long long sum = 0;
bool plus = false;
bool minus = false;
vector<long long> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
sum += A[i];
}
for (int i = 0; i < n; i++) {
nowsum += A[i];
if (i == 0) {
if (nowsum == 0) {
if (A[1] > 0) {
ans = A[i] - 1;
nowsum = (-1) * abs(A[i] - 1);
minus = true;
} else if (A[1] < 0) {
ans = A[i] + 1;
nowsum = abs(A[i] - 1);
plus = true;
} else {
if (sum >= 0) {
ans = 1;
nowsum = 1;
plus = true;
} else {
ans = 1;
nowsum = -1;
minus = true;
}
}
} else if (nowsum >= 0)
plus = true;
else
minus = true;
} else {
if (plus) {
if (i % 2 == 0) {
if (nowsum <= 0) {
ans += abs(nowsum - A[i] - 1) - A[i];
nowsum = 1;
}
} else {
if (nowsum >= 0) {
ans += A[i] + abs(nowsum - A[i] + 1);
nowsum = -1;
}
}
} else if (minus) {
if (i % 2 == 0) {
if (nowsum >= 0) {
ans += A[i] + abs(nowsum - A[i] + 1);
nowsum = -1;
}
} else {
if (nowsum <= 0) {
ans += abs(nowsum - A[i] - 1) - A[i];
nowsum = 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;
const int maxn = 1e5 + 10;
int a[maxn];
int main() {
int n;
long long sum;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sum = a[0];
long long cnt = 0;
int flag = 0;
if (sum == 0) {
int i = 1;
while (a[i] == 0 && i < n) i++;
if (i == n) {
cnt = (n - 1) * 2 + 1;
printf("%lld\n", cnt);
return 0;
}
if (a[i] > 0 && (i & 1 == 1)) {
sum = -1;
cnt++;
} else if (a[i] > 0 && (i & 1 == 0)) {
sum = 1;
cnt++;
} else if (a[i] < 0 && (i & 1 == 1)) {
sum = 1;
cnt++;
} else if (a[i] < 0 && (i & 1 == 0)) {
sum = -1;
cnt++;
}
} else if (sum > 0) {
int f = 1;
int t = sum;
for (int i = 1; i < n; i++) {
t += a[i];
if ((f == 1 && t >= 0) || (f == -1 && t <= 0)) {
flag = 1;
break;
} else
f = -f;
}
} else {
int f = 1;
int t = sum;
for (int i = 1; i < n; i++) {
t += a[i];
if ((f == 1 && t <= 0) || (f == -1 && t >= 0)) {
flag = 1;
break;
} else
f = -f;
}
}
if (!flag) {
cout << "0" << endl;
return 0;
}
for (int i = 1; i < n - 1; i++) {
if (sum > 0) {
int t = sum + a[i];
if (t == -1)
sum = t;
else {
if (t < 0) {
int b = abs(-1 - t);
cnt += b;
sum = -1;
} else {
int b = abs(1 + t);
cnt += b;
sum = -1;
}
}
} else if (sum < 0) {
int t = sum + a[i];
if (t == 1)
sum = t;
else {
if (t > 0) {
int b = abs(t - 1);
cnt += b;
sum = 1;
} else {
int b = abs(1 - t);
cnt += b;
sum = 1;
}
}
}
}
if (sum > 0) {
int t = sum + a[n - 1];
if (t < 0) {
cnt -= (-1 - t);
} else {
cnt += (t + 1);
}
} else {
int t = sum + a[n - 1];
if (t > 0) {
cnt -= (t - 1);
} else
cnt += (1 - t);
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
long long sum = a[0];
if (sum == 0) {
ans++;
sum = 1;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) >= 0) {
ans += abs((sum + a[i]) - (-sum / abs(sum)));
sum = -sum / abs(sum);
} else
sum += a[i];
}
long long t = 0;
sum = -1;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) >= 0) {
t += abs((sum + a[i]) - (-sum / abs(sum)));
sum = -sum / abs(sum);
} else
sum += a[i];
}
ans = min(t, ans);
} else {
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) >= 0) {
ans += abs((sum + a[i]) - (-sum / abs(sum)));
sum = -sum / abs(sum);
} else
sum += a[i];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
a = input().split()
a = list(map(lambda x: x.capitalize(), a))
a,b,c = a
print(a[0]+b[0]+c[0])
return
#B
def B():
a = II()
b = II()
if a > b:
print("GREATER")
if a < b:
print("LESS")
if a == b:
print("EQUAL")
return
#C
def C():
II()
a = LI()
def f(suma, b):
for i in a[1:]:
if (suma + i) * suma < 0:
suma += i
continue
b += abs(suma + i) + 1
suma = -1 * (suma > 0) or 1
return b
if a[0] == 0:
ans = f(1, 1)
else:
ans = f(a[0], 0)
if a[0] == 0:
ans = min(ans, f(-1, 1))
else:
ans = min(ans, f(-a[0], 2 * abs(a[0]) + 1))
print(ans)
return
#D
def D():
s = S()
for i in range(len(s) - 1):
if s[i] == s[i+1]:
print(i + 1, i + 2)
return
for i in range(len(s) - 2):
if s[i] == s[i + 2]:
print(i + 1, i + 3)
return
print(-1, -1)
return
#Solve
if __name__ == '__main__':
C()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
struct Accelerate_Cin {
Accelerate_Cin() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
};
signed main() {
int n;
cin >> n;
static int a[3][100010], sum[3][100010];
for (int t = 0; t < n; t++) cin >> a[0][t];
for (int t = 0; t < n; t++) a[1][t] = a[0][t];
int ans = INF;
for (int i = 0; i < 2; i++) {
int cont = 0;
for (int t = 0; t < n; t++) {
if (a[i][0] == 0 && i == 0) {
a[i][0] = 1;
cont++;
}
if (a[i][0] == 0 && i == 1) {
a[i][0] = -1;
cont++;
}
sum[i][0] = a[i][0];
if (t != 0) sum[i][t] = sum[i][t - 1] + a[i][t];
if (sum[i][t] * sum[i][t - 1] >= 0 && t != 0) {
cont += abs(sum[i][t]) + 1;
if (sum[i][t - 1] > 0) sum[i][t] = -1;
if (sum[i][t - 1] < 0) sum[i][t] = 1;
}
}
ans = min(ans, cont);
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll atcoder_mod = 1e9 + 7;
template <typename T = int>
T in() {
T x;
cin >> x;
return (x);
}
template <typename T = int, typename C = vector<T>>
C vecin(int N) {
C x(N);
for (int i = 0; i < (N); ++i) {
x[i] = in<T>();
}
return move(x);
}
void vout() { cout << endl; }
template <typename Head, typename... Tail>
void vout(Head&& h, Tail&&... t) {
cout << ' ' << h;
vout(forward<Tail>(t)...);
}
void out() { cout << endl; }
template <typename Head, typename... Tail>
void out(Head&& h, Tail&&... t) {
cout << h;
vout(forward<Tail>(t)...);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
int N = in();
auto V = vecin<ll>(N);
ll S1 = V[0], A1 = 0;
for (int i = 1; i < N; i++) {
if (S1 > 0) {
if (S1 + V[i] < 0) {
S1 += V[i];
} else {
A1 += S1 + V[i] + 1LL;
S1 = -1;
}
} else {
if (S1 + V[i] > 0) {
S1 += V[i];
} else {
A1 += abs(S1 + V[i]) + 1LL;
S1 = 1;
}
}
}
ll S2 = -V[0], A2 = 0;
for (int i = 1; i < N; i++) {
if (S2 > 0) {
if (S2 + V[i] < 0) {
S2 += V[i];
} else {
A2 += S2 + V[i] + 1LL;
S2 = -1;
}
} else {
if (S2 + V[i] > 0) {
S2 += V[i];
} else {
A2 += abs(S2 + V[i]) + 1LL;
S2 = 1;
}
}
}
out(min(A1, A2));
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;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int sum1 = 0, cost1 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
int diff = abs(sum1) + 1;
if (i % 2 == 0 && sum1 < 0) {
sum1 += diff;
cost1 += diff;
}
if (i % 2 == 1 && sum1 > 0) {
sum1 -= diff;
cost1 += diff;
}
if (sum1 == 0) {
if (i % 2 == 0) {
sum1++;
cost1++;
}
if (i % 2 == 1) {
sum1--;
cost1++;
}
}
}
int sum2 = 0, cost2 = 0;
for (int i = 0; i < n; i++) {
sum2 += a[i];
int diff = abs(sum2) + 1;
if (i % 2 == 0 && sum2 > 0) {
sum2 -= diff;
cost2 += diff;
}
if (i % 2 == 1 && sum2 < 0) {
sum2 += diff;
cost2 += diff;
}
if (sum2 == 0) {
if (i % 2 == 0) {
sum2--;
cost2++;
}
if (i % 2 == 1) {
sum2++;
cost2++;
}
}
}
cout << min(cost1, cost2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
bool code(long long int n) {
if (n < 0)
return 1;
else if (n > 0)
return 0;
}
int main() {
int n;
long long int sum = 0;
long long int ans = 0;
long long int ans2 = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
sum = a.at(0);
if (a.at(0) != 0) {
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << ans << endl;
return 0;
} else if (a.at(0) == 0) {
sum = -1;
ans = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
sum = 1;
ans2 = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans2++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans2 += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << min(ans, ans2) << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
*A, = map(int,input().split())
ans = 0
S = A[0]
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans += abs(S1)+1
S1 = -S//abs(S)
S = S1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"fmt"
"math"
)
func main() {
var n int
fmt.Scan(&n)
sum := 0
ans := 0
for i := 0; i < n; i++ {
var tmp int
fmt.Scan(&tmp)
prev := sum
sum += tmp
if prev < 0 && sum <= 0 {
ans += abs(sum) + 1
sum = 1
} else if prev > 0 && sum >= 0 {
ans += abs(sum) + 1
sum = -1
}
}
fmt.Println(ans)
}
func abs(x int) int {
return int(math.Abs(float64(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 | python3 | #
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
r_ = n / m
if (r_ - (n // m)) > 0:
return (n//m) + 1
else:
return (n//m)"""
""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
fac[0] = 1 % mod
for i in range(1,N):
fac[i] = fac[i-1] * i % mod"""
"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
pw[0] = 1 % mod
for i in range(1,N):
pw[i] = pw[i-1] * c % mod"""
"""
def YEILD():
yield 'one'
yield 'two'
yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
if b == 0:#結局はc,0の最大公約数はcなのに
return a
return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
d = a
if b!=0:
d = extgcd(b,a%b,y,x)
y -= (a//b) * x
print(x,y)
else:
x = 1
y = 0
return d"""
def readInts():
return list(map(int,input().split()))
mod = 10**9 + 7
def main():
n = int(input())
A = readInts()
# 符号 positive?
#po_ = True
# 変わったか変わってないか
if A[0] >= 0: # if positive
po_ = True
else: # negative
po_ = False
Cost = 0
ANS = [0] * (n+1)
ANS[0] = A[0]
for i in range(1,n):
#print(ANS[i-1],po_,ANS[i-1] + A[i],Cost)
if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった
po_ = True
ANS[i] = ANS[i-1] + A[i]
# これで終わり
elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ?
# 負にしなければならない
Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
A[i] += -1 - (ANS[i-1] + A[i])
# -4
ANS[i] = ANS[i-1] + A[i]
po_ = False
elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
# -1 はここ
# print(A[i])
Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
A[i] += 1 - (ANS[i-1] + A[i])
ANS[i] = ANS[i-1] + A[i]
po_ = True
elif ANS[i-1]+A[i] == 0 and po_: # nega: pos
po_ = False
A[i] += 1
Cost += 1
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1]+A[i] < 0 and po_:
po_ = False
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1]+A[i] == 0 and not po_:
po_ = True
A[i] += 1
Cost += 1
ANS[i] = ANS[i-1] + A[i]
print(Cost)
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
int ans = 0;
int positive = 1;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (sum == 0) {
sum += a;
} else if (sum > 0) {
if (sum + a >= 0) {
ans += abs(-sum - 1 - a);
sum = -1;
} else {
sum += a;
}
} else {
if (sum + a <= 0) {
ans += abs(-sum + 1 - a);
sum = 1;
} else {
sum += a;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int sumi = a[0];
int sump = a[0];
int cnt = 0;
for (int i = 0; i < n - 1; i++) {
sump += a[i + 1];
if (sumi < 0) {
if (sump <= 0) {
cnt += 1 - sump;
sump = 1;
}
} else if (sumi > 0) {
if (sump >= 0) {
cnt += sump + 1;
sump = -1;
}
}
sumi = sump;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
const int MOD = INF + 7;
struct edge {
int to, cost;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i < n; i++) {
a[i] += a[i - 1];
}
long long sum = 0;
long long ans[2] = {};
long long tmp;
for (int i = 0; i <= 1; i++) {
sum = 0;
for (int j = 0; j < n; j++) {
tmp = abs(a[j] + sum);
if ((i + j) % 2 == 0 && a[j] + sum <= 0) {
ans[i] += tmp + 1;
sum += tmp + 1;
}
if ((i + j) % 2 == 1 && a[j] + sum >= 0) {
ans[i] += tmp + 1;
sum -= tmp + 1;
}
}
}
cout << min(ans[0], ans[1]) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
for (int i = 0; i < n - 1; i++) {
int sum = 0;
for (int j = i; j >= 0; j--) sum += a.at(j);
while (sum * a.at(i + 1) >= 0 || sum + a.at(i + 1) == 0) {
if (a.at(i) > 0) {
a.at(i + 1)--;
ans++;
}
if (a.at(i) < 0) {
a.at(i + 1)++;
ans++;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long[] map = new long[N];
for (int i = 0; i < N; i++) {
map[i] = sc.nextLong();
}
long ans1 = 0;
long ai = map[0];
if (map[0] < 0) {
ans1 += Math.abs(map[0] - 1);
ai = 1;
}
for (int i = 1; i < N; i++) {
long a = map[i];
long total = ai + a;
if (ai > 0) {
if (total < 0) {
ai = total;
continue;
} else {
long tmp = total + 1;
ans1 += Math.abs(tmp);
ai = -1;
}
} else if(ai < 0) {
if (total > 0) {
ai = total;
continue;
} else {
long tmp = total - 1;
ans1 += Math.abs(tmp);
ai = 1;
}
}
}
long ans2 = 0;
if (map[0] > 0) {
ans2 += Math.abs(map[0] + 1);
ai = -1;
}
for (int i = 1; i < N; i++) {
long a = map[i];
long total = ai + a;
if (ai > 0) {
if (total < 0) {
ai = total;
continue;
} else {
long tmp = total + 1;
ans2 += Math.abs(tmp);
ai = -1;
}
} else if(ai < 0) {
if (total > 0) {
ai = total;
continue;
} else {
long tmp = total - 1;
ans2 += Math.abs(tmp);
ai = 1;
}
}
}
System.out.println(Math.min(ans1, ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void DEBUG(T e) {
if (true == false) return;
std::cout << e << " ";
}
template <typename T>
void DEBUG(const std::vector<T>& v) {
if (true == false) return;
for (const auto& e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T>
void DEBUG(const std::vector<std::vector<T> >& vv) {
if (true == false) return;
for (const auto& v : vv) {
DEBUG(v);
}
}
template <class T, class... Ts>
void DEBUG(T d, Ts... e) {
if (true == false) return;
DEBUG(d);
DEBUG(e...);
}
template <class T>
void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
return a > b && (a = b, true);
}
void solve(void) {
long long N;
cin >> N;
vector<long long> vec(N, 0);
for (long long i = 0; i < N; i++) {
cin >> vec[i];
}
long long SUM = 0;
long long ANS = 0;
for (long long i = 0; i < N; i++) {
if (i == 0) {
SUM += vec[i];
continue;
}
long long tmp = SUM + vec[i];
if (tmp * SUM >= 0) {
if (SUM >= 0)
vec[i] -= abs(tmp) + 1;
else {
vec[i] += abs(tmp) + 1;
}
ANS += abs(abs(tmp) + 1);
}
SUM += vec[i];
}
cout << ANS << endl;
return;
}
int32_t main(int32_t argc, const char* argv[]) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed;
std::cout << std::setprecision(9);
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void scan(vector<T>& a, long long n, istream& cin) {
T c;
for (long long(i) = 0; (i) < (n); ++(i)) {
cin >> c;
a.push_back(c);
}
}
using vs = vector<string>;
using vi = vector<long long>;
using pii = pair<long long, long long>;
using psi = pair<string, long long>;
using vvi = vector<vi>;
template <class T>
bool valid(T x, T w) {
return 0 <= x && x < w;
}
long long dx[4] = {1, -1, 0, 0};
long long dy[4] = {0, 0, 1, -1};
vi a;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
;
long long n;
cin >> n;
for (long long(i) = 0; (i) < (n); ++(i)) {
long long c;
cin >> c;
a.push_back(c);
}
long long k = a[0];
bool f = k > 0;
long long ans = 0;
for (long long(i) = (1); (i) < (n); ++(i)) {
if (k == 0) {
if (f) {
k--;
ans++;
} else {
k++;
ans++;
}
f = !f;
continue;
}
k += a[i];
if (f) {
if (k < 0) {
f = !f;
continue;
} else {
ans += k + 1;
k = -1;
f = !f;
continue;
}
} else {
if (k > 0) {
f = !f;
continue;
} else {
ans += (-k) + 1;
k = 1;
f = !f;
continue;
}
}
}
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 a[100005];
int sum[100005];
int main() {
int n;
scanf("%d", &n);
int ans = 0;
scanf("%d", &a[1]);
if (a[1] == 0) {
a[1] = 1;
ans++;
}
sum[1] = a[1];
for (int i = 2; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
if (sum[i] == 0 && sum[i - 1] < 0) {
sum[i] += 1;
a[i] += 1;
ans++;
} else if (sum[i] == 0 && sum[i - 1] > 0) {
sum[i] -= 1;
a[i] -= 1;
ans++;
} else if (sum[i - 1] < 0 && sum[i] < 0) {
int t = -sum[i];
sum[i] += t + 1;
a[i] += t + 1;
ans += t + 1;
} else if (sum[i - 1] > 0 && sum[i] > 0) {
int t = sum[i];
sum[i] -= t + 1;
a[i] -= t + 1;
ans += t + 1;
}
}
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #[macro_use]
mod input {
use std;
use std::io;
const SPLIT_DELIMITER: char = ' ';
#[macro_export]
#[allow(unused_macros)]
macro_rules! input {
( $($x:expr ),*) => {
{
let temp_str = input_line_str();
let mut split_result_iter = temp_str.split_whitespace();
$(
let buf_split_result = split_result_iter.next();
let buf_split_result = buf_split_result.unwrap();
($x) = buf_split_result.parse().unwrap();
)*
}
};
}
#[allow(dead_code)]
pub fn input_line_str() -> String {
let mut s = String::new();
io::stdin().read_line(&mut s).unwrap();
s.trim().to_string()
}
#[allow(dead_code)]
pub fn p<T>(t: T)
where
T: std::fmt::Display,
{
println!("{}", t);
}
#[allow(dead_code)]
pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
where
T: std::str::FromStr,
{
let mut v: Vec<Vec<T>> = Vec::new();
for _ in 0..line {
let vec_line = input_vector();
v.push(vec_line);
}
v
}
#[allow(dead_code)]
pub fn input_vector<T>() -> Vec<T>
where
T: std::str::FromStr,
{
let mut v: Vec<T> = Vec::new();
let s = input_line_str();
let split_result = s.split(SPLIT_DELIMITER);
for z in split_result {
let buf = match z.parse() {
Ok(r) => r,
Err(_) => panic!("Parse Error"),
};
v.push(buf);
}
v
}
#[allow(dead_code)]
pub fn str2vec(s: &str) -> Vec<char> {
let mut v: Vec<char> = Vec::new();
for c in s.chars() {
v.push(c);
}
v
}
}
use input::*;
use std::cmp;
fn main() {
let n: usize;
let mut sum = 0;
let mut ans1 = 0;
let mut ans2 = 0;
let mut flag = true;
input!(n);
let s = input_vector::<isize>();
for z in s.iter() {
sum += z;
if flag {
if sum <= 0 {
ans1 += -1 * sum + 1;
sum = 1;
}
flag = false;
} else {
if sum >= 0 {
ans1 += sum + 1;
sum = -1;
}
flag = true;
}
}
flag = false;
sum = 0;
for z in s.iter() {
sum += z;
if flag {
if sum <= 0 {
ans2 += -1 * sum + 1;
sum = 1;
}
flag = false;
} else {
if sum >= 0 {
ans2 += sum + 1;
sum = -1;
}
flag = true;
}
}
p(cmp::min(ans1, ans2));
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
class Program
{
#region Lib
static int Gcd(int a, int b)
{
if (a % b == 0)
return b;
else
return Gcd(b, a % b);
}
#endregion
static void Main()
{
ReadInts();
var nums = ReadInts();
var e = nums.AsEnumerable<int>().GetEnumerator();
var zeroCount = 0;
var f = false;
while ((f = e.MoveNext()) && e.Current == 0)
zeroCount++;
var sum = 0;
var count = 0;
if (zeroCount != 0)
{
count = zeroCount * 2 - 1;
}
while (f)
{
var c = e.Current;
var ps = Math.Sign(sum);
var ns = Math.Sign(sum + c);
if (ps == 0)
ps = -ns;
if (ns == 0)
{
sum = -ps;
count++;
}
else if (ps == ns)
{
count += Math.Abs(sum + c) + 1;
sum = -ps;
}
else
{
sum += c;
}
f = e.MoveNext();
}
Console.WriteLine(count);
}
#region Utils
public static int[] ReadInts()
{
return Console.ReadLine().Split().Select(int.Parse).ToArray();
}
#endregion
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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) {
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 ans1 = 0, ans2 = 0;
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0) {
ans1 += max(0, -sum1 + 1);
ans2 += max(0, sum2 + 1);
sum1 += max(0, -sum1 + 1);
sum2 -= max(0, sum2 + 1);
} else {
ans1 += max(0, sum1 + 1);
ans2 += max(0, -sum2 + 1);
sum1 -= max(0, sum1 + 1);
sum2 += max(0, -sum2 + 1);
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long cnt1 = 0;
long long cnt2 = 0;
for (int i = 0; i < n; i += 2) {
if (a[i] <= 0) {
cnt1 += 1 - a[i];
}
}
for (int i = 1; i < n; i += 2) {
if (0 <= a[i]) {
cnt1 += 1 + a[i];
}
}
for (int i = 0; i < n; i += 2) {
if (0 <= a[i]) {
cnt2 += 1 + a[i];
}
}
for (int i = 1; i < n; i += 2) {
if (a[i] <= 0) {
cnt2 += 1 - a[i];
}
}
long long ans = min(cnt1, cnt2);
cout << ans;
cout << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.ByteString.Char8 as BC
import Data.Maybe (fromJust)
main = do
n <- readLn :: IO Int
(a:as) <- getIntListBC
let ans1 = solve a as
x = (+1) $ abs a
ans2 = x + solve (a+x) as
ans3 = x + solve (a-x) as
print $ minimum [ans1, ans2, ans3]
bsToInt :: BC.ByteString -> Int
bsToInt = fst . fromJust . BC.readInt
getIntListBC :: IO [Int]
getIntListBC = map bsToInt . BC.words <$> BC.getLine
solve :: Int -> [Int] -> Int
solve _ [] = 0
solve s (a:as)
| s > 0 = let n = negate $ s + 1
in if n > a then solve (s + a) as
else (abs $ a - n) + solve (s + n) as
| otherwise = let n = negate $ s - 1
in if n < a then solve (s + a) as
else (abs $ n - a) + solve (s + n) as
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 = 2e18;
const long long MOD = 1e9 + 7;
long long N;
long long a[100010];
long long Calc() {
long long ret = 0;
long long sum = a[0];
long long preSum;
for (long long i = 1; i < N; i++) {
preSum = sum;
sum += a[i];
if (sum == 0 || ((preSum < 0) ^ (sum > 0))) {
if (preSum > 0) {
ret += abs(-1 - sum);
sum = -1;
} else {
ret += abs(1 - sum);
sum = 1;
}
}
}
return ret;
}
int main() {
cin >> N;
for (long long i = 0; i < N; i++) cin >> a[i];
if (a[0] == 0) {
a[0] = 1;
long long tmp = Calc();
a[0] = -1;
cout << min(tmp, Calc()) + 1 << endl;
return 0;
}
cout << Calc() << 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;
template <typename T>
bool PN(T x) {
if (x <= 1) return false;
if (x == 2) return true;
for (int i = 2; i < sqrt(x) + 1; i++)
if (x % i == 0) return false;
return true;
}
const long long MOD = 1e9 + 7;
void solve() {
int n;
cin >> n;
int a[n];
long long sum = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = 0;
for (int i = 0; i < n; ++i) {
if (i == 0) {
sum += a[i];
continue;
}
if (sum > 0) {
sum += a[i];
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else if (sum < 0) {
continue;
} else {
ans++;
sum = -1;
}
} else if (sum < 0) {
sum += a[i];
if (sum > 0) {
continue;
} else if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
} else {
ans++;
sum = 1;
}
}
}
cout << ans << endl;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys, os
f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
sys.stdin = open('./input.txt', 'r')
def solve():
n = f()[0]
a = f()
suma = [0] * n
minop = 1e9
for greater0 in [True, False]:
oper = 0
for i in range(n):
if i == 0:
suma[i] = a[i]
if suma[i]<0:
greater0 = False
else:
suma[i] = a[i] + suma[i-1]
greater0 = not greater0
if greater0 and suma[i]<=0:
oper += 1 - suma[i]
suma[i] = 1
continue
if (not greater0) and suma[i]>=0:
oper += 1 + suma[i]
suma[i] = -1
continue
minop = min(minop, oper)
print(oper)
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = INF;
for (long long i = 0; i < 2; ++i) {
long long sum = 0;
long long cnt = 0;
for (long long j = 0; j < n; ++j) {
sum += a[j];
if (j % 2 == i && sum <= 0) {
cnt += 1 - sum;
sum = 1;
} else if (j % 2 == i - 1 && sum >= 0) {
cnt += 1 + sum;
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;
using ll = long long;
using ull = unsigned long long;
using unsi = unsigned;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using db = double;
using plex = complex<double>;
using vs = vector<string>;
template <class T>
inline bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool amin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const db EPS = 1e-9;
const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1},
dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
signed main() {
int n;
cin >> n;
int odd{};
int ans{};
int even{};
vector<int> a(n);
for (auto i = 0; i != n; ++i) {
cin >> a.at(i);
}
if (a[0] < 0) {
for (auto i = 0; odd < n; ++i) {
odd = 2 * i + 1;
while (a[odd] <= 0) {
++a[odd];
++ans;
}
}
}
if (a[0] = 0) {
++a[0];
}
if (a[0] > 0) {
for (auto i = 1; even < n; ++i) {
even = 2 * i;
while (a[even] > 0) {
++a[even];
++ans;
}
}
}
cout << 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 | python3 | N = int(input())
A = list(map(int,input().split()))
#print(A)
def getSign(a):
if a < 0:
return -1
elif a == 0:
return 0
else:
return 1
count = 0
sumN = A[0]
beforeSign = getSign(A[0])
for i in range(1,N):
sumN += A[i]
#print("be",i,sumN,A[i],count)
if 0 <= beforeSign * sumN:
add = -sumN - beforeSign
A[i] += add
sumN += add
count += abs(add)
beforeSign = getSign(sumN)
#print("af",i,sumN,A[i],count)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using namespace std;
#if __has_include("print.hpp")
#include "print.hpp"
#endif
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007
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; }
typedef long long ll;
typedef pair<ll, ll> p;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
rep(i, n) cin >> v[i];
ll sum = v[0];
ll res = 0;
if(sum == 0){
sum = 1;
res = 1;
}
for (int i = 1; i < n; ++i) {
// cout << i << del;
if((sum + v[i]) * sum < 0){
sum += v[i];
}else{
ll t = -sum - (sum / abs(sum)) ;
ll remain = t - v[i];
// cout << t << endl;
// cout << remain << endl;
res += abs(remain);
sum += t;
v[i] += t;
}
// cout << sum << endl;
// cout << "===" << endl;
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t) {
stringstream ss;
ss << f;
ss >> t;
}
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < int(n); ++i) {
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for (int i = 0; i < int(n); ++i) {
long long nextSum = sum + a[i];
if (nextSum == 0) {
if (i == 0) {
if (n >= 2) {
if (a[1] > 0)
--a[0];
else
++a[0];
} else {
++a[0];
}
} else {
if (sum > 0) {
--a[i];
} else {
++a[i];
}
}
nextSum = sum + a[i];
++ans;
}
if ((i > 0) && (sum * nextSum > 0)) {
a[i] += (nextSum > 0 ? -1 : 1) * (abs(nextSum) + 1);
ans += abs(nextSum) + 1;
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100000], ans = 0, sum = 0, tmp;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
ans += abs(sum);
ans++;
sum = 1;
}
if (i % 2 != 0 && sum >= 0) {
ans += sum;
ans++;
sum = -1;
}
}
tmp = ans;
ans = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
ans += sum;
ans++;
sum = -1;
}
if (i % 2 != 0 && sum <= 0) {
ans += abs(sum);
ans++;
sum = 1;
}
}
cout << min(tmp, 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>
const long long int INF = 1e9;
using namespace std;
struct Point {
int x, y;
};
bool vector_finder(std::vector<int> vec, int number) {
auto itr = std::find(vec.begin(), vec.end(), number);
size_t index = std::distance(vec.begin(), itr);
if (index != vec.size()) {
return true;
} else {
return false;
}
}
long long int factorial(long long int N) {
long long int ans = 1;
for (long long int i = 1; i <= (long long int)(N); i++) {
ans *= i;
}
return ans;
}
vector<long long int> Eratosthenes(long long int N) {
bool arr[N + 1];
arr[0] = false;
arr[1] = false;
for (long long int i = 2; i < N + 1; i++) {
arr[i] = true;
}
for (long long int i = 2; i <= sqrt(N); i++) {
if (arr[i]) {
for (long long int j = 0; i * (j + 2) <= N; j++) {
arr[i * (j + 2)] = false;
}
}
}
vector<long long int> prime;
for (long long int i = 1; i <= (long long int)(N); i++) {
if (arr[i] == true) {
prime.push_back(i);
}
}
return prime;
}
char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int main() {
long long int n;
cin >> n;
long long int sum[n + 2];
long long int A[n + 2];
for (long long int i = 1; i <= (long long int)(n); i++) {
cin >> A[i];
}
sum[0] = 0;
sum[1] = A[1];
long long int cnt = 0;
for (long long int i = 2; i <= n; i++) {
sum[i] = sum[i - 1] + A[i];
if (i % 2 == 0) {
if (sum[i] > -1) {
long long int new_sum = -1;
cnt += abs(new_sum - sum[i]);
sum[i] = new_sum;
}
} else {
if (sum[i] < 1) {
long long int new_sum = 1;
cnt += abs(new_sum - sum[i]);
sum[i] = new_sum;
}
}
}
long long int cnt2 = 0;
for (long long int i = 2; i <= n; i++) {
sum[i] = sum[i - 1] + A[i];
if (i % 2 == 1) {
if (sum[i] > -1) {
long long int new_sum = -1;
cnt2 += abs(new_sum - sum[i]);
sum[i] = new_sum;
}
} else {
if (sum[i] < 1) {
long long int new_sum = 1;
cnt2 += abs(new_sum - sum[i]);
sum[i] = new_sum;
}
}
}
cout << min(cnt, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100001];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
for (int i = 1; i < N; i++) {
a[i] += a[i - 1];
}
long long add = 0, res1 = 0, res2 = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (a[i] + add > 0) {
res1 += abs(a[i] + add + 1);
add -= (a[i] + add + 1);
} else if (a[i] + add == 0) {
add--;
res1++;
}
} else {
if (a[i] + add < 0) {
cout << a[i] << endl;
res1 += abs(-a[i] + add + 1);
add += (-a[i] + add + 1);
} else if (a[i] + add == 0) {
add++;
res1++;
}
}
}
cout << res1 << endl;
add = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 1) {
if (a[i] + add > 0) {
res2 += abs(a[i] + add + 1);
add -= (a[i] + add + 1);
} else if (a[i] == 0) {
add--;
res2++;
}
} else {
if (a[i] + add < 0) {
res2 += abs(-a[i] + add + 1);
add += (-a[i] + add + 1);
} else if (a[i] == 0) {
add++;
res2++;
}
}
}
cout << min(res1, res2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
bool plus;
int first;
cin >> first;
if (first > 0) {
plus = true;
} else if (first < 0) {
plus = false;
} else {
count++;
first++;
plus = true;
}
int sum = first;
for (int i = 0; i < n - 1; i++) {
plus = !plus;
int a;
cin >> a;
sum += a;
if (plus) {
if (sum > 0) {
continue;
} else {
count += 1 - sum;
sum = 1;
}
} else {
if (sum < 0) {
continue;
} else {
count += 1 + sum;
sum = -1;
}
}
}
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;
const int ms = 1e5 + 9;
int val;
int vet[ms];
int main() {
int f = 0;
long long soma = 0, ans = 0;
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> vet[i];
soma = vet[0];
if (soma < 0)
f = 1;
else if (soma == 0 and vet[1] < 0) {
ans++;
soma++;
} else if (soma == 0 and vet[1] >= 0) {
ans++;
soma--;
f = 1;
}
for (int i = 1; i < n; i++) {
val = vet[i];
soma += val;
if (f) {
if (soma == 0) {
ans += 1;
soma++;
} else if (soma < 0) {
ans += ((-soma) + 1);
soma = 1;
}
} else {
if (soma == 0) {
ans++;
soma--;
} else if (soma > 0) {
ans += (soma + 1);
soma = -1;
}
}
f = 1 - f;
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> S(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> S[i];
S[i] += S[i - 1];
}
int ans = 0;
int add = 0;
int sign = ((S[1] >> 31) << 1) + 1;
for (int i = 2; i <= N; ++i) {
S[i] += add;
int sign_i = ((S[i] >> 31) << 1) + 1;
if (sign_i == sign) {
ans += abs(-sign_i - S[i]);
add += -sign_i - S[i];
S[i] = -sign_i;
sign_i = -sign_i;
} else if (S[i] == 0) {
ans += 1;
add += -sign;
S[i] += -sign;
sign_i = -sign;
}
sign = sign_i;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
import copy
N=int(input())
l=list(map(int, input().split()))
cp = copy.copy(l)
if l[0]!=0:
for k in range(1,N):
if sum(l[:k])*sum(l[:k+1])>0:
l[k]=-np.sign(sum(l[:k]))-sum(l[:k])
if sum(l)==0:
print(1+sum([abs(l[n]-cp[n]) for n in range(N)]))
else:
print(sum([abs(l[n]-cp[n]) for n in range(N)]))
else:#頭0のとき;和0なので変える =>1,-1にした時両方やって良いほうに
#1にする場合
sei_l=copy.copy(l)
sei_l[0]=1
for k in range(1,N):
if sum(sei_l[:k])*sum(sei_l[:k+1])>0:
sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])
if sum(sei_l)==0:
c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
else:
c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])
#-1にする場合
fu_l=copy.copy(l)
fu_l[0]=-1
for k in range(1,N):
if sum(fu_l[:k])*sum(fu_l[:k+1])>0:
fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])
if sum(fu_l)==0:
c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
else:
c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])
print(max(c1,c2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
vector<long long int> vec;
vector<vector<long long int> > vec2;
long long int MOD = 1000000007;
int main() {
long long int N;
cin >> N;
vector<long long int> vec(N, 0);
for (long long int i = 0; i < N; i++) {
cin >> vec[i];
}
long long int ans = 0;
long long int g_ans = 0;
long long int k_ans = 0;
long long int sum = 0;
bool flg = true;
for (long long int i = 0; i < N; i++) {
sum += vec[i];
if (flg == true) {
if (sum <= 0) {
k_ans += abs(sum) + 1;
sum += 1;
}
flg = false;
} else {
if (sum >= 0) {
k_ans += abs(sum) + 1;
sum += -1;
}
flg = true;
}
}
flg = true;
sum = 0;
for (long long int i = 0; i < N; i++) {
sum += vec[i];
if (flg == false) {
if (sum <= 0) {
g_ans += abs(sum) + 1;
sum += 1;
}
flg = true;
} else {
if (sum >= 0) {
g_ans += abs(sum) + 1;
sum += -1;
}
flg = false;
}
}
ans = min(k_ans, g_ans);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # sys.stdin.readline
import sys
input = sys.stdin.readline
class AtCoder:
def main(self):
n = int(input())
a = list(map(int, input().split()))
ans = 0
if a[0] == 0:
a[0] = 1
ans += 1
for i in range(1, n):
a[i] = a[i] + a[i - 1]
if a[i - 1] > 0 and a[i] >= 0:
ans += a[i] + 1
a[i] = - 1
elif a[i - 1] < 0 and a[i] < 0:
ans += -1 * a[i] + 1
a[i] = 1
elif a[i] == 0:
a[i] = 1
ans += 1
print(ans)
# Run main
if __name__ == '__main__':
AtCoder().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 <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main(){
int N;
cin >> N;
vector<long long int> A(N);
for(int i = 0; i < N; i++) cin >> A[i];
long long unsigned int cnt, cnt1 ,cnt2 = 0;
long long unsigned int M = 0;
M = A[0];
for(int i = 1 ; i < N; i++){
if(i % 2 == 0){
cnt1 += abs(1 - M);
}else{
cnt1 += abs(-1 - M);
}
M += A[i];
}
M = A[0]
for(int i = 1 ; i < N; i++){
if(i % 2 == 1){
cnt2 += abs(1 - M);
}else{
cnt2 += abs(-1 - M);
}
}
cnt = min(cnt1, cnt2);
cout << cnt;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
int n;
long long sum = 0;
int ans = 0;
bool flag = true;
bool preflag = false;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
flag = false;
preflag = true;
}
sum += a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (flag == true) {
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
flag = false;
} else {
if (sum <= 0) {
ans += (sum * -1 + 1);
sum = 1;
}
flag = true;
}
cerr << 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())
nums = list(map(int, input().split()))
sum_n = 0
before = 0
ans = 10*14
for n in [-1, 1]:
cnt = 0
before = n
sum_n = nums[0]
if sum_n * before >= 0:
if before > 0:
cnt += abs(-1-sum_n)
sum_n = -1
else:
cnt += abs(1-sum_n)
sum_n = 1
before = sum_n
for num in nums[1:]:
sum_n += num
if before * sum_n >= 0:
if before > 0:
cnt += abs(-1-sum_n)
sum_n = -1
else:
cnt += abs(1-sum_n)
sum_n = 1
before = sum_n
ans = min(ans, cnt)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sum1, sum2;
int cnt = 0;
sum1 = 0;
for (int i = 0; i < n; i++) {
int a1_p = a[i];
sum1 += a[i];
if (sum1 == 0) {
if (a[i + 1] >= 0)
a[i]++;
else
a[i]--;
cnt++;
}
sum1 -= a1_p;
sum1 += a[i];
if (i < n - 1) {
sum2 = sum1 + a[i + 1];
if (sum1 * sum2 >= 0) {
int a2_p = a[i + 1];
if (sum1 > 0)
a[i + 1] = -sum1 - 1;
else if (sum1 < 0)
a[i + 1] = -sum1 + 1;
cnt += abs(a[i + 1] - a2_p);
}
}
}
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;
int a[100100];
int main() {
cin >> n;
for (long long i = 1; i < n + 1; i++) cin >> a[i];
int check = 0;
long long ans = 0;
for (long long i = 1; i < n + 1; i++) {
check += a[i];
if (i % 2 != 0 && check < 0) {
ans += abs(1 - check);
check = 1;
} else if (i % 2 == 0 && check > 0) {
ans += abs(check + 1);
check = -1;
}
}
check = 0;
long long tmp = 0;
for (long long i = 1; i < n + 1; i++) {
check += a[i];
if (i % 2 != 0 && check >= 0) {
tmp += abs(check + 1);
check = -1;
} else if (i % 2 == 0 && check <= 0) {
tmp += abs(1 - check);
check = -1;
}
}
cout << min(tmp, ans) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, i, sum, count;
cin >> N;
vector<long long> input(N);
for (i = 0; i < N; i++) {
cin >> input.at(i);
}
sum = 0;
count = 0;
sum = input.at(0);
if (input.at(0) >= 0) {
for (i = 1; i < N; i++) {
if (i % 2 == 1) {
sum += input.at(i);
if (sum >= 0) {
count += 1 + sum;
sum = -1;
}
}
if (i % 2 == 0) {
sum += input.at(i);
if (sum < 0) {
count += 1 - sum;
sum = 1;
}
}
}
}
if (input.at(0) < 0) {
for (i = 1; i < N; i++) {
if (i % 2 == 1) {
sum += input.at(i);
if (sum <= 0) {
while (sum <= 0) {
count += 1 - sum;
sum = 1;
}
}
}
if (i % 2 == 0) {
sum += input.at(i);
if (sum >= 0) {
count += 1 + sum;
sum = -1;
}
}
}
}
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()))
cnt=0
for i in range(0,n):
if i==0: num=a[0]
else:
if a[0] > 0:
tmp = num + a[i]
if i % 2 == 0:
if tmp > 0:
num += a[i]
continue
elif tmp == 0:
cnt+=1
num += a[i]+1
continue
elif tmp < 0:
while True:
tmp += 1
cnt += 1
if tmp > 0:
a[i]+=cnt
break
num += a[i]
if i % 2 == 1:
if tmp < 0:
num += a[i]
continue
elif tmp == 0:
cnt+=1
num += a[i]-1
continue
elif tmp > 0:
while True:
tmp -= 1
cnt += 1
if tmp < 0:
a[i]-=cnt
break
num += a[i]
elif a[0] < 0:
tmp = num + a[i]
if i % 2 == 1:
if tmp > 0:
num += a[i]
continue
elif tmp == 0:
cnt+=1
num += a[i]+1
continue
elif tmp < 0:
while True:
tmp += 1
cnt += 1
if tmp > 0:
a[i] += cnt
break
num += a[i]
if i % 2 == 0:
if tmp < 0:
num += a[i]
continue
elif tmp == 0:
cnt+=1
num += a[i]-1
continue
elif tmp > 0:
while True:
tmp -= 1
cnt += 1
if tmp < 0:
a[i] -= cnt
break
num += a[i]
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int, input().split()))
c=0
s=a[0]
for i in range(1, len(a)):
S = s
if s>0:
S+=a[i]
if S>=0:
c+=abs(a[i]-s)
s = -1
else:
s = S
else:
S+=a[i]
if S<=0:
c+=abs(a[i]-s)
s = 1
else:
s = S
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 | python3 | N=int(input())
A=list(map(int,input().split()))
Acop = A.copy()
cum = [0]*N
cum[0] = A[0]
for i in range(1, N):
cum[i] = cum[i-1] + A[i]
cumcop = cum.copy()
# + - + - ...
start = N
for i in range(N):
if i % 2 == 0:
if cum[i] <= 0:
start = i
break
else:
if cum[i] >= 0:
start = i
break
if start == N:
print(0)
exit()
ans1 = 0
for i in range(start, N):
cum[i] = cum[i-1] + A[i]
if i % 2 == 0:
if cum[i] <= 0:
ans1 += abs(cum[i]) + 1
A[i] += abs(cum[i]) + 1
cum[i] = 1
else:
if cum[i] >= 0:
ans1 += abs(cum[i]) + 1
A[i] -= abs(cum[i]) + 1
cum[i] = -1
# cum[i] = cum[i-1] + A[i]
# - + - +
A = Acop
cum = cumcop
start = N
for i in range(N):
if i % 2 == 1:
if cum[i] <= 0:
start = i
break
else:
if cum[i] >= 0:
start = i
break
if start == N:
print(0)
exit()
ans2 = 0
for i in range(start, N):
cum[i] = cum[i-1] + A[i]
if i % 2 == 1:
if cum[i] <= 0:
ans2 += abs(cum[i]) + 1
A[i] += abs(cum[i]) + 1
cum[i] = 1
else:
if cum[i] >= 0:
ans2 += abs(cum[i]) + 1
A[i] -= abs(cum[i]) + 1
cum[i] = -1
# cum[i] = cum[i-1] + A[i]
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func getScanner(fp *os.File) *bufio.Scanner {
scanner := bufio.NewScanner(fp)
scanner.Split(bufio.ScanWords)
scanner.Buffer(make([]byte, 500001), 500000)
return scanner
}
func getNextString(scanner *bufio.Scanner) string {
scanner.Scan()
return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
i, _ := strconv.Atoi(getNextString(scanner))
return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
return i
}
func main() {
fp := os.Stdin
if len(os.Args) > 1 {
fp, _ = os.Open(os.Args[1])
}
scanner := getScanner(fp)
writer := bufio.NewWriter(os.Stdout)
n := getNextInt(scanner)
aa := make([]int64, n)
ss := make([]int64, n+1)
for i := 0; i < n; i++ {
aa[i] = getNextInt64(scanner)
ss[i+1] = ss[i] + aa[i]
}
var ans, adjust int64
// i%2 == 0を正
for i := 1; i < n+1; i++ {
if i%2 == 0 {
if ss[i]+adjust <= 0 {
diff := int64(1) - (ss[i] + adjust)
ans += diff
adjust += diff
}
continue
}
if ss[i]+adjust >= 0 {
diff := int64(1) + (ss[i] + adjust)
ans += diff
adjust -= diff
}
}
ans1 := ans
ans = 0
// i%2 == 1を正
for i := 1; i < n+1; i++ {
if i%2 == 1 {
if ss[i]+adjust <= 0 {
diff := int64(1) - (ss[i] + adjust)
ans += diff
adjust += diff
}
continue
}
if ss[i]+adjust >= 0 {
diff := int64(1) + (ss[i] + adjust)
ans += diff
adjust -= diff
}
}
if ans > ans1 {
ans = ans1
}
fmt.Fprintln(writer, ans)
writer.Flush()
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
int i;
long a[n], su, cnt;
su = 0;
cnt = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
su += a[i];
if (a[0] >= 0) {
if (i % 2 == 0) {
if (su <= 0) {
cnt += 1 - su;
su = 1;
}
} else {
if (su >= 0) {
cnt += su + 1;
su = -1;
}
}
} else {
if (i % 2 == 0) {
if (su >= 0) {
cnt += su + 1;
su = -1;
}
} else {
if (su <= 0) {
cnt += 1 - su;
su = 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 | java |
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num_count = sc.nextInt();
int[] array = new int[num_count];
int first_plus_cost = 0;
int sum = 0;
for(int i = 0;i < num_count;i++){
array[i] = sc.nextInt();
}
for(int i = 0;i < num_count;i++){
int temp = sum;
temp += array[i];
if(i % 2 == 0 && temp <= 0){
int cost = 1 - temp;
first_plus_cost += cost;
temp += cost;
}else if(i % 2 == 1 && temp >= 0){
int cost = 1 + temp;
first_plus_cost += cost;
temp -= cost;
}
sum = temp;
}
System.out.println();
int second_plus_cost = 0;
sum = 0;
for(int i = 0;i < num_count;i++){
int temp = sum;
temp += array[i];
if(i % 2 == 0 && temp >= 0){
int cost = 1 + temp;
second_plus_cost += cost;
temp -= cost;
}else if(i % 2 == 1 && temp <= 0){
int cost = 1 - temp;
second_plus_cost += cost;
temp += cost;
}
sum = temp;
}
int min_cost = first_plus_cost < second_plus_cost ? first_plus_cost : second_plus_cost;
System.out.println(min_cost);
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | N=gets.to_i
list=gets.split(" ").map(&:to_i)
sum = 0
res = 0
if(list[0] == 0) then
temp = 0
flag= true
(N-1).times{|i|
temp += list[i+1]
if(temp > 0 && flag) then
res += 1
list[0] = -1
flag = false
elsif(temp < 0 && flag) then
res +=1
list[0] = 1
flag = false
end
if(i == N-2) then
res += 1
list[0]=1
end
}
end
N.times{|i|
before_sum = sum
sum += list[i]
if (sum*before_sum> 0) then
if(sum > 0) then
res += (sum+1)
sum = -1
else
res += (-sum+1)
sum = 1
end
elsif sum*before_sum==0 then
if(before_sum < 0 )then
res += 1
sum = 1
elsif(before_sum > 0) then
sum = -1
res += 1
end
end
}
puts(res)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long>(A);
for (int x = 0; x < (N); x++) {
long long aa;
cin >> aa;
(A).push_back(aa);
}
if (A.at(0) == 0) {
long long ans1 = 1, ans2 = 1;
vector<long long>(B);
for (int x = 0; x < (N); x++) {
(B).push_back((A.at(x)));
}
A.at(0) = 1;
B.at(0) = -1;
for (int y = (1); y < (N); y++) {
A.at(y) += A.at(y - 1);
if (A.at(y) * A.at(y - 1) >= 0) {
ans1 += abs(A.at(y)) + 1;
if (A.at(y - 1) < 0) {
A.at(y) = 1;
} else {
A.at(y) = -1;
}
}
}
for (int y = (1); y < (N); y++) {
B.at(y) += B.at(y - 1);
if (B.at(y) * B.at(y - 1) >= 0) {
ans2 += abs(B.at(y)) + 1;
if (B.at(y - 1) < 0) {
B.at(y) = 1;
} else {
B.at(y) = -1;
}
}
}
cout << min(ans1, ans2) << endl;
} else {
long long ans = 0;
for (int y = (1); y < (N); y++) {
A.at(y) += A.at(y - 1);
if (A.at(y) * A.at(y - 1) >= 0) {
ans += abs(A.at(y)) + 1;
if (A.at(y - 1) < 0) {
A.at(y) = 1;
} else {
A.at(y) = -1;
}
}
}
cout << ans << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
int sum = a[0];
bool plus = (sum > 0);
if (sum == 0) {
ans++;
sum++;
plus = true;
}
for (int i = 1; i < n; i++) {
sum += a[i];
if ((plus && sum < 0) || (!plus && sum > 0)) {
plus = !plus;
continue;
}
if (plus) {
ans += (sum + 1);
sum = -1;
} else {
ans += (-sum + 1);
sum = 1;
}
plus = !plus;
}
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 sum1 = 0;
int sum2 = 0;
int cnt1 = 0;
int cnt2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a.at(i);
sum2 += a.at(i);
if (i % 2 == 0) {
if (sum1 <= 0) {
cnt1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
cnt2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum1 >= 0) {
cnt1 += sum1 + 1;
sum1 = -1;
}
if (sum2 <= 0) {
cnt2 += 1 - sum2;
sum2 = 1;
}
}
}
cout << min(cnt1, cnt2) << 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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
long long int i, a, n, num;
int sum = 0, bsum = 0, ans = 0, m = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a);
bsum = sum;
sum += a;
if (bsum > 0) {
if (sum > 0) {
num = sum;
do {
num--;
ans++;
m++;
} while (num >= 0);
sum -= m;
m = 0;
}
if (sum = 0) {
ans++;
sum -= 1;
}
}
if (bsum < 0) {
if (sum < 0) {
num = sum;
do {
num++;
ans++;
m++;
} while (num <= 0);
sum += m;
m = 0;
}
if (sum = 0) {
ans++;
sum += 1;
}
}
}
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
int s = a[0];
int ss = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
s = ss;
ss = s + a[i];
if (ss * s < 0) continue;
if (ss == 0) {
ans += abs(ss) + 1;
ss = -s / abs(s);
} else {
ans += abs(ss - (-ss / abs(ss)));
ss = -ss / abs(ss);
}
}
cout << ans << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.