Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
ArrayList<Integer> a = new ArrayList<>();
for(int i=0; i<n; i++){
a.add(Integer.parseInt(sc.next()));
}
ArrayList<Integer> a1 = new ArrayList<>(a);
ArrayList<Integer> a2 = new ArrayList<>(a);
int ans1 = 0;
for(int i=0; i<n; i++){
if (i%2 == 0){
int b = Math.max(1 - a1.subList(0, i+1).stream().mapToInt(Integer::intValue).sum(), 0);
a1.set(i, a1.get(i) + b);
ans1 += b;
}else {
int b = Math.max(a1.subList(0, i+1).stream().mapToInt(Integer::intValue).sum() + 1, 0);
a1.set(i, a1.get(i) - b);
ans1 += b;
}
}
int ans2 = 0;
for(int i=0; i<n; i++){
if (i%2 == 0){
int b = Math.max(a2.subList(0, i+1).stream().mapToInt(Integer::intValue).sum() + 1, 0);
a2.set(i, a2.get(i) - b);
ans2 += b;
}else {
int b = Math.max(1 - a2.subList(0, i+1).stream().mapToInt(Integer::intValue).sum(), 0);
a2.set(i, a2.get(i) + b);
ans2 += b;
}
}
System.out.println(Math.min(ans1, ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
vector<long long> data(N);
vector<long long> rui(N + 1);
long long K = 0;
long long G = 0;
for (int(i) = int(0); (i) < int(N); (i)++) {
cin >> data[i];
rui[i + 1] = data[i] + rui[i];
if ((i % 2) == 0) {
K += rui[i + 1];
} else {
G += rui[i + 1];
}
}
long long p = 0;
long long ans = 0;
for (int(i) = int(1); (i) < int(N + 1); (i)++) {
if (K < G) {
if ((i % 2) == 0) {
if (rui[i] + p <= 0) {
ans += (rui[i] + p) * -1 + 1;
p += (rui[i] + p) * -1 + 1;
}
} else {
if (rui[i] + p >= 0) {
ans += (rui[i] + p) + 1;
p += (rui[i] + p) * -1 - 1;
}
}
} else {
if ((i % 2) == 1) {
if (rui[i] + p <= 0) {
ans += (rui[i] + p) * -1 + 1;
p += (rui[i] + p) * -1 + 1;
}
} else {
if (rui[i] + p >= 0) {
ans += (rui[i] + p) + 1;
p += (rui[i] + p) * -1 - 1;
}
}
}
}
cout << (ans) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, curr = 0, prev;
long long t = 0;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
prev = curr;
curr += a[i];
if (i != 0) {
if (prev < 0 && curr <= 0) {
t += -curr + 1;
curr = 1;
} else if (prev > 0 && curr >= 0) {
t += curr + 1;
curr = -1;
}
}
}
cout << 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 | python3 | N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
sam += a[i]
if sam >= 0 and old > 0:
num += (abs(sam) + 1)
sam -= (sam + 1)
elif sam <= 0 and old < 0:
num += (abs(sam) + 1)
sam -= (sam + 1)
old = sam
print(num)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s = [a[0]]
for i in range(1, n):
s.append(s[-1]+a[i])
cnt_a = 0
base = 0
for i in range(n):
if (base + s[i] > 0) and (i % 2 == 1) or (base + s[i] < 0) and (i % 2 == 0):
cnt_a += (1 + abs(base + s[i]))
base += (1 + abs(base + s[i])) * (-1) ** i
cnt_b = 0
base = 0
for i in range(n):
if (base + s[i] < 0) and (i % 2 == 1) or (base + s[i] > 0) and (i % 2 == 0):
cnt_b += (1 + abs(base + s[i]))
base -= (1 + abs(base + s[i])) * (-1) ** i
print(min(cnt_a, cnt_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 | UNKNOWN | main :: IO()
main = do
getLine
lis <- words <$> getLine
let numlis = map read lis
print $ solve numlis 0
solve :: [Int] -> Int -> Int
solve [] x = 0
solve (x:xs) 0 = solve xs x
solve (x:xs) m
| m > 0 = max 0 (sum+1) + solve xs (min sum (-1))
| m < 0 = max 0 (1-sum) + solve xs (max sum 1)
where sum = m+x
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | if __name__ == '__main__':
N = input()
array = raw_input().split()
ans = 0
total = int(array[0])
totalZero = False
if total == 0:
totalZero = True
flag = False
if total >= 0:
flag = True
for a in array[1:]:
if totalZero == True:
ans += 1
if a > 0:
total = -1
flag = False
else:
total = 1
flag = True
totalZero = False
total += int(a)
if total > 0 and flag == True:
ans += total + 1
total = -1
elif total < 0 and flag ==False:
ans += -1 * total + 1
total = 1
elif total == 0:
totalZero = True
if total > 0:
flag = True
elif total < 0:
flag = False
if totalZero == True:
ans += 1
print ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i_i = pair<int, int>;
using ll_ll = pair<ll, ll>;
using d_ll = pair<double, ll>;
using ll_d = pair<ll, double>;
using d_d = pair<double, double>;
template <class T>
using vec = vector<T>;
static constexpr ll LL_INF = 1LL << 60;
static constexpr int I_INF = 1 << 28;
static constexpr double PI =
static_cast<double>(3.14159265358979323846264338327950288);
static constexpr double EPS = numeric_limits<double>::epsilon();
static map<type_index, const char* const> scanType = {{typeid(int), "%d"},
{typeid(ll), "%lld"},
{typeid(double), "%lf"},
{typeid(char), "%c"}};
template <class T>
static void scan(vector<T>& v);
[[maybe_unused]] static void scan(vector<string>& v, bool isWord = true);
template <class T>
static inline bool chmax(T& a, T b);
template <class T>
static inline bool chmin(T& a, T b);
template <class T>
static inline T gcd(T a, T b);
template <class T>
static inline T lcm(T a, T b);
template <class A, size_t N, class T>
static void Fill(A (&arr)[N], const T& val);
template <class T>
T mod(T a, T m);
template <class Monoid>
struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {
sz = 1;
while (sz < n) sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid& x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid& x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) L = f(L, seg[a++]);
if (b & 1) R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int& k) const { return seg[k + sz]; }
template <class C>
int find_subtree(int a, const C& check, Monoid& M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <class C>
int find_first(int a, const C& check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt)) return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <class C>
int find_last(int b, const C& check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R))) return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt)) return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main(int argc, char* argv[]) {
ll n;
cin >> n;
vec<ll> a(n);
scan(a);
SegmentTree<ll> seg(
n, [](ll x, ll y) { return x + y; }, 0LL);
for (int i = (0); i < (n); i++) {
seg.set(i, a[i]);
}
seg.build();
ll ans = 0;
bool next_sign = (a[0] < 0) ? true : false;
if (a[0] == 0) {
seg.update(0, 1LL);
next_sign = false;
ans++;
}
for (int i = (2); i < (n + 1); i++) {
ll sum = seg.query(0, i);
if ((next_sign && sum > 0) || (!next_sign && sum < 0)) {
next_sign = !next_sign;
continue;
}
ll to = (next_sign) ? 1 : -1;
ll diff = abs(sum - to);
ans += diff;
seg.update(i - 1, seg[i - 1] + (to - sum));
next_sign = !next_sign;
}
ll ans2 = 0;
for (int i = (0); i < (n); i++) {
seg.update(i, a[i]);
}
next_sign = (a[0] < 0) ? true : false;
if (a[0] == 0) {
seg.update(0, -1LL);
next_sign = true;
ans2++;
}
for (int i = (2); i < (n + 1); i++) {
ll sum = seg.query(0, i);
if ((next_sign && sum > 0) || (!next_sign && sum < 0)) {
next_sign = !next_sign;
continue;
}
ll to = (next_sign) ? 1 : -1;
ll diff = abs(sum - to);
ans2 += diff;
seg.update(i - 1, seg[i - 1] + (to - sum));
next_sign = !next_sign;
}
((cout) << (min(ans, ans2)) << (endl));
return 0;
}
template <class T>
static void scan(vector<T>& v) {
auto tFormat = scanType[typeid(T)];
for (T& n : v) {
scanf(tFormat, &n);
}
}
static void scan(vector<string>& v, bool isWord) {
if (isWord) {
for (auto& n : v) {
cin >> n;
}
return;
}
int i = 0, size = v.size();
string s;
getline(cin, s);
if (s.size() != 0) {
i++;
v[0] = s;
}
for (; i < size; ++i) {
getline(cin, v[i]);
}
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline T gcd(T a, T b) {
return __gcd(a, b);
}
template <class T>
inline T lcm(T a, T b) {
T c = min(a, b), d = max(a, b);
return c * (d / gcd(c, d));
}
template <class A, size_t N, class T>
void Fill(A (&arr)[N], const T& val) {
std::fill((T*)arr, (T*)(arr + N), val);
}
template <class T>
T mod(T a, T m) {
return (a % m + m) % m;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> sum(n + 1, 0);
int evenSum = 0, oddSum = 0;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
sum[i + 1] = a[i] + sum[i];
}
ll add1 = 0, add2 = 0;
for (int i = 1; i < n + 1; i++) {
ll tmp = sum[i] + add1;
if (i % 2) {
if (tmp >= 0) {
int b = sum[i] + add1 + 1;
oddSum += b;
add1 -= b;
}
if (tmp <= 0) {
int b = 1 - (sum[i] + add2);
evenSum += b;
add2 += b;
}
} else {
if (tmp <= 0) {
int b = 1 - (sum[i] + add1);
oddSum += b;
add1 -= b;
}
if (tmp <= 0) {
int b = sum[i] + add2 + 1;
evenSum += b;
add2 -= b;
}
}
}
cout << min(oddSum, evenSum) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
a_1 = a
ans = 0
ans_2 = 0
o = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
o = 0
a = a_1
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "-"
ans_2 += abs(-1 - a[0])
a[i] = -1
elif a[0] < 0:
ans_2 += abs(1 - a[0])
a[i] = 1
f = "+"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
#print(a)
print(min(ans,ans_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i];
long long sum = 0;
long long prev = 0;
sum += a[0];
long long ans = 0;
if (sum != 0) {
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
} else {
ans++;
sum = (prev < 0 ? 1 : -1);
}
}
}
} else {
ans = INF;
}
sum = 0;
prev = 0;
long long ans2 = 0;
sum += a[0];
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
} else {
ans2++;
sum = 1;
}
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
} else {
ans2++;
sum = (prev < 0 ? 1 : -1);
}
}
}
sum = 0;
prev = 0;
long long ans3 = 0;
sum += a[0];
if (sum > 0) {
ans3 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans3 += abs(sum) + 1;
sum = 1;
} else {
ans3++;
sum = -1;
}
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans3 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans3 += abs(sum) + 1;
sum = 1;
} else {
ans3++;
sum = (prev < 0 ? 1 : -1);
}
}
}
cout << min(ans, min(ans, ans2)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long op = 0LL;
long long sum = 0LL;
sum = a[0];
for (int i = 1; i < n; i++) {
if (!(sum * (sum + a[i]) < 0)) {
long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1);
op += abs(tmp_a - a[i]);
sum = sum + tmp_a;
} else {
sum += a[i];
}
}
long long op_m = op;
if (a[0] > 0) {
sum = -1LL;
op = a[0] + 1;
} else {
sum = 1LL;
op = -1 * a[0] + 1;
}
for (int i = 1; i < n; i++) {
if (!(sum * (sum + a[i]) < 0)) {
long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1);
op += abs(tmp_a - a[i]);
sum = sum + tmp_a;
} else {
sum += a[i];
}
}
op = min(op, op_m);
cout << op << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100004];
int main() {
scanf("%d", &n);
for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]);
long long ans = 0;
printf("1000000000000000000\n");
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long s[maxn];
long long ans[maxn];
int main() {
int n, j;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] == 0) {
if (s[i + 1] <= 0) {
sum += -s[i + 1] + 1;
ans[i] = -s[i + 1] + 1;
} else if (s[i + 1] > 0) {
sum += s[i + 1] + 1;
ans[i] = -s[i + 1] - 1;
}
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << sum << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int N;
cin >> N;
vector<int> A(N);
for (long long i = 0; i < long long(N); i++) {
cin >> A[i];
}
int ansA = 0;
int sumA = 0;
for (long long i = 0; i < long long(N); i++) {
sumA += A[i];
if (i % 2 == 0) {
if (sumA <= 0) {
sumA += (abs(A[i]) + 1);
ansA += (abs(A[i]) + 1);
}
} else {
if (sumA >= 0) {
sumA -= (abs(A[i]) + 1);
ansA += (abs(A[i]) + 1);
}
}
}
int ansB = 0;
int sumB = 0;
for (long long i = 0; i < long long(N); i++) {
sumB += A[i];
if (i % 2 != 0) {
if (sumB <= 0) {
sumB += (abs(A[i]) + 1);
ansB += (abs(A[i]) + 1);
}
} else {
if (sumB >= 0) {
sumB -= (abs(A[i]) + 1);
ansB += (abs(A[i]) + 1);
}
}
}
cout << min(ansA, ansB) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int calc(bool firstPositive, int a[], int n) {
bool positive = firstPositive;
int cost = 0;
long sum = 0;
for (int i = 0; i < n; ++i) {
bool sumpos = (sum + a[i]) >= 0;
if (sumpos != positive) {
while (((sum + a[i]) >= 0) == sumpos) {
a[i] += sumpos ? -1 : 1;
++cost;
}
}
if ((sum + a[i]) == 0) {
a[i] += sumpos ? -1 : 1;
++cost;
}
sum += a[i];
positive = !positive;
}
return cost;
}
int main(int argc, char *argv[]) {
int n;
std::cin >> n;
int a[1 << 20], b[1 << 20];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
b[i] = a[i];
}
std::cout << std::min(calc(true, a, n), calc(false, b, n)) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t n;
cin >> n;
vector<int64_t> a(n);
for (int64_t i = 0; i < n; i++) cin >> a[i];
int64_t sum1 = 0, cost1 = 0;
for (int64_t i = 0; i < n; i++) {
sum1 += a[i];
int64_t diff = abs(sum1) + 1;
if (i % 2 == 0 && sum1 < 0) {
sum1 += diff;
cost1 += diff;
}
if (i % 2 == 1 && sum1 > 0) {
sum1 -= diff;
cost1 += diff;
}
if (sum1 == 0) {
if (i % 2 == 0) {
sum1--;
cost1++;
}
if (i % 2 == 1) {
sum1++;
cost1++;
}
}
}
int64_t sum2 = 0, cost2 = 0;
for (int64_t i = 0; i < n; i++) {
sum2 += a[i];
int64_t diff = abs(sum2) + 1;
if (i % 2 == 0 && sum2 > 0) {
sum2 -= diff;
cost2 += diff;
}
if (i % 2 == 1 && sum2 < 0) {
sum2 += diff;
cost2 += diff;
}
if (sum2 == 0) {
if (i % 2 == 0) {
sum2++;
cost2++;
}
if (i % 2 == 1) {
sum2--;
cost2++;
}
}
}
cout << min(cost1, cost2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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], kaisu = 0;
if (sum == 0) {
kaisu++;
sum++;
}
for (int i = 1; i < n; i++) {
long long presum = sum;
sum += a[i];
if (presum > 0) {
if (sum >= 0) {
kaisu += sum + 1;
a[i] = a[i] - sum - 1;
sum = -1;
}
}
if (presum < 0) {
if (sum <= 0) {
kaisu += 1 - sum;
sum = 1;
}
}
}
cout << kaisu << 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()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
sum+=1
cnt+=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<=0:
cnt+=(1-z)
sum=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
cnt_plus=cnt
sum=-1
cnt=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<=0:
cnt+=(1-z)
sum=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
cnt_sbst=cnt
print(cnt_sbst)
print(cnt_plus)
print(min(cnt_plus,cnt_sbst))
sys.exit()
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<=0:
cnt+=(1-z)
sum=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
print(cnt)
# 6
# 0 0 -1 3 5 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>
constexpr long long INFL = 1LL << 60;
constexpr int INF = 1 << 30;
using namespace std;
using ll = long long;
using P = tuple<int, int>;
using iarr = valarray<int>;
int main() {
int N;
cin >> N;
int a[100000];
for (int i = 0; i < N; ++i) cin >> a[i];
ll partial_sum = 0;
ll cnt = 0;
int index = 0;
for (int i = 0; i < N; ++i) {
if (a[i] != 0) {
if (i == 0) {
partial_sum = a[i];
index = i + 1;
break;
} else {
partial_sum += 1 + 2 * (i - 1) + abs(a[i]);
index = i + 1;
break;
}
}
}
if (index == 0) {
cout << 1 + 2 * (N - 1) << endl;
return 0;
}
for (int i = index; i < N; ++i) {
if (partial_sum > 0) {
if (a[i] + partial_sum > 0) {
cnt += abs(-(partial_sum + 1) - a[i]);
a[i] = -(partial_sum + 1);
partial_sum = -1;
} else if (a[i] + partial_sum == 0) {
cnt += 1;
a[i] = -1;
partial_sum = -1;
} else {
partial_sum += a[i];
}
} else {
if (a[i] + partial_sum < 0) {
cnt += abs(-(partial_sum - 1) - a[i]);
a[i] = -(partial_sum - 1);
partial_sum = 1;
} else if (a[i] + partial_sum == 0) {
cnt += 1;
a[i] = 1;
partial_sum = 1;
} else {
partial_sum += a[i];
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using st = string;
using db = double;
using vll = vector<long long>;
using vvll = vector<vll>;
using vst = vector<st>;
using vchar = vector<char>;
ll mod = 1000000007;
ll sum(vll a, ll n) { return accumulate(a.begin(), a.begin() + n + 1, 0); }
int main() {
ll n;
cin >> n;
vll a(n);
for (auto& i : a) cin >> i;
vll a1 = a, a2 = a;
ll ans1 = 0, ans2 = 0;
ll pointer = 0;
while (pointer < n) {
if (pointer % 2 == 0) {
if (sum(a1, pointer) > 0)
pointer++;
else {
ans1 += -1 * sum(a1, pointer) + 1;
a1.at(pointer) += -1 * sum(a1, pointer) + 1;
pointer++;
}
} else {
if (sum(a1, pointer) < 0)
pointer++;
else {
ans1 += sum(a1, pointer) + 1;
a1.at(pointer) -= sum(a1, pointer) + 1;
pointer++;
}
}
}
pointer = 0;
while (pointer < n) {
if (pointer % 2 == 1) {
if (sum(a2, pointer) > 0)
pointer++;
else {
ans2 += -1 * sum(a2, pointer) + 1;
a2.at(pointer) += -1 * sum(a2, pointer) + 1;
pointer++;
}
} else {
if (sum(a2, pointer) < 0)
pointer++;
else {
ans2 += sum(a2, pointer) + 1;
a2.at(pointer) -= sum(a2, pointer) + 1;
pointer++;
}
}
}
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 | UNKNOWN | using static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System;
class Program
{
#region Reader
static string ReadStr => Console.ReadLine();
static string[] ReadStrs => Console.ReadLine().Split(' ');
static int ReadInt => Convert.ToInt32(Console.ReadLine().Trim());
static int[] ReadInts => Console.ReadLine().Trim().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
static long ReadLong => Convert.ToInt64(Console.ReadLine().Trim());
static long[] ReadLongs => Console.ReadLine().Trim().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
static List<int[]> ReadPairs(int N)
{
List<int[]> ans = new List<int[]>();
for (int i = 0; i < N; i++) { ans.Add(ReadInts); }
return ans;
}
static List<long[]> ReadPairsLong(int N)
{
List<long[]> ans = new List<long[]>();
for (int i = 0; i < N; i++) { ans.Add(ReadLongs); }
return ans;
}
#endregion
#region Method
const int mod = 1000000007;
public static int Mod(int a, int mod) { return a % mod >= 0 ? a % mod : a % mod + mod; }
public static long Mod(long a, long mod = mod) { return a % mod >= 0 ? a % mod : a % mod + mod; }
bool eq<T, U>() => typeof(T).Equals(typeof(U));
T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
: eq<T, long>() ? ct<T, long>(long.Parse(s))
: eq<T, double>() ? ct<T, double>(double.Parse(s))
: eq<T, char>() ? ct<T, char>(s[0])
: ct<T, string>(s);
void Multi<T>(out T a) => a = cv<T>(ReadStr);
void Multi<T, U>(out (T,U) ans)
{
var ar = ReadStrs;
ans =(cv<T>(ar[0]), cv<U>(ar[1]));
}
void Multi<T, U, V>(out (T,U,V) ans)
{
var ar = ReadStrs;
ans =(cv<T>(ar[0]), cv<U>(ar[1]),cv<V>(ar[2]));
}
void Multi<T,U>(out T a,out U b)
{
var ar = ReadStrs;
a = cv<T>(ar[0]); b = cv<U>(ar[1]);
}
void Multi<T, U, V>(out T a,out U b,out V c)
{
var ar = ReadStrs;
a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
}
#endregion
public static Dictionary<int, int> PrimeFactors(int n)
{
int i = 2;
int tmp = n;
Dictionary<int, int> d = new Dictionary<int, int>();
while (i * i <= n) //※1
{
if (tmp % i == 0)
{
tmp /= i;
if (d.ContainsKey(i)) d[i]++;
else d.Add(i, 1);
}
else
{
i++;
}
}
if (tmp != 1)
{
if (d.ContainsKey(tmp)) d[tmp]++;
else d.Add(tmp, 1);
}
return d;
}
static List<long> Divisor(long N)
{
List<long> ans = new List<long>();
int max = (int)Math.Sqrt(N);
for (int i = 1; i < max; i++)
{
if (N % i == 0)
{
ans.Add(i);
ans.Add(N / i);
}
}
if (N % max == 0)
{
if (N / max == max) ans.Add(max);
else
{
ans.Add(max);
ans.Add(N / max);
}
}
return ans;
}
#region Mod
public static long GetInv(long a, long mod = 1000000007)
{
return PowerMod(a, mod - 2, mod);
}
public static long Combination(long a, long b, long mod = 1000000007)
{
if (b < 0 || b > a) return 0;
if (b > a / 2) b = a - b;
long numerator = Factorial(a, mod);
long denominator = (Factorial(b, mod) * Factorial(a - b, mod)) % mod;
return (numerator * GetInv(denominator, mod)) % mod;
}
public static long Factorial(long n, long mod = 1000000007)
{
if (n == 0) return 1;
if (n == 1) return 1;
long ans = n;
for (int i = 2; i < n; i++)
{
ans = (ans * i) % mod;
}
return ans;
}
private static long PowerMod(long a, long n, long mod = 1000000007)
{
if (n == 0) return 1;
if (n == 1) return a;
long p = PowerMod(a, n / 2, mod);
if (n % 2 == 1) return ((p * p) % mod * a) % mod;
else return (p * p) % mod;
}
#endregion
static HashSet<int> Primes(int limit)
{
bool[] done = new bool[limit+1];
var ans = new HashSet<int>();
for (int i = 2; i <= limit; i++)
{
if (done[i]) continue;
ans.Add(i);
int index = i;
while (index<=limit)
{
done[index] = true;
index += i;
}
}
return ans;
}
#region SegmentTree
/// <summary>
/// 行う演算は結合法則を満たす必要あり
/// </summary>
public class SegmentTree<T> where T : IComparable
{
/// <summary>
/// 完全二分木
/// </summary>
public T[] Data { get; }
/// <summary>
/// 完全二分木に含まれる葉の数
/// </summary>
private int LeavesCount { get; }
/// <summary>
/// 数列(問題などで与えられる)の要素数
/// </summary>
public int Count { get; }
/// <summary>
/// 葉のうち使われなかったもの、そしてQueryで引数として取得した範囲がDataのインデックスの外である場合に使う
/// </summary>
public T Default { get; }
public SegmentTree(T[] data, T def)
{
//初期値の設定
Count = data.Length;
Default = def;
LeavesCount = (int)Pow(2, Ceiling(Log(data.Length, 2)));
Data = new T[LeavesCount * 2];
//
//完全二分木の全要素を初期化
for (int i = 0; i < LeavesCount * 2; i++)
{
Data[i] = def;
}
//dataを用いて必要な要素をすべて埋める
for (int i = 0; i < data.Length; i++)
{
this[i] = data[i];
}
}
public T this[int index]
{
get
{
//取得したい要素の位置は、実際は LeavesCount-1+index となる
index = LeavesCount - 1 + index;
return Data[index];
}
set
{
//偏光したい要素の位置は、実際は LeavesCount-1+index となる
index = LeavesCount - 1 + index;
//子を変更
Data[index] = value;
//親たちも含めて、要素を変更
while (index > 0)
{
//indexを親のインデックスに更新
index = GetParentIndex(index);
//要素を更新
Data[index] = DecideNewElementFromTwoChildren(index);
}
}
}
/// <summary>
/// 唯一の親のインデックスを返す
/// </summary>
/// <param name="childIndex"></param>
/// <returns></returns>
private int GetParentIndex(int childIndex)
{
return (childIndex - 1) / 2;
}
/// <summary>
/// 二つの子のインデックスを返す
/// </summary>
/// <param name="parentIndex"></param>
/// <returns>小さいほうはl、大きいほうはr</returns>
private (int l, int r) GetChildIndexes(int parentIndex)
{
return (2 * parentIndex + 1, 2 * parentIndex + 2);
}
/// <summary>
/// 親の値を二つの子から決定する
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private T DecideNewElementFromTwoChildren(int index)
{
var children = GetChildIndexes(index);
return Select(Data[children.l], Data[children.r]);
}
/// <summary>
/// [a,b)の範囲で回答を探す
/// </summary>
/// <param name="a"></param>
/// <param name="halfOpen_b"></param>
/// <returns></returns>
public T Query(int a, int halfOpen_b)
{
return _Query(a, halfOpen_b, 0, LeavesCount, 0);
}
private T _Query(int a, int b, int l, int r, int current)
{
//Φ
if (a >= r || b <= l)
{
return Default;
}
//部分集合
else if (a <= l && b >= r)
{
return Data[current];
}
//一部
else
{
//(1) 範囲[l,r)を分割し、子lの回答と子rの回答を取得
//(2) 二つの回答を比べて、適切(Selectメソッド)な方を返す
//(1)
var children = GetChildIndexes(current);
T t1 = _Query(a, b, l, (l + r) / 2, children.l);
T t2 = _Query(a, b, (l + r) / 2, r, children.r);
//(2)
return Select(t1, t2);
}
}
/// <summary>
/// カスタマイズするところ。二つの子の値から、親の値を決定する方法を定める。
/// </summary>
/// <param name="t1">子の値1</param>
/// <param name="t2">子の値2</param>
/// <returns></returns>
private T Select(T t1, T t2)
{
return t1.CompareTo(t2) < 0 ? t2 : t1;
}
}
#endregion
static void Main()
{
int N = ReadInt;
int[] As = ReadInts;
long sum = As[0];
long cost = 0;
for (int i = 1; i < N; i++)
{
if (sum * (sum+As[i]) < 0) {
sum += As[i];
continue;
}
if (sum < 0)
{
cost += Abs(sum * -1 + 1-As[i]);
sum =1;
}
else
{
cost += Abs(sum * -1 - 1 - As[i]);
sum = -1;
}
}
WriteLine(cost);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
As = list(mapint())
cum = As.pop(0)
cum2 = cum
ans = 0
for a in As:
if cum*(cum+a)>=0:
ans += abs(cum+a)+1
cum = -1 if cum>0 else 1
else:
cum += a
ans2 = abs(cum2)+1
cum2 = 1 if cum2<0 else -1
for a in As:
if cum2*(cum2+a)>=0:
ans2 += abs(cum2+a)+1
cum2 = -1 if cum2>0 else 1
else:
cum2 += a
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 | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
temp = a[0]
for i in range(1, n):
tar = temp + a[i]
if (tar * temp < 0):
temp = tar
else:
add = abs(tar) + 1
ans += add
if (temp > 0):
temp = -1
else:
temp = 1
#print(tar, ans)
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 t;
vector<int> answer(2);
int sumi;
bool flag = true;
cin >> t;
vector<int> A(t);
for (int i = 0; i < t; i++) {
cin >> A[i];
}
for (int j = 0; j < 2; j++) {
for (int i = 0; i < t; i++) {
sumi += A[i];
if (sumi == 0) {
answer[i] += 1;
if (flag) {
sumi = -1;
} else {
sumi = 1;
}
} else if (sumi > 0 == flag) {
answer[i] += abs(sumi) + 1;
if (sumi > 0) {
sumi = -1;
} else {
sumi = 1;
}
}
flag = !flag;
}
flag = false;
}
cout << min(answer[0], answer[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
int ans1 = 0;
vector<long long int> sums(n);
if (a[0] <= 0) {
ans1 -= a[0] - 1;
sums[0] = 1;
} else
sums[0] = a[0];
for (int i = 0; i < (n - 1); i++) {
if (i % 2 == 0) {
if (sums[i] + a[i + 1] >= 0) {
ans1 += sums[i] + a[i + 1] + 1;
sums[i + 1] = -1;
} else
sums[i + 1] = sums[i] + a[i + 1];
} else {
if (sums[i] + a[i + 1] <= 0) {
ans1 -= sums[i] + a[i + 1] - 1;
sums[i + 1] = 1;
} else
sums[i + 1] = sums[i] + a[i + 1];
}
}
int ans2 = 0;
if (a[0] >= 0) {
ans2 += a[0] + 1;
sums[0] = -1;
} else
sums[0] = a[0];
for (int i = 0; i < (n - 1); i++) {
if (i % 2 == 0) {
if (sums[i] + a[i + 1] <= 0) {
ans2 -= sums[i] + a[i + 1] - 1;
sums[i + 1] = 1;
} else
sums[i + 1] = sums[i] + a[i + 1];
} else {
if (sums[i] + a[i + 1] >= 0) {
ans2 += sums[i] + a[i + 1] + 1;
sums[i + 1] = -1;
} else
sums[i + 1] = sums[i] + a[i + 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.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
long[] k=new long[N];
for(int i=0; i<N; i++) {
k[i]=sc.nextLong();
}
for(int i=1; i<N; i++) {
k[i]=k[i-1]+k[i];
}
long counter=0;
if(k[0]>0) {
counter=1;
}
else {
counter=-1;
}
long[] tasu=new long[N];
long kaz=0;
for(int i=0; i<N; i++) {
tasu[i]=0;
}
for(int i=0; i<N; i++) {
long tmp=counter*(k[i]+tasu[i]);
if(tmp>0) {
//条件を満たすのでOK
}
else if(tmp<0){
if((k[i]+tasu[i])>0) {
long tt=(k[i]+tasu[i])+1;
kaz+=tt;
tasu[i]-=tt;
}
else if((k[i]+tasu[i])<0) {
long tt=((k[i]+tasu[i])-1)*-1;
kaz+=tt;
tasu[i]+=tt;
}
}
else if(tmp==0) {
if(counter==-1) {
tasu[i]--;
}
else if(counter==1) {
tasu[i]++;
}
kaz++;
}
if(i!=N-1) {
tasu[i+1]=tasu[i];
}
counter*=-1;
}
System.out.println(kaz);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
ll min(ll a, ll b) {
if (a >= b)
return b;
else
return a;
}
ll max(ll a, ll b) {
if (a >= b)
return a;
else
return b;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool flag = true;
bool fl = false;
bool f = false;
bool used[210];
bool graph[100][100];
bool visited[8];
int abc[26] = {0};
int main() {
ll n, a[100000], b[100000], ansA = 0, ansB = 0;
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i < n - 1;) {
if (a[i] <= 0) ansA += abs(1 - a[i]), a[i] = 1;
i++;
a[i] += a[i - 1];
if (a[i] >= 0) ansA += abs(a[i] + 1), a[i] = -1;
i++;
a[i] += a[i - 1];
}
for (int i = 0; i < n - 1;) {
if (b[i] >= 0) ansB += abs(b[i] + 1), b[i] = -1;
i++;
b[i] += b[i - 1];
if (b[i] <= 0) ansB += abs(1 - b[i]), b[i] = 1;
i++;
b[i] += b[i - 1];
}
std::cout << min(ansA, ansB) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000];
int main() {
int n;
long long sum = 0;
int ans = 0;
bool flag = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
flag = false;
}
sum += a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (flag == true) {
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
flag = false;
} else {
if (sum <= 0) {
ans += (sum * -1 + 1);
sum = 1;
}
flag = true;
}
cerr << ans << endl;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n, 0);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long long now_sum = a[0], cost = 0;
int before;
if (a[0] > 0)
before = 1;
else
before = -1;
for (long long i = 1; i < n; i++) {
now_sum += a[i];
if (now_sum == 0) {
now_sum -= a[i];
if (before == 1) {
a[i]--;
} else if (before == -1) {
a[i]++;
}
now_sum += a[i];
cost++;
continue;
}
if (before == 1 && now_sum > 0) {
cost += abs(now_sum) + 1;
now_sum -= a[i];
a[i] -= cost;
now_sum += a[i];
}
if (before == -1 && now_sum < 0) {
cost += abs(now_sum) + 1;
now_sum -= a[i];
a[i] += cost;
now_sum += a[i];
}
if (now_sum > 0)
before = 1;
else
before = -1;
}
cout << cost << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
sum := 0
cnt := 0
plus := false
a := make([]int, n)
for i := 0; i < n; i++ {
fmt.Scan(&a[i])
}
var base int
for i :=0; i <n; i++ {
if a[i] != 0 {
base = i
break
}
}
if base % 2 == 0 {
plus = true
} else {
plus = false
}
if a[base] < 0 {
plus = !plus
}
for i := 0; i < n; i++ {
sum += a[i]
if plus {
for sum <= 0 {
sum++
cnt++
}
plus = !plus
} else {
for sum >= 0 {
sum--
cnt++
}
plus = !plus
}
}
fmt.Println(cnt)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, ans = 0;
cin >> N;
long long sum = 0;
for (int i = 0; i < N; i++) {
long long a;
cin >> a;
long long nowsum = a + sum;
if (i == 0)
sum += a;
else {
if ((nowsum < 0) != (sum < 0)) {
if (nowsum == 0) {
if (sum < 0)
nowsum++;
else
nowsum--;
ans++;
}
} else {
if (nowsum < 0) {
for (;;) {
if (nowsum > 0) break;
nowsum++;
ans++;
}
} else {
for (;;) {
if (nowsum < 0) break;
nowsum--;
ans++;
}
}
}
sum = nowsum;
}
}
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>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define error(x) cerr << #x << " = " << x << endl
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
const int INF = 1e9 + 7;
int n, arr[100005];
ll f() {
ll res = 0;
ll sum = 0;
for (int i = 0; i < n; ++i) {
if (i&1) {
if (sum + arr[i] >= 0) {
res += llabs(sum + arr[i] + 1);
sum = 1;
} else {
sum += arr[i];
}
} else {
if (sum + arr[i] <= 0) {
res += llabs(sum + arr[i] - 1);
sum = 1;
} else {
sum += arr[i];
}
}
}
return res;
}
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; ++i) cin >> arr[i];
ll res = f();
for (int i = 0; i < n; ++i) arr[i] *= -1;
res = min(res,f());
cout << res << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
inline void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
inline void Yay(bool b) { cout << (b ? "Yay!" : ":(") << endl; }
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(15);
int N;
cin >> N;
vector<long long> v(N);
for (int i = 0, i_max = (N); i < i_max; ++i) cin >> v[i];
for (int i = 0, i_max = (N - 1); i < i_max; ++i) v[i + 1] += v[i];
long long count = 0;
long long sum = 0;
long long pre = -v[0];
for (int i = 0, i_max = (N); i < i_max; ++i) {
v[i] += sum;
if (pre * v[i] < 0) {
pre = v[i];
continue;
}
if (v[i] == 0) {
++count;
if (pre > 0) {
--sum;
--v[i];
} else {
++sum;
++v[i];
}
} else {
count += abs(v[i]) + 1;
if (pre > 0) {
sum -= v[i] + 1;
v[i] = -1;
} else {
sum -= v[i] - 1;
v[i] = 1;
}
}
pre = v[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<int> v(n);
cin >> v[0];
for (int i = (1); (i) < (n); (i)++) {
cin >> v[i];
}
int result_a = 0, result_b = abs(v[0]) + 1;
vector<int> tmp(n);
tmp[0] = v[0];
for (int i = (1); (i) < (n); (i)++) {
tmp[i] = v[i] + tmp[i - 1];
if (tmp[i] * tmp[i - 1] >= 0) {
result_a += abs(tmp[i]) + 1;
tmp[i] = ((tmp[i - 1] > 0) ? -1 : 1);
}
}
tmp[0] = v[0] > 0 ? -1 : 1;
for (int i = (1); (i) < (n); (i)++) {
tmp[i] = v[i] + tmp[i - 1];
if (tmp[i] * tmp[i - 1] >= 0) {
result_b += abs(tmp[i]) + 1;
tmp[i] = ((tmp[i - 1] > 0) ? -1 : 1);
}
}
cout << min(result_a, result_b) << 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, sum1;
long long ans = 0, ans1 = 0;
if (ai == 0)
ans = 1, ans1 = 1, sum = 1, sum1 = -1;
else if (ai > 0)
ans1 = ai + 1, sum = ai, sum1 = -1;
else
ans = ai + 1, sum = 1, sum1 = ai;
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 | import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Data.Char
solve :: VU.Vector Int -> Int -> Int
solve vec n
| VU.length vec == 2 && VU.sum vec == 0 = 1
| VU.length vec == 2 && VU.sum vec /= 0 = 0
| otherwise = minimum $ map fst [f, g]
where
t = VU.take 2 vec
d = VU.drop 2 vec
f = VU.foldl' step (fst $ init t) d
g = VU.foldl' step (snd $ init t) d
init :: VU.Vector Int -> ((Int, Int), (Int, Int))
init vec
| a + b == 0 = ((1, 1), (1, negate 1))
| a + b > 0 = ((0, a + b), (1 + a + b, negate 1))
| otherwise = ((0, a + b), (abs (1 - (a+b)), 1))
where
a = VU.head vec
b = VU.last vec
step :: (Int, Int) -> Int -> (Int, Int)
step (res, acc) x
| acc + x == 0 = (res + 1, negate (signum acc))
| (signum acc) /= signum (acc + x) = (res, acc + x)
| otherwise =
let
aim = negate $ signum acc
y = aim - (acc + x)
in
(res + abs y, aim)
main = do
n <- readLn :: IO Int
as <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine
print $ solve as 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;
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
struct edge {
int to, cost;
};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
long long int c(long long int a, long long int b, long long int m) {
long long int ans = 1;
for (long long int i = 0; i < b; i++) {
ans *= a - i;
ans %= m;
}
for (long long int i = 1; i <= b; i++) {
ans *= modinv(i, m);
ans %= m;
}
return ans;
}
void dijkdtra(int s, int v, vector<int>& d, vector<vector<edge>>& G) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
d[s] = 0;
que.push(pair<int, int>(0, s));
while (!que.empty()) {
pair<int, int> p = que.top();
que.pop();
int V = p.second;
if (d[V] < p.first) continue;
for (int i = 0; i < G[V].size(); i++) {
edge e = G[V][i];
if (d[e.to] > d[V] + e.cost) {
d[e.to] = d[V] + e.cost;
que.push(pair<int, int>(d[e.to], e.to));
}
}
}
}
long long int binary_search(vector<int>& s, long long int a) {
long long int l = -1;
long long int r = (int)s.size();
while (r - l > 1) {
long long int mid = l + (r - l) / 2;
if (s[mid] >= a)
r = mid;
else
l = mid;
}
return r;
}
int k(long long n) {
int x = 0;
while (n) {
x += n % 10;
n /= 10;
}
return x;
}
long long max(long long x, long long y) {
if (x < y) return y;
return x;
}
int main() {
long long n, ans;
cin >> n;
vector<long long> a(n), t(n), s(n);
for (int i = (0); i < (n); i++) {
cin >> a[i];
t[i] = a[i];
s[i] = a[i];
}
long long int w = a[0];
if (w <= 0) {
w = 1;
}
for (int i = (1); i < (n); i++) {
if (i % 2 == 0) {
if (abs(w) >= a[i]) {
a[i] = abs(w) + 1;
}
w += a[i];
} else {
if (w >= abs(a[i])) {
a[i] = -1 * (w + 1);
}
w += a[i];
}
}
w = t[0];
if (w >= 0) {
w = -1;
}
for (int i = (1); i < (n); i++) {
if (i % 2 == 1) {
if (abs(w) >= t[i]) {
t[i] = abs(w) + 1;
}
w += t[i];
} else {
if (w >= abs(t[i])) {
t[i] = -1 * (w + 1);
}
w += t[i];
}
}
long long cost1 = 0, cost2 = 0;
for (int i = (0); i < (n); i++) {
cost1 += abs(s[i] - a[i]);
cost2 += abs(s[i] - t[i]);
}
ans = min(cost1, cost2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]
use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;
struct Scanner<I: Iterator<Item = char>> {
iter: std::iter::Peekable<I>,
}
macro_rules! exit {
() => {{
exit!(0)
}};
($code:expr) => {{
if cfg!(local) {
writeln!(std::io::stderr(), "===== Terminated =====")
.expect("failed printing to stderr");
}
std::process::exit($code);
}}
}
impl<I: Iterator<Item = char>> Scanner<I> {
pub fn new(iter: I) -> Scanner<I> {
Scanner {
iter: iter.peekable(),
}
}
pub fn safe_get_token(&mut self) -> Option<String> {
let token = self.iter
.by_ref()
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect::<String>();
if token.is_empty() {
None
} else {
Some(token)
}
}
pub fn token(&mut self) -> String {
self.safe_get_token().unwrap_or_else(|| exit!())
}
pub fn get<T: FromStr>(&mut self) -> T {
self.token().parse::<T>().unwrap_or_else(|_| exit!())
}
pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
(0..len).map(|_| self.get()).collect()
}
pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
(0..row).map(|_| self.vec(col)).collect()
}
pub fn char(&mut self) -> char {
self.iter.next().unwrap_or_else(|| exit!())
}
pub fn chars(&mut self) -> Vec<char> {
self.get::<String>().chars().collect()
}
pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
(0..row).map(|_| self.chars()).collect()
}
pub fn line(&mut self) -> String {
if self.peek().is_some() {
self.iter
.by_ref()
.take_while(|&c| !(c == '\n' || c == '\r'))
.collect::<String>()
} else {
exit!();
}
}
pub fn peek(&mut self) -> Option<&char> {
self.iter.peek()
}
}
fn main() {
let cin = stdin();
let cin = cin.lock();
let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
let n: usize = sc.get();
let a: Vec<i64> = sc.vec(n);
let mut p = 0;
let mut ans1 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s >= 0 {
ans1 += s.abs()+1;
s += -s - 1;
} else if s * p > 0 || s == 0 {
ans1 += s.abs()+1;
s += if p > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
let mut p = 0;
let mut ans2 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s <= 0 {
ans2 += s.abs()+1;
s += s + 1;
} else if s * p > 0 || s == 0 {
ans2 += s.abs()+1;
s += if p > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
println!("{}", 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()))
cum_n = A.pop(0)
count = 0
for i in range(len(A)):
temp_n = A.pop(0)
temp_cum_n = cum_n + temp_n
if i==0 and cum_n==0:
count += 1
if temp_n > 0:
cum_n = -1
else:
cum_n = 1
elif cum_n*temp_cum_n >= 0:
count += abs(temp_cum_n)+1
if temp_cum_n > 0:
cum_n = -1
else:
cum_n = 1
else:
cum_n = temp_cum_n
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n], sum[n], sum2[n];
cin >> a[0];
sum[0] = a[0];
long long int ans = 0;
for (int i = 1; i < n; i++) cin >> a[i];
if (sum[0] > 0) {
int ans2 = 0, ans3 = 0;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] >= 0) {
ans2 += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] <= 0) {
ans2 += 1 - sum[i];
sum[i] = 1;
}
}
}
ans3 += sum[0] + 1;
sum[0] = -1;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] <= 0) {
ans3 += 1 - sum[i];
sum[i] = 1;
}
} else {
if (sum[i] >= 0) {
ans3 += sum[i] + 1;
sum[i] = -1;
}
}
}
ans = min(ans2, ans3);
} else if (sum[0] < 0) {
int ans2 = 0, ans3 = 0;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] <= 0) {
ans2 += 1 - sum[i];
sum[i] = 1;
}
} else {
if (sum[i] >= 0) {
ans2 += sum[i] + 1;
sum[i] = -1;
}
}
}
ans3 += 1 - sum[0];
sum[0] = 1;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] >= 0) {
ans3 += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] <= 0) {
ans3 += 1 - sum[i];
sum[i] = 1;
}
}
}
ans = min(ans2, ans3);
} else {
long long int ans2 = 1, ans3 = 1;
sum[0] = 1;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] >= 0) {
ans2 += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] <= 0) {
ans2 += 1 - sum[i];
sum[i] = 1;
}
}
}
sum2[0] = -1;
for (int i = 1; i < n; i++) {
sum2[i] = sum2[i - 1] + a[i];
if (i & 1) {
if (sum2[i] <= 0) {
ans3 += 1 - sum2[i];
sum2[i] = 1;
}
} else {
if (sum2[i] >= 0) {
ans3 += sum2[i] + 1;
sum2[i] = -1;
}
}
}
ans = min(ans2, ans3);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, i;
long long sum, ans;
long long int a[100005];
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
ans = 0;
sum = 0;
for (i = 1; i <= n; i++) {
if (a[i] == 0) {
sum++;
} else
break;
}
if (sum % 2 == 0 && sum != 0) {
if (a[sum + 1] > 0) {
a[1] = 1;
ans += 1;
} else {
a[1] = -1;
ans += 1;
}
} else if (sum % 2 != 0) {
if (a[sum + 1] > 0) {
a[1] = -1;
ans += 1;
} else {
a[1] = 1;
ans += 1;
}
}
sum = a[1];
for (i = 2; i <= n; i++) {
if (sum == 0) {
if (a[i - 1] > 0) {
ans++;
sum--;
} else {
ans++;
sum++;
}
}
if (sum > 0) {
if (a[i] + sum >= 0) {
ans += a[i] + sum + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (a[i] + sum <= 0) {
ans += abs(a[i] + sum) + 1;
sum = 1;
} else {
sum += a[i];
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
if L[0] < 0:
L = -L
count = 0
s = L[0]
if L[0] == 0:
if L[1] > 0:
L[0] = -1
else:
L[0] = 1
count += 1
loopnum = n//2
if n%2 == 0:
loopnum -= 1
for i in range(loopnum):
s = s + L[2*i+1]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
#print("s, count = {0}, {1}".format(s, count))
s = s + L[2*i+2]
if s <= 0:
subt = s - 1
count -= subt
s = s + subt
#print("s, count = {0}, {1}".format(s, count))
if n%2 == 0:
s = s + L[-1]
if s >= 0:
subt = s + 1
count += subt
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
sum+=1
cnt+=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if a[i-1]>0:
sum=-1
cnt+=1
elif a[i-1]<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
cnt_plus=cnt
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if a[i-1]>0:
sum=-1
cnt+=1
elif a[i-1]<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
cnt_sbst=cnt
print(min(cnt_plus,cnt_sbst))
sys.exit()
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if a[i-1]>0:
sum=-1
cnt+=1
elif a[i-1]<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(x) for x in input().split()]
S = a[0]
ans = 0
for _a in a[1:]:
if (S < 0 and _a > -S) or (S > 0 and _a < -S):
S += _a
continue
else:
if S < 0:
ans += -S + 1 - _a
S = 1
elif S > 0:
ans += _a + S + 1
S = -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 | UNKNOWN | import System.IO
import Control.Monad
import Control.Applicative
import Data.List
main = do
n <- getLine
as <- map read . words <$> getLine
putStrLn . show . head . [xs | xs <- iterate mani as, jouken n xs]
mani [] = []
mani [a] = [a-1, a+1]
mani (a:as) = [(a-1) : x | x <- mani as] ++ [(a+1) : x | x <- mani as]
subsum xs j = sum . take j $ xs
jouken n xs = length [i | i <- [1 .. n-1], (subsum xs i) * (subsum xs (i+1)) >= 0] == 0 |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
typedef long long ll;
const int INF = 1000000000;
const long INF64 = 1000000000000000ll;
const ll MOD = 1000000007ll;
int main(){
ll n;
std::cin >> n;
std::vector<ll> a(n);
rep(i,n)std::cin >> a[i];
ll ans1=0,ans2=0;
ll sum=0;
ll han=-1;
rep(i,n){
han*=-1;
sum+=a[i];
if(han<0){
ans1+=max(0,sum+1);
sum-=max(0,sum+1);
}else{
ans1-=min(0,sum-1);
sum-=min(0,sum-1);
}
// std::cout << ans1 << std::endl;
}
han=1;sum=0;
rep(i,n){
han*=-1;
sum+=a[i];
if(han<0){
ans2+=max(0,sum+1);
sum-=max(0,sum+1);
}else{
ans2-=min(0,sum-1);
sum-=min(0,sum-1);
}
// std::cout <<sum<<' '<< ans2 << std::endl;
}
std::cout << min(ans1,ans2) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count1 += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count1 += abs(currentSum) + 1
currentSum = -1
elif currentSum == 0 and restSum == 0:
count1 += 1
currentSum = -1
currentSum = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count2 += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count2 += abs(currentSum) + 1
currentSum = -1
elif currentSum == 0 and restSum == 0:
count2 += 1
currentSum = 1
currentSum = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count3 += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count3 += abs(currentSum) + 1
currentSum = -1
elif A[0] <= 0 and restSum == 0:
count3 += 1
currentSum = 1
currentSum = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count4 += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count4 += abs(currentSum) + 1
currentSum = -1
elif A[0] >= 0 and restSum == 0:
count4 += 1
currentSum = -1
print(count1, count2, count3, count4)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 = list(map(int,input().split()))
count = 0
summary = 0
previous = ''
for i,a in enumerate(a_list):
if i == 0:
if a > 0:
previous = '+'
elif a < 0:
previous = '-'
else:
flag = False
for idx,num in enumerate(a_list):
if num != 0:
flag = True
if num > 0:
if i % 2 == 1:
previous = '-'
a = -1
else:
previous = '+'
a = 1
else:
if i % 2 == 0:
previous = '-'
a = -1
else:
previous = '+'
a = 1
if flag == False:
previous = '+'
a = 1
count += 1
summary += a
else:
if previous == '+':
if summary + a < 0:
a_result = a
else:
a_result = (-1)*abs(-1-summary)
count += abs(a-a_result)
previous = '-'
else:
if summary + a > 0:
a_result = a
else:
a_result = abs(1-summary)
count += abs(a-a_result)
previous = '+'
summary += a_result
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i];
long long sum = 0;
long long prev = 0;
sum += a[0];
long long ans = 0;
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
} else {
ans++;
sum = (prev < 0 ? 1 : -1);
}
}
}
sum = 0;
prev = 0;
long long ans2 = 0;
sum += a[0];
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
} else {
ans2++;
sum = 1;
}
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
} else {
ans2++;
sum = (prev < 0 ? 1 : -1);
}
}
}
sum = 0;
prev = 0;
long long ans3 = 0;
sum += a[0];
if (sum > 0) {
ans3 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans3 += abs(sum) + 1;
sum = 1;
} else {
ans3++;
sum = -1;
}
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans3 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans3 += abs(sum) + 1;
sum = 1;
} else {
ans3++;
sum = (prev < 0 ? 1 : -1);
}
}
}
cout << min(ans, min(ans3, 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 count1, count2, sum1, sum2;
count1 = count2 = sum1 = sum2 = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int a0 = a[0];
while (a[0] <= 0) {
a[0]++;
count1++;
}
sum1 = a[0];
for (int i = 1; i < n; i++) {
sum1 += a[i];
if (i % 2 == 1) {
while (sum1 >= 0) {
sum1--;
count1++;
}
} else {
while (sum1 <= 0) {
sum1++;
count1++;
}
}
}
a[0] = a0;
while (a[0] >= 0) {
a[0]--;
count2++;
}
sum2 = a[0];
for (int i = 1; i < n; i++) {
sum2 += a[i];
if (i % 2 == 1) {
while (sum2 <= 0) {
sum2++;
count2++;
}
} else {
while (sum2 >= 0) {
sum2--;
count2++;
}
}
}
if (count1 >= count2)
cout << count2 << endl;
else
cout << count1 << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
B = A.copy()
gokei = 0
ans1 = 0
for i in range(N):
gokei = sum(A[:i+1])
# 偶数番目を負に
if i % 2 == 0:
if gokei >= 0:
ans1 += abs(-1 - gokei)
A[i] -= abs(-1 - gokei)
if i % 2 == 1:
if gokei <= 0:
ans1 += 1 - gokei
A[i] += (1 - gokei)
gokei = 0
ans2 = 0
for i in range(N):
gokei = sum(B[:i+1])
# 偶数番目を正に
if i % 2 == 0:
if gokei <= 0:
ans2 += 1 - gokei
B[i] += (1 - gokei)
else:
if gokei >= 0:
ans2 += abs(-1 - gokei)
B[i] -= abs(-1 - gokei)
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let mut s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}
macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}
macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
fn main() {
input!{
n: usize,
a: [i32; n],
};
let mut ans_minus = 0;
let mut ans_plus = 0;
let mut tmp = 0;
for (i, v) in a.iter().enumerate(){
if i % 2 == 0{
tmp = tmp + v;
if tmp >= 0{
ans_minus += tmp + 1;
tmp = -1;
}
} else {
tmp = tmp + v;
if tmp <= 0{
ans_minus += -tmp + 1;
tmp = 1;
}
}
}
let mut tmp = 0;
for (i, v) in a.iter().enumerate(){
if i % 2 == 1{
tmp = tmp + v;
if tmp >= 0{
ans_plus += tmp + 1;
tmp = -1;
}
} else {
tmp = tmp + v;
if tmp <= 0{
ans_plus += -tmp + 1;
tmp = 1;
}
}
}
println!(
"{}",
if ans_minus < ans_plus { ans_minus}else{ans_plus}
);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; ++i) {
A[i] = sc.nextInt();
}
sc.close();
int sum1 = 0;
int sum2 = 0;
int ans1 = 0;
int ans2 = 0;
for (int i = 0; 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 += Math.abs(sum1) + 1;
sum1 = 1;
}
}
for (int i = 0; i < N; ++i) {
sum2 += A[i];
if (i % 2 == 0 && sum2 <= 0) {
ans2 += sum1 +1;
sum2 = 1;
} else if (i % 2 != 0 && sum2 >= 0) {
ans2 += Math.abs(sum2) + 1;
sum2 = -1;
}
}
System.out.println(Math.min(ans1, ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
def judge(count):
idx = 0
for i in range(2,n+1):
if count == -1:
if sum(a[0:i]) < 0:
pass
else:
check = sum(a[0:i]) + 1
j = a[i-1]
a[i-1] = j - check
idx += check
elif count == 1:
if sum(a[0:i]) > 0:
pass
else:
check = 1 - sum(a[0:i])
j = a[i-1]
a[i-1] = check+ j
idx += check
count *= -1
return idx
if a[0] > 0:
b = judge(-1)
print(b)
elif a[0] < 0:
c = judge(1)
print(c)
else:
a[0] = 1
b = judge(-1)
a[0] = -1
c = judge(1)
print(min(b,c))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.PrintWriter;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
//入力
int n = sc.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextLong();
}
sc.close();
//処理
long ans = -1;
boolean bool = true;
for(int count = 0; count < 2; count++){
long temp = 0;
bool ^= true;
boolean f = bool;
long sum = 0;
for(int i = 0; i < n; i++){
sum += a[i];
if(sum > 0 == f){
//nothing
}else{
temp += Math.abs(sum) + 1;
if(f){
sum = 1;
}else{
sum = -1;
}
}
f ^= true;
}
if(ans == -1){
ans = temp;
}else{
ans = Math.min(ans, temp);
}
}
//出力
out.println(ans);
out.flush();
}
static class Pair{
int w,v;
public Pair(int a, int b){
this.w = a;
this.v = b;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int C[200000];
int count = 0;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> C[i];
}
int sum = C[0];
for (int i = 1; i < N; i++) {
if (sum < 0) {
sum += C[i];
while (sum <= 0) {
sum++;
cout << sum << endl;
count++;
}
continue;
}
if (sum > 0) {
sum += C[i];
while (sum >= 0) {
sum--;
cout << sum << endl;
count++;
}
continue;
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int N[100000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> N[i];
}
int ans = 0;
if (N[0] == 0) {
if (N[1] >= 0) {
N[0] = -1;
}
if (N[1] < 0) {
N[0] = 1;
}
ans = 1;
}
int sum = N[0];
if (N[0] > 0) {
for (int i = 1; i < n; i++) {
sum = sum + N[i];
if (i % 2 == 0 && sum <= 0) {
N[i] = N[i] - sum + 1;
ans = ans - sum + 1;
sum = 1;
}
if (i % 2 == 1 && sum >= 0) {
N[i] = N[i] - sum - 1;
ans = ans + sum + 1;
sum = -1;
}
}
}
if (N[0] < 0) {
for (int i = 1; i < n; i++) {
sum = sum + N[i];
if (i % 2 == 0 && sum >= 0) {
N[i] = N[i] - sum - 1;
ans = ans + sum + 1;
sum = -1;
}
if (i % 2 == 1 && sum <= 0) {
N[i] = N[i] - sum + 1;
ans = ans - sum + 1;
sum = 1;
}
}
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100100];
int main() {
cin >> n;
for (long long i = 1; i < n + 1; i++) cin >> a[i];
long long check = 0;
long long ans = 0;
for (long long i = 1; i < n + 1; i++) {
check += a[i];
if (i % 2 != 0 && check < 0) {
ans += abs(1 - check);
check = 1;
} else if (i % 2 == 0 && check > 0) {
ans += abs(check + 1);
check = -1;
}
}
check = 0;
long long tmp = 0;
for (long long i = 1; i < n + 1; i++) {
check += a[i];
if (i % 2 != 0 && check >= 0) {
tmp += abs(check + 1);
check = -1;
} else if (i % 2 == 0 && check <= 0) {
tmp += abs(1 - check);
check = -1;
}
}
cout << min(tmp, ans) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0, cumsum = a[0];
if (a[0] == 0) {
int i = 1;
while (a[i] == 0 && i < n) i++;
if (i == n || (a[i] < 0 && i % 2 == 1) || (a[i] > 0 && i % 2 == 0))
cumsum = 1;
else
cumsum = -1;
ans += 1;
}
for (int i = 1; i < n; i++) {
if (cumsum > 0) {
if (cumsum + a[i] >= 0) {
ans += cumsum + a[i] + 1;
cumsum = -1;
} else
cumsum += a[i];
} else {
if (cumsum + a[i] <= 0) {
ans += 1 - cumsum - a[i];
cumsum = 1;
} else
cumsum += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split(' ')))
result = 0
h = 0
if a[0] == 0:
a[0] += 1
result += 1
h = 1
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
if h == 1:
result = 1
if a[0] > 0:
result += a[0] +1
counter = -1
else:
result += 1-a[0]
counter = 1
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() {
int n, ansa = 0, ansb = 0, sum = 0;
cin >> n;
bool plus = true;
vector<int> t(n);
for (int i = 0; i < (n); i++) {
int a;
cin >> t[i];
a = t[i];
while (plus && sum + a <= 0) {
a++;
ansa++;
}
while (!plus && sum + a >= 0) {
a--;
ansa++;
}
sum += a;
plus = !plus;
}
plus = false;
sum = 0;
for (int i = 0; i < (n); i++) {
int a;
a = t[i];
while (plus && sum + a >= 0) {
a++;
ansb++;
}
while (!plus && sum + a <= 0) {
a--;
ansb++;
}
sum += a;
plus = !plus;
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | n=int(raw_input())
a=map(int,raw_input().split(' '))
p_s=a[0]
c=0
for i in range(1,n):
s=p_s+a[i]
#print i,p_s,s
if s==0:
if i==n-1:
a[i]+=1
c+=1
else:
if a[i]>=0:
a[i]+=1
c+=1
else:
a[i]-=1
c+=1
s=p_s+a[i]
if p_s*s>=0:
if p_s>0:
c+=abs(p_s*-1 - 1 - a[i])
a[i]=p_s*-1 - 1
else:
c+=abs(p_s*-1 + 1 - a[i])
a[i]=p_s*-1 + 1
p_s=p_s+a[i]
#print c,a
print c |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, a[100000];
cin >> N;
for (int i = 0; i < N; ++i) cin >> a[i];
int counter = 0;
long long b[100000];
if (a[0] == 0) {
++a[0];
++counter;
b[0] = a[0];
for (int i = 1; i < N; ++i) {
b[i] = b[i - 1] + a[i];
if (b[i - 1] * b[i] >= 0) {
if (i % 2 == 0) {
counter += abs(1 - b[i]);
b[i] = 1;
} else {
counter += abs((-1) - b[i]);
b[i] = -1;
}
}
}
} else if (a[0] > 0) {
b[0] = a[0];
for (int i = 1; i < N; ++i) {
b[i] = b[i - 1] + a[i];
if (b[i - 1] * b[i] >= 0) {
if (i % 2 == 0) {
counter += abs(1 - b[i]);
b[i] = 1;
} else {
counter += abs((-1) - b[i]);
b[i] = -1;
}
}
}
} else {
b[0] = a[0];
for (int i = 1; i < N; ++i) {
b[i] = b[i - 1] + a[i];
if (b[i - 1] * b[i] >= 0) {
if (i % 2 == 0) {
counter += abs((-1) - b[i]);
b[i] = -1;
} else {
counter += abs(1 - b[i]);
b[i] = 1;
}
}
}
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const auto INF = (ll)1e9;
using v = vector<ll>;
using p = pair<ll, ll>;
using m = map<ll, ll>;
using vv = vector<v>;
int gcd(int a, int b) { return a == 0 ? b : gcd(a, a % b); }
int main() {
int n;
cin >> n;
auto a = vector<ll>(n + 1, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll sum = 0;
ll c = 0;
for (ll i = 0; i < n; i++) {
if (sum + a[i] == 0) {
if (a[i] >= a[i + 1]) {
sum = -1;
} else {
sum = 1;
}
c++;
} else if (sum > 0 && sum + a[i] > 0) {
c += abs(-1 - sum - a[i]);
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
c += abs(+1 - sum - a[i]);
sum = 1;
} else {
sum += a[i];
}
}
cout << c;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<long long int> B(N);
B[0] = A[0];
for (int i = 1; i < N; i++) B[i] = B[i - 1] + A[i];
int ans = 0;
int base = 0;
for (int i = 1; i < N; i++) {
if ((B[i] + base) * (B[i - 1] + base) > 0) {
if (B[i] + base > 0) {
ans += abs(B[i] + base) + 1;
base -= abs(B[i] + base) + 1;
continue;
} else if (B[i] + base < 0) {
ans += abs(B[i] + base) + 1;
base += abs(B[i] + base) + 1;
continue;
}
}
if (B[i - 1] + base == 0) {
if (B[i] + base > 0) {
ans += 1;
base -= 1;
continue;
} else if (B[i] + base < 0) {
ans += 1;
base += 1;
continue;
}
}
if (i == N - 1 && B[i] + base == 0) ans++;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n, 0);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long long now_sum = a[0], cost = 0;
int before;
if (a[0] > 0)
before = 1;
else
before = -1;
for (long long i = 1; i < n; i++) {
now_sum += a[i];
if (now_sum == 0) {
now_sum -= a[i];
if (before == 1) {
a[i]--;
} else if (before == -1) {
a[i]++;
}
now_sum += a[i];
cost++;
}
if (before == 1 && now_sum > 0) {
cost += abs(now_sum) + 1;
now_sum -= a[i];
a[i] -= cost;
now_sum += a[i];
}
if (before == -1 && now_sum < 0) {
cost += abs(now_sum) + 1;
now_sum -= a[i];
a[i] += cost;
now_sum += a[i];
}
if (now_sum > 0)
before = 1;
else
before = -1;
}
cout << cost << 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 sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int count1 = 0;
int sum1 = 0;
// 最初の和が正
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
while (sum1 <= 0) {
sum1++;
count1++;
}
} else {
while (sum1 >= 0) {
sum1--;
count1++;
}
}
}
int count2 = 0;
int sum2 = 0;
// 最初の和が負
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
while (sum2 >= 0) {
sum2--;
count2++;
}
} else {
while (sum2 <= 0) {
sum2++;
count2++;
}
}
}
int ans = Integer.min(count1, count2);
System.out.println(ans);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> a(n);
int cnt = 0;
int sum = 0;
cin >> a[0];
sum += a[0];
for (int i = 1; i < n; ++i) {
cin >> a[i];
if (sum > 0) {
if (sum + a[i] == 0) {
a[i] -= 1;
cnt++;
} else if (sum + a[i] > 0) {
int b = sum + a[i];
a[i] -= (b + 1);
cnt += (b + 1);
} else {
;
}
} else {
if (sum + a[i] == 0) {
a[i] += 1;
cnt++;
} else if (sum + a[i] < 0) {
int b = -(sum + a[i]);
a[i] += (b + 1);
cnt += (b + 1);
} else {
;
}
}
sum += a[i];
}
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 | import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect
sys.setrecursionlimit(4100000)
def inputs(num_of_input):
ins = [input() for i in range(num_of_input)]
return ins
def int_inputs(num_of_input):
ins = [int(input()) for i in range(num_of_input)]
return ins
def solve(inputs):
A = string_to_int(inputs[0])
def j(x):
if x > 0:
return 1
elif x == 0:
return 0
else:
return -1
end_0 = None
next_0 = None
for i, a in enumerate(A):
if a == 0:
end_0 = i
elif end_0 is not None:
next_0 = j(a)
break
else:
break
sum_n = None
count = 0
is_prev = None
for i, a in enumerate(A):
if sum_n is None:
if end_0:
if not next_0:
sum_n = 1
else:
if end_0 % 2 != 0:
if next_0 == 1:
sum_n = 1
else:
sum_n = -1
else:
if next_0 == 1:
sum_n = -1
else:
sum_n = 1
count += 1
else:
sum_n = a
is_prev = j(sum_n)
else:
tmp_sum_n = sum_n + a
if is_prev == 1 and j(tmp_sum_n) >= 0:
count += abs(tmp_sum_n) + 1
tmp_sum_n -= (abs(tmp_sum_n) + 1)
elif is_prev == -1 and j(tmp_sum_n) <= 0:
count += abs(tmp_sum_n) + 1
tmp_sum_n += abs(tmp_sum_n) + 1
sum_n = tmp_sum_n
is_prev = j(sum_n)
return count
def string_to_int(string):
return list(map(int, string.split()))
if __name__ == "__main__":
input()
ret = solve(inputs(1))
print(ret)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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();
long ans = Math.min(count(n,a,0),count(n,a,1));
System.out.println(ans);
}
private static long count(int n, int []a, int pat) {
long count = 0;
long sum = 0;
sum += a[0];
int temp = (int)Math.pow(-1, 1+pat);
if(sum == 0) {
count++;
sum = temp;
}
for(int i = 1 ; i < n ; i++) {
sum += a[i];
temp *= -1;
if((long)temp * sum <= 0) {
count += Math.abs(sum) + 1 ;
sum = temp;
}
}
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;
template <class T>
void chmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void chmin(T &a, T b) {
if (a > b) a = b;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long sum = a[0];
long long change = 0LL;
if (sum > 0) {
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && sum + a[i] >= 0) {
sum += a[i];
change += abs(-1LL - sum);
sum = -1LL;
} else if (i % 2 == 0 && sum + a[i] <= 0) {
sum += a[i];
change += abs(1LL - sum);
sum = 1LL;
} else {
sum += a[i];
}
}
} else {
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && sum + a[i] >= 0) {
sum += a[i];
change += abs(-1LL - sum);
sum = -1LL;
} else if (i % 2 == 1 && sum + a[i] <= 0) {
sum += a[i];
change += abs(1 - sum);
sum = 1LL;
} else {
sum += a[i];
}
}
}
cout << change;
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 Control.Monad
import Data.List
main=do
_<-getLine
(a:as)<-map read.words<$>getLine::IO[Int]
print $ if a /= 0
then
min (sum.snd $ mapAccumL f a as)
((+) (1 + (abs a)) $ sum $ snd $ mapAccumL f (a`div`(abs a)) as)
else
min ((+) 1 $ sum $ snd $ mapAccumL f 1 as) ((+) 1 $ sum $ snd $ mapAccumL f (-1) as)
f a b
| signum a * signum (a+b) < 0 = (a+b,0)
| a < 0 = (1, 1-(a+b))
| otherwise = ((-1), 1+(a+b))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long int> A(N);
for (long long int i = 0LL; i < N; i++) {
cin >> A[i];
}
int cnt = 0;
if (A[0] < 0LL) {
long long int M = A[0];
for (long long int i = 1; i < N; i++) {
if (i % 2 == 1) {
if (M + A[i] >= 0) {
continue;
} else {
long long int tmp = M + A[i];
cnt += abs(tmp);
A[i] += abs(tmp);
}
} else {
if (M + A[i] < 0) {
continue;
} else {
long long int tmp = M + A[i];
cnt += abs(tmp) + 1;
A[i] -= abs(tmp) - 1;
}
M += A[i];
}
}
} else {
long long int M = A[0];
for (long long int i = 1; i < N; i++) {
if (i % 2 == 0) {
if (M + A[i] >= 0) {
continue;
} else {
long long int tmp = M + A[i];
cnt += abs(tmp);
A[i] += abs(tmp);
}
} else {
if (M + A[i] < 0) {
continue;
} else {
long long int tmp = M + A[i];
cnt += abs(tmp) + 1;
A[i] -= abs(tmp) - 1;
}
M += A[i];
}
}
}
cout << cnt;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int( input())
A = list( map( int, input().split()))
ansp = 0
sums = A[0]
if sums == 0:
ansp += 1
sums += 1
for i in range(1,n):
sums += A[i]
if i%2 == 1:
if sums < 0:
pass
else:
ansp += abs(-1-sums)
sums = -1
else:
if sums > 0:
pass
else:
ansp += abs(1 - sums)
sums = 1
sums = A[0]
ansm = 0
if sums == 0:
ansm += 1
sums -= 1
for i in range(1,n):
sums += A[i]
if i%2 == 0:
if sums < 0:
pass
else:
ansm += abs(-1-sums)
sums = -1
else:
if sums > 0:
pass
else:
ansm += abs(1 - sums)
sums = 1
print( min(ansp, ansm)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 = tuple(map(int, input().split(' ')))
cs = A[0]
ans = 0
for na in A[1:]:
if cs >= 0:
cs += na
if cs < 0:
continue
ans += cs + 1
cs = -1
else:
cs += na
if cs > 0:
continue
ans += -cs + 1
cs = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
point = a[0]
ans = 0
for i in a[1:]:
if point*i < 0:
if abs(point) < abs(i):
point += i
else:
ans += (abs(point)+1) - abs(i)
point = -int(point/abs(point))
else:
ans += (abs(point)+1) + i
point = -int(point/abs(point))
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;
vector<int> S(N + 1);
S[0] = 0;
for (int i = 1; i <= N; ++i) {
cin >> S[i];
S[i] += S[i - 1];
}
int ians = (1 << 30);
for (int j = -1; j <= 1; j += 2) {
vector<int> S_(S);
int ans = 0;
int add = 0;
int sign = j;
for (int i = 1; i <= N; ++i) {
S_[i] += add;
int sign_i = ((S_[i] >> 31) << 1) + 1;
if (S_[i] == 0) {
ans += 1;
add += -sign;
S_[i] += -sign;
} else if (sign_i == sign) {
ans += abs(-sign_i - S_[i]);
add += -sign_i - S_[i];
S_[i] = -sign_i;
}
sign = -sign;
}
ians = min(ans, ians);
}
cout << ians << 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
a_list = gets.split.map(&:to_i)
def calc(list, first_is_positive)
first = list[0]
count = if first_is_positive
first > 0 ? 0 : 1 - first
else
first < 0 ? 0 : (-1 - first).abs
end
sum = first_is_positive ? first + count : first - count
list[1..-1].each do |a|
if sum > 0
# plus to minus
if a >= 0
count += (a + sum) + 1
sum = -1
else
if sum + a < 0
sum += a
else
count += (sum + a) + 1
sum = -1
end
end
else
# minus to plus
if a >= 0
if sum + a > 0
sum += a
else
count += (sum + a).abs + 1
sum = -1
end
else
count += (a + sum).abs + 1
sum = 1
end
end
end
count
end
ans = [
calc(a_list, true),
calc(a_list, false)
].min
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};
int main() {
int n;
cin >> n;
long long sum1 = 0, sum2 = 0;
int ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum1 += a;
sum2 += a;
if (i % 2) {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
if (sum2 <= 0) {
ans2 += -sum2 + 1;
sum2 = 1;
}
} else {
if (sum1 <= 0) {
ans1 += -sum1 + 1;
sum1 = 1;
}
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
sum = a[0]
ans = 0
for i in range(1,n):
if (sum+a[i])*sum > 0:
if sum > 0:
ans += sum + a[i] + 1
sum = -1
else:
ans += -sum -a[i] + 1
sum = 1
else:
if sum < 0 and sum+a[i]==0:
sum = 1
ans +=1
elif sum > 0 and sum+a[i]==0:
sum = -1
ans +=1
else:
sum += a[i]
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = input()
a = [int(i) for i in input().split()]
cou = len(a)
counter = 0
X = a[0]
ans = 0
for i in a:
if X > 0:
b = -1 - X
ans += abs(b - a[i])
X = -1
counter += 1
if counter == cou - 1:
break
else:
b = 1 - X
ans += abs(b - a[i])
X = 1
counter += 1
if counter == cou - 1:
break
print (ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def chk(a,t): # t=True(奇数項が正), t=False(偶数項が正)
cnt=0 # 操作回数
x=0 # 塁積和
for i in a:
x+=i
if t==True and x<=0: # 正項予定なのに累積が負か0
cnt+=1-x
x=x+(1-x) # 今回のcntで符号変更
elif t==False and x>=0: # 負項予定なのに累積が正か0
cnt+=1+x
x=x-(1+x) # 今回のcntで符号変換
t = not t # 次の項は符号が逆
return cnt
print(min(chk(a,True),chk(a,False))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
li = list(map(int,input().split()))
ans = 0
cnt = 0
s = 0
for i in range(n):
if i == 0:
ans += li[i]
if ans > 0:
s = 1
else:
s = -1
else:
ans += li[i]
if ans <= 0 and s == -1:
cnt += -ans + 1
ans = 1
if ans >= 0 and s == 1:
cnt += ans + 1
ans = -1
s *= -1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[100010];
for (int i = 0; i < n; ++i) cin >> a[i];
int cur1 = 0;
int cur2 = 0;
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; ++i) {
cur1 += a[i];
if (i % 2 == 0) {
if (cur1 <= 0) {
ans1 = ans1 - cur1 + 1;
cur1 = 1;
}
}
if (i % 2 == 1) {
if (cur1 >= 0) {
ans1 = ans1 + cur1 + 1;
cur1 = -1;
}
}
}
for (int i = 0; i < n; ++i) {
cur2 += a[i];
if (i % 2 == 1) {
if (cur2 <= 0) {
ans2 = ans2 - cur2 + 1;
cur2 = 1;
}
}
if (i % 2 == 0) {
if (cur2 >= 0) {
ans2 = ans2 + cur2 + 1;
cur2 = -1;
}
}
cout << cur2 << endl;
}
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 | python3 | import numpy as np
n = int(input())
a_array = np.array([int(elem) for elem in input().split(' ')])
def calculate_cumulative_sum(a_array, n):
cumul_sum_array = np.zeros(n, dtype=np.int32)
cumul_sum_array[0] = a_array[0]
for i in range(1, n):
cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i]
return cumul_sum_array
def check_1(cumul_sum_array):
result = (cumul_sum_array != 0).all()
return result
def check_2(cumul_sum_array):
odd = np.sign(cumul_sum[::2])
even = np.sign(cumul_sum[1::2])
odd_sign = np.unique(odd)
even_sign = np.unique(even)
return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0])
cumul_sum = calculate_cumulative_sum(a_array, n)
count = {'plus': 0, 'minus': 0}
def calc_num_manipulation(cumul_sum, start, n):
cumul_sum = cumul_sum.copy()
count = 0
for i in range(n):
if check_1(cumul_sum) and check_2(cumul_sum):
break
else:
if i == 0:
diff = cumul_sum[i] - start
count += abs(diff)
cumul_sum[i:] -= diff
else:
if cumul_sum[i - 1] < 0 and cumul_sum[i] <= 0:
diff = cumul_sum[i] - 1
count += abs(diff)
cumul_sum[i:] -= diff
elif cumul_sum[i - 1] > 0 and cumul_sum[i] >= 0:
diff = cumul_sum[i] - (-1)
count += abs(diff)
cumul_sum[i:] -= diff
else:
continue
return count
count['plus'] = calc_num_manipulation(cumul_sum, 1, n)
count['minus'] = calc_num_manipulation(cumul_sum, -1, n)
print(min(count.values()))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
cnt = 0
sum = [0]*n
sum[0] = a[0]
for i in range(1,n):
sum[i] = sum[i-1]+a[i]
if sum[i]*sum[i-1]<0:
continue
if sum[i-1]*a[i]<0:
cnt += abs(sum[i-1])-abs(a[i])+1
else:
cnt += abs(sum[i-1])+abs(a[i])+1
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
static int ANS;
static int N;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = Integer.parseInt(sc.nextLine());
long[] a = new long[N];
long[] sum = new long[N];
long tsum = 0L;
for (int i = 0; i < N; i++) {
long elm = Long.parseLong(sc.next());
a[i] = elm;
tsum += elm;
sum[i] = tsum;
}
solve(sum, a, 0);
System.out.println(ANS);
}
private static void solve(long[] sum, long[] a, int start) {
for (int i = start; i < N - 1; i++) {
long one = sum[i];
long two = sum[i + 1];
if (two == 0) {
if (one >= 0) {
ANS++;
a[i + 1]--;
sumStream(sum, a);
solve(sum, a, i);
} else {
ANS++;
a[i + 1]++;
sumStream(sum, a);
solve(sum, a, i);
}
}
if (one >= 0 && two >= 0) {
ANS += Math.abs(two) + 1;
a[i + 1] -= Math.abs(two) + 1;
sumStream(sum, a);
solve(sum, a, i);
}
if (one < 0 && two < 0) {
ANS += Math.abs(two) + 1;
a[i + 1] += Math.abs(two) + 1;
sumStream(sum, a);
solve(sum, a, i);
}
}
}
private static void sumStream(long[] sum, long[] a) {
long limit = a.length;
long tsum = 0L;
for (int i = 0; i < limit; i++) {
tsum += a[i];
sum[i] = tsum;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -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,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
using namespace std;
using Graph = vector<vector<int64_t>>;
const double pi = M_PI;
const int64_t MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int64_t n;
cin >> n;
vector<int64_t> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int64_t ans = 0, tem = a[0];
for (int i = 1; i < n; i++) {
if ((0 < tem + a[i] && tem < 0) || (tem + a[i] < 0 && 0 < tem)) {
tem += a[i];
} else {
if (0 <= tem + a[i] && 0 <= tem) {
ans += abs(-1 - (tem + a[i]));
tem = -1;
} else {
ans += abs(1 - (tem + a[i]));
tem = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
namespace V
{
partial class Solver
{
public void Solve()
{
//var n = Read;
Write(SolveLong());
//YesNo(SolveBool());
}
public long SolveLong()
{
var n = Read;
var a = Arr(n);
var a0 = a[0];
var res1 = Slv(a, a0 > 0 ? -1 : a0);
var res2 = Slv(a, a0 < 0 ? 1 : a0);
return Math.Min(res1, res2);
}
long Slv(IReadOnlyList<long> a, long initial)
{
var res = Math.Abs(initial - a[0]);
var sum = initial;
foreach (var aa in a.Skip(1))
{
var s2 = sum + aa;
if (sum > 0 && s2 >= 0)
{
res += Math.Abs(s2 + 1);
s2 = -1;
}
else if (sum < 0 && s2 <= 0)
{
res += Math.Abs(s2 - 1);
s2 = 1;
}
sum = s2;
}
return res;
}
public bool SolveBool()
{
var n = Read;
var res = false;
return res;
}
}
}
namespace V
{
class StartingPoint
{
static void Main(string[] args)
{
try
{
var streamReader = args.Any() ? new StreamReader(args[0]) : new StreamReader(Console.OpenStandardInput());
var streamWriter = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
var scanner = new Scanner(streamReader);
var printer = new Printer(streamWriter);
var solver = new Solver(scanner, printer);
solver.Solve();
streamWriter.Flush();
}
catch (Exception e)
{
Console.WriteLine(e);
if (args.Any() == false)
throw e;
}
if (args.Any())
Console.ReadKey();
}
}
partial class Solver
{
public Solver(Scanner sc, Printer pr) { this.sc = sc; this.pr = pr; }
private readonly Scanner sc;
private readonly Printer pr;
private IEnumerable<int> Loop(int n) => C.Loop(n);
private IEnumerable<long> Loop(long n) => C.Loop(n);
private int RdInt => sc.Int;
private int ReadInt => sc.Int;
private long Rd => sc.Long;
private long Read => sc.Long;
private long ReadLong => sc.Long;
private double RdDouble => sc.Double;
private double ReadDouble => sc.Double;
private string Str => sc.Str;
private string RdStr => sc.Str;
private string ReadStr => sc.Str;
private int[] ArrInt(int n) => sc.Ints(n);
private int[] ArrInt(long n) => sc.Ints(n);
private long[] Arr(int n) => sc.Longs(n);
private long[] Arr(long n) => sc.Longs(n);
private long[] ArrLong(int n) => sc.Longs(n);
private long[] ArrLong(long n) => sc.Longs(n);
private double[] ArrDouble(int n) => sc.Doubles(n);
private double[] ArrDouble(long n) => sc.Doubles(n);
private string[] ArrStr(int n) => sc.Strs(n);
private string[] ArrStr(long n) => sc.Strs(n);
private void Wr(string s) => pr.Write(s);
private void Wr(object obj) => pr.Write(obj);
private void Wr<T>(IEnumerable<T> ts) => pr.Write(ts);
private void Wr(params object[] objs) => pr.Write(objs);
private void Write(string s) => pr.Write(s);
private void Write(object obj) => pr.Write(obj);
private void Write<T>(IEnumerable<T> ts) => pr.Write(ts);
private void Write(params object[] objs) => pr.Write(objs);
private void YesNo(bool b) => Write(b ? "Yes" : "No");
private void YESNO(bool b) => Write(b ? "YES" : "NO");
}
class Scanner
{
private readonly TextReader reader;
public Scanner(TextReader reader) { this.reader = reader; }
private Queue<string> strQueue = new Queue<string>();
public string Str
{
get
{
if (strQueue.Count > 0)
return strQueue.Dequeue();
string[] strs = null;
do
{
strs = reader.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
} while (strs.Any() == false);
foreach (var s in strs.Skip(1))
strQueue.Enqueue(s);
return strs[0];
}
}
public int Int => int.Parse(this.Str);
public long Long => long.Parse(this.Str);
public double Double => double.Parse(this.Str);
public static bool TypeEquals<T1, T2>() => typeof(T1).Equals(typeof(T2));
public static T1 ChangeTypes<T1, T2>(T2 t2) => (T1)System.Convert.ChangeType(t2, typeof(T1));
public static T1 Convert<T1>(string s) =>
TypeEquals<T1, int>() ? ChangeTypes<T1, int>(int.Parse(s)) :
TypeEquals<T1, long>() ? ChangeTypes<T1, long>(long.Parse(s)) :
TypeEquals<T1, double>() ? ChangeTypes<T1, double>(int.Parse(s)) :
TypeEquals<T1, char>() ? ChangeTypes<T1, char>(s[0]) : ChangeTypes<T1, string>(s);
public Pair<TX, TY> P2<TX, TY>() => new Pair<TX, TY>(Convert<TX>(this.Str), Convert<TY>(this.Str));
public Pair3<TX, TY, TZ> P3<TX, TY, TZ>() => new Pair3<TX, TY, TZ>(Convert<TX>(this.Str), Convert<TY>(this.Str), Convert<TZ>(this.Str));
public Pair4<TX, TY, TZ, TW> P4<TX, TY, TZ, TW>() => new Pair4<TX, TY, TZ, TW>(Convert<TX>(this.Str), Convert<TY>(this.Str), Convert<TZ>(this.Str), Convert<TW>(this.Str));
}
static class ScannerExtension
{
public static int[] Ints(this Scanner scanner, int n) => scanner.Ints((long)n);
public static int[] Ints(this Scanner scanner, long n) => scanner.ScanStrs<int>(n).ToArray();
public static long[] Longs(this Scanner scanner, int n) => scanner.Longs((long)n);
public static long[] Longs(this Scanner scanner, long n) => scanner.ScanStrs<long>(n).ToArray();
public static double[] Doubles(this Scanner scanner, int n) => scanner.Doubles((long)n);
public static double[] Doubles(this Scanner scanner, long n) => scanner.ScanStrs<double>(n).ToArray();
public static string[] Strs(this Scanner scanner, int n) => scanner.Strs((long)n);
public static string[] Strs(this Scanner scanner, long n) => scanner.ScanStrs<string>(n).ToArray();
private static IEnumerable<T> ScanStrs<T>(this Scanner scanner, long n) { for (long i = 0; i < n; i++) yield return Scanner.Convert<T>(scanner.Str); }
public static Pair<TX, TY>[] Pairs<TX, TY>(this Scanner scanner, int n) => scanner.Pairs<TX, TY>((long)n);
public static Pair<TX, TY>[] Pairs<TX, TY>(this Scanner scanner, long n) => scanner.ScanPairs<TX, TY>(n).ToArray();
public static Pair<long, long>[] Pairs(this Scanner scanner, int n) => scanner.Pairs((long)n);
public static Pair<long, long>[] Pairs(this Scanner scanner, long n) => scanner.ScanPairs<long, long>(n).ToArray();
public static Pair<int, int>[] PairsInt(this Scanner scanner, int n) => scanner.PairsInt((long)n);
public static Pair<int, int>[] PairsInt(this Scanner scanner, long n) => scanner.ScanPairs<int, int>(n).ToArray();
private static IEnumerable<Pair<TX, TY>> ScanPairs<TX, TY>(this Scanner scanner, long n) { for (long i = 0; i < n; i++) yield return scanner.P2<TX, TY>(); }
public static Pair3<TX, TY, TZ>[] Pairs3<TX, TY, TZ>(this Scanner scanner, int n) => scanner.Pairs3<TX, TY, TZ>((long)n);
public static Pair3<TX, TY, TZ>[] Pairs3<TX, TY, TZ>(this Scanner scanner, long n) => scanner.ScanPairs3<TX, TY, TZ>(n).ToArray();
public static Pair3<long, long, long>[] Pairs3(this Scanner scanner, int n) => scanner.Pairs3((long)n);
public static Pair3<long, long, long>[] Pairs3(this Scanner scanner, long n) => scanner.ScanPairs3<long, long, long>(n).ToArray();
public static Pair3<int, int, int>[] Pairs3Int(this Scanner scanner, int n) => scanner.Pairs3Int((long)n);
public static Pair3<int, int, int>[] Pairs3Int(this Scanner scanner, long n) => scanner.ScanPairs3<int, int, int>(n).ToArray();
private static IEnumerable<Pair3<TX, TY, TZ>> ScanPairs3<TX, TY, TZ>(this Scanner scanner, long n) { for (long i = 0; i < n; i++) yield return scanner.P3<TX, TY, TZ>(); }
public static Pair4<TX, TY, TZ, TW>[] Pairs4<TX, TY, TZ, TW>(this Scanner scanner, int n) => scanner.Pairs4<TX, TY, TZ, TW>((long)n);
public static Pair4<TX, TY, TZ, TW>[] Pairs4<TX, TY, TZ, TW>(this Scanner scanner, long n) => scanner.ScanPairs4<TX, TY, TZ, TW>(n).ToArray();
public static Pair4<long, long, long, long>[] Pairs4(this Scanner scanner, int n) => scanner.Pairs4((long)n);
public static Pair4<long, long, long, long>[] Pairs4(this Scanner scanner, long n) => scanner.ScanPairs4<long, long, long, long>(n).ToArray();
public static Pair4<int, int, int, int>[] Pairs4Int(this Scanner scanner, int n) => scanner.Pairs4Int((long)n);
public static Pair4<int, int, int, int>[] Pairs4Int(this Scanner scanner, long n) => scanner.ScanPairs4<int, int, int, int>(n).ToArray();
private static IEnumerable<Pair4<TX, TY, TZ, TW>> ScanPairs4<TX, TY, TZ, TW>(this Scanner scanner, long n) { for (long i = 0; i < n; i++) yield return scanner.P4<TX, TY, TZ, TW>(); }
}
class Pair<TX, TY> { public TX X { get; } public TY Y { get; } public Pair(TX x, TY y) { this.X = x; this.Y = y; } }
class Pair : Pair<long, long> { public Pair(long x, long y) : base(x, y) { } }
class PairInt : Pair<int, int> { public PairInt(int x, int y) : base(x, y) { } }
class Pair3<TX, TY, TZ> { public TX X { get; } public TY Y { get; } public TZ Z { get; } public Pair3(TX x, TY y, TZ z) { this.X = x; this.Y = y; this.Z = z; } }
class Pair3 : Pair3<long, long, long> { public Pair3(long x, long y, long z) : base(x, y, z) { } }
class Pair3Int : Pair3<int, int, int> { public Pair3Int(int x, int y, int z) : base(x, y, z) { } }
class Pair4<TX, TY, TZ, TW> { public TX X { get; } public TY Y { get; } public TZ Z { get; } public TW W { get; } public Pair4(TX x, TY y, TZ z, TW w) { this.X = x; this.Y = y; this.Z = z; this.W = w; } }
class Pair4 : Pair4<long, long, long, long> { public Pair4(long x, long y, long z, long w) : base(x, y, z, w) { } }
class Pair4Int : Pair4<int, int, int, int> { public Pair4Int(int x, int y, int z, int w) : base(x, y, z, w) { } }
class Printer
{
private readonly TextWriter writer;
public Printer(TextWriter writer) { this.writer = writer; }
public void Write(string obj) { writer.WriteLine(obj); }
public void Write(object obj) { writer.WriteLine(obj); }
public void Write<T>(IEnumerable<T> ts) { writer.WriteLine(string.Join(" ", ts)); }
public void Write(params object[] objs) { writer.WriteLine(string.Join(" ", objs)); }
}
static class Extension
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool SafeAdd<T>(this HashSet<T> ts, T t)
{
if (ts.Contains(t))
{
return false;
}
else
{
ts.Add(t);
return false;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool SafeRemove<T>(this HashSet<T> ts, T t)
{
if (ts.Contains(t))
{
ts.Remove(t);
return true;
}
else
{
return false;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SafeSet<T>(this Dictionary<T, long> ts, T t, long value)
{
if (ts.ContainsKey(t))
ts[t] = value;
else
ts.Add(t, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SafePlus<T>(this Dictionary<T, long> ts, T t, long value)
{
if (ts.ContainsKey(t))
ts[t] += value;
else
ts.Add(t, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SafeSub<T>(this Dictionary<T, long> ts, T t, long value)
{
if (ts.ContainsKey(t))
ts[t] -= value;
else
ts.Add(t, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SafeMult<T>(this Dictionary<T, long> ts, T t, long value)
{
if (ts.ContainsKey(t))
ts[t] *= value;
else
ts.Add(t, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SafeDiv<T>(this Dictionary<T, long> ts, T t, long value)
{
if (ts.ContainsKey(t))
ts[t] /= value;
else
ts.Add(t, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> ts) => new HashSet<T>(ts.Distinct());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToDigit(this char c) => (long)(c - '0');
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToSmallAbcIndex(this char c) => (long)(c - 'a');
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToLargeAbcIndex(this char c) => (long)(c - 'A');
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Count<T1, T2>(this IGrouping<T1, T2> gs) => gs.LongCount();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToStr(this IEnumerable<char> cs) => new string(cs.ToArray());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToLong(this IEnumerable<char> s)
{
var basis = 1L;
var res = 0L;
foreach (var c in s)
{
var d = c.ToSmallAbcIndex() + 1;
res += d * basis;
basis *= 27;
}
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryMin<T>(this ref T current, T newer) where T : struct, IComparable<T>
{
if (current.CompareTo(newer) <= 0)
return false;
current = newer;
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryMax<T>(this ref T current, T newer) where T : struct, IComparable<T>
{
if (current.CompareTo(newer) >= 0)
return false;
current = newer;
return true;
}
}
class C
{
public class SegmentTree<T>
{
private readonly int valueCount;
private readonly int baseCount;
private readonly int baseIndex;
private readonly T[] nodes;
private readonly Func<T, T, T> func;
private readonly T defaultValue;
public SegmentTree(IReadOnlyList<T> ts, Func<T, T, T> func, T filling = default(T))
{
this.func = func;
this.defaultValue = filling;
valueCount = ts.Count;
baseCount = 1;
while (valueCount > baseCount)
baseCount <<= 1;
nodes = new T[baseCount * 2 - 1];
baseIndex = baseCount - 1;
for (int i = 0; i < ts.Count; i++)
nodes[baseIndex + i] = ts[i];
for (int i = ts.Count; i < baseCount; i++)
nodes[baseIndex + i] = filling;
for (int i = baseIndex - 1; i >= 0; i--)
nodes[i] = func.Invoke(nodes[i * 2 + 1], nodes[i * 2 + 2]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(int index, T t)
{
var i = baseIndex + index;
nodes[i] = t;
while (i > 0)
{
i -= 1;
i /= 2;
nodes[i] = func.Invoke(nodes[i * 2 + 1], nodes[i * 2 + 2]);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Query(int leftIndex, int rightNextIndex)
{
T left = defaultValue;
T right = defaultValue;
int l = leftIndex + baseCount - 1;
int r = rightNextIndex + baseCount - 1;
for (; l < r; l >>= 1, r >>= 1)
{
if ((l & 1) == 0)
{
left = func.Invoke(left, nodes[l]);
}
if ((r & 1) == 0)
{
r--;
right = func.Invoke(right, nodes[r]);
}
}
return func.Invoke(left, right);
}
}
public class SegmentTree
{
public static SegmentTree<int> Sum(IReadOnlyList<int> values) => new SegmentTree<int>(values, (x, y) => x + y);
public static SegmentTree<long> Sum(IReadOnlyList<long> values) => new SegmentTree<long>(values, (x, y) => x + y);
public static SegmentTree<double> Sum(IReadOnlyList<double> values) => new SegmentTree<double>(values, (x, y) => x + y);
public static SegmentTree<int> Mult(IReadOnlyList<int> values) => new SegmentTree<int>(values, (x, y) => x * y, 1);
public static SegmentTree<long> Mult(IReadOnlyList<long> values) => new SegmentTree<long>(values, (x, y) => x * y, 1);
public static SegmentTree<double> Mult(IReadOnlyList<double> values) => new SegmentTree<double>(values, (x, y) => x * y, 1);
public static SegmentTree<int> Min(IReadOnlyList<int> values) => new SegmentTree<int>(values, Math.Min, int.MaxValue);
public static SegmentTree<long> Min(IReadOnlyList<long> values) => new SegmentTree<long>(values, Math.Min, long.MaxValue);
public static SegmentTree<double> Min(IReadOnlyList<double> values) => new SegmentTree<double>(values, Math.Min, double.MaxValue);
public static SegmentTree<int> Max(IReadOnlyList<int> values) => new SegmentTree<int>(values, Math.Max, int.MinValue);
public static SegmentTree<long> Max(IReadOnlyList<long> values) => new SegmentTree<long>(values, Math.Max, long.MinValue);
public static SegmentTree<double> Max(IReadOnlyList<double> values) => new SegmentTree<double>(values, Math.Max, double.MinValue);
}
public class UnionFind
{
private int[] parents;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnionFind(int count)
{
parents = Enumerable.Repeat(-1, count).ToArray();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnite(int x, int y)
{
var rootX = GetRoot(x);
var rootY = GetRoot(y);
if (rootX == rootY)
return false;
if (parents[rootY] < parents[rootX])
{
var temp = rootX;
rootX = rootY;
rootY = temp;
}
parents[rootX] += parents[rootY];
parents[rootY] = rootX;
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Find(int x, int y) => GetRoot(x) == GetRoot(y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetRoot(int x)
{
while (parents[x] >= 0)
x = parents[x];
return x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long GetSize(int x) => -parents[GetRoot(x)];
}
public class IterTools
{
/// <summary>
/// 組み合わせ(重複なし)
/// n = 4, k = 3 => (0,1,2) (0,1,3) (0,2,3) (1,2,3)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IReadOnlyList<long>> Combinations(long n, long k)
{
if (k <= 0)
yield break;
long[] indices = new long[k];
long pointer = 0;
while (pointer >= 0)
{
if (indices[pointer] < n)
{
if (pointer >= k - 1)
{
yield return indices;
indices[pointer]++;
}
else
{
indices[pointer + 1] = indices[pointer] + 1;
pointer++;
}
}
else
{
pointer--;
if (pointer >= 0)
indices[pointer]++;
}
}
}
/// <summary>
/// 組み合わせ(重複なし)
/// n = 4, k = 3 => (0,1,2) (0,1,3) (0,2,3) (1,2,3)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IReadOnlyList<T>> Combinations<T>(T[] ts, long k)
{
if (k <= 0)
yield break;
long[] indices = new long[k];
T[] container = new T[k];
long pointer = 0;
long n = ts.LongLength;
while (pointer >= 0)
{
if (indices[pointer] < n)
{
container[pointer] = ts[indices[pointer]];
if (pointer >= k - 1)
{
yield return container;
indices[pointer]++;
}
else
{
indices[pointer + 1] = indices[pointer] + 1;
pointer++;
}
}
else
{
pointer--;
if (pointer >= 0)
indices[pointer]++;
}
}
}
/// <summary>
/// 組み合わせ(重複あり)
/// n = 3, k = 2 => (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IReadOnlyList<long>> CombinationsWithReplacement(long n, long k)
{
if (k <= 0)
yield break;
long[] container = new long[k];
long pointer = 0;
while (pointer >= 0)
{
if (container[pointer] < n)
{
if (pointer >= k - 1)
{
yield return container;
container[pointer]++;
}
else
{
container[pointer + 1] = 0;
pointer++;
}
}
else
{
pointer--;
if (pointer >= 0)
container[pointer]++;
}
}
}
/// <summary>
/// 組み合わせ(重複あり)
/// n = 3, k = 2 => (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IReadOnlyList<T>> CombinationsWithReplacement<T>(T[] ts, long k)
{
if (k <= 0)
yield break;
long[] indices = new long[k];
T[] container = new T[k];
long pointer = 0;
long n = ts.LongLength;
while (pointer >= 0)
{
if (indices[pointer] < n)
{
container[pointer] = ts[indices[pointer]];
if (pointer >= k - 1)
{
yield return container;
indices[pointer]++;
}
else
{
indices[pointer + 1] = 0;
pointer++;
}
}
else
{
pointer--;
if (pointer >= 0)
indices[pointer]++;
}
}
}
/// <summary>
/// 順列
/// n = 3, k = 2 => (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IReadOnlyList<long>> Permutations(long n, long k)
{
if (k <= 0)
yield break;
HashSet<long> enumed = new HashSet<long>();
long[] container = new long[k];
long pointer = 0;
while (pointer >= 0)
{
if (container[pointer] < n)
{
if (enumed.Contains(container[pointer]))
{
container[pointer]++;
}
else if (pointer >= k - 1)
{
yield return container;
container[pointer]++;
}
else
{
enumed.Add(container[pointer]);
container[pointer + 1] = 0;
pointer++;
}
}
else
{
pointer--;
if (pointer >= 0)
{
enumed.Remove(container[pointer]);
container[pointer]++;
}
}
}
}
/// <summary>
/// 順列
/// n = 3, k = 2 => (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<IReadOnlyList<T>> Permutations<T>(T[] ts, long k)
{
if (k <= 0)
yield break;
HashSet<long> enumed = new HashSet<long>();
long[] indices = new long[k];
T[] container = new T[k];
long pointer = 0;
long n = ts.LongLength;
while (pointer >= 0)
{
if (indices[pointer] < n)
{
if (enumed.Contains(indices[pointer]))
{
indices[pointer]++;
}
else if (pointer >= k - 1)
{
container[pointer] = ts[indices[pointer]];
yield return container;
indices[pointer]++;
}
else
{
container[pointer] = ts[indices[pointer]];
enumed.Add(indices[pointer]);
indices[pointer + 1] = 0;
pointer++;
}
}
else
{
pointer--;
if (pointer >= 0)
{
enumed.Remove(indices[pointer]);
indices[pointer]++;
}
}
}
}
}
public class Tree
{
public Tree() { toNodes = new Dictionary<long, long[]>(); }
public Tree(Scanner sc, long n, bool base1 = true, bool singleDirection = false) { Adjust(sc.Pairs(n), base1, singleDirection); }
public Tree(Pair<long, long>[] edges, bool base1 = true, bool singleDirection = false) { Adjust(edges, base1, singleDirection); }
public Tree(IEnumerable<long> ps, IEnumerable<long> qs, bool base1 = true, bool singleDirection = false) { Adjust(ps.Zip(qs, (p, q) => new Pair<long, long>(p, q)).ToArray(), base1, singleDirection); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Adjust(Pair<long, long>[] edges, bool base1, bool singleDirection)
{
var arrows = base1
? edges.Select(x => new { from = x.X - 1, to = x.Y - 1 })
: edges.Select(x => new { from = x.X, to = x.Y });
if (singleDirection == false)
arrows = arrows.Concat(arrows.Select(x => new { from = x.to, to = x.from }));
toNodes = arrows.GroupBy(x => x.from).ToDictionary(x => x.Key, x => x.Select(xs => xs.to).ToArray());
}
private long[] empty = new long[0];
private Dictionary<long, long[]> toNodes;
public long[] To(long from)
{
long[] res = null;
if (toNodes.TryGetValue(from, out res))
return res;
else
return empty;
}
public IEnumerable<Pair<long, long>> GetRouteEdges(long from, long to)
{
return GetRouteEdgesImpl(from, to).Skip(1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private IEnumerable<Pair<long, long>> GetRouteEdgesImpl(long from, long to)
{
var routeNodes = GetRouteNodes(from, to);
var current = -1L;
foreach (var next in routeNodes)
{
yield return new Pair<long, long>(current, next);
current = next;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IEnumerable<long> GetRouteNodes(long from, long to)
{
Stack<long> routeNodes = new Stack<long>();
HashSet<long> checkedNodes = new HashSet<long>();
GetRouteNodes(from, to, routeNodes, checkedNodes);
return routeNodes.Reverse();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool GetRouteNodes(long current, long dest, Stack<long> routeNodes, HashSet<long> checkedNodes)
{
routeNodes.Push(current);
checkedNodes.Add(current);
if (current == dest)
return true;
foreach (var next in toNodes[current])
{
if (checkedNodes.Contains(next))
continue;
if (GetRouteNodes(next, dest, routeNodes, checkedNodes))
return true;
}
routeNodes.Pop();
return false;
}
/// <summary>
/// 木の直径(一番長い枝)を求める
/// </summary>
/// <returns>木の直径(一番長い枝)</returns>
public long GetDiameter()
{
long firstFarthest = 0;
long _1 = 0;
GetDiameterImpl(-1, 0, 0, ref _1, ref firstFarthest);
long maxDistance = 0;
long _2 = 0;
GetDiameterImpl(-1, firstFarthest, 0, ref maxDistance, ref _2);
return maxDistance;
}
private void GetDiameterImpl(long from, long current, long distance, ref long maxDistance, ref long farthest)
{
if (distance > maxDistance)
{
maxDistance = distance;
farthest = current;
}
foreach (var to in To(current))
{
if (from == to)
continue;
GetDiameterImpl(current, to, distance + 1, ref maxDistance, ref farthest);
}
}
}
public class PriorityQueue<TKey, TState> where TKey : IComparable<TKey>
{
public int Count { get; private set; }
private readonly Func<TState, TKey> keySelector;
private readonly bool desc;
private TState[] states = new TState[65536];
private TKey[] keys = new TKey[65536];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public PriorityQueue(Func<TState, TKey> keySelector, bool desc = false) { this.keySelector = keySelector; this.desc = desc; }
public TState Top
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { ValidateNonEmpty(); return states[1]; }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TState Dequeue()
{
var top = Top;
var item = states[Count];
var key = keys[Count];
Count--;
int index = 1;
while (true)
{
if ((index << 1) >= Count)
{
if (index << 1 > Count)
break;
if (key.CompareTo(keys[index << 1]) <= 0 ^ desc)
break;
states[index] = states[index << 1];
keys[index] = keys[index << 1];
index <<= 1;
}
else
{
var nextIndex = keys[index << 1].CompareTo(keys[(index << 1) + 1]) <= 0 ^ desc
? (index << 1)
: (index << 1) + 1;
if (key.CompareTo(keys[nextIndex]) <= 0 ^ desc)
break;
states[index] = states[nextIndex];
keys[index] = keys[nextIndex];
index = nextIndex;
}
}
states[index] = item;
keys[index] = key;
return top;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(TState state)
{
var key = keySelector.Invoke(state);
Count++;
int index = Count;
if (states.Length == Count)
Extend(states.Length * 2);
while ((index >> 1) != 0)
{
if (keys[index >> 1].CompareTo(key) <= 0 ^ desc)
break;
states[index] = states[index >> 1];
keys[index] = keys[index >> 1];
index >>= 1;
}
states[index] = state;
keys[index] = key;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Extend(int newSize)
{
TState[] newStates = new TState[newSize];
TKey[] newKeys = new TKey[newSize];
states.CopyTo(newStates, 0);
keys.CopyTo(newKeys, 0);
states = newStates;
keys = newKeys;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ValidateNonEmpty()
{
if (Count == 0)
throw new IndexOutOfRangeException();
}
}
public class BinaryIndexTree
{
long length;
long[] binaryIndexedTree;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BinaryIndexTree(long length)
{
this.length = length;
binaryIndexedTree = new long[length + 1];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(long indexZeroBase, long additional)
{
// i += i & -i
// 1が立っている最下位ビットを足す、の意味
for (long i = indexZeroBase + 1; i <= length; i += i & -i)
{
binaryIndexedTree[i] += additional;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Sum(long indexZeroBase)
{
long result = 0;
// i += i & -i
// 1が立っている最下位ビットを引く、の意味
for (long i = indexZeroBase + 1; i > 0; i -= i & -i)
{
result += binaryIndexedTree[i];
}
return result;
}
}
public static class BinarySearch
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetCountLarger<T>(T x, IList<T> listOrdered) where T : IComparable
{
return listOrdered.Count - GetCountSmallerOrEqual(x, listOrdered);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetCountLargerOrEqual<T>(T x, IList<T> listOrdered) where T : IComparable
{
return listOrdered.Count - GetCountSmaller(x, listOrdered);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetCountSmaller<T>(T x, IList<T> listOrdered) where T : IComparable
{
return GetLastIndexLess(x, listOrdered) + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetCountSmallerOrEqual<T>(T x, IList<T> listOrdered) where T : IComparable
{
return GetFirstIndexGreater(x, listOrdered);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetFirstIndexGreater<T>(T x, IList<T> listOrdered) where T : IComparable
{
return GetFirstIndexGreater(x, listOrdered, 0, listOrdered.Count - 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetFirstIndexGreater<T>(T x, IList<T> listOrdered, int low, int high) where T : IComparable
{
var count = listOrdered.Count;
if (count == 0)
return low;
if (listOrdered[high].CompareTo(x) <= 0)
return high + 1;
while (low < high)
{
var mid = (low + high) / 2;
if (listOrdered[mid].CompareTo(x) > 0)
high = mid;
else
low = mid + 1;
}
return low;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetLastIndexLess<T>(T x, IList<T> listOrdered) where T : IComparable
{
return GetLastIndexLess(x, listOrdered, 0, listOrdered.Count - 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long GetLastIndexLess<T>(T x, IList<T> listOrdered, int low, int high) where T : IComparable
{
var count = listOrdered.Count;
if (count == 0)
return low - 1;
if (listOrdered[0].CompareTo(x) >= 0)
return low - 1;
while (low < high)
{
var mid = (low + high + 1) / 2;
if (listOrdered[mid].CompareTo(x) < 0)
low = mid;
else
high = mid - 1;
}
return low;
}
}
public static class BellmanFord
{
public class Vertex
{
public long Distance { get; set; }
public Vertex()
{
Distance = long.MaxValue;
}
}
public class Edge
{
public int From { get; private set; }
public int To { get; private set; }
public long Weight { get; private set; }
public Edge(int from, int to, long weight)
{
From = from;
To = to;
Weight = weight;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void GetReachable(int origin, ref HashSet<int> reached, ref Dictionary<int, int[]> paths)
{
if (reached.Contains(origin))
return;
reached.Add(origin);
if (paths.ContainsKey(origin) == false)
return;
foreach (var p in paths[origin])
GetReachable(p, ref reached, ref paths);
}
/// <summary>
/// null: 負の無限大
/// long.MaxValue: たどり着けない
/// その他: 距離
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long? RunBellmanFord(int vertexCount, List<Edge> rawEdges, int source, int dest)
{
var forwards = rawEdges.GroupBy(x => x.From).ToDictionary(x => x.Key, x => x.Select(xs => xs.To).ToArray());
var reverses = rawEdges.GroupBy(x => x.To).ToDictionary(x => x.Key, x => x.Select(xs => xs.From).ToArray());
var fromSource = new HashSet<int>();
var toDest = new HashSet<int>();
GetReachable(source, ref fromSource, ref forwards);
GetReachable(dest, ref toDest, ref reverses);
var edges = rawEdges
.Where(e => fromSource.Contains(e.From))
.Where(e => fromSource.Contains(e.To))
.Where(e => toDest.Contains(e.From))
.Where(e => toDest.Contains(e.To))
.ToArray();
// initialize distances
var vertices = new List<Vertex>();
for (int i = 0; i < vertexCount; i++)
vertices.Add(new Vertex());
vertices[source].Distance = 0L;
// update distances
for (int i = 0; i < vertices.Count; i++)
{
foreach (var e in edges)
{
var from = vertices[e.From];
var to = vertices[e.To];
if (from.Distance == long.MaxValue)
continue;
var newDistance = from.Distance + e.Weight;
if (to.Distance > newDistance)
{
to.Distance = newDistance;
}
}
}
// check negative cycle
foreach (var e in edges)
{
var from = vertices[e.From];
var to = vertices[e.To];
if (from.Distance + e.Weight < to.Distance)
return null;
}
return vertices[dest].Distance;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Gcd(int a, int b) => Gcd((long)a, (long)b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Gcd(long a, long b)
{
if (a < b)
return GcdImpl(b, a);
else
return GcdImpl(a, b);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long GcdImpl(long a, long b)
{
var remainder = a % b;
if (remainder == 0)
return b;
else
return GcdImpl(b, remainder);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Lcm(int a, int b) => Lcm((long)a, (long)b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Lcm(long a, long b)
{
return a / Gcd(a, b) * b;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Pow(int n, int p) => Pow((long)n, (long)p);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Pow(long n, long p)
{
var res = 1L;
for (long i = 0; i < p; i++)
res *= n;
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Dictionary<long, long> Factorize(int n) => Factorize((long)n);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Dictionary<long, long> Factorize(long n)
{
var res = new Dictionary<long, long>();
var r = n;
for (long i = 2; i * i <= r; i++)
{
var c = 0L;
while (r % i == 0)
{
c++;
r /= i;
}
if (c > 0)
res.Add(i, c);
}
if (r > 1)
res.Add(r, 1);
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<long> Divisors(int n) => Divisors((long)n);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<long> Divisors(long n)
{
var cache = new Stack<long>();
for (long i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
yield return i;
cache.Push(i);
}
}
var r = cache.Peek();
if (r * r == n)
cache.Pop();
while (cache.Count > 0)
{
var i = cache.Pop();
yield return n / i;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long InversionNumberCountWithCompression<T>(IList<T> list) where T : IComparable<T>
{
var compressed = list
.Select((n, i) => new { n, i })
.GroupBy(x => x.n)
.OrderBy(x => x.Key)
.Select((g, i) => new { g, i })
.SelectMany(x => x.g.Select(xs => new { order = xs.i, index = x.i }))
.OrderBy(x => x.index)
.Select(x => x.order);
return InversionNumberCount(compressed, list.Count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long InversionNumberCountWithMinusCheck(IList<int> list)
{
var min = list.Min();
var max = list.Max();
if (min < 0)
return InversionNumberCount(list.Select(x => x - min), max - min);
else
return InversionNumberCount(list, max);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long InversionNumberCount(IEnumerable<int> list, int maxValue)
{
var bit = new BinaryIndexTree(maxValue + 1);
var res = 0L;
var i = 0;
foreach (var n in list)
{
res += i - bit.Sum(n);
bit.Add(n, +1);
i++;
}
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<int> Loop(int n)
{
for (int i = 0; i < n; i++)
yield return i;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<long> Loop(long n)
{
for (long i = 0L; i < n; i++)
yield return i;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> Repeat<T>(T t, long n)
{
for (long i = 0L; i < n; i++)
yield return t;
}
}
struct Mint
{
public static long Divider { set { divider = value; } }
private static long divider = 1000000007L;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Set998244353() { divider = 998244353L; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Set1000000009() { divider = 1000000009L; }
public long Value { get; }
public override string ToString() => this.Value.ToString();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Mint(long value)
{
this.Value = value % divider;
if (this.Value < 0)
this.Value += divider;
}
//public static implicit operator Mint(long a) => new Mint(a % divider);
//public static implicit operator Mint(int a) => new Mint(a % divider);
//public static explicit operator long(Mint a) => a.Value;
//public static explicit operator int(Mint a) => (int)a.Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator +(Mint a, Mint b) => new Mint((a.Value + b.Value) % divider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator +(Mint a, long b) => a + new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator +(Mint a, int b) => a + new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator -(Mint a, Mint b)
{
var t = (a.Value - b.Value) % divider;
if (t < 0L)
t += divider;
return new Mint(t);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator -(Mint a, long b) => a - new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator -(Mint a, int b) => a - new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator *(Mint a, Mint b) => new Mint((a.Value * b.Value) % divider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator *(Mint a, long b) => a * new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator *(Mint a, int b) => a * new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator /(Mint a, Mint b) => new Mint((a.Value * InvImpl(b.Value)) % divider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator /(Mint a, long b) => a / new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint operator /(Mint a, int b) => a / new Mint(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Mint Pow(long p) => new Mint(PowImpl(Value, p));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint Pow(long a, long p) => new Mint(PowImpl(a, p));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long PowImpl(long a, long p)
{
if (p == 0L)
return 1L;
if (p == 1L)
return a;
var halfP = p / 2L;
var halfPowered = PowImpl(a, halfP);
var powered = halfPowered * halfPowered % divider;
return p % 2L == 0L ? powered : powered * a % divider;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint Inv(long a) => new Mint(InvImpl(a));
private static readonly Dictionary<long, long> invCache = new Dictionary<long, long>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long InvImpl(long a)
{
long cache;
if (invCache.TryGetValue(a, out cache))
return cache;
var result = PowImpl(a, divider - 2L);
invCache.Add(a, result);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint Fac(long a) => new Mint(FacImpl(a));
private static long[] facCache = new long[262144];
private static long cachedFac = 0L;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long FacImpl(long a)
{
if (a >= divider)
return 0L;
facCache[0] = 1L;
if (facCache.LongLength <= a)
{
long newSize = facCache.LongLength;
while (newSize <= a)
{
newSize *= 2;
}
ExtendFacCache(newSize);
}
if (cachedFac < a)
{
var val = facCache[cachedFac];
var start = cachedFac + 1L;
for (long i = start; i <= a; i++)
{
val = (val * i) % divider;
facCache[i] = val;
}
cachedFac = a;
}
return facCache[a];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ExtendFacCache(long newSize)
{
long[] newFacCache = new long[newSize];
facCache.CopyTo(newFacCache, 0);
facCache = newFacCache;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint Perm(long n, long r) => new Mint(PermImpl(n, r));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long PermImpl(long n, long r)
{
if (n < r)
return 0L;
if (r <= 0L)
return 1L;
var nn = FacImpl(n);
var nr = FacImpl(n - r);
return (nn * InvImpl(nr)) % divider;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint Comb(long n, long r) => new Mint(CombImpl(n, r));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long CombImpl(long n, long r)
{
if (n < r)
return 0L;
if (n == r)
return 1L;
if (n - r < r)
return CombImpl(n, n - r);
var nn = FacImpl(n);
var nr = FacImpl(n - r);
var rr = FacImpl(r);
var nrrr = (nr * rr) % divider;
return (nn * InvImpl(nrrr)) % divider;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Mint CombOneByOne(long n, long r)
{
if (n < r)
return new Mint(0);
if (n == r)
return new Mint(1);
if (n - r < r)
return CombOneByOne(n, n - r);
var res = new Mint(1);
for (long i = 1; i <= r; i++)
{
res *= n - i + 1;
res /= i;
}
return res;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int INF = 1e9;
template <typename T>
struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i > 0; i -= i & -i) {
x += d[i];
}
return x;
}
};
int main() {
int n;
cin >> n;
vector<int> a(n);
BIT<ll> tree1(100005), tree2(100005);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
tree1.add(i, a[i]);
tree2.add(i, a[i]);
}
ll ans = 1e18;
ll count = 0;
ll flag;
if (a[0] > 0)
flag = 1;
else
flag = -1;
for (int i = 1; i < n; i++) {
ll sum = tree1.sum(i);
if (sum * flag >= 0) {
tree1.add(i, -(llabs(sum) + 1) * flag);
count += llabs(sum) + 1;
}
flag *= -1;
}
ans = min(ans, count);
if (a[0] > 0)
flag = 1;
else
flag = -1;
count = 0;
for (int i = 0; i < n; i++) {
ll sum = tree2.sum(i);
if (sum * flag >= 0) {
tree2.add(i, -(llabs(sum) + 1) * flag);
count += llabs(sum) + 1;
}
flag *= -1;
}
ans = min(ans, count);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = list(map(int,input().split()))
b = copy.copy(a)
sum_a = []
sum_b = []
ans_a = 0
ans_b = 0
for i in range(n):
if i%2==0:
if sum(a[:i+1])>0:
sum_a.append(sum(a[:i+1]))
tmp = 0
else:
tmp = abs(sum(a[:i+1]))+1
a[i] += tmp
ans_a += tmp
sum_a.append(sum(a[:i+1]))
else:
if sum(a[:i+1])<0:
sum_a.append(sum(a[:i+1]))
tmp = 0
else:
tmp = abs(sum(a[:i+1]))+1
a[i] -= tmp
ans_a += tmp
sum_a.append(sum(a[:i+1]))
if i%2==1:
if sum(b[:i+1])>0:
sum_b.append(sum(b[:i+1]))
tmp = 0
else:
tmp = abs(sum(b[:i+1]))+1
b[i] += tmp
ans_b += tmp
sum_b.append(sum(b[:i+1]))
else:
if sum(b[:i+1])<0:
sum_b.append(sum(b[:i+1]))
tmp = 0
else:
tmp = abs(sum(b[:i+1]))+1
b[i] -= tmp
ans_b += tmp
sum_b.append(sum(b[:i+1]))
#print(a)
#print(sum_a)
print(min(ans_a,ans_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>
#define int ll
#define abs(x) llabs((x))
using namespace std;
typedef long long ll;
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int count=0;
int ans=0;
for(int i=0;i<n;i++){
count+=a[i];
if(i%2==0){
if(count<=0){
ans+=abs(count)+1;
count=1;
}
}
else{
if(count>=0){
ans+=count+1;
count=-1;
}
}
}
int count2=0;
int ans2=0;
for(int i=0;i<n;i++){
count2+=a[i];
if(i%2==1){
if(count2<=0){
ans2+=abs(count2)+1;
count2=1;
}
}
else{
if(count2>=0){
ans2+=count2+1;
count2=-1;
}
}
}
cout<<min(ans,ans2)<<endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int MOD = 10000007;
static const int MAX = 100005;
int N;
int A[MAX], S[MAX];
int change(bool pos) {
int k = -1, diff = 0, cnt = 0, counter;
if (pos) {
for (int i = (0); i < (N); i++) {
if (S[i] + diff > 0 && i % 2 == 0)
continue;
else if (S[i] + diff < 0 && i % 2 == 1)
continue;
else {
k = i;
if (k % 2 == 0) {
counter = abs(1 - (S[k] + diff));
diff += 1 - (S[k] + diff);
cnt += counter;
} else {
counter = abs((S[k] + diff) + 1);
diff += -1 - (S[k] + diff);
cnt += counter;
}
}
}
} else {
for (int i = (0); i < (N); i++) {
if (S[i] + diff > 0 && i % 2 == 1)
continue;
else if (S[i] + diff < 0 && i % 2 == 0)
continue;
else {
k = i;
if (k % 2 == 1) {
counter = abs(1 - (S[k] + diff));
diff += 1 - (S[k] + diff);
cnt += counter;
} else {
counter = abs(1 + (S[k] + diff));
diff += -1 - (S[k] + diff);
cnt += counter;
}
}
}
}
return cnt;
}
int main() {
cin >> N;
int sum = 0;
for (int i = (0); i < (N); i++) {
int a;
cin >> a;
A[i] = a;
sum += a;
S[i] = sum;
}
bool pos = true;
int plus, minus;
plus = change(pos);
pos = false;
minus = change(pos);
cout << min(plus, minus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[110000], ans1, ans2, sum1, sum2;
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
}
}
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void chmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void chmin(T &a, T b) {
if (a > b) a = b;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long sum = a[0];
long long change = 0LL;
if (sum > 0) {
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && sum + a[i] >= 0) {
sum += a[i];
change += abs(-1LL - sum);
sum = -1LL;
} else if (i % 2 == 0 && sum + a[i] <= 0) {
sum += a[i];
change += abs(1LL - sum);
sum = 1LL;
} else {
sum += a[i];
}
}
} else {
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && sum + a[i] >= 0) {
sum += a[i];
change += abs(-1LL - sum);
sum = -1LL;
} else if (i % 2 == 1 && sum + a[i] <= 0) {
sum += a[i];
change += abs(1LL - sum);
sum = 1LL;
} else {
sum += a[i];
}
}
}
cout << change;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100010], sum[100010], ans, s;
long long delta;
int main() {
scanf("%d", &n);
sum[0] = 0LL;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
ans = 0LL;
delta = 0LL;
if (sum[1] == 0) {
delta = 1LL;
ans = 1LL;
sum[1] = 1;
}
for (int i = 2; i <= n; i++) {
sum[i] += delta;
if (sum[i - 1] < 0) {
if (sum[i] <= 0) {
ans += 1 - sum[i];
delta += 1 - sum[i];
sum[i] = 1;
}
} else if (sum[i] >= 0) {
ans += sum[i] + 1;
delta -= sum[i] + 1;
sum[i] = -1;
}
}
s = 0LL;
delta = 0LL;
if (sum[1] < 0) {
s += 1 - sum[1];
delta += 1 - sum[1];
sum[1] = 1;
} else {
s += sum[1] + 1;
delta -= sum[1] + 1;
sum[1] = -1;
}
for (int i = 2; i <= n; i++) {
sum[i] += delta;
if (sum[i - 1] < 0) {
if (sum[i] <= 0) {
s += 1 - sum[i];
delta += 1 - sum[i];
sum[i] = 1;
}
} else if (sum[i] >= 0) {
s += sum[i] + 1;
delta -= sum[i] + 1;
sum[i] = -1;
}
}
printf("%lld\n", min(s, 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;
using P = pair<int, int>;
int INF = 1e9;
template <typename T>
struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i > 0; i -= i & -i) {
x += d[i];
}
return x;
}
};
int sign(ll n) {
if (n > 0)
return 1;
else if (n < 0)
return -1;
else
return 0;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
BIT<ll> tree1(100005), tree2(100005);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
tree1.add(i, a[i]);
tree2.add(i, a[i]);
}
ll ans = 1e18;
ll count = 0;
for (int i = 0; i < n; i++) {
ll sum = tree1.sum(i);
int flag = 1 - i % 2 * 2;
if (sum * flag <= 0) {
tree1.add(i, -(llabs(sum) + 1) * sign(sum));
count += llabs(sum) + 1;
}
}
ans = min(ans, count);
count = 0;
for (int i = 0; i < n; i++) {
ll sum = tree2.sum(i);
int flag = -1 + i % 2 * 2;
if (sum * flag <= 0) {
tree2.add(i, -(llabs(sum) + 1) * sign(sum));
count += llabs(sum) + 1;
}
}
ans = min(ans, count);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
import java.io.*;
public class Main{
static final Reader sc = new Reader();
static final PrintWriter out = new PrintWriter(System.out,false);
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
long[] a = new long[n];
for(int i=0;i<n;i++){
a[i] = sc.nextLong();
}
long counter = 0;
long total = a[0];
for(int i=1;i<n;i++){
if(total>0 && total+a[i]<0){
total += a[i];
}
else if(total<0 && total+a[i]>0){
total += a[i];
}
else{
long x = 0;
if(total>0){
x = -1 - total - a[i];
total = -1;
}
else{
x = 1 - total - a[i];
total = 1;
}
counter += (long)Math.abs(x);
}
//out.println(total+" "+counter);
}
out.println(counter);
out.flush();
sc.close();
out.close();
}
static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
class Reader {
private final InputStream in;
private final byte[] buf = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public Reader() { this(System.in);}
public Reader(InputStream source) { this.in = source;}
private boolean hasNextByte() {
if (ptr < buflen) return true;
ptr = 0;
try{
buflen = in.read(buf);
}catch (IOException e) {e.printStackTrace();}
if (buflen <= 0) return false;
return true;
}
private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;}
private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;}
public boolean hasNext() {skip(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
boolean minus = false;
long num = readByte();
if(num == '-'){
num = 0;
minus = true;
}else if (num < '0' || '9' < num){
throw new NumberFormatException();
}else{
num -= '0';
}
while(true){
int b = readByte();
if('0' <= b && b <= '9')
num = num * 10 + (b - '0');
else if(b == -1 || !isPrintableChar(b))
return minus ? -num : num;
else
throw new NoSuchElementException();
}
}
public int nextInt() {
long num = nextLong();
if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num)
throw new NumberFormatException();
return (int)num;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char nextChar() {
if (!hasNext()) throw new NoSuchElementException();
return (char)readByte();
}
public String nextLine() {
while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++;
if (!hasNextByte()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (b != '\n' && b != '\r') {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i=0; i<n; i++) res[i] = nextInt();
return res;
}
public char[] nextCharArray(int n) {
char[] res = new char[n];
for (int i=0; i<n; i++) res[i] = nextChar();
return res;
}
public void close() {try{ in.close();}catch(IOException e){ e.printStackTrace();}};
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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
if a[0] == 0:
ans += 1
a[0] = 1
s = a[0]
for i in range(1,n):
if s+a[i] == 0:
if s < 0:
ans += 1
s = 1
else:
ans += 1
s = -1
else:
if s > 0:
if s + a[i] >= 0:
ans += abs(a[i]+s+1)
s = -1
else:
s += a[i]
else:
if s + a[i] <= 0:
ans += abs(-a[i]-s+1)
s = 1
else:
s += a[i]
print(ans) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.