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;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (l == 0 && a[0] == 0) {
for (long long j = 0; j < n - 1; j++) {
if (a[j + 1]) {
a[0] = a[j + 1] / abs(a[j + 1]);
if ((j + 1) & 1) a[0] *= (-1);
break;
}
}
if (!a[0]) a[0] = 1;
res++;
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
} else if (l == 0) {
l = a[i];
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum += a[0];
int count = 0;
for (int i = 1; i < n; i++) {
if (sum < 0) {
sum += a[i];
if (sum <= 0) {
count += abs(sum) + 1;
sum = 1;
}
} else {
sum += a[i];
if (sum >= 0) {
count += sum + 1;
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 | UNKNOWN | n = gets.to_i
ary = gets.split.map(&:to_i)
#even > 0
sumE = ary[0]
sumO = ary[0] > 0 ? -1 : 1
ansE = 0
ansO = ary[0].abs + 1
for i in 1 .. n - 1
if sumE * (sumE + ary[i]) >= 0
if sumE > 0
ansE += (- sumE - 1 - ary[i]).abs
sumE = -1
else
ansE += (- sumE - 1 - ary[i]).abs
sumE = 1
end
else
sumE += ary[i]
end
if sumO * (sumO + ary[i]) >= 0
if sumO > 0
ansO += (- sumO - 1 - ary[i]).abs
sumO = -1
else
ansO += (- sumO - 1 - ary[i]).abs
sumO = 1
end
else
sumO += ary[i]
end
end
puts [ansE, ansO].min
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
ll sum = a[0];
ll ans = 0;
if (sum >= 0) {
ans = (sum - (-1));
}
for (int i = (1); i < (n); ++i) {
sum += a[i];
if (sum * (sum - a[i]) < 0)
;
else {
if (sum == 0) {
if (sum - a[i] < 0) {
sum = 1;
ans++;
} else {
ans++;
sum = -1;
}
} else if (sum > 0) {
ans += (sum - (-1));
sum = -1;
} else {
ans += (1 - sum);
sum = 1;
}
}
}
ll tmp = ans;
sum = a[0];
ans = 0;
if (sum <= 0) {
ans = (1 - sum);
}
for (int i = (1); i < (n); ++i) {
sum += a[i];
if (sum * (sum - a[i]) < 0)
;
else {
if (sum == 0) {
if (sum - a[i] < 0) {
sum = 1;
ans++;
} else {
ans++;
sum = -1;
}
} else if (sum > 0) {
ans += (sum - (-1));
sum = -1;
} else {
ans += (1 - sum);
sum = 1;
}
}
}
ans = min(tmp, 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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int i = 0;
int N;
int a[100000];
int wa[2] = {0, 0};
int ans[2] = {0, 0};
cin >> N;
for (i = 0; i < N; i++) {
cin >> a[i];
}
wa[0] = a[0];
if (wa[0] < 0) {
ans[0] = ans[0] + 1 - wa[0];
wa[0] = 1;
}
for (i = 1; i < N; i++) {
wa[1] = wa[0] + a[i];
if (wa[0] > 0) {
if (wa[1] >= 0) {
ans[0] = ans[0] + wa[1] + 1;
wa[1] = -1;
}
} else {
if (wa[1] <= 0) {
ans[0] = ans[0] + 1 - wa[1];
wa[1] = 1;
}
}
wa[0] = wa[1];
}
wa[0] = a[0];
if (wa[0] > 0) {
ans[1] = ans[1] + 1 + wa[0];
wa[0] = -1;
}
for (i = 1; i < N; i++) {
wa[1] = wa[0] + a[i];
if (wa[0] > 0) {
if (wa[1] >= 0) {
ans[1] = ans[1] + wa[1] + 1;
wa[1] = -1;
}
} else {
if (wa[1] <= 0) {
ans[1] = ans[1] + 1 - wa[1];
wa[1] = 1;
}
}
wa[0] = wa[1];
}
if (ans[0] < ans[1]) {
cout << ans[0];
} else {
cout << ans[1];
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
vector<long long> s(n + 1);
vector<long long> sm(n + 1);
for (int i = 0; i < (int)(n); i++) {
s[i + 1] = s[i] + a[i];
sm[i + 1] = s[i] + a[i];
}
long long ans1 = 0;
long long ans2 = 0;
long long cnt1 = 0;
long long cnt2 = 0;
if (0 < s[1]) {
ans2 = s[1] + 1;
sm[1] = -1;
cnt2 = -s[1] - 1;
} else if (0 > s[1]) {
ans1 = -s[1] + 1;
s[1] = 1;
cnt1 = abs(s[1]) + 1;
} else {
sm[1] = -1;
cnt2 = -1;
s[1] = 1;
ans1 = 1;
cnt1 = 1;
ans2 = 1;
}
for (int i = 1; i < (int)(n); i++) {
s[i + 1] += cnt1;
sm[i + 1] += cnt2;
if (0 <= s[i] * (s[i + 1])) {
if ((i + 1) % 2 == 1) {
ans1 += abs(s[i + 1]) + 1;
cnt1 += abs(s[i + 1]) + 1;
s[i + 1] = 1;
} else {
ans1 += abs(s[i + 1]) + 1;
cnt1 += -1 * abs(s[i + 1]) - 1;
s[i + 1] = -1;
}
}
if (0 <= sm[i] * sm[i + 1]) {
if ((i + 1) % 2 == 0) {
ans2 += abs(sm[i + 1]) + 1;
cnt2 += abs(sm[i + 1]) + 1;
sm[i + 1] = 1;
} else {
ans2 += abs(sm[i + 1]) + 1;
cnt2 += -1 * sm[i + 1] - 1;
sm[i + 1] = -1;
}
}
}
long long res = min(ans1, ans2);
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 | java | import java.util.Scanner;
class Abc059c {
static public void main(String argv[]) {
Scanner sc = new Scanner(System.in);
int num;
num = sc.nextInt();
int a[] = new int[num];
for (int i = 0; i < num; i++) {
a[i] = sc.nextInt();
}
int nowPN = 0;
int curPN = 0;
nowPN = sign(a[0]);
int sum = a[0];
int op = 0;
int totalop = 0;
for (int i = 1; i < num; i++) {
sum = sum + a[i];
nowPN = -nowPN;
curPN = sign(sum);
System.out.println("nowPN: " + nowPN + " / curPN: " + curPN);
System.out.println("a[" + i + "]= " + a[i] + " / sum = " + sum);
if (curPN != nowPN) {
if (nowPN == 1) { // ++++
op = 1 - sum;
sum = sum + op;
} if (nowPN == -1) { // ----
op = sum + 1;
sum = sum - op;
}
totalop += op;
System.out.println(" op: " + op + " / newsum: " + sum);
}
}
System.out.println(totalop);
}
static public int sign(int a) {
if (a == 0) {
return 0;
} else if (a > 0) {
return 1;
} else if (a < 0) {
return -1;
}
return 0;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
s = a[0]
ans = 0
for e in a[1:]:
if (s+e)*s >= 0:
if s+e < 0:
ans += 1-(s+e)
s = 1
else:
ans += 1+(s+e)
s = -1
else:
s += e
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import itertools
def sign(num):
if num < 0:
return -1
elif num > 0:
return 1
else:
return 0
N = input()
a_i = list(map(int, input().split()))
a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
i += 1
a_sum.append(a_sum[-1]+a)
signs = [1, -1]
for i, sum_i in enumerate(a_sum):
if sum_i != 0 && i%2 == 0:
signs = [sign(sum_i), -sign(sum_i)]
break
elif sum_i != 0 && i%2 == 1:
signs = [-sign(sum_i), sign(sum_i)]
break
a_sum = 0
changes = 0
for i, a in enumerate(a_i):
a_sum += a
if sign(a_sum) != signs[i%2]:
changes += abs(a_sum) + 1
a_sum = signs[i%2]
print(changes)
#
# for i, sum_i in enumerate(a_sum):
# if i == 0:
# signs = [sign(sum_i), -sign(sum_i)]
# elif sign(sum_i) != signs[i%2]:
# a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
# changes += abs(sum_i) + 1
# # print(a_sum)
# print(changes)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
vector<long long> sum(n);
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (signbit(sum[i]) == signbit(sum[i - 1])) {
ans += abs(sum[i]) + 1;
sum[i] = sum[i - 1] / abs(sum[i - 1]) * (-1);
} else if (sum[i] == 0) {
sum[i] = sum[i - 1] / abs(sum[i - 1]) * (-1);
ans += 1;
}
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
cin >> n;
vector<int> a(n), s(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
s.at(0) = a.at(0);
for (int i = 1; i <= n - 1; i++) {
s.at(i) = a.at(i) + s.at(i - 1);
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && s.at(i) + sum1 <= 0) {
sum2 += 1 - s.at(i) - sum1;
ans1 += abs(1 - s.at(i) - sum1);
} else if (i % 2 == 1 && s.at(i) + sum1 >= 0) {
sum2 -= s.at(i) + sum1 + 1;
ans1 += abs(s.at(i) + sum1 + 1);
}
sum1 = sum2;
}
sum1 = 0;
sum2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1 && s.at(i) + sum1 <= 0) {
sum2 += 1 - s.at(i) - sum1;
ans2 += abs(1 - s.at(i) - sum1);
} else if (i % 2 == 0 && s.at(i) + sum1 >= 0) {
sum2 -= s.at(i) + sum1 + 1;
ans2 += abs(s.at(i) + sum1 + 1);
}
sum1 = sum2;
}
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 sum = a[0];
long long cnt = 0;
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
if (nsum == 0) {
sum = (sum > 0 ? -1 : 1);
cnt += 1;
} else {
if (sum > 0 && nsum > 0)
sum = -1;
else
sum = 1;
cnt += abs(nsum) + 1;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | ///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define fi first
#define si second
#define mp make_pair
#define pb push_back
#define pi pair<ll,ll>
#define clr(x) memset(x,0,sizeof(x));
#define f(i,l,r) for(int i=l;i<=r;i++)
#define rf(i,r,l) for(int i=r;i>=l;i--)
#define done(i) cout<<"done = "<<i<<endl;
#define show(x,y) cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll inf=1e18;
const int mod=1e9+7;
const int M=100005;
int n;
int a[M];
main()
{
fast
cin>>n;
int ses;
f(i,1,n)
{
cin>>a[i];
}
int sum=0;
int cost=0;
f(i,1,n)
{
sum+=a[i];
if(i%2==0)
{
if(sum>0)
{
cost+=(sum+1);
sum=-1;
}
}
else
{
if(sum<0)
{
cost+=(abs(sum)+1);
sum=1;
}
}
}
//cout<<cost<<"!"<<endl;
ses=cost;
sum=0;
cost=0;
f(i,1,n)
{
sum+=a[i];
if(i%2==1)
{
if(sum>=0)
{
cost+=(sum+1);
sum=-1;
}
}
else
{
if(sum<=0)
{
cost+=(abs(sum)+1);
sum=1;
}
}
}
// cout<<cost<<"!!"<<endl;
ses=min(ses,cost);
cout<<ses<<endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
Cost = lambda a, Sum:0 if (Sum > 0) ^ (Sum + a > 0) else abs(Sum + a) + 1
Sum = lambda a, preSum:preSum+a if (Sum > 0) ^ (preSum + a > 0) else -preSum//abs(preSum)
pSum = Sum(A[0],0)
nSum = Sum(A[0],0)
pCost = 0
nCost = 0
for a in A[1:]:
pCost += Cost(a, pSum)
pSum = Sum(a, pSum)
nCost += Cost(a, nSum)
nSum = Sum(a, nSum)
print(min(pCost, nCost))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
sm=A[0]
cnt=0
if A[0]>0:
for i in range(1,N):
sm+=A[i]
if i%2==1:
if sm>=0:
cnt+=sm+1
sm=-1
else:
if sm<=0:
cnt+=sm*-1+1
sm=1
elif A[0]<0:
for i in range(1,N):
sm+=A[i]
if i%2==1:
if sm<=0:
cnt+=sm*-1+1
sm=1
else:
if sm>=0:
cnt+=sm+1
sm=-1
elif A[0]==0:
a=0
for i in range(1,N):
if A[i]==0:
a=i
else:
break
if A[a+1]>0:
sm=-1
for i in range(a+1,N):
sm+=A[i]
if i%2==1:
if sm>=0:
cnt+=sm+1
sm=-1
else:
if sm<=0:
cnt+=sm*-1+1
sm=1
if A[a+1]<0:
sm=1
for i in range(a+1,N):
sm+=A[i]
if i%2==1:
if sm<=0:
cnt+=sm*-1+1
sm=1
else:
if sm>=0:
cnt+=sm+1
sm=-1
cnt+=2*a+1
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[110000], sumpl[110000] = {};
int summi[110000] = {};
int mi = 0, pl = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
sumpl[0] = a[i];
summi[0] = a[i];
} else {
sumpl[i] = sumpl[i - 1] + a[i];
summi[i] = summi[i - 1] + a[i];
}
if (i % 2 == 0) {
if (sumpl[i] <= 0) {
pl += abs(sumpl[i]) + 1;
sumpl[i] += (-1 * sumpl[i] + 1);
}
if (summi[i] >= 0) {
mi += abs(summi[i]) + 1;
summi[i] += (-1 * summi[i] - 1);
}
} else {
if (sumpl[i] >= 0) {
pl += abs(sumpl[i]) + 1;
sumpl[i] += (-1 * sumpl[i] - 1);
}
if (summi[i] <= 0) {
mi += abs(summi[i]) + 1;
summi[i] += (-1 * summi[i] + 1);
}
}
}
cout << (pl < mi ? pl : mi) << 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;
vector<int> v(1e5);
long long solve(int n) {
long long sum = v[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + v[i] > 0)
sum += v[i];
else if (sum > 0 && sum + v[i] < 0)
sum += v[i];
else if (sum < 0 && sum + v[i] <= 0)
ans += abs(sum + v[i]) + 1, sum = 1;
else if (sum > 0 && sum + v[i] >= 0)
ans += abs(sum + v[i]) + 1, sum = -1;
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
long long ans = solve(n);
for (int i = 0; i < n; i++) v[i] *= -1;
ans = max(ans, solve(n));
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t N;
cin >> N;
int64_t score = 0;
int64_t hard;
for (int64_t i = 1; i <= N; i++) {
score += i;
if (score >= N) {
hard = i;
break;
}
}
vector<int64_t> ans;
ans.push_back(hard);
N -= hard;
for (int64_t i = hard - 1; i > 0; i--) {
if (N >= i) {
ans.push_back(i);
N -= i;
}
}
for (int64_t i = 0; i < ans.size(); i++) {
cout << ans[i] << "\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 a[100000];
int getTotal(int n, int dir) {
int total{}, sum{};
for (int i{0}; i < n; ++i) {
sum += a[i];
if (dir > 0 && sum <= 0) {
total += -sum + 1;
sum = 1;
} else if (dir < 0 && sum >= 0) {
total += sum + 1;
sum = -1;
}
dir *= -1;
}
return total;
}
int main() {
int n;
cin >> n;
for (int i{0}; i < n; ++i) cin >> a[i];
int try1 = getTotal(n, 1);
int try2 = getTotal(n, -1);
printf("%d\n", ((try1) < (try2) ? (try1) : (try2)));
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 ans = 0;
vector<long long> inp;
vector<long long> comp;
long long num1;
cin >> num1;
comp.push_back(num1);
if (num1 >= 0) {
for (int i = 0; i < n - 1; i++) {
long long num;
cin >> num;
if (i % 2 == 0) {
if (num + comp[i] < 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(-1);
ans += max(num, comp[i]) + 1 + min(num, comp[i]);
}
} else {
if (num + comp[i] > 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(1);
ans += abs(min(num, comp[i])) + 1 - max(num, comp[i]);
}
}
}
} else {
for (int i = 0; i < n - 1; i++) {
long long num;
cin >> num;
if (i % 2 != 0) {
if (num + comp[i] < 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(-1);
ans += max(num, comp[i]) + 1 + min(num, comp[i]);
}
} else {
if (num + comp[i] > 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(1);
ans += abs(min(num, comp[i])) + 1 - max(num, comp[i]);
}
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, cnt = 0;
cin >> n;
bool flag;
ll a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] >= 0) {
flag = true;
} else {
flag = false;
}
int sum = a[0];
for (int i = 1; i < n; i++) {
bool flag2;
int tmp = sum;
sum += a[i];
if (sum == 0) {
if (flag) {
sum -= 1;
flag = false;
cnt++;
} else {
sum += 1;
flag = true;
cnt++;
}
} else {
if (sum > 0) {
flag2 = true;
} else {
flag2 = false;
}
if (flag == flag2) {
if (flag2) {
while (sum >= 0) {
sum--;
cnt++;
}
flag2 = false;
} else {
while (sum <= 0) {
sum++;
cnt++;
}
flag2 = true;
}
}
flag = flag2;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
nums = list(map(int, input().split()))
ans = 10**10+1
for start in [-1, 1]:
before = start
cnt = 0
sum_n = 0
for num in nums:
sum_n += num
if before*sum_n >= 0:
if before < 0:
cnt += 1-sum_n
sum_n = 1
else:
cnt += 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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.run();
}
public void run() {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int sum[]=new int[n];
sum[0]=sc.nextInt();
boolean evenPlus = true;
if(sum[0]<0) evenPlus=false;
int ans = 0;
for(int i=1; i<n; i++) {
int a = sc.nextInt();
if(i%2==0) {
if(evenPlus) {
if(sum[i-1]+a <= 0) {
ans += 1-(sum[i-1]+a);
sum[i]=1;
} else {
sum[i]=sum[i-1]+a;
}
} else {
if(sum[i-1]+a >= 0) {
ans += Math.abs(sum[i-1]+a)+1;
sum[i]=-1;
} else {
sum[i]=sum[i-1]+a;
}
}
} else {
if(!evenPlus) {
if(sum[i-1]+a <= 0) {
ans += 1-(sum[i-1]+a);
sum[i]=1;
} else {
sum[i]=sum[i-1]+a;
}
} else {
if(sum[i-1]+a >= 0) {
ans += Math.abs(sum[i-1]+a)+1;
sum[i]=-1;
} else {
sum[i]=sum[i-1]+a;
}
}
}
}
System.out.println(ans);
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>
signed main() {
long long n;
std::cin >> n;
std::vector<long long> a(n);
for (long long i = 0; i < (n); i++) std::cin >> a[i];
long long sum = a[0];
long long count = 0;
for (long long i = 1; i < n; i++) {
if ((sum + a[i]) * sum >= 0) {
if (sum > 0) {
count += a[i] + sum + 1;
sum = -1;
} else if (sum < 0) {
count += -a[i] - sum + 1;
sum = 1;
}
} else
sum += a[i];
}
std::cout << (count) << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
counter = 0 ####操作回数
A.reverse()
S = 0
a = A.pop()
if a==0:
counter += 1
while A:
b = A.pop()
if b == 0:
counter += 2
elif b>0:
A.append(b)
S = -1
break
elif b<0:
A.append(b)
S = 1
break
else:
S += a
while A:
c = A.pop()
if c>=0 and S>0:
counter += abs(c+S)+1
S = -1
elif c<=0 and S<0:
counter += abs(c+S)+1
S = 1
elif (c>=0 and S<0) and S+c<=0:
counter += abs(S+c)+1
S = 1
elif (c=<0 and S>0) and S+c>=0:
counter += abs(S+c)+1
S = -1
else:
S += c
print(counter)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long a[N];
for (int i = 0; i < N; i++) cin >> a[i];
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < N; i++) {
if (sum + a[i] > 0 && sum > 0) {
ans += abs(-sum - 1 - a[i]);
sum = -1;
} else if (sum + a[i] < 0 && sum < 0) {
ans += abs(-sum + 1 - a[i]);
sum = 1;
} else if (sum + a[i] == 0) {
if (sum < 0) {
sum = 1;
ans++;
} else {
sum = -1;
ans++;
}
} 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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = __int64_t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};
void solve() {
int n;
ll x, sum = 0, count = 0;
cin >> n;
for (int(i) = 0; (i) < (n); (i)++) {
cin >> x;
if (i > 0) {
ll temp;
if (sum < 0) {
if (sum + x < 0) {
temp = (sum + x) * (-1) + 1;
x += temp;
count += temp;
} else if (sum + x == 0) {
x++;
count++;
}
} else {
if (sum + x > 0) {
temp = (sum + x) * (-1) - 1;
x += temp;
count += abs(temp);
} else if (sum + x == 0) {
x--;
count++;
}
}
}
sum += x;
cout << "sum:" << sum << endl;
}
cout << count << 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 | n = int(input())
a = list(map(int, input().split()))
S = 0
C = 0
S = a[0]
if S > 0:
pm = 1
else:
pm = 0
for i in range(1, n):
S += a[i]
if pm == 1 and S >= 0:
C += S + 1
S -= S + 1
elif pm == 0 and S <= 0:
C += -S + 1
S += -S + 1
pm = 1 - pm
T = 0
D = 0
T = a[0]
if T > 0:
D += T + 1
T -= T + 1
pm = 0
else:
D += -T + 1
T += -T + 1
pm = 1
for i in range(1, n):
T += a[i]
if pm == 1 and T >= 0:
D += T + 1
T -= T + 1
elif pm == 0 and T <= 0:
D += -T + 1
T += -T + 1
pm = 1 - pm
print(min(C, D)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n), tot(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
tot[0] = a[0];
for (int i = 0; i < (n - 1); ++i) tot[i + 1] += tot[i] + a[i + 1];
int p = tot[0] / abs(tot[0]);
long long ans = 0;
long long wa = 0;
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
ans += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
ans += abs(tot[i]) + 1;
}
}
p *= -1;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LLINF = 1e18;
int main() {
int n;
cin >> n;
long long ruisekiwa;
cin >> ruisekiwa;
long long ans = 0;
if (ruisekiwa != 0) {
for (int i = (1); i < (n); i++) {
long long a;
cin >> a;
if (ruisekiwa * (ruisekiwa + a) < 0) {
ruisekiwa += a;
} else if (ruisekiwa > 0) {
ans += (ruisekiwa + a) + 1;
ruisekiwa = -1;
} else {
ans += 1 - (ruisekiwa + a);
ruisekiwa = 1;
}
}
} else {
long long ans1 = 1;
ruisekiwa = 1;
long long a[n];
a[0] = 0;
for (int i = (1); i < (n); i++) cin >> a[i];
for (int i = (1); i < (n); i++) {
if (ruisekiwa * (ruisekiwa + a[i]) < 0) {
ruisekiwa += a[i];
} else if (ruisekiwa > 0) {
ans1 += (ruisekiwa + a[i]) + 1;
ruisekiwa = -1;
} else {
ans1 += 1 - (ruisekiwa + a[i]);
ruisekiwa = 1;
}
}
long long ans2 = 1;
ruisekiwa = -1;
for (int i = (1); i < (n); i++) {
if (ruisekiwa * (ruisekiwa + a[i]) < 0) {
ruisekiwa += a[i];
} else if (ruisekiwa > 0) {
ans2 += (ruisekiwa + a[i]) + 1;
ruisekiwa = -1;
} else {
ans2 += 1 - (ruisekiwa + a[i]);
ruisekiwa = 1;
}
}
ans = min(ans1, ans2);
}
cout << (ans) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n;
long long sum1 = 0;
long long sum2 = 0;
long long tmp;
long long lcount = 0;
long long rcount = 0;
long long a[100000];
char input[1000000];
int i = 0, j = 0;
int cp = 0, tcp = 0;
char tp[12];
tp[12] = '\0';
fgets(input, 1000000, stdin);
n = atoi(input);
fgets(input, 1000000, stdin);
for (i = 0; i < n; i++) {
while (input[cp] != ' ' && input[cp] != '\n') {
tp[tcp] = input[cp];
tcp++;
cp++;
}
tp[tcp] = '\0';
tcp = 0;
cp++;
a[i] = atoi(tp);
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
tmp += a[i];
while (tmp > -1) {
lcount++;
tmp--;
}
} else {
tmp += a[i];
while (tmp < 1) {
lcount++;
tmp++;
}
}
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
tmp += a[i];
while (tmp > -1) {
rcount++;
tmp--;
}
} else {
tmp += a[i];
while (tmp < 1) {
rcount++;
tmp++;
}
}
}
printf("%lld\n", lcount > rcount ? rcount : lcount);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using static Input;
public class Prog
{
private const int INF = 1000000007;
public static void Main()
{
int n = NextInt();
int[] a = LineInt();
//凸凹の2パターンっぽい
int costA = 0;
int costB = 0;
int now = a[0];
if (now < 0) { now = 1; costA = Math.Abs(a[0]) + 1; }
for (int i = 1; i < n; i++)
{
if (now > 0)
{
//今0より大きいので小さくする.
if (now + a[i] < 0)
{
//ok
now += a[i];
}
else
{
//ng
costA += Math.Abs(now + a[i]) + 1;
now = -1;
}
}
else
{
//今0より小さいので大きくする
if (now + a[i] > 0)
{
//ok
now += a[i];
}
else
{
//ng
costA += Math.Abs(now + a[i]) + 1;
now = 1;
}
}
}
now = a[0];
if (now > 0) { now = -1; costB = Math.Abs(a[0]) + 1; }
for (int i = 1; i < n; i++)
{
if (now > 0)
{
//今0より大きいので小さくする.
if (now + a[i] < 0)
{
//ok
now += a[i];
}
else
{
//ng
costB += Math.Abs(now + a[i]) + 1;
now = -1;
}
}
else
{
//今0より小さいので大きくする
if (now + a[i] > 0)
{
//ok
now += a[i];
}
else
{
//ng
costB += Math.Abs(now + a[i]) + 1;
now = 1;
}
}
}
Console.WriteLine(Math.Min(costA, costB));
}
}
public class Input
{
private static Queue<string> q = new Queue<string>();
private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
public static string NextString() { Confirm(); return q.Dequeue(); }
public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[100010];
long long ans = 0;
long long cnt = 0;
int flag = 1;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
cnt = a[0];
for (int i = 1; i < n; i++) {
cnt += a[i];
if (cnt * flag >= 0) {
ans += abs(cnt) + 1;
if (flag == -1) {
cnt = 1;
} else {
cnt = -1;
}
}
if (flag == -1) {
flag = 1;
} else {
flag = -1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
A = []
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) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 double sequence(int a[], double start) {
double count = 0.0, presum = -1.0 * start, sum = 0.0;
for(int i = 0; i < a.length; ++i) {
sum += (double)a[i];
if(i == 0)sum = start;
if(sum * presum > 0) {
double min = Math.abs(sum) + 1;
if(presum > 0)sum -= min;
else sum += min;
count += min;
}
if(sum == 0) {
if(presum > 0)sum--;
else sum++;
++count;
}
presum = sum;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, a[];
double count = 0.0, tmp2 = 0.0, tmp3 = 0.0;
n = sc.nextInt();
a = new int[n];
for(int i = 0; i < n; ++i) a[i] = sc.nextInt();
sc.close();
if(a[0] == 0) {
a[0]++;
++tmp3;
}
int tmp = Math.abs(a[0]) + 1;
if(a[0] > 0) {
tmp2 = tmp + tmp3;
tmp = a[0] - tmp;
}
else {
tmp2 = tmp + tmp3;
tmp = a[0] + tmp;
}
count = Math.min((tmp3 + sequence(a, (double)a[0])),(tmp2 + sequence(a, (double)tmp)));
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 | python3 | n = int(input())
data = input().split()
for i in range(n):
data[i] = int(data[i])
count = 0
sum = data[0]
i = 1
while(i < n):
while(sum * (sum + data[i]) >= 0):
if sum > 0:
data[i] -= 1
count += 1
elif sum < 0:
data[i] += 1
count += 1
sum += data[i]
i += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
a = [A,A]
res = [0,0]
sum = 0
for check in range(2):
sum = 0
if check == 1:
if a[1][0] > 0:
temp = -1 - a[1][0]
a[1][0] += temp
res[1] += temp * -1
elif a[1][0] < 0:
temp = 1 - a[1][0]
a[1][0] += temp
res[1] += temp
# if a[check][0] == 0:
# if check == 0:
# a[0][0] += 1
# else:
# a[0][0] -= 1
# res[check] += 1
for i in range(n-1):
sum += a[check][i]
if sum * (sum + a[check][i+1]) >= 0:
if sum > 0:
temp = -1 - sum - a[check][i+1]
a[check][i+1] += temp
res[check] += temp * -1
else:
temp = 1 - sum - a[check][i+1]
a[check][i+1] += temp
res[check] += temp
print(min(res[0],res[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 | python3 | n = int(input())
a = list(map(int, input().split(' ')))
result = 0
h = 0
if a[0] == 0:
counter += 1
result += 1
else:
counter += a[0]
for i in range(1, n):
if counter < 0:
counter += a[i]
if counter == 0:
counter += 1
result +=1
elif counter > 0:
continue
else:
result += 1-counter
counter = 1
else:
counter += a[i]
if counter == 0:
counter -= 1
result += 1
elif counter < 0:
continue
else:
result += counter+1
counter = -1
out = []
out.append(result)
result = 0
counter = 0
if a[0] == 0:
counter -= 1
result +=1
elif a[0]< 0:
counter += 1
result += 1-a[0]
else:
counter -= 1
result += 1+a[0]
for i in range(1, n):
if counter < 0:
counter += a[i]
if counter == 0:
counter += 1
result += 1
elif counter > 0:
continue
else:
result += 1-counter
counter = 1
else:
counter += a[i]
if counter == 0:
counter -= 1
result +=1
elif counter < 0:
continue
else:
result += counter+1
counter = -1
out.append(result)
print(min(out)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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, ai;
cin >> n;
cin >> ai;
long long sum = ai;
long long ans = 0;
for (int i = (1); i < (n); ++i) {
cin >> ai;
if (sum > 0) {
if (sum < -ai)
sum += ai;
else {
ans += ai + sum + 1;
sum = -1;
}
} else {
if (-sum < ai)
sum += ai;
else {
ans += -sum + 1 - ai;
sum = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long cnt = 0;
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
if (nsum == 0) {
sum = (sum >= 0 ? -1 : 1);
cnt += 1;
} else {
if (sum > 0 && nsum > 0)
sum = -1;
else
sum = 1;
cnt += abs(nsum) + 1;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
vector<int> v;
int res = 0;
int sign = 0;
int n, t;
int sum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t;
v.push_back(t);
}
sign = 0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
if (sign == 0) {
if (sum >= 0) {
res += (sum + 1);
sum = -1;
}
} else {
if (sum <= 0) {
res += (abs(sum) + 1);
sum = 1;
}
}
sign = 1 - sign;
}
t = 0;
sign = 1;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
if (sign == 0) {
if (sum >= 0) {
t += (sum + 1);
sum = -1;
}
} else {
if (sum <= 0) {
t += (abs(sum) + 1);
sum = 1;
}
}
sign = 1 - sign;
}
res = min(res, t);
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
lst1 = list(map(int,input().split()))
odd = sum(lst1[::2])
even = sum(lst1[1::2])
if odd < even:
need = "-"
else:
need = "+"
ans = 0
now = 0#現在のi迄の和
for i in range(n):
if lst1[i] < 0:
if need == "-":
if abs(now) >= abs(lst1[i]):
ans += abs(now)-lst1[i]+1
now = -1
else:
now += lst1[i]
need = "+"
else: #need == "+"
ans += abs(now)-lst1[i] + 1
now = 1
need = "-"
else:
if need == "+":
if abs(now) >= abs(lst1[i]):
ans += abs(now)-lst1[i]+1
now = 1
else:
now += lst1[i]
need = "-"
else: #need == "-"
ans += abs(now)+lst1[i]+1
now = -1
need = "+"
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;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
const int inf = 1e9 + 7;
const ll INF = 1e18;
int main() {
int n;
cin >> n;
vll a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
ll count1 = 0;
ll count2 = 0;
ll sum = 0;
if (a[0] != 0) {
sum += a[0];
for (int i = (1); i < (int)(n); i++) {
if ((sum + a[i]) * sum >= 0) {
count1 += abs(-abs(sum) / sum - sum - a[i]);
sum = -abs(sum) / sum;
} else
sum += a[i];
}
sum = -abs(a[0]) / a[0];
for (int i = (1); i < (int)(n); i++) {
if ((sum + a[i]) * sum >= 0) {
count2 += abs(-abs(sum) / sum - sum - a[i]);
sum = -abs(sum) / sum;
} else
sum += a[i];
}
cout << min(count1, count2) << endl;
return 0;
}
sum = 1;
count1++;
for (int i = (1); i < (int)(n); i++) {
if ((sum + a[i]) * sum >= 0) {
count1 += abs(-abs(sum) / sum - sum - a[i]);
sum = -abs(sum) / sum;
} else
sum += a[i];
}
sum = -1;
count2++;
for (int i = (1); i < (int)(n); i++) {
if ((sum + a[i]) * sum >= 0) {
count2 += abs(-abs(sum) / sum - sum - a[i]);
sum = -abs(sum) / sum;
} else
sum += a[i];
}
cout << min(count1, count2) << 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())
numbers = list(map(int, input().split()))
counter = 0
for i in range(len(numbers) - 1):
sum_i_n = sum(numbers[:i + 1])
sum_i_n_1 = sum(numbers[:i + 2])
if sum_i_n == 0:
numbers[i] += 1
counter += 1
if sum_i_n * sum_i_n_1 > 0:
if sum_i_n_1 > 0:
numbers[i + 1] -= (sum_i_n_1 + 1)
counter += sum_i_n_1 + 1
else:
numbers[i + 1] += (abs(sum_i_n_1) + 1)
counter += abs(sum_i_n_1) + 1
if sum(numbers) == 0:
counter += 1
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long sum = 0;
long answer = 0;
for (int i = 0; i < n; i++) {
int a = in.nextInt();
if (i > 0) {
if (sum < 0 && sum + a < 0) {
answer += 1 - (sum + a);
sum = 1;
} else if (sum > 0 && sum + a > 0) {
answer += Math.abs(-1 - (sum + a));
sum = -1;
} else if (sum + a == 0) {
answer++;
if (sum < 0) {
sum = 1;
} else {
sum = -1;
}
} else {
sum += a;
}
} else {
sum += a;
}
}
System.out.println(answer);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, ans1 = 0, ans2 = 0;
cin >> N;
vector<int> A1(N), A2(N);
for (int i = 0; i < N; i++) {
cin >> A1[i];
A2[i] = A1[i];
}
if (A1[0] <= 0) {
ans1 -= A1[0] - 1;
A1[0] -= A1[0] - 1;
}
for (int i = 1; i < N; i++) {
A1[i] += A1[i - 1];
if (A1[i - 1] > 0 && A1[i] >= 0) {
ans1 += A1[i] + 1;
A1[i] -= A1[i] + 1;
}
if (A1[i - 1] < 0 && A1[i] <= 0) {
ans1 -= A1[i] - 1;
A1[i] -= A1[i] - 1;
}
}
if (A2[0] >= 0) {
ans2 += A2[0] + 1;
A2[0] -= A2[0] + 1;
}
for (int i = 1; i < N; i++) {
A2[i] += A2[i - 1];
if (A2[i - 1] > 0 && A2[i] >= 0) {
ans2 += A2[i] + 1;
A2[i] -= A2[i] + 1;
}
if (A2[i - 1] < 0 && A2[i] <= 0) {
ans2 -= A2[i] - 1;
A2[i] -= A2[i] - 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 a[100001];
int main() {
long long n, ai;
cin >> n;
cin >> ai;
a[0] = ai;
long long sum = ai, sum1 = -1;
if (sum == 0) sum = 1;
long long ans = 0, ans1 = 0;
for (int i = (1); i < (n); ++i) {
cin >> ai;
a[i] = ai;
if (sum > 0) {
if (sum < -ai)
sum += ai;
else {
ans += ai + sum + 1;
sum = -1;
}
} else {
if (-sum < ai)
sum += ai;
else {
ans += -sum + 1 - ai;
sum = 1;
}
}
}
for (int i = (1); i < (n); ++i) {
ai = a[i];
if (sum1 > 0) {
if (sum1 < -ai)
sum1 += ai;
else {
ans1 += ai + sum1 + 1;
sum1 = -1;
}
} else {
if (-sum1 < ai)
sum1 += ai;
else {
ans1 += -sum1 + 1 - ai;
sum1 = 1;
}
}
}
cout << min(ans, ans1) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | io = STDIN
$n=io.gets.chomp.to_i
$a=io.gets.chomp.split.map(&:to_i)
$b=[]
$a.unshift 0
sm=0
(1..$n).each do |i|
sm+=$a[i]
$b<<sm
end
def adjust(b,i,delta)
(i..($n-1)).each do |j|
b[j]+=delta
end
b
end
def calc(ar)
cnt=0
($n-1).times do |i|
delta=0
if ar[i]>0 && ar[i+1]<0
elsif ar[i]<0 && ar[i+1]>0
elsif ar[i]>0 && ar[i+1]>0
delta =-(ar[i+1]+1)
elsif ar[i]<0 && ar[i+1]<0
delta =(ar[i+1].abs+1)
else
if ar[i]>0
delta=-1
else
delta=1
end
end
(ar = adjust(ar,i+1,delta)) unless delta==0
cnt+=delta.abs
end
cnt
end
if $a[1]==0
b1=$b.clone
b2=$b.clone
puts [calc(adjust(b1,0,1)),calc(adjust(b1,0,-1))].min
else
puts calc($b)
end
__END__
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
trueans = 1000000000000
for k in [-1,1]:
sign = k
ans = 0
s = 0
for i in range(n):
s += a[i]
if s == 0:
s = sign
ans += 1
elif s//abs(s) == sign:
pass
else:
ans += abs(sign-s)
s = sign
sign *= -1
trueans = min(ans,trueans)
print(trueans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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, A[100000];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", &A[i]);
long long cnt = 0, sum = A[0];
if (sum == 0 && sum + A[1] < 0)
cnt++, sum++;
else if (sum == 0 && sum + A[1] > 0)
cnt++, sum--;
else if (sum == 0)
cnt++, sum++;
for (int i = 1; i < N; i++) {
long long prev = sum;
sum += A[i];
if (sum < 0 && prev < 0)
cnt += sum + 1, sum = 1;
else if (sum > 0 && prev > 0)
cnt += sum + 1, sum = -1;
else if (sum == 0 && prev > 0)
cnt++, sum = -1;
else if (sum == 0 && prev < 0)
cnt++, sum = 1;
}
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() {
long n;
cin >> n;
int i;
long a[n], su, cnt, cnt2;
su = 0;
cnt = 0;
cnt2 = 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;
}
}
}
}
su = 0;
for (i = 0; i < n; i++) {
su += a[i];
if (a[0] > 0) {
if (i % 2 == 0) {
if (su <= 0) {
cnt2 += 1 - su;
su = 1;
}
} else {
if (su >= 0) {
cnt2 += su + 1;
su = -1;
}
}
} else {
if (i % 2 == 0) {
if (su >= 0) {
cnt2 += su + 1;
su = -1;
}
} else {
if (su <= 0) {
cnt2 += 1 - su;
su = 1;
}
}
}
}
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 | python3 | import math
N = int(input())
a = [int(i) for i in input().split()]
num = 0
i = 0
if a[0] == 0:
while a[i] == 0:
i += 1
if i % 2 == 0:
a[0] = math.copysign(1, a[i])
else:
a[0] = math.copysign(1, -a[i])
num += 1
old = a[0]
sam = a[0]
for n in range(2):
if n != 0:
old = -a[0]
sam = -a[0]
for i in range(1, len(a)):
sam += a[i]
sam_sign = int(math.copysign(1, sam))
old_sign = int(math.copysign(1, old))
if sam_sign == old_sign or sam == 0:
num += abs(sam) + 1
a[i] = a[i] + (-old_sign)*(abs(sam)+1)
old += a[i]
sam = old
if n == 0:
num2 = num
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100001];
int sumo[100001];
int sume[100001];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i + 1];
}
int anso = 0;
int anse = 0;
sumo[0] = 0;
sume[0] = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
if (sume[i - 1] + a[i] > 0) sume[i] = sume[i - 1] + a[i];
if (sume[i - 1] + a[i] <= 0) {
sume[i] = 1;
anse += 1 - (sume[i - 1] + a[i]);
}
if (sumo[i - 1] + a[i] < 0) sumo[i] = sumo[i - 1] + a[i];
if (sumo[i - 1] + a[i] >= 0) {
sumo[i] = -1;
anso += sumo[i - 1] + a[i] + 1;
}
} else if (i % 2 == 1) {
if (sumo[i - 1] + a[i] > 0) sumo[i] = sumo[i - 1] + a[i];
if (sumo[i - 1] + a[i] <= 0) {
sumo[i] = 1;
anso += 1 - (sumo[i - 1] + a[i]);
}
if (sume[i - 1] + a[i] < 0) sume[i] = sume[i - 1] + a[i];
if (sume[i - 1] + a[i] >= 0) {
sume[i] = -1;
anse += sume[i - 1] + a[i] + 1;
}
}
}
int ans;
ans = min(anso, anse);
printf("%d", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
ll sum = a[0];
ll count = 0;
for (int i = 1; i < n; i++) {
if (signbit(sum) != signbit(sum + a[i]))
if (sum + a[i] == 0) {
count += abs(sum + a[i]) + 1;
if (sum < 0)
a[i] += count;
else
a[i] -= count;
sum += a[i];
} else
sum += a[i];
else {
count += abs(sum + a[i]) + 1;
if (sum < 0)
a[i] += count;
else
a[i] -= count;
sum += a[i];
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
long long ans, cnt;
long long z[100005];
int main() {
int n, flag, a;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &z[i]);
if (z[0] != 0) {
if (z[0] > 0)
flag = 1;
else
flag = -1;
ans = z[0];
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
} else {
flag = cnt = ans = 1;
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
long long tmp = cnt;
flag = ans = -1;
cnt = 1;
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
cnt = std::min(cnt, tmp);
}
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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int num_integer;
if (scanf("%d", &num_integer) != 1) {
puts("num_integer input error.");
return 1;
}
int integer, sum_1 = 0, sum_2 = 0, num_operation_1 = 0, num_operation_2 = 0;
for (int i = 0; i < num_integer; i++) {
if (scanf("%d", &integer) != 1) {
puts("integer input error.");
}
if (sum_1 > 0 && (sum_1 + integer) >= 0) {
num_operation_1 += sum_1 + integer + 1;
sum_1 = -1;
} else if (sum_1 < 0 && (sum_1 + integer) <= 0) {
num_operation_1 += -(sum_1 + integer) + 1;
sum_1 = 1;
} else {
sum_1 += integer;
}
if (sum_2 > 0 && (sum_2 + integer) >= 0) {
num_operation_2 += sum_2 + integer + 1;
sum_2 = -1;
} else if (sum_2 < 0 && (sum_2 + integer) <= 0) {
num_operation_2 += -(sum_2 + integer) + 1;
sum_2 = 1;
} else {
num_operation_2 = (integer > 0) ? integer + 1 : -integer + 1;
sum_2 = (integer > 0) ? integer : -integer;
}
}
printf("%d", (num_operation_1 < num_operation_2) ? num_operation_1
: num_operation_2);
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
import sys
sys.setrecursionlimit(300000)
def solve(n: int, a: "List[int]"):
o, e = 0, 0
tmp = 0
for i, v in enumerate(a):
if i % 2 == 0:
e += v
if e < 0:
tmp += abs(e) + 1
e = 1
else:
tmp += max(0, abs(o) - abs(e) + 1)
e += tmp
else:
o += v
if o > 0:
tmp += abs(o) + 1
o = -1
else:
tmp += max(0, abs(e) - abs(o) + 1)
o -= tmp
ret = tmp
o, e = 0, 0
tmp = 0
for i, v in enumerate(a):
if i % 2 == 0:
e += v
if e > 0:
tmp += abs(e) + 1
e = -1
else:
tmp += max(0, abs(o) - abs(e) + 1)
e -= tmp
else:
o += v
if o < 0:
tmp += abs(o) + 1
o = 1
else:
tmp += max(0, abs(e) - abs(o) + 1)
o += tmp
ret = min(ret, tmp)
print(ret)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
n = int(next(tokens)) # type: int
a = [ int(next(tokens)) for _ in range(n) ] # type: "List[int]"
solve(n, a)
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 | var read = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
var obj;
var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
obj = init(inLine);
myerr("-----start-----");
var start = new Date();
Main();
var end = new Date() - start;
myerr("----- end -----");
myerr("time : " + (end) + "ms");
});
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
var returnObj = {
list : input, index : 0, max : input.length,
hasNext : function(){return (this.index < this.max);},
next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
};
return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割 7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}
function Main(){
var N = nextInt();
var list = nextIntArray();
var oddCount = 0;
var evenCount = 0;
var oddSum = new Array(N);//1, -1, 1, -1
var evenSum = new Array(N);//-1, 1 ,-1 ,1
if(list[0] == 0){
oddCount++;
evenCount++;
}else{
if(list[0] < 0){
oddCount += Math.abs(list[0]) + 1;
oddSum[0] = 1;
evenSum[0] = list[0];
}else{
evenCount += Math.abs(list[0]) + 1;
evenSum[0] = -1;
oddSum[0] = list[0];
}
}
for(var i = 1; i < N; i++){
oddSum[i] = oddSum[i - 1] + list[i];
evenSum[i] = evenSum[i - 1] + list[i];
if((oddSum[i - 1] < 0 && oddSum[i] > 0) || (oddSum[i - 1] > 0 && oddSum[i] < 0)){
}else{
if((oddSum[i - 1] > 0)){
oddCount += oddSum[i] + 1;
oddSum[i] = -1;
}else{
oddCount += Math.abs(oddSum[i]) + 1;
oddSum[i] = 1;
}
}
if((evenSum[i - 1] < 0 && evenSum[i] > 0) || (evenSum[i - 1] > 0 && evenSum[i] < 0)){
}else{
if((evenSum[i - 1] > 0)){
evenCount += evenSum[i] + 1;
evenSum[i] = -1;
}else{
evenCount += Math.abs(evenSum[i]) + 1;
evenSum[i] = 1;
}
}
}
myout(Math.min(evenCount, oddCount));
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans = 0
sum_a = a[0]
if sum_a > 0:
for i in range(1,n):
sum_a += a[i]
if i%2 == 1 and sum_a >= 0:
ans += sum_a + 1
sum_a = -1
elif i%2 == 0 and sum_a <= 0:
ans += -sum_a + 1
sum_a = 1
else:
for i in range(1,n):
sum_a += a[i]
if i%2 == 0 and sum_a >= 0:
ans += sum_a + 1
sum_a = -1 #いや違くね?
elif i%2 == 1 and sum_a <= 0:
ans += -sum_a + 1
sum_a = 1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100100];
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0, sum = a[0];
int sign = (a[0] > 0 ? 1 : -1);
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum == 0) {
sum += -sign;
ans++;
}
if (sign > 0 && sum > 0) {
int x = sum + 1;
sum -= x;
ans += x;
} else if (sign < 0 && sum < 0) {
int y = abs(sum) + 1;
sum += y;
ans += y;
}
sign *= -1;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int beginPositive(std::vector<int> &v) {
int sum = v[0] + v[1];
int count = 0;
if (sum < 0) {
count = 1 - sum;
sum = 1;
}
for (int i = 2; i < v.size(); i++) {
int tmp = sum + v[i];
if (sum * tmp < 0) {
sum = tmp;
continue;
}
int next_sum = (-1) * sum / abs(sum);
count += abs(next_sum - tmp);
sum = next_sum;
}
return count;
}
int beginNegative(std::vector<int> &v) {
int sum = v[0] + v[1];
int count = 0;
if (sum > 0) {
count = sum + 1;
sum = -1;
}
for (int i = 2; i < v.size(); i++) {
int tmp = sum + v[i];
if (sum * tmp < 0) {
sum = tmp;
continue;
}
int next_sum = (-1) * sum / abs(sum);
count += abs(next_sum - tmp);
sum = next_sum;
}
return count;
}
int main() {
int n;
std::cin >> n;
std::vector<int> v(n);
for (int i = 0; i < v.size(); i++) {
std::cin >> v[i];
}
int count;
count = std::min(beginPositive(v), beginNegative(v));
std::cout << count << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import java.util.*
fun main(args: Array<String>) {
val sc = Scanner(System.`in`)
val n = sc.nextInt()
val a = (0 until n).map { sc.next().toLong() }
println(problem059c(n, a))
}
fun problem059c(n: Int, a: List<Long>): Long {
val count1 = compute(n, a)
// val a = a.toMutableList()
// var a0 = a[0]
// var count = 0L
// if (a0 > 0) {
// val tmp = a0 + 1
// a[0] = a0 - tmp
// count += tmp
// } else {
// val tmp = a0 - 1
// a[0] = a0 - tmp
// count -= tmp
// }
// val count2 = compute(n, a) + count
return count1
}
fun compute(n: Int, a: List<Long>): Long {
val a = a.toMutableList()
var count = 0L
var sum = 0L
for (i in 0 until n) {
val ai = a[i]
sum = sum + ai
if (sum == 0L) {
break
}
if (i >= n - 1) {
continue
}
val sum2 = sum + a[i + 1]
if (sum * sum2 < 0) {
continue
} else {
if (sum > 0) {
val tmp = sum2 + 1
a[i + 1] = sum2 - tmp
count += tmp
} else {
val tmp = sum2 - 1
a[i + 1] = sum2 - tmp
count -= tmp
}
}
}
return count
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
long long sum = v[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + v[i] > 0)
sum += v[i];
else if (sum > 0 && sum + v[i] < 0)
sum += v[i];
else if (sum < 0 && sum + v[i] <= 0)
ans += abs(sum + v[i]) + 1, sum = 1;
else if (sum > 0 && sum + v[i] >= 0)
ans += abs(sum + v[i]) + 1, sum = -1;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> x;
int temp, ans = 0;
for (int i = 0; i != n; ++i) {
cin >> temp;
x.push_back(temp);
}
if (!x[0]) {
x[0] = 1;
++ans;
int val, ind;
for (int i = 1; i != n; ++i) {
if (x[i]) {
val = x[i];
ind = i;
break;
}
}
if ((val > 0 && ind % 2) || (val < 0 && !(ind % 2))) x[0] = -1;
}
int sum = x[0];
for (int i = 1; i != n; ++i) {
int sum2 = sum + x[i];
if (sum * sum2 >= 0) {
ans += abs(sum2) + 1;
if (sum < 0)
sum2 = 1;
else
sum2 = -1;
}
sum = sum2;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
import numpy as np
na = np.array(a).cumsum()
cnt = 0
if(na[0] > 0):
for i in range(n):
delta = abs(na[i]) + 1
if(i % 2 == 0 and na[i] <= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] += delta
elif(i % 2 == 1 and na[i] >= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] -= delta
else:
na[i]
else:
for i in range(n):
delta = abs(na[i]) + 1
if(i % 2 == 1 and na[i] <= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] += delta
elif(i % 2 == 0 and na[i] >= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] -= delta
else:
na[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 | # encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
mod = 10**9+7
n = int(input())
a = [int(i) for i in input().split()]
sums = [0 for i in range(n)]
tmp = 0
if a[0] > 0:
status_pos = True
else:
status_pos = False
ans = 0
for i in range(n):
tmp += a[i]
if status_pos and tmp <= 0:
ans += 1-tmp
tmp = 1
elif status_pos == False and tmp >= 0:
ans += 1+tmp
tmp = -1
status_pos = not(status_pos)
print(ans)
# ans2 = abs(a[0])+1
# if a[0] > 0:
# a[0] = -1
# else:
# a[0] = 1
#
# tmp = a[0]
# if a[0] > 0:
# status_pos = True
# else:
# status_pos = False
# for i in range(1,n):
# tmp += a[i]
# if status_pos and tmp <= 0:
# ans2 += 1-tmp
# tmp = 1
# elif status_pos == False and tmp >= 0:
# ans2 += 1+tmp
# tmp = -1
# status_pos = not(status_pos)
#
# print(min(ans,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long cnt1 = 0;
long long sum = a[0];
if (sum == 0) {
sum = 1;
cnt1++;
}
for (int i = 1; i < n; i++) {
long long sumNext = sum + a[i];
if ((sum > 0) && (sumNext >= 0) || (sum < 0) && (sumNext <= 0)) {
if (sum < 0) {
cnt1 += 1 - sumNext;
sumNext = 1;
} else {
cnt1 += 1 + sumNext;
sumNext = -1;
}
}
sum = sumNext;
}
long long cnt2 = 0;
sum = a[0];
if (sum == 0) {
sum = -1;
cnt2++;
}
for (int i = 1; i < n; i++) {
long long sumNext = sum + a[i];
if ((sum > 0) && (sumNext >= 0) || (sum < 0) && (sumNext <= 0)) {
if (sum < 0) {
cnt2 += 1 - sumNext;
sumNext = 1;
} else {
cnt2 += 1 + sumNext;
sumNext = -1;
}
}
sum = sumNext;
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def is_plus(x):
return 0 <= x
_=int(input())
A = list(map(int, input().split()))
total_cnt = 1 if A[0] == 0 else 0
cur_sum = A[0] + total_cnt
pre_sum = cur_sum
for a in A[1:]:
cur_sum += a
if is_plus(pre_sum) == is_plus(cur_sum):
total_cnt += abs(cur_sum) + 1
cur_sum = -1 if is_plus(cur_sum) else +1
elif cur_sum == 0:
total_cnt += 1
cur_sum = 1
else:
pass
pre_sum = cur_sum
print(total_cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> A(n);
vector<int> B(n + 1);
vector<int> B2(n + 1);
B[0] = 0;
B2[0] = 0;
for (long long i = 0; i < n; i++) {
cin >> A[i];
B[i + 1] = A[i] + B[i];
B2[i + 1] = B[i + 1];
}
long long sum_p = 0;
long long pm = 0;
for (long long i = 1; i < n + 1; i++) {
long long del = 0;
if (i % 2 && B[i] + pm <= 0) del = abs(B[i] + pm) + 1;
if (i % 2 == 0 && B[i] + pm >= 0) del = -(B[i] + pm + 1);
pm += del;
sum_p += abs(del);
}
int sum_m = 0;
pm = 0;
for (long long i = 1; i < n + 1; i++) {
long long del = 0;
if (i % 2 == 0 && B2[i] + pm <= 0) del = abs(B2[i] + pm) + 1;
if (i % 2 && B2[i] + pm >= 0) del = -(B2[i] + pm + 1);
pm += del;
sum_m += abs(del);
}
cout << ((sum_p < sum_m) ? sum_p : sum_m) << 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 N = 2e5 + 100;
const int mod = 1e9 + 7;
int a[N];
int n;
int slove(int f) {
int sum = 0, ans = 0;
for (int i = 1; i <= n; i++) {
sum += a[i];
if (sum * f <= 0) {
ans += abs(f - sum);
sum = f;
}
f = -f;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = min(slove(1), slove(-1));
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
import sys
sum=0
cnt=0
# 奇数+
for i in range(n):
z=sum+a[i]
if i%2==0:
if z<0:
sum=z
else:
cnt+=(z+1)
sum=-1
else:
if z>0:
sum=z
else:
cnt+=(1-z)
sum=1
cnt_sbst=cnt
# 奇数-
for i in range(n):
z=sum+a[i]
if i%2==1:
if z<0:
sum=z
else:
cnt+=(z+1)
sum=-1
else:
if z>0:
sum=z
else:
cnt+=(1-z)
sum=1
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
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;
void solve() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long sum = 0, cnt = 0, sum1 = 0, cnt1 = 0, sum2 = 0, cnt2 = 0;
if (a[0])
sum += a[0];
else {
cnt1++;
cnt2++;
sum1 = 1, sum2 = -1;
}
for (long long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
cnt += (abs(sum + a[i]) + 1);
sum = (sum < 0 ? 1 : -1);
}
if (sum1 * (sum1 + a[i]) < 0)
sum1 += a[i];
else {
cnt1 += (abs(sum1 + a[i]) + 1);
sum1 = (sum1 < 0 ? 1 : -1);
}
if (sum2 * (sum2 + a[i]) < 0)
sum2 += a[i];
else {
cnt2 += (abs(sum2 + a[i]) + 1);
sum2 = (sum2 < 0 ? 1 : -1);
}
}
cout << (a[0] ? cnt : min(cnt1, cnt2)) << endl;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
wa=a[0]
ans1,ans2=0,0
if a[0]==0:
ans1+=1
wa=1
for i in range(1,n):
# print(wa)
if wa>0:
if wa+a[i]<0:
wa+=a[i]
else:
ans1+=abs(wa+a[i])+1
wa=-1
else:
if wa+a[i]>0:
wa+=a[i]
else:
ans1+=abs(wa+a[i])+1
wa=1
if a[0]>0:
ans2+=a[0]+1
wa=-1
else:
ans2+=-a[0]+1
wa=1
for i in range(1,n):
if wa>0:
if wa+a[i]<0:
wa+=a[i]
else:
ans2+=abs(wa+a[i])+1
wa=-1
else:
if wa+a[i]>0:
wa+=a[i]
else:
ans2+=abs(wa+a[i])+1
wa=1
print(min(ans1,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
W=[]
wa=0
for i in range(n):
wa=A[i]+wa
W.append(wa)
counter=0
for i in range(n):
if i==n-1:
break
elif W[i]<0 and W[i+1]<0:
counter=abs(W[i])-abs(A[i+1])+1+counter
A[i+1]=1
elif W[i]>0 and W[i+1]>0:
counter=abs(W[i])+1+counter-abs(A[i+1])
A[i+1]=-1
if A[n-1]==0:
if W[n-2]*W[n-1]>0:
counter=counter+abs(W[n-2])+1
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n, sign;
long long a[100002], sum = 0, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (i == 0) {
if (a[i] >= 0) sign = 1;
if (a[i] < 0) sign = -1;
}
sum += a[i];
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;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
int count = 0;
if (a.at(0) == 0) {
if (a.at(1) < 0) {
a.at(0) = -1;
} else {
a.at(0) = 1;
}
count++;
}
int sum = a.at(0);
for (int i = 1; i < N; i++) {
if (sum > 0 && sum + a.at(i) >= 0) {
count += abs(-1 - sum - a.at(i));
a.at(i) = -1 - sum;
} else if (sum < 0 && sum + a.at(i) <= 0) {
count += abs(1 - sum - a.at(i));
a.at(i) = 1 - sum;
}
sum += a.at(i);
}
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 unsigned long long MOD = 1000000000 + 7;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int minn = 1 << 30;
for (int j = 0; j < 2; j++) {
int cnt = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
if (j == 1 && i == 0) {
a.at(0) *= -1;
}
int s = a.at(i) + sum;
if (s == 0) {
if (sum <= 0) {
cnt += 1 - s;
sum = 1;
} else {
cnt += 1 + s;
sum = 1;
}
} else if (sum < 0 && s < 0) {
cnt += 1 - s;
sum = 1;
} else if (sum > 0 && s > 0) {
cnt += 1 + s;
sum = -1;
} else {
sum = s;
}
}
minn = min(minn, cnt);
}
cout << minn << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
s = [0]*(n+1)
for i in range(n):
s[i+1] = s[i] + a[i]
if s[i] < 0:
if s[i+1] != 0 and s[i+1] > 0:
continue
else:
ans += abs(s[i+1])+1
s[i+1] = 1
elif s[i] > 0:
if s[i+1] != 0 and s[i+1] < 0:
continue
else:
ans += abs(s[i+1])+1
s[i+1] = -1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
long long int A[100005];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lld", &A[i]);
}
long long int plus, sum;
if (A[0] <= 0) {
plus = abs(1 - A[0]);
sum = 1;
} else {
plus = 0;
sum = A[0];
}
for (int i = 1; i < N; i++) {
sum += A[i];
if (i % 2 == 1) {
if (sum < 0) continue;
plus += abs(-1 - sum);
sum = -1;
} else {
if (sum > 0) continue;
plus += abs(1 - sum);
sum = 1;
}
}
long long int minus;
if (A[0] >= 0) {
minus = (-1 - A[0]);
sum = -1;
} else {
minus = 0;
sum = A[0];
}
for (int i = 1; i < N; i++) {
sum += A[i];
if (i % 2 == 1) {
if (sum > 0) continue;
minus += abs(1 - sum);
sum = 1;
} else {
if (sum < 0) continue;
minus += abs(-1 - sum);
sum = -1;
}
}
printf("%lld\n", min(plus, minus));
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();
}
sc.close();
int count = 0;
int sum = a[0];
for(int i=1; i<N; i++){
int diff = checkSum(sum, sum+a[i]);
count += Math.abs(diff);
sum = sum+a[i]+diff;
}
System.out.println(count);
}
private static int checkSum(int a, int b){
if ( a>0 && b>=0){
return -(b+1);
}else if( a<0 && b<=0){
return -(b-1);
}else{
return 0;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
as = gets.chomp.split.map(&:to_i)
ans_o = ans_e = 0
sum_o = sum_e = as[0]
1.upto(n-1) do |i|
sum_e += as[i]
sum_o += as[i]
if i.even?
until sum_e > 0
ans_e += 1
sum += 1
end
until sum_o < 0
ans_o += 1
sum_o -= 1
end
else
until sum_e < 0
ans_e += 1
sum -= 1
end
until sum_o > 0
ans_o += 1
sum += 1
end
end
end
puts [ans_e,ans_o].min
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A) {
long res = 0;
long sum = A[0];
for (int i = 1; i < A.size(); i++) {
if (sum > 0) {
sum += A[i];
while (sum >= 0) {
res++;
sum--;
}
} else if (sum < 0) {
sum += A[i];
while (sum <= 0) {
res++;
sum++;
}
}
}
return res;
}
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
long res;
if (A[0] != 0) {
res = solve(A);
cout << res << endl;
} else {
long res_first_plus = 1, res_first_minus = 1;
A[0] = 1;
res_first_plus += solve(A);
A[0] = -1;
res_first_minus += solve(A);
res = min(res_first_plus, res_first_minus);
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;
int n, b, a[100005];
int abs(int p) { return p > 0 ? p : -p; }
int f(int p) {
int i, s = 0;
for (i = 1; i < n; i++) {
if (p > 0) {
p += a[i];
if (p >= 0) s += p + 1, p = -1;
} else {
p += a[i];
if (p <= 0) s += -p + 1, p = 1;
}
}
return s;
}
int main() {
int i, t;
cin >> n;
for (i = 0; i < n; i++) scanf("%d", &a[i]);
if (a[0] < 0)
t = f(1) - a[0] + 1;
else
t = f(-1) + a[0] - 1;
cout << min(f(a[0]), t);
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 | fun main() {
val n = readLine()!!.toInt()
val a = readLine()!!.split(" ").map { it.toLong() }
var answer = 0L
var total = a[0]
for (i in 1 until n) {
val tmp = total
total = total + a[i]
if (total == 0L) {
if (tmp > 0) {
answer += 1
total = -1
} else if (tmp < 0) {
answer += 1
total = 1
}
}
if (tmp > 0 && total > 0) {
answer += (total + 1)
total = -1
} else if (tmp < 0 && total < 0) {
answer += (-total + 1)
total = 1
}
}
println("$answer")
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int n, a[111111], hoge, huga, nyaa = 0, nyan = 0;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
if (!a[0]) {
hoge = 1;
huga = -1;
nyaa = nyan = 1;
} else {
hoge = ((a[0]) > (-a[0]) ? (a[0]) : (-a[0]));
huga = ((a[0]) > (-a[0]) ? (-a[0]) : (a[0]));
}
int p = 1;
for (int i = 1; i < n; i++) {
hoge += a[i];
if (p) {
if (hoge >= 0) {
nyaa += hoge + 1;
hoge = -1;
}
} else {
if (hoge <= 0) {
nyaa += 1 - hoge;
hoge = 1;
}
}
huga += a[i];
if (p) {
if (huga <= 0) {
nyan += 1 - huga;
huga = 1;
}
} else {
if (huga >= 0) {
nyan += huga + 1;
huga = -1;
}
}
p ^= 1;
}
printf("%d\n", ((nyaa) > (nyan) ? (nyan) : (nyaa)));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
enum PosiNega { POSITIVE = 0, NEGATIVE = 1 };
int solve(int N, long long *a, PosiNega odd_posinega) {
long long ans = 0;
long long sum = 0;
PosiNega posi_nega = odd_posinega;
for (int i = 0; i < N; i++) {
sum += a[i];
if (POSITIVE == posi_nega) {
if (0 >= sum) {
ans += 1 - sum;
sum = 1;
}
posi_nega = NEGATIVE;
} else {
if (0 <= sum) {
ans += 1 + sum;
sum = -1;
}
posi_nega = POSITIVE;
}
}
return ans;
}
void _main() {
int N;
cin >> N;
long long a[N];
for (int i = 0; i < N; i++) cin >> a[i];
long long ans = solve(N, a, POSITIVE);
long long _ans = solve(N, a, NEGATIVE);
chmin(ans, _ans);
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> a;
for (int i = 0; i < n; i++) {
int an;
scanf("%d", &an);
a.push_back(an);
}
int op_count = 0;
int now_sum = 0;
if (a[0] == 0) {
a[0] = 1;
op_count++;
}
int adding = a[0] > 0 ? -1 : 1;
for (int i = 0; i < n; i++) {
now_sum += a[i];
adding *= -1;
if (now_sum == 0) {
a[i] += adding;
now_sum += adding;
op_count++;
continue;
}
if (adding > 0) {
while (now_sum <= 0) {
a[i] += adding;
now_sum += adding;
op_count++;
}
} else {
while (now_sum >= 0) {
a[i] += adding;
now_sum += adding;
op_count++;
}
}
}
printf("%d\n", op_count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
bool diff(long long a, long long b) {
if (a < 0 && b > 0) return true;
if (a > 0 && b < 0) return true;
return false;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
long long ans = 0, sum = a[0];
for (int i = (1); i < (n); ++i) {
if (diff(sum + a[i], sum)) {
sum += a[i];
} else {
long long need = (sum > 0 ? -1 : 1);
long long now = need - sum;
ans += abs(now - a[i]);
sum = need;
}
}
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 | UNKNOWN | from strutils import split, parseInt, parseFloat
from sequtils import map
import macros
macro unpack*(input: seq; count: static[int]): untyped =
result = quote do: ()
when NimMinor <= 13: # 本当にここが区切りかどうかは知らない
for i in 0..<count: result[0].add quote do: `input`[`i`]
else:
for i in 0..<count: result.add quote do: `input`[`i`]
# count == 0 のとき unpackしない
# count > 0 のとき count個分 unpack した結果の tuple を返す
type UnselectableTypeError = object of Exception
template input(typ: typedesc; count: static[Natural] = 0): untyped =
let line = stdin.readLine.split
when count == 0:
when typ is int: line.map(parseInt)
elif typ is float: line.map(parseFloat)
elif typ is string: line
else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
else:
when typ is int: line.map(parseInt).unpack(count)
elif typ is float: line.map(parseFloat).unpack(count)
elif typ is string: line.unpack(count)
else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from math import nextPowerOfTwo
from sequtils import newSeqWith
type SegmentTree*[T] = ref object of RootObj
tree: seq[T]
leafCount: Natural
initValue: T
mergeProc: proc (x, y: T): T {.closure.}
proc merge[T](this: SegmentTree[T]; left, right: Natural): T =
this.mergeProc(this.tree[left], this.tree[right])
proc toSegmentTree*[T](a: openArray[T]; initValue: T; mergeProc: proc (x, y: T): T {.closure.}): SegmentTree[T] =
let leafCount = a.len.nextPowerOfTwo
result = SegmentTree[T](tree: newSeqWith(2 * leafCount - 1, initValue),
leafCount: leafCount,
initValue: initValue,
mergeProc: mergeProc)
for i, ai in a:
result.tree[i + result.leafCount - 1] = ai
for i in countdown(result.leafCount - 2, 0):
result.tree[i] = result.merge(2 * i + 1, 2 * i + 2)
proc update*[T](this: SegmentTree[T]; i, v: int): SegmentTree[T] =
result = this
var j = result.leafCount + i - 1
result.tree[j] = v
while j > 0:
j = (j - 1) div 2
result.tree[j] = result.merge(2 * j + 1, 2 * j + 2)
proc update*[T](this: var SegmentTree[T]; i, v: int) =
var j = this.leafCount + i - 1
this.tree[j] = v
while j > 0:
j = (j - 1) div 2
this.tree[j] = this.merge(2 * j + 1, 2 * j + 2)
proc query*[T](this: SegmentTree[T]; requiredRange: Slice[int]; k = 0; coveredRange: Slice[Natural] = 0.Natural..high(int).Natural): T =
let
left = coveredRange.a
right = coveredRange.b
if right == high(int) and this.leafCount != high(int):
return this.query(requiredRange, k, left..(this.leafCount - 1).Natural)
if right < requiredRange.a or requiredRange.b < left:
return this.initValue
if requiredRange.a <= left and right <= requiredRange.b:
return this.tree[k]
let
lv = this.query(requiredRange, 2 * k + 1, left..((left + right) div 2).Natural)
rv = this.query(requiredRange, 2 * k + 2, ((left + right + 1) div 2).Natural..right)
return this.mergeProc(lv, rv)
let
n = input(int, 1)
var
a = input(int, 0).toSegmentTree(0, proc (x, y: int): int = x + y)
result = 0
for i in 1..<n:
let
sum = a.query(0..i)
preSum = a.query(0..(i - 1))
if sum > 0 and preSum > 0 or sum < 0 and preSum < 0 or sum == 0:
let
p = preSum.abs - 1
c = sum.abs - p + 1
result += p + c
if sum <= 0:
a.update(i - 1, a.tree[a.leafCount + i - 2] + p)
a.update(i, a.tree[a.leafCount + i - 1] + c)
else:
a.update(i - 1, a.tree[a.leafCount + i - 2] - p)
a.update(i, a.tree[a.leafCount + i - 1] - c)
echo result
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = a.copy()
for i in range(1, n):
if sum(a[:i]) >= 0 and sum(a[:i+1]) >= 0:
a[i] = -1 - sum(a[:i])
elif sum(a[:i]) <= 0 and sum(a[:i+1]) <= 0:
a[i] = 1 - sum(a[:i])
print(sum(abs(b-a))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
int n;
cin >> n;
long long a[n];
long long sum = 0;
long long count = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
sum = a[i];
} else {
if (sum < 0) {
if (sum + a[i] <= 0) {
count += 1 - (sum + a[i]);
sum = 1;
} else {
sum = sum + a[i];
}
} else if (sum > 0) {
if (sum + a[i] >= 0) {
count += sum + a[i] - (-1);
sum = -1;
} else {
sum = sum + a[i];
}
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner no=new Scanner(System.in);
int n=no.nextInt();
int arr[]=new int[n];
// int sum1[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=no.nextInt();
/* if(i==0){
sum1[i]=arr[i];
}
else{
sum1[i]=sum1[i]+sum1[i-1];
}*/
}
int sum=0;
int count=0;
if(arr[0]>0){
sum=arr[0];
for(int i=1;i<n;i++){
if(i%2==1&&sum+arr[i]>0){
int t=arr[i];
arr[i]=(sum+1)*-1;
count=count+Math.abs((t-arr[i]));
// sum=sum+arr[i];
}
else if(i%2==0&&sum+arr[i]<0){
int t=arr[i];
arr[i]=(Math.abs(sum)+1);
count=count+Math.abs((Math.abs(t)-arr[i]));
//sum=sum+arr[i];
}
else if(sum+arr[i]==0){
count++;
}
sum=sum+arr[i];
}
}
else if(arr[0]<0){
sum=arr[0];
for(int i=1;i<n;i++){
if(i%2==1&&sum+arr[i]<0){
int t=arr[i];
arr[i]=(Math.abs(sum)+1);
count=count+Math.abs((Math.abs(t)-arr[i]));
// sum=sum+arr[i];
// System.out.println(count);
}
else if(i%2==0&&sum+arr[i]>0){
int t=arr[i];
arr[i]=(sum+1)*-1;
count=count+Math.abs((t-arr[i]));
//System.out.println(count);
//sum=sum+arr[i];
}
else if(sum+arr[i]==0){
count++;
}
sum=sum+arr[i];
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
class MYCP {
public:
static const long long TEISUU = 1000 * 1000 * 1000 + 7;
static long long DebugFlag;
static string MakeString_LongLong(vector<long long> const& numbers,
string const& str) {
if (numbers.size() == 0) return "";
string result = "" + to_string(numbers[0]);
for (long long i = 1; i < numbers.size(); i++) {
result += str;
result += to_string(numbers[i]);
}
return result;
}
static string MakeString_LongLong(vector<long long> const& numbers) {
if (numbers.size() == 0) return "";
string result = "" + to_string(numbers[0]);
for (long long i = 1; i < numbers.size(); i++) {
result += " ";
result += to_string(numbers[i]);
}
return result;
}
static string MakeString_VectorString(vector<string> const& str) {
string result = "";
for (long long i = 0; i < str.size(); i++) {
result += str[i] + "\n";
}
return result;
}
static vector<string> MyReadLineSplit(long long n) {
vector<string> str(n);
for (long long i = 0; i < n; i++) {
std::cin >> str[i];
}
return str;
}
static vector<long long> ReadInts(long long number) {
vector<long long> a(number);
for (int i = 0; i < number; i++) {
std::cin >> a[i];
}
return a;
}
static bool PrimeCheck_Int(long long number) {
if (number < 2) return false;
for (unsigned long long i = 2; i * i <= number; i++) {
if (number % i == 0) return false;
}
return true;
}
static vector<long long> MakePrimeList(long long n) {
vector<long long> list;
long long i, j, p;
bool flag;
for (i = 2; i <= n; i++) {
flag = true;
for (j = 0; j < list.size(); j++) {
if (!(list[j] * list[j] <= i)) break;
if (i % list[j] == 0) {
flag = false;
break;
}
}
if (flag) list.push_back(i);
}
return list;
}
static vector<string> split(string const& str, char sep) {
vector<std::string> v;
auto first = str.begin();
while (first != str.end()) {
auto last = first;
while (last != str.end() && *last != sep) last++;
v.push_back(string(first, last));
if (last != str.end()) last++;
first = last;
}
return v;
}
static long long Sum(vector<long long> a) {
long long i, sum = 0;
for (i = 0; i < a.size(); i++) {
sum += a[i];
}
return sum;
}
static bool Komoji(char a) {
if (a >= 'a' && a <= 'z') return true;
return false;
}
static bool Oomoji(char a) {
if (a >= 'A' && a <= 'Z') return true;
return false;
}
static long long KiriageWarizan(long long a, long long b) {
long long result = a / b;
if (a % b > 0) result++;
return result;
}
static long long GreatestCommonFactor(long long a, long long b) {
long long temp;
if (a < b) {
temp = b;
b = a;
a = temp;
}
while (true) {
temp = a % b;
a = b;
b = temp;
if (b == 0) break;
}
return a;
}
static long long LeastCommonMultiple(long long a, long long b) {
return (a / GreatestCommonFactor(a, b)) * b;
}
static vector<vector<long long> > PrimeFactorization(long long n) {
vector<long long> p_list, s_list;
long long i, j, k, count;
for (i = 2; n > 1; i++) {
if (i * i > n) {
p_list.push_back(n);
s_list.push_back(1);
break;
}
if (n % i == 0) {
count = 0;
while (n % i == 0) {
n /= i;
count++;
}
p_list.push_back(i);
s_list.push_back(count);
}
}
vector<vector<long long> > result;
result.push_back(p_list);
result.push_back(s_list);
return result;
}
static long long Combination(long long n, long long r) {
r = min(r, n - r);
vector<long long> p(n + 1, 0);
long long i, j, k, a, b, c;
for (i = 1; i <= r; i++) {
auto temp = MYCP::PrimeFactorization(i);
for (j = 0; j < temp[0].size(); j++) {
p[temp[0][j]] -= temp[1][j];
}
a = i + n - r;
temp = MYCP::PrimeFactorization(a);
for (j = 0; j < temp[0].size(); j++) {
p[temp[0][j]] += temp[1][j];
}
}
long long result = 1;
for (i = 0; i < p.size(); i++) {
if (p[i] > 0) {
for (j = 0; j < p[i]; j++) {
result *= i;
result %= MYCP::TEISUU;
}
}
}
return result;
}
static long long sign(long long x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
static long long DebugPrintf(string output) {
if (MYCP::DebugFlag != 0) {
std::cout << output << endl;
}
return MYCP::DebugFlag;
}
static long long DebugCin() {
long long a;
if (MYCP::DebugFlag != 0) {
cin >> a;
}
return a;
}
};
long long MYCP::DebugFlag = 0;
class Syakutori {
private:
vector<long long> list;
public:
void MakeArray(vector<long long> data) {
long long i;
list = data;
list.push_back(0);
list[0] = 0;
for (i = 1; i < list.size(); i++) {
list[i] = list[i - 1] + data[i - 1];
}
}
long long Sum(long long start, long long end) {
if (end < start) {
std::cout << "startがendより大きいです";
return 0;
}
if (start < 0 || end >= list.size()) {
std::cout << "範囲が異常";
return 0;
}
return list[end] - list[start];
}
};
int main(void) {
MYCP::DebugFlag = 0;
long long i, j, k, n, m;
long long count = 0;
cin >> n;
auto a = MYCP::ReadInts(n);
long long sum = a[0], next;
for (i = 1; i < n; i++) {
long long sign = MYCP::sign(sum);
sign *= MYCP::sign(sum + a[i]);
if (sign != -1) {
sign = MYCP::sign(sum) * (-1);
k = sign - sum;
count += abs(a[i] - k);
a[i] = k;
}
sum += a[i];
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long int> b = a, c = a;
long long int count1 = 0, count2 = 0;
if (b[0] <= 0) {
count1 += 1 - b[0];
b[0] = 1;
}
if (c[0] >= 0) {
count2 += c[0] - (-1);
c[0] = -1;
}
for (int i = 1; i < n; i++) {
b[i] += b[i - 1];
c[i] += c[i - 1];
if (i % 2 == 0) {
if (b[i] <= 0) {
count1 += 1 - b[i];
b[i] = 1;
}
if (c[i] >= 0) {
count2 += c[i] - (-1);
c[i] = -1;
}
}
if (i % 2 != 0) {
if (b[i] >= 0) {
count1 += b[i] - (-1);
b[i] = -1;
}
if (c[i] <= 0) {
count2 += 1 - c[i];
c[i] = -1;
}
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[200005] = {0};
int main() {
long long n, i;
cin >> n;
for (i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
long long sum1 = 0, sum2 = 0;
long long ans1 = 0, ans2 = 0;
sum1 += a[1], sum2 += a[1];
if (a[1] == 0) ans1 += 1, sum1 = 1, ans2 += 1, sum2 = -1;
for (i = 2; i <= n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
} else if (i % 2 != 0 && sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
}
for (i = 2; i <= n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
} else if (i % 2 == 1 && 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 | java | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Long> alist = new ArrayList<>();
for (int i = 0; i < n; i++) {
alist.add(sc.nextLong());
}
int cntOdd = 0;
int cntEvn = 0;
long sum = 0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
//iが偶数のとき正
if(i%2 == 0) {
if(sum > 0) {
continue;
} else {
while(sum <= 0) {
int calc = 1;
sum += calc;
cntEvn++;
}
}
} else {
if(sum < 0) {
continue;
} else {
while(sum >= 0) {
int calc = -1;
sum += calc;
cntEvn++;
}
}
}
}
sum =0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
//iが偶数のとき負
if (i%2 == 0) {
if(sum < 0) {
continue;
} else {
while(sum >= 0) {
int calc = -1;
sum += calc;
cntOdd++;
}
}
} else {
if(sum > 0) {
continue;
} else {
while(sum <= 0) {
int calc = 1;
sum += calc;
cntOdd++;
}
}
}
}
if(cntOdd <= cntEvn) {
System.out.println(cntOdd);
} else {
System.out.println(cntEvn);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 = 200010;
long long n, m, md, ans;
long long a[maxn], pre[maxn];
;
long long read() {
long long s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
int main() {
md = 0, ans = 0;
memset(pre, 0, sizeof(pre));
n = read();
for (long long i = 1; i <= n; i++) {
a[i] = read();
pre[i] = a[i];
pre[i] += pre[i - 1];
}
if (pre[1] != 0) {
for (long long i = 1; i < n; i++) {
long long tmp = md;
if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) {
if ((pre[i] + tmp) < 0) {
md += (1ll - (pre[i + 1] + tmp));
ans += (1ll - (pre[i + 1] + tmp));
} else {
md -= ((pre[i + 1] + tmp) + 1ll);
ans += (1ll + (pre[i + 1] + tmp));
}
}
}
} else {
long long ans1 = 0, ans2 = 0;
md = 0, ans = 0;
pre[0] = -1;
for (long long i = 0; i < n; i++) {
long long tmp = md;
if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) {
if ((pre[i] + tmp) < 0) {
md += (1ll - (pre[i + 1] + tmp));
ans1 += (1ll - (pre[i + 1] + tmp));
} else {
md -= ((pre[i + 1] + tmp) + 1ll);
ans1 += (1ll + (pre[i + 1] + tmp));
}
}
}
md = 0, ans = 0;
pre[0] = 1ll;
for (long long i = 0; i < n; i++) {
long long tmp = md;
if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) {
if ((pre[i] + tmp) < 0) {
md += (1ll - (pre[i + 1] + tmp));
ans2 += (1ll - (pre[i + 1] + tmp));
} else {
md -= ((pre[i + 1] + tmp) + 1ll);
ans2 += (1ll + (pre[i + 1] + tmp));
}
}
}
ans = min(ans1, ans2);
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long cal(long long b0, int n, long long* a, long long ans) {
long long b[n];
b[0] = b0;
for (int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
if (b[i] == 0) {
ans++;
b[i] = -1 * b[i - 1] / b[i - 1];
}
if (a[i] * b[i - 1] > 0 || (abs(a[i]) - abs(b[i - 1])) < 0) {
ans += abs(a[i] + b[i - 1]) + 1;
b[i] = -1 * b[i - 1] / b[i - 1];
}
}
return ans;
}
int main() {
int n;
cin >> n;
long long a[n], ans = 0;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] != 0) {
cout << cal(a[0], n, a, ans) << endl;
} else {
ans++;
cout << (cal(1, n, a, ans) < cal(-1, n, a, ans) ? cal(1, n, a, ans)
: cal(-1, n, a, ans))
<< endl;
return 0;
}
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 | # input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
n=int(input())
A=list(map(int,input().split()))
def main(c,temp):
if temp>0:
flag=1
elif temp<0:
flag=0
for i in range(1,n):
temp+=A[i]
if flag:
if temp>=0:
c+=temp-(-1)
temp=-1
flag=0
else:
if temp<=0:
c+=1-temp
temp=1
flag=1
return(c)
temp=A[0]
if temp:
print(main(0,temp))
else:
temp=1
a=main(1,temp)
temp=-1
b=main(1,temp)
print(min(a,b))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100010] = {0};
long long sequence(long long n) {
long long sum = 0, count = 0;
bool sign = true;
if (a[1] > 0) {
sum = a[1];
sign = false;
for (long long i = 2; i <= n; i++) {
sum += a[i];
if (!sign) {
if (sum > 0) {
count += sum + 1;
sum = sum - sum - 1;
sign = true;
} else {
if (sum == 0) {
count++;
sum -= 1;
sign = true;
} else
sign = true;
}
} else {
if (sum < 0) {
count += -sum + 1;
sum = sum - sum + 1;
sign = false;
} else {
if (sum == 0) {
count++;
sum += 1;
sign = false;
} else
sign = false;
}
}
}
} else {
if (a[1] < 0) {
sum = a[1];
sign = true;
for (long long i = 2; i <= n; i++) {
sum += a[i];
if (!sign) {
if (sum > 0) {
count += sum + 1;
sum = sum - sum - 1;
sign = true;
} else {
if (sum == 0) {
count++;
sum -= 1;
sign = true;
} else
sign = true;
}
} else {
if (sum < 0) {
count += -sum + 1;
sum = sum - sum + 1;
sign = false;
} else {
if (sum == 0) {
count++;
sum += 1;
sign = false;
} else
sign = false;
}
}
}
} else {
count++;
sum = 1;
sign = false;
for (long long i = 2; i <= n; i++) {
sum += a[i];
if (!sign) {
if (sum > 0) {
count += sum + 1;
sum = sum - sum - 1;
sign = true;
} else {
if (sum == 0) {
count++;
sum -= 1;
sign = true;
} else
sign = true;
}
} else {
if (sum < 0) {
count += -sum + 1;
sum = sum - sum + 1;
sign = false;
} else {
if (sum == 0) {
count++;
sum += 1;
sign = false;
;
} else
sign = false;
}
}
}
}
}
return count;
}
int main() {
long long n, sum = 0, count = 0;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
cout << sequence(n) << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.