Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; long long sum = a[0], befsum = a[0]; long long ans = 0; for (int i = 1; i < n; ++i) { sum += a[i]; if (sum * befsum >= 0) { if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum < 0) { ans += -sum + 1; sum = 1; } else if (sum == 0) { ans += 1; if (befsum > 0) sum = -1; else sum = 1; } } befsum = sum; } long long tmp = abs(a[0]) + 1; if (a[0] > 0) { sum = -1; befsum = -1; } else { sum = 1; befsum = 1; } for (int i = 1; i < n; ++i) { sum += a[i]; if (sum * befsum >= 0) { if (sum > 0) { tmp += abs(sum) + 1; sum = -1; } else if (sum < 0) { tmp += abs(sum) + 1; sum = 1; } else if (sum == 0) { tmp += 1; if (befsum > 0) sum = -1; else sum = 1; } } befsum = sum; } ans = min(ans, tmp); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n, buf, ans; int flg, flg1; ans = 0; buf = 0; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; buf += a[i]; if (i == 0) { if (a[i] < 0) flg = -1; if (a[i] > 0) flg = 1; } else { if (buf < 0) flg = -1; if (buf > 0) flg = 1; } if (i != 0) { if (flg == flg1) { if (buf > 0) { ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = -1; flg = -1; } else if (buf < 0) { ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = 1; flg = 1; } else if (buf == 0) { ans += 1; if (flg1 == 1) flg = -1; if (flg1 == -1) flg = 1; } } } flg1 = flg; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Code3 { static void Main(string[] args) { string s1 = Console.ReadLine(); string s2 = Console.ReadLine(); Console.WriteLine(funcMain(s1,s2)); } static private string funcMain(string arg1, string arg2) { long cnt = 0; long ret = 0; long ret1 = 0; long ret2 = 0; long sum = 0; short sign = 0; for (int i = 0; i <= 1; i++) // 0はそのまま、1は逆符号 { cnt = sum = ret = 0; foreach (string buf in arg2.Split()) { cnt++; if (cnt > long.Parse(arg1)) break; if (sum == 0) { sum = long.Parse(buf); if (sum >= 0) sign = 1; else sign = -1; if (i == 1) { ret += Math.Abs(sum) + 1; sum = sign * -1; sign *= -1; } } else { sum += long.Parse(buf); if ((sum * sign) >= 0) { ret += Math.Abs(sum) + 1; sum = sign * -1; } sign *= -1; } } if (i == 0) ret1 = ret; else ret2 = ret; } ret = Math.Min(ret1, ret2); return ret.ToString(); } static private void test() { string arg1, arg2; arg1 = "4"; arg2 = "1 -3 1 0"; Console.WriteLine("4" == funcMain(arg1, arg2)); arg1 = "5"; arg2 = "3 -6 4 -5 7"; Console.WriteLine("0" == funcMain(arg1, arg2)); arg1 = "6"; arg2 = "-1 4 3 2 -5 4"; Console.WriteLine("8" == funcMain(arg1, arg2)); arg1 = "6"; arg2 = "-1 -2 -3 -4 -5 -6"; Console.WriteLine("16" == funcMain(arg1, arg2)); arg1 = "3"; arg2 = "1 10 -100"; Console.WriteLine("2" == funcMain(arg1, arg2)); Console.ReadKey(); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] def sign(x) : if x > 0 : return 1 elif x < 0 : return -1 else : return 0 def calc() : ret = 0 s = a[0] for i in range(1, n) : pre = sign(s) cur = sign(s+a[i]) if pre == cur or cur == 0: d = (abs(s) + 1) * pre * -1 ret += abs(d - a[i]) s = 1 * pre * -1 else : s += a[i] return ret if a[0] == 0 : a[0] = 1 s = calc() a[1] = -1 t = calc() print(min(s, t) + 1) else : print(calc())
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v.at(i); int sum1 = 0, ans1 = 0; for (int i = 0; i < n; i++) { sum1 += v.at(i); if (i % 2 == 0) { if (sum1 <= 0) { ans1 += 1 - sum1; sum1 += 1 - sum1; } } else { if (sum1 >= 0) { ans1 += 1 + sum1; sum1 -= 1 + sum1; } } } int sum2 = 0, ans2 = 0; for (int i = 0; i < n; i++) { sum2 += v.at(i); if (i % 2 == 0) { if (sum2 >= 0) { ans2 += 1 + sum2; sum2 -= 1 + sum2; } } else { if (sum2 <= 0) { ans2 += 1 - sum2; sum2 += 1 - sum2; } } } 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; using P = pair<long long, long long>; const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1}; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N; cin >> N; ; vector<long long> A(N); for (int i = 0; i < N; i++) cin >> A[i]; ; long long cnt = 0; long long ans1 = 0; long long ans2 = 0; long long sum = 0; for (int i = 0; i < N; i++) { sum += A[i]; A[i] = sum; } vector<long long> B = A; for (int i = 0; i < N - 1; i++) { A[i + 1] += cnt; if (i % 2 == 0) { if (A[i] < A[i + 1] && (A[i] >= 0 || A[i + 1] <= 0)) { if (abs(A[i] - (-1)) > abs(A[i + 1] - 1)) { ans1 += abs(A[i + 1] - 1); cnt += 1 - A[i + 1]; A[i + 1] = 1; } else { ans1 += abs(A[i] - (-1)); cnt += -1 - A[i]; A[i + 1] += -1 - A[i]; A[i] = -1; } } else if (A[i] >= A[i + 1]) { ans1 += abs(A[i] - (-1)); cnt += -1 - A[i]; A[i + 1] += -1 - A[i]; A[i] = -1; ans1 += abs(A[i + 1] - 1); cnt += 1 - A[i + 1]; A[i + 1] = 1; } } else { if (A[i] > A[i + 1] && (A[i] <= 0 || A[i + 1] >= 0)) { if (abs(A[i + 1] - (-1)) > abs(A[i] - 1)) { ans1 += abs(A[i] - 1); cnt += 1 - A[i]; A[i + 1] += 1 - A[i]; A[i] = 1; } else { ans1 += abs(A[i + 1] - (-1)); cnt += -1 - A[i + 1]; A[i + 1] = -1; } } else if (A[i] <= A[i + 1]) { ans1 += abs(A[i] - 1); cnt += 1 - A[i]; A[i + 1] += 1 - A[i]; A[i] = 1; ans1 += abs(A[i + 1] - (-1)); cnt += -1 - A[i + 1]; A[i + 1] = -1; } } } cnt = 0; for (int i = 0; i < N - 1; i++) { B[i + 1] += cnt; if (i % 2 == 1) { if (B[i] < B[i + 1] && (B[i] >= 0 || B[i + 1] <= 0)) { if (abs(B[i] - (-1)) > abs(B[i + 1] - 1)) { ans2 += abs(B[i + 1] - 1); cnt += 1 - B[i + 1]; B[i + 1] = 1; } else { ans2 += abs(B[i] - (-1)); cnt += -1 - B[i]; B[i + 1] += -1 - B[i]; B[i] = -1; } } else if (B[i] >= B[i + 1]) { ans2 += abs(B[i] - (-1)); cnt += -1 - B[i]; B[i + 1] += -1 - B[i]; B[i] = -1; ans2 += abs(B[i + 1] - 1); cnt += 1 - B[i + 1]; B[i + 1] = 1; } } else { if (B[i] > B[i + 1] && (B[i] <= 0 || B[i + 1] >= 0)) { if (abs(B[i + 1] - (-1)) > abs(B[i] - 1)) { ans2 += abs(B[i] - 1); cnt += 1 - B[i]; B[i + 1] += 1 - B[i]; B[i] = 1; } else { ans2 += abs(B[i + 1] - (-1)); cnt += -1 - B[i + 1]; B[i + 1] = -1; } } else if (B[i] <= B[i + 1]) { ans2 += abs(B[i] - 1); cnt += 1 - B[i]; B[i + 1] += 1 - B[i]; B[i] = 1; ans2 += abs(B[i + 1] - (-1)); cnt += -1 - B[i + 1]; B[i + 1] += -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 T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template <class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>; using ld = long double; using vld = vector<ld>; using vi = vector<int>; using vvi = vector<vi>; vll conv(vi &v) { vll r(v.size()); for (long long i = 0; i < (long long)(v.size()); i++) r[i] = v[i]; return r; } inline void input(int &v) { v = 0; char c = 0; int p = 1; while (c < '0' || c > '9') { if (c == '-') p = -1; c = getchar(); } while (c >= '0' && c <= '9') { v = (v << 3) + (v << 1) + c - '0'; c = getchar(); } v *= p; } template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << "(" << v.first << ", " << v.second << ")"; return o; } template <size_t...> struct seq {}; template <size_t N, size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, size_t... Is> void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using s = int[]; (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...}; } template <class Ch, class Tr, class... Args> auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t) -> basic_ostream<Ch, Tr> & { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; } ostream &operator<<(ostream &o, const vvll &v) { for (long long i = 0; i < (long long)(v.size()); i++) { for (long long j = 0; j < (long long)(v[i].size()); j++) o << v[i][j] << " "; o << endl; } return o; } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; for (long long i = 0; i < (long long)(v.size()); i++) o << v[i] << (i != v.size() - 1 ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) { o << '['; for (long long i = 0; i < (long long)(v.size()); i++) o << v[i] << (i != v.size() - 1 ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const unordered_set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U, typename V> ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]"; return o; } vector<int> range(const int x, const int y) { vector<int> v(y - x + 1); iota(v.begin(), v.end(), x); return v; } template <typename T> istream &operator>>(istream &i, vector<T> &o) { for (long long j = 0; j < (long long)(o.size()); j++) i >> o[j]; return i; } template <typename T, typename S, typename U> ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " "; } return o; } template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.front(); tmp.pop(); o << x << " "; } return o; } template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " "; } return o; } template <typename T> unordered_map<T, ll> counter(vector<T> vec) { unordered_map<T, ll> ret; for (auto &&x : vec) ret[x]++; return ret; }; string substr(string s, P x) { return s.substr(x.first, x.second - x.first); } void vizGraph(vvll &g, int mode = 0, string filename = "out.png") { ofstream ofs("./out.dot"); ofs << "digraph graph_name {" << endl; set<P> memo; for (long long i = 0; i < (long long)(g.size()); i++) for (long long j = 0; j < (long long)(g[i].size()); j++) { if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i)))) continue; memo.insert(P(i, g[i][j])); ofs << " " << i << " -> " << g[i][j] << (mode ? " [arrowhead = none]" : "") << endl; } ofs << "}" << endl; ofs.close(); system(((string) "dot -T png out.dot >" + filename).c_str()); } size_t random_seed; namespace std { using argument_type = P; template <> struct hash<argument_type> { size_t operator()(argument_type const &x) const { size_t seed = random_seed; seed ^= hash<ll>{}(x.first); seed ^= (hash<ll>{}(x.second) << 1); return seed; } }; }; // namespace std int main() { int n, a[100100]; cin >> n; int total = 0; int pcount = 0; int mcount = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { total += a[i]; if (i % 2 == 1) { if (total >= 0) { pcount += total + 1; total = -1; } } else { if (total <= 0) { pcount += abs(total) + 1; total = 1; } } } if (total == 0) ++pcount; total = 0; for (int i = 0; i < n; ++i) { total += a[i]; if (i % 2 == 0) { if (total >= 0) { mcount += total + 1; total = -1; } } else { if (total <= 0) { mcount += abs(total) + 1; total = 1; } } } cerr << pcount << mcount << endl; if (total == 0) ++mcount; if (pcount > mcount) cout << mcount << endl; else cout << pcount << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) b = [a[0]] su = 0 for i in range(n-1): b.append(b[-1]+a[1+i]) if b[-1] * b[-2] > 0: su += abs(b[-1]) + 1 if b[-2] > 0: b[-1] = -1 else: b[-1] = 1 else: if b[-1] == 0: su += 1 if b[-2] > 0: b[-1] = -1 else: b[-1] = 1 print(su)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using VB = vector<bool>; using VVB = vector<VB>; using VS = vector<string>; using PII = pair<int, int>; using VPII = vector<PII>; using VL = vector<long long>; using VVL = vector<VL>; int n; VI A; long long numoperations() { long long ret = (A[0] == 0) ? 1 : 0; long long sum = (A[0] == 0) ? 1 : A[0]; for (int i = 1; i < (int)n; ++i) { long long prevsum = sum; sum += A[i]; if (prevsum > 0 && sum >= 0) { ret += abs(-1 - sum); sum = -1; } else if (prevsum < 0 && sum <= 0) { ret += 1 - sum; sum = 1; } } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; A = VI(n); for (int i = 0; i < (int)n; ++i) cin >> A[i]; cout << numoperations() << 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())) s=0 l=a[0] if l==0: s+=1 if a[1]<=0: l=1 else: l=-1 for i in range(n-1): r=l+a[i+1] if r*l>=0: if l<=0: s+=1-r r=1 else: s+=1+r r=-1 l=r print(s)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a_list = [int(i) for i in input().split(' ')] total = a_list[0] pre_total = 0 counter = 0 if total == 0: total = -1*a_list[1]/abs(a_list[1]) for a in a_list[1:]: pre_total = total total += a if pre_total>0 and total>=0: counter += total+1 total = -1 elif pre_total<0 and total<=0: counter += abs(total)+1 total = 1 else: pass print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) now=a[0] if now==0: c=1 flag=1 for i in range(1,n): if not a[i]==0: flag=abs(a[i])//a[i] #print(flag) if i%2==1: flag*=-1 now=flag else: flag=abs(a[0])//a[0] c=0 #print(c,now,flag) for i in range(1,n): tmp=now+a[i] if not tmp*flag<0: c+=abs(flag*-1-tmp) now=flag*-1 else: now=tmp flag*=-1 #print(c,now,flag) print(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 d[n]; for (int i = 0; i < n; i++) { cin >> d[i]; } int count = 0; int sum = d[0]; int f = 0; if (d[0] > 0) { f = -1; } if (d[0] < 0) { f = 1; } for (int i = 1; i < n; i++) { sum += d[i]; if (sum == 0) { if (f == 1) { count++; f = -1; continue; } if (f == -1) { count++; f = 1; continue; } } if (sum > 0) { if (f == 1) { f = -1; continue; } if (f == -1) { count += sum + 1; sum = -1; f = 1; continue; } } if (sum < 0) { if (f == -1) { f = 1; continue; } if (f == 1) { count += 1 - sum; sum = 1; f = -1; 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; long long cal(long long b0, int n, long long* a, long long ans) { long long b[n]; b[0] = b0; for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; if (b[i] == 0) { ans++; b[i] = -1 * b[i - 1] / abs(b[i - 1]); } if (a[i] * b[i - 1] > 0 || (abs(a[i]) - abs(b[i - 1])) < 0) { ans += abs(a[i] + b[i - 1]) + 1; b[i] = -1 * b[i - 1] / abs(b[i - 1]); } } return ans; } int main() { int n; cin >> n; long long a[n], ans = 0; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] != 0) { cout << cal(a[0], n, a, ans) << endl; } else { ans++; cout << (cal(1, n, a, ans) < cal(-1, n, a, ans) ? cal(1, n, a, ans) : cal(-1, n, a, ans)) << endl; return 0; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long ans = 0; if (a[0] == 0) { sum = 1; ans++; } for (int i = 1; i < n; i++) { long long sum1 = sum + a[i]; if (sum1 == 0 || sum / abs(sum) == sum1 / abs(sum1)) { ans += abs(sum1 + sum / abs(sum)); sum1 = sum / abs(sum) * -1; } sum = sum1; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = input() b = input().split() a = [int(b[i]) for i in range(len(b))] def check(a): sum = 0 for i in range(len(a)): if(i == 0): sum += a[0] continue if(sum > 0): sum += a[i] if(a[i] == 0 or sum >= 0): return (i, -1) else: sum += a[i] if(a[i] == 0 or sum <= 0): return (i, +1) return True ans = 0 while(True): c = check(a) if(c == True): break a[c[0]] += c[1] 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; int main() { int n; cin >> n; vector<long long> a(n + 1, 0); for (long long &x : a) cin >> x; long long ans_1 = 0, ans = 0; long long sum_1 = a[0], sum = a[0]; for (int i = 1; i < n; i++) { sum_1 += a[i]; if (i % 2 == 1) { while (sum_1 >= 0) { sum_1--; ans_1++; } } if (i % 2 == 0) { while (sum_1 <= 0) { sum_1++; ans_1++; } } } for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1) { while (sum <= 0) { sum++; ans++; } } if (i % 2 == 0) { while (sum >= 0) { sum--; ans++; } } } cout << min(ans_1, ans) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n; long sum1 = 0; long sum2 = 0; long tmp; long count = 0; int a[100000]; char input[2000000]; int i = 0, j = 0; int cp = 0, tcp = 0; char tp[12]; tp[12] = '\0'; fgets(input, 2000000, stdin); n = atoi(input); fgets(input, 2000000, stdin); for (i = 0; i < n; i++) { while (input[cp] != ' ' && input[cp] != '\n') { tp[tcp] = input[cp]; tcp++; cp++; } tp[tcp] = '\0'; tcp = 0; cp++; a[i] = atoi(tp); } for (i = 0; i < n; i++) { if (i % 2 == 0) sum2 += a[i]; else sum1 += a[i]; } tmp = a[0]; if (sum1 == sum2) { if (a[0] < 0) { sum1++; } else { sum2++; } } for (i = 1; i < n; i++) { if (sum1 > sum2) { if (i % 2 == 0) { tmp += a[i]; while (tmp > -1) { count++; tmp--; } } else { tmp += a[i]; while (tmp < 1) { count++; tmp++; } } } else if (sum2 > sum1) { if (i % 2 == 1) { tmp += a[i]; while (tmp > -1) { count++; tmp--; } } else { tmp += a[i]; while (tmp < 1) { count++; tmp++; } } } } printf("%ld\n", count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(ai) for ai in input().split()] count = 0 a_sum = 0 for i, ai in enumerate(a): if i == 0: a_sum += ai else: tmp_sum = a_sum tmp_sum += ai if tmp_sum < 0 and a_sum < 0: c = abs(tmp_sum) + 1 elif tmp_sum > 0 and a_sum > 0: c = -abs(tmp_sum) - 1 elif tmp_sum == 0 and a_sum < 0: c = 1 elif tmp_sum == 0 and a_sum > 0: c = -1 else: c = 0 count += abs(c) a_sum = tmp_sum + c 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; template <class T> inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } struct aaa { aaa() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } aaaaaaa; const int INF = 1001001001; const long long LINF = 1001001001001001001ll; const int MOD = 1e9 + 7; const double EPS = 1e-9; const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; signed main() { int n; cin >> n; vector<long long> a(n); for (int i = 0, i_len = n; i < i_len; ++i) { cin >> a.at(i); } int cnt{}; int cnt2{}; int sum = 0; for (int i = 0, i_len = n; i < i_len; ++i) { sum += a.at(i); if (i % 2 != 0) { if (sum <= 0) { cnt += abs(1 - sum); sum = 1; } } else { if (sum >= 0) { cnt += abs(sum + 1); sum = -1; } } } sum = 0; for (auto i = 0; i != n; ++i) { sum += a.at(i); if (i % 2 == 0) { if (sum <= 0) { cnt2 += abs(1 - sum); sum = 1; } } else { if (sum >= 0) { cnt2 += abs(sum + 1); sum = -1; } } } cout << min(cnt, cnt2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<ll> a(n); vector<ll> A(n); for (int i = 0; i < (int)(n); i++) { cin >> a[i]; } ll cnt = 0; for (int j = 0; j < (int)(n); j++) { A[j] += a[j]; if (a[0] == 0) { cnt++; a[0]++; } if (a[0] > 0) { cnt += a[0] + 1; for (int i = 2; i < n; i++) { if (i % 2 == 0) { cnt += abs(a[i] - 2); } else { cnt += abs(a[i] + 2); } } } if (a[0] < 0) { cnt += -a[0] + 1; for (int i = 2; i < n; i++) { if (i % 2 == 0) { cnt += abs(a[i] + 2); } else { cnt += abs(a[i] - 2); } } } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0}; const double EPS = 1e-10; bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int n; int a[100010]; long long ans = INF; long long solve(int a[], int x, int y) { long long sum = 0, cnt = 0, res = INF; for (int i = 0; i < n; i++) { if (sum + a[i] == 0) { (a[i] >= 0 ? a[i]++ : a[i]--); cnt++; } sum += a[i]; if (i % 2 == x && sum < 0) { cnt += abs(sum) + 1; sum = 1; } if (i % 2 == y && sum > 0) { cnt += abs(sum) + 1; sum = -1; } } res = min(res, cnt); return res; } int main(void) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; ans = min(solve(a, 0, 1), solve(a, 1, 0)); printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; long long b[n]; long long c[n]; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; c[i] = a[i]; } long long ans1 = 0; long long ans2 = b[0] - 1; long long ans3 = c[0] + 1; b[0] = 1; c[0] = -1; for (int i = 1; i < n; i++) { a[i] += a[i - 1]; if (a[i] >= 0 && a[i - 1] > 0) { ans1 += abs(a[i] + 1); a[i] = -1; } else if (a[i] <= 0 && a[i - 1] < 0) { ans1 += abs(a[i] - 1); a[i] = 1; } } for (int i = 1; i < n; i++) { b[i] += b[i - 1]; if (b[i] >= 0 && b[i - 1] > 0) { ans2 += abs(b[i] + 1); b[i] = -1; } else if (b[i] <= 0 && b[i - 1] < 0) { ans2 += abs(b[i] - 1); b[i] = 1; } } for (int i = 1; i < n; i++) { c[i] += c[i - 1]; if (c[i] >= 0 && c[i - 1] > 0) { ans3 += abs(c[i] + 1); c[i] = -1; } else if (c[i] <= 0 && c[i - 1] < 0) { ans3 += abs(c[i] - 1); c[i] = 1; } } if (a[0] == 0) { cout << min(ans2, ans3) << endl; } else { cout << min(ans1, min(ans2, ans3)) << 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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); pair<long long, long long> maxP(vector<long long> a, long long size) { pair<long long, long long> p; long long Max = a[0]; long long place = 0; for (int i = (0); i < (size); ++i) { if (a[i] > Max) { Max = a[i]; place = i; } } p.first = Max; p.second = place; return p; } pair<long long, long long> minP(vector<long long> a, long long size) { pair<long long, long long> p; long long min = a[0]; long long place = 0; for (int i = (0); i < (size); ++i) { if (a[i] < min) { min = a[i]; place = i; } } p.first = min; p.second = place; return p; } long long sumL(vector<long long> a, long long size) { long long sum = 0; for (int i = (0); i < (size); ++i) { sum += a[i]; } return sum; } long long counT(vector<long long> a, long long t) { sort(a.begin(), a.end()); return upper_bound(a.begin(), a.end(), t) - lower_bound(a.begin(), a.end(), t); } long long DIV[1000 + 1][1000 + 1]; void divide(long long n, long long m) { DIV[0][0] = 1; for (int i = (1); i < (n + 1); ++i) { DIV[i][0] = 0; } for (int i = (0); i < (n + 1); ++i) { DIV[i][1] = 1; } for (int i = (1); i < (m + 1); ++i) { for (int t = (0); t < (n + 1); ++t) { if (DIV[t][i] > 0) continue; if (t >= i) { DIV[t][i] = DIV[t - i][i] + DIV[t][i - 1]; } else { DIV[t][i] = DIV[t][i - 1]; } } } } bool IsPrime(int num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { return false; } } return true; } class UnionFind { public: vector<long long> par; vector<long long> rank; UnionFind(long long N) : par(N), rank(N) { for (int i = (0); i < (N); ++i) par[i] = i; for (int i = (0); i < (N); ++i) rank[i] = 0; } ~UnionFind() {} long long root(long long x) { if (par[x] == x) return x; else { par[x] = root(par[x]); return par[x]; } } void unite(long long x, long long y) { long long rx = root(x); long long ry = root(y); if (rx == ry) return; if (rank[rx] < rank[ry]) { par[rx] = ry; } else { par[ry] = rx; if (rank[rx] == rank[ry]) { rank[rx]++; } } } bool same(long long x, long long y) { long long rx = root(x); long long ry = root(y); return rx == ry; } }; class BFS_shortestDistance { public: BFS_shortestDistance(vector<vector<char> > p_, long long h_, long long w_) { p = p_; h = h_; w = w_; initial_number = h * w * 2; for (int i = (0); i < (h); ++i) { vector<long long> k(w); for (int t = (0); t < (w); ++t) k[t] = initial_number; field.push_back(k); } } vector<vector<char> > p; long long h; long long w; long long initial_number; vector<vector<long long> > field; pair<long long, long long> plus(pair<long long, long long> &a, pair<long long, long long> &b) { pair<long long, long long> p; p.first = a.first + b.first; p.second = a.second + b.second; return p; } bool equal(pair<long long, long long> &a, pair<long long, long long> &b) { return (a.first == b.first && a.second == b.second); } bool is_in_field(int h, int w, const pair<long long, long long> &point) { const int c = point.second; const int r = point.first; return (0 <= c && c < w) && (0 <= r && r < h); } void init() { for (int i = (0); i < (field.size()); ++i) { for (int t = (0); t < (field[i].size()); ++t) { field[i][t] = initial_number; } } } void shortest(long long sy, long long sx) { init(); pair<long long, long long> c[4]; c[0].first = 0; c[0].second = 1; c[1].first = 0; c[1].second = -1; c[2].first = 1; c[2].second = 0; c[3].first = -1; c[3].second = 0; queue<pair<long long, long long> > Q; pair<long long, long long> s; s.first = sy; s.second = sx; field[sy][sx] = 0; Q.push(s); while (Q.empty() == false) { pair<long long, long long> now = Q.front(); Q.pop(); for (int u = 0; u < 4; u++) { pair<long long, long long> x = c[u]; pair<long long, long long> next = plus(now, x); if (is_in_field(h, w, next)) { if (p[next.first][next.second] == '.') { if (field[next.first][next.second] == initial_number) { field[next.first][next.second] = field[now.first][now.second] + 1; Q.push(next); } else { } } } } } } }; bool Ischanged(long long a, long long b) { if (a * b < 0) { return true; } else { return false; } } int main() { long long n; cin >> n; vector<long long> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; long long sum = 0; long long count = 0; for (int i = (0); i < (n); ++i) { if (i == 0) { sum += a[i]; if (sum == 0 && n != 1) { if (a[1] >= 0) { sum = -1; } else { sum = 1; } count++; } else if (sum == 0 && n == 1) { count++; } } else { long long was = sum; sum += a[i]; if (Ischanged(was, sum)) { continue; } else { if (sum < 0) { count += abs(sum) + 1; sum = 1; } else if (sum > 0) { count += abs(sum) + 1; sum = -1; } else { if (was < 0) { sum = 1; } else { sum = -1; } count++; } } } } 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() { long n; cin >> n; vector<long> a(n + 1); vector<long> b(n + 1); for (long i = 1; i <= n; i++) cin >> a.at(i); for (long i = 1; i <= n; i++) b.at(i) = a.at(i); long ans = 0; long anst = 0; if (a.at(1) != 0) { for (long i = 1; i <= n - 1; i++) { if (abs(a.at(i + 1)) > abs(a.at(i)) && a.at(i + 1) * a.at(i) < 0) { a.at(i + 1) += a.at(i); } else { ans += abs(a.at(i + 1) - ((abs(a.at(i)) + 1) * (-1) * a.at(i) / abs(a.at(i)))); a.at(i + 1) = (-1) * a.at(i) / abs(a.at(i)); } } } else { b.at(1) = 1; anst = 1; for (long i = 1; i <= n - 1; i++) { if (abs(b.at(i + 1)) > abs(b.at(i)) && b.at(i + 1) * b.at(i) < 0) { b.at(i + 1) += b.at(i); } else { anst += abs(b.at(i + 1) - ((abs(b.at(i)) + 1) * (-1) * b.at(i) / abs(b.at(i)))); b.at(i + 1) = (-1) * b.at(i) / abs(b.at(i)); } } a.at(1) = -1; ans = 1; for (long i = 1; i <= n - 1; i++) { if (abs(a.at(i + 1)) > abs(a.at(i)) && a.at(i + 1) * a.at(i) < 0) { a.at(i + 1) += a.at(i); } else { ans += abs(a.at(i + 1) - ((abs(a.at(i)) + 1) * (-1) * a.at(i) / abs(a.at(i)))); a.at(i + 1) = (-1) * a.at(i) / abs(a.at(i)); } } ans = min(ans, anst); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) sum_odd = sum(a[1::2]) sum_eve = sum(a[::2]) ans = 0 sum_a = 0 if sum_odd >= sum_eve:#奇数の和がおおきいので、偶数 0,2,4...を負にする for i in range(n): sum_a = sum_a + a[i] if sum_a *(-1)**(i+1) < 1: kari = 1-sum_a *(-1)**(i+1) a[i] += 1*(-1)**(i+1) *(kari) sum_a += 1*(-1)**(i+1) *(kari) ans = ans + abs(kari) else: for i in range(n): sum_a = sum_a + a[i] if sum_a *(-1)**(i) < 1: kari = (1-sum_a *(-1)**(i)) a[i] += 1*(-1)**(i) * kari sum_a += 1*(-1)**(i) * kari ans = ans + abs(kari) 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
#!/usr/bin/env python3 import sys def solve(n: int, a: "List[int]"): def _solve(): from itertools import cycle ab = 0 for aa in map(lambda a_o: a_o[0]*a_o[1], zip(a, cycle([1, -1] if a[0] > 0 else [-1, 1]))): ab -= aa if ab >= 0: yield ab + 1 ab = -1 ab = abs(ab) return sum(_solve()) # Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template) def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() n = int(next(tokens)) # type: int a = [int(next(tokens)) for _ in range(n)] # type: "List[int]" print(solve(n, a)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { long long g = gcd(a, b); return a / g * b; } bool prime(long long n) { for (long long i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } const long long MOD = 1000000007; const long long INF = 1e17; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (n); i++) cin >> a[i]; long long ans = 0; long long ans2 = 0; long long sum = 0; for (long long i = 0; i < (n); i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } else { if (sum >= 0) { ans += sum + 1; sum = -1; } } } sum = 0; for (long long i = 0; i < (n); i++) { sum += a[i]; if (i % 2 == 1) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } else { if (sum >= 0) { ans += sum + 1; sum = -1; } } } cout << (min(ans, ans2)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool DifSign(int a, int b) { if (a == 0 || b == 0) return false; return ((a > 0 && b < 0) || (a < 0 && b > 0)); } int main() { int N; int ans = 0; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; if (!DifSign(A[0], A[0] + A[1])) { if (A[0] >= 0 && A[0] < A[1]) { A[0] = -1; ans += abs(A[0]) + 1; } else if (A[0] < 0 && A[0] > A[1]) { A[0] = 1; ans += abs(A[0]) + 1; } } int sum = A[0]; for (int i = 1; i < N; i++) { if (!DifSign(sum, sum + A[i])) { int tmp = abs(sum + A[i]) + 1; ans += tmp; if (sum + A[i] > 0) A[i] -= tmp; else A[i] += tmp; } sum += A[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) arr = [int(x) for x in input().split()] def exec(sign): a = [x for x in arr] res = 0 if a[0] == 0: a[0] = sign * (-1) res += 1 x = 0 for i in range(n-1): x += a[i] tmp = sign - (x + a[i+1]) if sign < 0: tmp = min(tmp, 0) else: tmp = max(tmp, 0) res += abs(tmp) a[i+1] += tmp sign *= (-1) return res print(min(exec(1), exec(-1)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n = 0; long sum[2] = {0, 0}, ans[2] = {0, 0}; long hoge, foo; long flag[2] = {0, 1}; scanf("%d", &n); int in[n]; for (int i = 0; i < n; i++) { scanf("%ld", &in[i]); } for (int i = 0; i < n; i++) { long tmp[2]; tmp[0] = in[i]; tmp[1] = tmp[0]; if (i == 0) { sum[0] = tmp[0]; sum[1] = tmp[1]; continue; } long foo[2] = {tmp[0], tmp[0]}; for (int j = 0; j < 2; j++) { if (sum[j] + tmp[j] <= 0 && !flag[j]) { tmp[j] = abs(sum[j]) + 1; ans[j] += abs(abs(sum[j]) - abs(foo[j])) + 1; sum[j] += tmp[j]; flag[j] = 1; } else if (sum[j] + tmp[j] > 0 && !flag[j]) { flag[j] = 1; sum[j] += tmp[j]; } else if (sum[j] + tmp[j] < 0 && flag[j]) { sum[j] += tmp[j]; flag[j] = 0; } else if (sum[j] + tmp[j] >= 0 && flag[j]) { tmp[j] = -1 * (abs(sum[j]) + 1); ans[j] += abs(sum[j]) + abs(foo[j]) + 1; sum[j] += tmp[j]; flag[j] = 0; } else printf("ogehogeho"); } } printf("%ld\n", ans[0] < ans[1] ? ans[0] : ans[1]); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def main(): import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) A = [] A_append = A.append cnt = 0 for i in range(n-1): A_append((a[i])) x = sum(A) if x > 0 and x + a[i+1] > 0: y = -(x + a[i+1] + 1) cnt -= y a[i+1] += y elif x < 0 and x + a[i+1] < 0: y = -(x - a[i+1]+ 1) cnt += y a[i+1] += y if sum(a) == 0: cnt += 1 print(cnt) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD7 = 1000000007; const long long MOD9 = 1000000009; int main() { cin.tie(0); ios::sync_with_stdio(false); long long N; cin >> N; vector<long long> vec(N); for (long long i = 0; i < N; i++) cin >> vec[i]; vector<long long> partial; copy((vec).begin(), (vec).end(), back_inserter(partial)); partial_sum(partial.begin(), partial.end(), partial.begin()); long long res = 0; bool flag_plus = partial[0] > 0; for (long long i = 1; i < N; ++i) { if (flag_plus) { if (partial[i] < 0) { } else { vec[i] -= abs(partial[i]) + 1; res += abs(partial[i]) + 1; partial_sum(vec.begin(), vec.end(), partial.begin()); } } else { if (partial[i] > 0) { } else { vec[i] += abs(partial[i]) + 1; res += abs(partial[i]) + 1; partial_sum(vec.begin(), vec.end(), partial.begin()); } } flag_plus = !flag_plus; } 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
python3
N = int(input()) A2 = list(map(int,input().split())) #print(A) def getSign(a): if a < 0: return -1 elif a == 0: return 0 else: return 1 counts = [] for j in range(2): A = list(A2) count = 0 sumN = A[0] beforeSign = getSign(A[0]) if j == 0: add = -A[0] - beforeSign A[0] += add count += abs(add) for i in range(1,N): sumN += A[i] #print("be",i,sumN,A[i],count) if 0 <= beforeSign * sumN: add = -sumN - beforeSign A[i] += add sumN += add count += abs(add) beforeSign = getSign(sumN) #print("af",i,sumN,A[i],count) counts.append(count) print(min(counts))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int 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) { sum++; if (s[i + 1] <= 0) { ans[i]++; } else if (s[i + 1] > 0) { ans[i]--; } } else if (ans[i] < 0) { if (s[i + 1] > 0) { if (abs(ans[i]) < s[i + 1]) { } else { sum += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } else { sum += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } } cout << sum << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import itertools from collections import Counter from collections import defaultdict import bisect from heapq import heappush, heappop def main(): n = int(input()) a = list(map(int, input().split())) ans = 0 cumulative = 0 count = 0 for i in range(len(a)): cumulative += a[i] if i % 2 == 0: # positive if cumulative <= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) else: # negative if cumulative >= 0: count += abs(cumulative) + 1 cumulative += (abs(cumulative) + 1) ans = max(ans, count) for i in range(len(a)): cumulative += a[i] if i % 2 == 0: # negative if cumulative >= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) else: # positive if cumulative <= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) ans = max(ans, count) print(ans) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> 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]; ll mn=1e18; for (int i=0;i<2;i++) { ll sm=0; ll cnt=0; for (int j=0;j<n;j++) { sm+=a[j]; if ((i+j)%2==0) { if (sm<=0) { cnt+=-sm+1; sm=1; } } else { if (sm>=0) { cnt+=sm+1; sm=-1; } } } mn=min(mn,cnt); } 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 llong = long long; int main() { uint n; cin >> n; vector<llong> a(n, 0); vector<llong> S(n, 0); for (size_t i = 0; i < a.size(); ++i) cin >> a[i]; llong op = 0; S[0] = a[0]; if (S[0] == 0) { size_t j = 0; while (j < a.size()) { if (a[j] != 0) break; ++j; } if (j == a.size()) { S[0] = 1; ++op; } else if (j % 2 == 0) { S[0] = a[j] / abs(a[j]); ++op; } else { S[0] = -a[j] / abs(a[j]); ++op; } } for (size_t i = 1; i < a.size(); ++i) { S[i] = S[i - 1] + a[i]; if (S[i] == 0) { S[i] = -(S[i - 1] / abs(S[i - 1])); ++op; } else { if (S[i - 1] * S[i] > 0) { op = op + abs(S[i]) + 1; S[i] = -(S[i] / abs(S[i])); } } } cout << op << 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())) count = 0 sum_ = 0 for i in range(n): if sum_ * (sum_+a[i]) <0 or i == 0: sum_ += a[i] elif sum_ > 0: count += sum_+a[i]+1 a[i] = -sum_-1 sum_ += a[i] elif sum_ < 0: count += abs(sum_+a[i])+1 a[i] = -sum_+1 sum_ += a[i] 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; using ll = long long; using vi = vector<int>; int solve(bool sign, vi &a, int N) { int S = a.at(0); int ans = 0; if (sign) { if (S <= 0) { ans = -S + 1; S = 1; } } else { if (S >= 0) { ans = S + 1; S = -1; } } for (int i = (1); i < (N); ++i) { if (S > 0) { S += a.at(i); if (S >= 0) { ans += (S + 1); S = -1; } } else { S += a.at(i); if (S <= 0) { ans += (-S + 1); S = 1; } } } return ans; } int main() { int N; cin >> N; vi a(N); for (int i = (0); i < (N); ++i) { cin >> a.at(i); } int ans; ans = min(solve(false, a, N), solve(true, a, N)); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; template <class T> void cout_vec(const vector<T> &vec1) { for (long long i = 0; i < long long(vec1.size()); i++) { cout << vec1[i] << ' '; } cout << '\n'; } int main() { cin.tie(0); ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> a(n + 1), sum(n + 1, 0); for (long long i = 1; i < n + 1; i++) cin >> a[i]; long long ans = 0; for (long long i = 1; i < n + 1; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i] == 0) { if (sum[i - 1] < 0) { sum[i]++; ans++; } else { sum[i]--; ans++; } } if (sum[i] > 0 && sum[i - 1] > 0) { sum[i] = -1; ans += a[i] + sum[i - 1] + 1; } if (sum[i] < 0 && sum[i - 1] < 0) { sum[i] = 1; ans += a[i] - sum[i - 1] - 1; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; int main() { int n; cin >> n; long long ans = 0, tmp, wa = 0; cin >> tmp; wa = tmp; bool issei; if (wa > 0) issei = true; else issei = false; for (int i = 0; i < n - 1; i++) { cin >> tmp; if (i == 0 and wa == 0) { if (tmp < 0) { wa = 1; ans = 1; issei = true; } else { wa = -1; ans = 1; issei = false; } } if (issei) { ans += max(long long(0), wa + tmp + 1); wa = min(wa + tmp, long long(-1)); issei = false; } else { ans += max(long long(0), 1 - (wa + tmp)); wa = max(wa + tmp, long long(1)); issei = true; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; class Main { int n; int[] a; public static void main(String[] args) { Scanner sc = new Scanner(System.in); Main m = new Main(sc); m.solve(); sc.close(); } Main(Scanner sc) { n = sc.nextInt(); a = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } } void solve() { int sign = (a[0]>=0)?1:-1; int cnt = (a[0]==0)?1:0; int sum = (a[0]==0)?1:a[0]; //System.out.println(sum); for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt += Math.abs(sum) + 1; sum = -sign; } //System.out.println(sum); sign *= -1; } System.out.println(cnt); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void print(const T &value) { std::cout << value << std::endl; } void yesno(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YESNO(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int cnt1 = 0; int sum1[n + 1]; sum1[0] = 0; for (int i = 0; i < n; i++) { sum1[i + 1] = sum1[i] + a[i]; if (i % 2 == 0 && sum1[i + 1] <= 0) { cnt1 += 1 - sum1[i + 1]; sum1[i + 1] = 1; } if (i % 2 == 1 && sum1[i + 1] >= 0) { cnt1 += 1 + sum1[i + 1]; sum1[i + 1] = -1; } } int cnt2 = 0; int sum2[n + 1]; sum2[0] = 0; for (int i = 0; i < n; i++) { sum2[i + 1] = sum2[i] + a[i]; if (i % 2 == 1 && sum2[i + 1] <= 0) { cnt2 += 1 - sum2[i + 1]; sum2[i + 1] = 1; } if (i % 2 == 0 && sum2[i + 1] >= 0) { cnt2 += 1 + sum2[i + 1]; sum2[i + 1] = -1; } } print(min(cnt1, cnt2)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n, cnt = 0; cin >> n; bool flag; vector<ll> a; ll x; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } if (a[0] >= 0) { flag = true; } else { flag = false; } int sum = a[0]; if (sum == 0) { if (a[1] >= 0) { sum++; cnt++; } else { sum--; cnt++; } } for (int i = 1; i < n; i++) { bool flag2; int tmp = sum; sum += a[i]; if (sum == 0) { if (flag) { sum -= 1; flag = false; cnt++; } else { sum += 1; flag = true; cnt++; } } else { if (sum > 0) { flag2 = true; } else if (sum < 0) { flag2 = false; } if (flag == flag2) { if (flag2) { while (sum >= 0) { sum--; cnt++; } flag2 = false; } else { while (sum <= 0) { sum++; cnt++; } flag2 = true; } } flag = flag2; } } if (sum == 0) cnt++; cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ttl = a[0] cst = 0 if a[0]>=0: flg = 1 elif a[0]<0: flg = -1 for i in range(1,n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+1 ttl -= memo cst += memo elif flg < 0: memo = abs(ttl)+1 ttl += memo cst += memo flg *= -1 ttl = a[0] cst2 = 0 if a[0]>0: flg = -1 cst2 += abs(ttl)+1 ttl += 0-ttl-1 elif a[0]<0: flg = 1 cst2 += abs(ttl)+1 ttl += 0-ttl+1 for i in range(1,n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+1 ttl -= memo cst2 += memo elif flg < 0: memo = abs(ttl)+1 ttl += memo cst2 += memo flg *= -1 print(min(cst,cst2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long top = a[0], cnt1 = 0; if (top >= 0) { top = -1; cnt1 = a[0] + 1; } for (int i = 1, sign = 1; i < n; i++, sign *= -1) { if ((top + a[i]) * sign < 0) { cnt1 += abs(top) + abs(a[i]) + 1; top = sign; } else if ((top + a[i]) == 0) { cnt1++; top = sign; } else { top += a[i]; } } top = a[0]; long long cnt2 = 0; if (top <= 0) { top = 1; cnt2 = abs(a[0]) + 1; } for (int i = 1, sign = -1; i < n; i++, sign *= -1) { if ((top + a[i]) * sign < 0) { cnt2 += abs(top) + abs(a[i]) + 1; top = sign; } else if ((top + a[i]) == 0) { cnt2++; top = sign; } else { top += a[i]; } } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; import java.io.*; import java.math.*; public class Main{ //Don't have to see. start------------------------------------------ static class InputIterator{ ArrayList<String> inputLine = new ArrayList<String>(1024); int index = 0; int max; String read; InputIterator(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try{ while((read = br.readLine()) != null){ inputLine.add(read); } }catch(IOException e){} max = inputLine.size(); } boolean hasNext(){return (index < max);} String next(){ if(hasNext()){ return inputLine.get(index++); }else{ throw new IndexOutOfBoundsException("There is no more input"); } } } static HashMap<Integer, String> CONVSTR = new HashMap<Integer, String>(); static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem. static PrintWriter out = new PrintWriter(System.out); static void flush(){out.flush();} static void myout(Object t){out.println(t);} static void myerr(Object t){System.err.print("debug:");System.err.println(t);} static String next(){return ii.next();} static boolean hasNext(){return ii.hasNext();} static int nextInt(){return Integer.parseInt(next());} static long nextLong(){return Long.parseLong(next());} static double nextDouble(){return Double.parseDouble(next());} static ArrayList<String> nextStrArray(){return myconv(next(), 8);} static ArrayList<String> nextCharArray(){return myconv(next(), 0);} static ArrayList<Integer> nextIntArray(){ ArrayList<String> input = nextStrArray(); ArrayList<Integer> ret = new ArrayList<Integer>(input.size()); for(int i = 0; i < input.size(); i++){ ret.add(Integer.parseInt(input.get(i))); } return ret; } static ArrayList<Long> nextLongArray(){ ArrayList<String> input = nextStrArray(); ArrayList<Long> ret = new ArrayList<Long>(input.size()); for(int i = 0; i < input.size(); i++){ ret.add(Long.parseLong(input.get(i))); } return ret; } static String myconv(Object list, int no){//only join String joinString = CONVSTR.get(no); if(list instanceof String[]){ return String.join(joinString, (String[])list); }else if(list instanceof ArrayList){ return String.join(joinString, (ArrayList)list); }else{ throw new ClassCastException("Don't join"); } } static ArrayList<String> myconv(String str, int no){//only split String splitString = CONVSTR.get(no); return new ArrayList<String>(Arrays.asList(str.split(splitString))); } public static void main(String[] args){ CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, ""); solve();flush(); } //Don't have to see. end------------------------------------------ static void solve(){//Here is the main function int N = nextInt(); ArrayList<Integer> tmp = nextIntArray(); int[] list = new int[N]; for(int i = 0; i < N; i++){ list[i] = tmp.get(i); } int oddCount = 0; int evenCount = 0; int[] oddSum = new int[N];//1, -1, 1, -1 int[] evenSum = new int[N];//-1, 1 ,-1 ,1 if(list[0] == 0){ oddSum[0] = 1; evenSum[0] = -1; oddCount++; evenCount++; }else{ if(list[0] < 0){ oddCount += Math.abs(list[0]) + 1; oddSum[0] = 1; evenSum[0] = list[0]; }else{ evenCount += Math.abs(list[0]) + 1; evenSum[0] = -1; oddSum[0] = list[0]; } } for(int i = 1; i < N; i++){ oddSum[i] = oddSum[i - 1] + list[i]; evenSum[i] = evenSum[i - 1] + list[i]; if((oddSum[i - 1] < 0 && oddSum[i] > 0) || (oddSum[i - 1] > 0 && oddSum[i] < 0)){ }else{ if((oddSum[i - 1] > 0)){ oddCount += oddSum[i] + 1; oddSum[i] = -1; }else{ oddCount += Math.abs(oddSum[i]) + 1; oddSum[i] = 1; } } if((evenSum[i - 1] < 0 && evenSum[i] > 0) || (evenSum[i - 1] > 0 && evenSum[i] < 0)){ }else{ if((evenSum[i - 1] > 0)){ evenCount += evenSum[i] + 1; evenSum[i] = -1; }else{ evenCount += Math.abs(evenSum[i]) + 1; evenSum[i] = 1; } } } myout(Math.min(evenCount, oddCount)); } //Method addition frame start //Method addition frame end }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using static System.Console; using static System.Convert; class Program { static void Main(string[] args) { var length = ToInt32(ReadLine()); var result = 0; var nums = Array.ConvertAll(ReadLine().Split(' '), int.Parse); var sum = nums[0]; if (sum == 0) { sum++; result++; } var lastSum = sum; for(var i = 1; i < length; i++) { sum += nums[i]; while (!IsDifferentSign(lastSum, sum)) { sum = lastSum > 0 ? --sum : ++sum; result++; } lastSum = sum; } WriteLine(result); } private static bool IsDifferentSign(int lastSum,int sum) { return sum != 0 && ((lastSum > 0 && sum < 0) || (lastSum < 0 && sum > 0)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input=sys.stdin.readline def main(): N = int(input()) A = list(map(int, input().split())) s = A[0] n = 0 for i in range(1,N): if s * (s+A[i]) >= 0: if s < 0: n += abs(-s+1 -A[i]) A[i] = -s+1 else: n += abs(-s-1 -A[i]) A[i] = -s-1 s += A[i] print(n) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input().strip()) A = list(map(int, input().strip().split(" "))) prev = A[0] s = prev sign = prev > 0 count = 0 for a in A[1:]: prev = s sign = prev > 0 s += a if s == 0: count += 1 if sign: # previous is positive s -= 1 else: # prev is negative s += 1 elif sign == (s > 0): # previous and current have the same sign count += abs(s)+1 if s > 0: s = -1 else: s = 1 else: pass 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 sum1 = 0, sum2 = 0; long long ans1 = 0, ans2 = 0; int first; cin >> first; if (first > 0) { sum1 = first; sum2 = -first; ans2 += first * 2; } else if (first < 0) { sum1 = -first; sum2 = first; ans1 += -first * 2; } else { sum1 = 1; sum2 = -1; ans1++, ans2++; } for (int i = 0; i < n - 1; i++) { int a; cin >> a; if (sum1 > 0) { if (sum1 + a >= 0) { ans1 += abs(-sum1 - 1 - a); sum1 = -1; } else { sum1 += a; } } else if (sum1 < 0) { if (sum1 + a <= 0) { ans1 += -sum1 + 1 - a; sum1 = 1; } else { sum1 += a; } } if (sum2 > 0) { if (sum2 + a >= 0) { ans2 += abs(-sum2 - 1 - a); sum2 = -1; } else { sum2 += a; } } else if (sum2 < 0) { if (sum2 + a <= 0) { ans2 += -sum2 + 1 - a; sum2 = 1; } else { sum2 += a; } } } 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())) ans = 0 cumsum = a[0] p = a[0] > 0 for i in range(1, n): if p: if cumsum+a[i] >= 0: ans += cumsum+a[i]+1 cumsum = -1 else: cumsum += a[i] p = False else: if cumsum+a[i] <= 0: ans += 1-(cumsum+a[i]) cumsum = 1 else: cumsum += a[i] p = True # print(cumsum) 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> a(N); for (int i = 0; i < N; i++) cin >> a.at(i); int count1 = 0; int sum = 0; for (int i = 0; i < N; i++) { int new_sum = sum + a.at(i); if (i == 0) { if (a.at(0) == 0) { sum = 1; count1++; } else sum = a.at(0); } else if (i % 2 == 0 && new_sum >= 0) { count1 += new_sum + 1; sum = -1; } else if (i % 2 != 0 && sum + a.at(i) <= 0) { count1 += -new_sum + 1; sum = 1; } else { sum += a.at(i); } } int count2 = 0; sum = 0; for (int i = 0; i < N; i++) { int new_sum = sum + a.at(i); if (i == 0) { if (a.at(0) == 0) { sum = -1; count2++; } else sum = a.at(0); } else if (i % 2 != 0 && new_sum >= 0) { count2 += new_sum + 1; sum = -1; } else if (i % 2 == 0 && sum + a.at(i) <= 0) { count2 += -new_sum + 1; sum = 1; } else { sum += a.at(i); } } if (count1 < count2) { cout << count1 << endl; } else { cout << count2 << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], cntplus = 0, cntplus1 = 0, cntminus = 0, cntminus1 = 0; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] > 0) { int s = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { while (0 <= s + a[i]) { a[i]--; cntplus++; } } else { while (s + a[i] <= 0) { a[i]++; cntplus++; } } s += a[i]; } s = -1; cntplus1 += a[0] + 1; for (int i = 1; i < n; i++) { if (i % 2) { while (s + a[i] <= 0) { a[i]++; cntplus1++; } } else { while (0 <= s + a[i]) { a[i]--; cntplus1++; } } s += a[i]; } cout << min(cntplus, cntplus1) << endl; } else { int s = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { while (s + a[i] <= 0) { a[i]++; cntminus++; } } else { while (0 <= s + a[i]) { a[i]--; cntminus++; } } s += a[i]; } s = 1; cntplus1 += -a[0] + 1; for (int i = 1; i < n; i++) { if (i % 2) { while (0 <= s + a[i]) { a[i]--; cntminus1++; } } else { while (s + a[i] <= 0) { a[i]++; cntminus1++; } } s += a[i]; } cout << min(cntminus, cntminus1) << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) if a[0] < 0: for i in range(n): a[i] *= -1 a_orig = a[:] ans1 = 0 ans2 = 0 tot = [0 for i in range(n)] tot[0] = a[0] for i in range(1, n): tot[i] = tot[i-1] + a[i] if i % 2 == 0: if tot[i] <= 0: tot[i] = 1 a[i] = tot[i] - tot[i-1] else: if tot[i] >= 0: tot[i] = -1 a[i] = tot[i] - tot[i-1] for i in range(n): ans1 += abs(a[i]-a_orig[i]) tot = [0 for i in range(n)] tot[0] = a[0] for i in range(1, n): tot[i] = tot[i-1] + a[i] if i % 2 == 1: if tot[i] <= 0: tot[i] = 1 a[i] = tot[i] - tot[i-1] else: if tot[i] >= 0: tot[i] = -1 a[i] = tot[i] - tot[i-1] for i in range(n): ans2 += abs(a[i]-a_orig[i]) print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int* a = new int[n]; bool flg = false; long long sum = 0; long long cnt = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; if (i == 0) { if (a[0] < 0) flg = false; else flg = true; } sum += a[i]; if (flg == false && i % 2 == 0 && sum >= 0) { while (sum >= 0) { --sum; ++cnt; } } else if (flg == false && i % 2 == 1 && sum <= 0) { while (sum <= 0) { ++sum; ++cnt; } } else if (flg == true && i % 2 == 0 && sum <= 0) { while (sum <= 0) { ++sum; ++cnt; } } else if (flg == true && i % 2 == 1 && sum >= 0) { while (sum >= 0) { --sum; ++cnt; } } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) l = list(map(int,input().split())) import copy l1 = copy.copy(l) l2 = copy.copy(l) ans1 = 0 ans2 = 0 goukei = 0 #最初が正の時 if l1[0]<0: while l1[0]==+1: l1[0]+1 ans1 = ans1 + 1 for i in range(n): goukei = sum(l1[:i+1]) #print(i,goukei) if i%2 == 0: while goukei<=0: l1[i] = l1[i] + 1 goukei = sum(l1[:i+1]) ans1 = ans1 + 1 else: while goukei>=0: l1[i] = l1[i] - 1 goukei = sum(l1[:i+1]) ans1 = ans1 + 1 #print(l) #print(l1) #print(ans1) #最初が負の時 if l2[0]>0: while l2[0]==-1: l2[0]-1 ans2 = ans2 + 1 for i in range(n): goukei = sum(l2[:i+1]) #print(i,goukei) if i%2 != 0: while goukei<=0: l2[i] = l2[i] + 1 goukei = sum(l2[:i+1]) ans2 = ans2 + 1 else: while goukei>=0: l2[i] = l2[i] - 1 goukei = sum(l2[:i+1]) ans2 = ans2 + 1 #print(l) #print(l2) #print(ans2) print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
l = int(input()) n = [int(i) for i in input().split()] count = 0 nsum = n[0] for i in range(1, l): tmp = n[i] tmp_1 = n[i-1] while(True): if (tmp_1 * tmp >= 0) or (nsum * (nsum + tmp) >= 0): if tmp_1 < 0: count += 1 tmp += 1 else: count += 1 tmp -= 1 else: n[i] = tmp nsum += tmp break 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; using lint = long long; using uli = unsigned long long; uli gcd(uli a, uli b) { while (1) { if (a < b) swap(a, b); if (!b) break; a %= b; } return a; } uli lcm(uli a, uli b) { return a * b / gcd(a, b); } const uli mod = 1000000007; const double pi = 3.141592653589793238462; const lint intmax = 9223372036854775807; uli _PowMod(uli x, uli y, uli _mod) { if (y == 0) { return 1; } else if (y == 1) { return x % _mod; } else if (y % 2 == 0) { auto tmp = _PowMod(x, y / 2, _mod); return tmp * tmp % _mod; } else { auto tmp = _PowMod(x, y / 2, _mod); return (tmp * tmp % _mod) * x % _mod; } } uli PowMod(uli x, uli y) { return _PowMod(x, y, mod); } uli getModInv(uli N) { return PowMod(N, mod - 2); } lint nCrMod(lint start, lint n, lint r) { if (n < r) { return 0; } lint a = start; for (size_t i = n; i >= n - r + 1; i--) { a *= i; a %= mod; } for (size_t i = 1; i <= r; i++) { a *= getModInv(i); a %= mod; } return a; } lint nHrMod(lint start, lint n, lint r) { return nCrMod(start, n + r - 1, r); } lint _nCrMod(lint start, lint n, lint r) { if (n <= 0) { return 0; } return nCrMod(start, n, r); } struct uf { vector<lint> p; uf(lint n) : p(n) { for (size_t i = 0; i < n; i++) { p[i] = i; } } lint rt(lint n) { return p[n] == n ? n : p[n] = rt(p[n]); } void un(lint n, lint m) { p[rt(n)] = p[rt(m)]; } bool eq(lint n, lint m) { return rt(n) == rt(m); } }; bool lineCol(lint a1x, lint a1y, lint a2x, lint a2y, lint b1x, lint b1y, lint b2x, lint b2y) { auto ta = (b1x - b2x) * (a1y - b1y) + (b1y - b2y) * (b1x - a1x); auto tb = (b1x - b2x) * (a2y - b1y) + (b1y - b2y) * (b1x - a2x); auto tc = (a1x - a2x) * (b1y - a1y) + (a1y - a2y) * (a1x - b1x); auto td = (a1x - a2x) * (b2y - a1y) + (a1y - a2y) * (a1x - b2x); return tc * td < 0 && ta * tb < 0; } lint powInt(lint a, lint b) { if (b == 0) { return 1; } if (b == 1) { return a; } lint tmp = powInt(a, b / 2); return (b % 2 == 1 ? a * tmp * tmp : tmp * tmp); } lint _sMod(string n, lint mod) { lint k = (n[0] - '0') % mod; for (size_t i = 1; i < n.length(); i++) { k *= 10; k += (n[i] - '0'); k %= mod; } return k; } template <typename T> void vsort(vector<T>& v) { sort(v.begin(), v.end()); } template <typename T> void vsortr(vector<T>& v) { sort(v.rbegin(), v.rend()); } lint div2(lint p, lint q) { return (p + q - 1) / q; } struct xy { lint x, y; xy() : x(0), y(0) {} xy(lint _x, lint _y) : x(_x), y(_y) {} }; template <class T> bool exist(vector<T>& v, const T& val) { return find(v.begin(), v.end(), val) != v.end(); } template <class T, class Pr> bool exist_if(vector<T>& v, Pr pred) { return find_if(v.begin(), v.end(), pred) != v.end(); } lint n_dig(lint n) { lint ans = 0; while (n > 0) { n /= 10; ans++; } return ans; } string yn(bool f, string y, string n) { return f ? y : n; } const lint alpn = 'z' - 'a' + 1; template <class T> T sgn(T val) { if (val == T(0)) return T(0); if (val < 0) return T(-1); if (val > 0) return T(1); } int main() { lint n; cin >> n; vector<lint> a(n); for (lint i = 0; i < n; i++) cin >> a[i]; lint s = 0, sg = sgn(a[0]), ans = 0; for (lint i = 0; i < n; i++) { s += a[i]; if (sgn(s) != sg) { ans += abs(sg - s); s = sg; } sg *= (-1); } s = 0, sg = sgn(a[0]) * (-1); lint ans2 = 0; for (lint i = 0; i < n; i++) { s += a[i]; if (sgn(s) != sg) { ans2 += abs(sg - s); s = sg; } sg *= (-1); } cout << min(ans, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define int long long #define r(i,n) for(int i=0;i<n;i++) using namespace std; int a[100009],n; int d1(){ int sum=0,ans=0; for(int i=0;i<n;i++){ if(i%2==0){ if(sum+a[i]>=0)ans+=abs(sum-a[i]+1),sum=-1; else sum+=a[i]; } else{ if(sum+a[i]<=0)ans+=abs(1-sum+a[i]),sum=1; else sum+=a[i]; } } return ans; } int d2(){ int sum=0,ans=0; for(int i=0;i<n;i++){ if(i%2==1){ if(sum+a[i]>=0)ans+=abs(sum-a[i]+1),sum=-1; else sum+=a[i]; } else{ if(sum+a[i]<=0)ans+=abs(1-sum+a[i]),sum=1; else sum+=a[i]; } } return ans; } main(){ cin>>n; r(i,n)cin>>a[i]; cout<<min(d1(),d2())<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { double num[10 * 10 * 10 * 10 * 10]; int i, n, ssign; double sum = 0; double count = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf", &num[i]); } if (num[0] == 0) { num[0]++; count++; } for (i = 1; i < n; i++) { sum += num[i - 1]; while (1) { if (fabs(sum) > fabs(num[i])) { if (sum < 0) { num[i]++; count++; } else if (sum > 0) { num[i]--; count++; } } else if (fabs(sum) == fabs(num[i])) { if (sum < 0) { num[i]++; count++; } else { num[i]--; count++; } } else if (sum > 0 && num[i] > 0 && fabs(sum) < fabs(num[i])) { num[i]--; count++; } else if (sum < 0 && num[i] < 0 && fabs(sum) < fabs(num[i])) { num[i]++; count++; } else break; } } printf("%f\n", count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>; const long long MOD = 1000000007; ll N, a[100010]; ll solve(ll z) { ll ans = !(a[0] == z), sum = a[0]; a[0] = z; for (int(i) = (1); (i) < (N); ++(i)) { if (sum > 0) { ans += max(0LL, sum + a[i] + 1); sum += a[i] - max(0LL, sum + a[i] + 1); } else if (sum < 0) { ans += max(0LL, -a[i] - sum + 1); sum += a[i] + max(0LL, -a[i] - sum + 1); } } return ans; } signed main() { cin >> N; for (int(i) = 0; (i) < (N); ++(i)) cin >> a[i]; if (a[0] == 0) cout << (min(solve(1), solve(-1))) << "\n"; else cout << (solve(a[0])) << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) 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 = "+" a = a_1 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_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; long long s[100005], h[100005]; int main() { long long n, sum = 0; scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", &s[i]); for (int i = 2; i <= n; i++) { if (s[i] + s[i - 1] >= 0 && s[i - 1] > 0) { sum += abs(s[i] + s[i - 1] + 1); s[i] = -1; } else if (s[i] + s[i - 1] <= 0 && s[i - 1] < 0) { sum += abs(s[i] + s[i - 1] - 1); s[i] = 1; } else s[i] = s[i] + s[i - 1]; } printf("%lld", sum); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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()) b=list(map(int,input().split())) a=b condition='' cnt=0 for i in range(n): if i == 0: if a[i]>0: condition='minus' else: condition='plus' elif condition == 'plus': condition='minus' if sum(a[0:i+1])<=0: cnt+=abs(sum(a[0:i+1]))+1 a[i]+=abs(sum(a[0:i+1]))+1 elif condition == 'minus': condition='plus' if sum(a[0:i+1])>=0: cnt+=abs(sum(a[0:i+1]))+1 a[i]-=abs(sum(a[0:i+1]))+1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) sum=a[0] if(sum==0): opp=1 sum=1 for i in a[1:]: if(sum*(sum+i)>=0): opp+=abs(sum+i)+1 if(sum<0):sum=1 else:sum=-1 else:sum+=i opm=1 sum=-1 for i in a[1:]: if(sum*(sum+i)>=0): opm+=abs(sum+i)+1 if(sum<0):sum=1 else:sum=-1 else:sum+=i op=min(opm,opp) else: opp=0 for i in a[1:]: if(sum*(sum+i)>=0): opp+=abs(sum+i)+1 if(sum<0):sum=1 else:sum=-1 else:sum+=i opm=abs(sum)+1 if(sum>0):sum=-1 else:sum=1 for i in a[1:]: if(sum*(sum+i)>=0): opm+=abs(sum+i)+1 if(sum<0):sum=1 else:sum=-1 else:sum+=i op=min(opp,opm) print(op)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
fun main() { val n = readLine()!!.toInt() val a = readLine()!!.split(" ").map { it.toLong() } var answer = 0L var total = 0L for (i in 0 until n) { val tmp = total total = total + a[i] if (total == 0L) { if (tmp > 0) { answer += 1 total = -1 } else if (tmp < 0) { answer += 1 total = 1 } continue } if (tmp > 0 && total > 0) { answer += (total + 1) total = -1 } else if (tmp < 0 && total < 0) { answer += (-total + 1) total = 1 } } println(answer) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> A(n); for (int i = 0; i < n; i++) cin >> A[i]; long long sum1 = 0, cnt1 = 0; for (int i = 0; i < n; i++) { sum1 += A[i]; if (i % 2 == 0 && sum1 < 0) { cnt1 += 1 - sum1; sum1 = 1; } if (i % 2 == 1 && sum1 > 0) { cnt1 += 1 + sum1; sum1 = -1; } } long long sum2 = 0, cnt2 = 0; for (int i = 0; i < n; i++) { sum2 += A[i]; if (i % 2 == 0 && sum2 > 0) { cnt2 += 1 + sum2; sum2 = -1; } if (i % 2 == 1 && sum2 < 0) { cnt2 += 1 - sum2; sum2 = 1; } } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) N=list(map(int,input().split())) emptylists=[] number=0 for i in range(n): number+=N[i] emptylists.append(number) ans1=0 use=0 for i in range(n): #偶数番が正、奇数番が負の時 if i%2==0: #奇数番め if emptylists[i]+use<0: continue if emptylists[i]+use>=0: ans1+=emptylists[i]+1 use=use-emptylists[i]-1 if i%2!=0: #偶数番め if emptylists[i]+use>0: continue if emptylists[i]+use<=0: ans1+=1-emptylists[i]-use use=use+1-emptylists[i] ans2=0 uses=0 for i in range(n): #偶数番が負、奇数番が正の時 if i%2!=0: #ぐう数番め if emptylists[i]<0: continue if emptylists[i]>=0: ans2+=emptylists[i]+1 uses=uses-emptylists[i]-1 if i%2==0: #き数番め if emptylists[i]>0: continue if emptylists[i]<=0: ans2+=1-emptylists[i]-uses uses=uses+1-emptylists[i] print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long inf = 1ll << 60, mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long i, n, a[100000], s, ans = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } s = a[0]; for (i = 1; i < n; i++) { if (s > 0) { if (s + a[i] >= 0) { ans += abs(s + a[i]) + 1; s = -1; } else { s += a[i]; } } else { if (s + a[i] < 0) { ans += abs(s + a[i]) + 1; s = 1; } else { s += a[i]; } } } cout << ans; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) ans=0 s0=a[0] if a[0]==0: if a[1]>=0: s0=-1 ans+=1 else: s0=1 ans+=1 s1=s0+a[1] if s0*s1>=0: if s1>0: ans+=abs(s1)+1 s1=-1 elif s1<0: ans+=abs(s1)+1 s1=1 else: if s0>0: ans+=1 s1=-1 else: ans+=1 s1=1 for i in range(1,n-1): s0=s1 s1=s1+a[i+1] if s0*s1>=0: if s1>0: ans+=abs(s1)+1 s1=-1 elif s1<0: ans+=abs(s1)+1 s1=1 else: if s0>0: ans+=1 s1=-1 else: ans+=1 s1=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())) r_sum = [0]*(N+1) sum = a[0] ans = 0 for i in range(1, N): if sum < 0: if abs(sum) < a[i]: sum += a[i] else: ans += abs(sum)-a[i]+1 sum += 1 elif sum > 0: if sum + a[i] < 0: sum += a[i] else: ans += abs(-1-sum-a[i]) sum = -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; const int N = 1000000; int main() { int n; cin >> n; vector<int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; int ans_plus = 0; vector<int> sum_plus(n); if (a[0] > 0) { sum_plus[0] = a[0]; } else { ans_plus += 1 - a[0]; sum_plus[0] = 1; } for (int i = 1; i < n; ++i) { sum_plus[i] = sum_plus[i - 1] + a[i]; if (sum_plus[i] * sum_plus[i - 1] > 0) { if (sum_plus[i - 1] > 0) { ans_plus += 1 + sum_plus[i]; sum_plus[i] = -1; } else if (sum_plus[i - 1] < 0) { ans_plus += 1 - sum_plus[i]; sum_plus[i] = 1; } } else if (sum_plus[i] == 0) { if (sum_plus[i - 1] > 0) { sum_plus[i] = -1; } else if (sum_plus[i - 1] < 0) { sum_plus[i] = 1; } ans_plus++; } } int ans_minus = 0; vector<int> sum_minus(n); if (a[0] < 0) { sum_minus[0] = a[0]; } else { ans_minus += 1 + a[0]; sum_minus[0] = -1; } for (int i = 1; i < n; ++i) { sum_minus[i] = sum_minus[i - 1] + a[i]; if (sum_minus[i] * sum_minus[i - 1] > 0) { if (sum_minus[i - 1] > 0) { ans_minus += 1 + sum_minus[i]; sum_minus[i] = -1; } else if (sum_minus[i - 1] < 0) { ans_minus += 1 - sum_minus[i]; sum_minus[i] = 1; } else if (sum_minus[i] == 0) { if (sum_minus[i - 1] > 0) { sum_minus[i] = -1; } else if (sum_minus[i - 1] < 0) { sum_minus[i] = 1; } ans_minus++; } } } std::cout << min(ans_minus, ans_plus) << 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())) def ssa(n): arr = A memo =['inf'] * (n + 1) def _ssa(n): if n == 0: return arr[0] if memo[n] != 'inf': return memo[n] memo[n] = _ssa(n -1) + arr[n] return memo[n] return _ssa(n) cnt = 0 chg = 0 for i in range(n - 1): if ssa(i) + chg > 0: if ssa(i + 1) + chg >= 0: cnt += ssa(i + 1) + chg + 1 chg += -(ssa(i + 1)+ chg + 1) elif ssa(i) + chg < 0: if ssa(i + 1) + chg <= 0: cnt += 1 - ssa(i + 1) - chg chg += 1 - ssa(i + 1) - chg if ssa(n - 1) + chg == 0: cnt +=1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; long[] s1 = new long[n]; long[] s2 = new long[n]; int sum1 = 0; int sum2 = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } s1[0] = a[0]; int count = 0; // - + - + となる場合 while (s1[0] >= 0) { s1[0]--; count++; sum1++; } for (int i = 1; i < n; i++) { count = 0; s1[i] = s1[i-1] + a[i]; // iが奇数の場合は正にする if (i % 2 != 0) { while (s1[i-1]*s1[i] >= 0) { s1[i]++; count++; sum1++; } } // iが偶数の場合は負にする else { while (s1[i-1]*s1[i] >= 0) { s1[i]--; count++; sum1++; } } } s2[0] = a[0]; count = 0; // + - + - となる場合 while (s2[0] <= 0) { s2[0]++; count++; sum2++; } for (int i = 1; i < n; i++) { count = 0; s2[i] = s2[i-1] + a[i]; if (i % 2 != 0) { while (s2[i-1]*s2[i] >= 0) { s2[i]--; count++; sum2++; } } else { while (s2[i-1]*s2[i] >= 0) { s2[i]++; count++; sum2++; } } } System.out.println(Math.min(sum1, sum2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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())) if a[0] > 0: f = 1 else: f = -1 m = a[0] cnt = 0 for i in range(1, n): f *= -1 m += a[i] if f == 1: if m <= 0: cnt += f-m m += f-m else: if m >= 0: cnt += m-f m -= m-f print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[10002] = {}; int b[10002] = {}; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { b[i] = a[i]; } int eve = 0, sum = 0; for (int j = 0; j < n; j++) { if (j % 2 == 0 && sum + a[j] <= 0) { eve += abs(a[j] + sum) + 1; a[j] = abs(sum) + 1; } if (j % 2 == 1 && sum + a[j] >= 0) { eve += a[j] + sum + 1; a[j] = -abs(sum) - 1; } sum += a[j]; } sum = 0; int odd = 0; for (int k = 0; k < n; k++) { if (k % 2 == 0 && sum + b[k] >= 0) { odd += abs(b[k] + sum) + 1; b[k] = -abs(sum) - 1; } if (k % 2 == 1 && sum + b[k] <= 0) { odd += abs(sum + b[k]) + 1; b[k] = abs(sum) + 1; } sum += b[k]; } cout << min(odd, eve) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; long long sum = 0; long long ans = 0; int pre = 0; for (int i = 0; i < N; i++) { long long tmp; int next; cin >> tmp; sum += tmp; if (pre > 0) { if (sum < 0) ; else if (sum >= 0) { ans += (sum + 1); sum = -1; } } else if (pre < 0) { if (sum > 0) ; else if (sum <= 0) { ans += (-sum + 1); sum = 1; } } if (pre == 0) { if (sum > 0) next = 1; else next = -1; } else { next = -pre; } pre = next; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using 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<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; 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(void) { int n; cin >> n; ll a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } ll sum = 0L; int count = 0; int f, p, prev_p; sum = a[0]; if (sum > 0) { prev_p = 1; } else if (sum < 0) { prev_p = 0; } else { prev_p = 1; sum++; count++; } for (int i = 1; i < n; i++) { sum = sum + a[i]; if (sum > 0) { p = 1; } else if (sum < 0) { p = 0; } else { p = 1; sum++; count++; } if (prev_p == p) { if (sum < 0) { while (sum <= 0) { sum++; count++; p = 1; } } else { while (sum >= 0) { sum--; count++; p = 0; } } } prev_p = p; } 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
python3
n = int(input()) a = list([int(i) for i in input().split()]) sum_a = [] count = 0 sum_a.append(a[0]) print(a) for i in range(1,n): sum_a.append(sum_a[i-1] + a[i]) if sum_a[i] == 0: if sum_a[i-1] < 0: sum_a[i] += 1 count += 1 else: sum_a[i] -= 1 count += 1 elif sum_a[i-1] * sum_a[i]>0: if sum_a[i] > 0: sum_a[i] = -1 count += 1 + abs(sum_a[i-1]+a[i]) else: sum_a[i] = 1 count += 1 + abs(sum_a[i-1]+a[i]) print(sum_a) 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
import copy n = int(input()) a = [int(i) for i in input().split()] b=a.copy() s0p = a[0] s0n = b[0] countp = 0 countn = 0 if a.count(0)==n: print(2*n-1) exit() if s0p<=0: s0p+=(abs(s0p)+1) countp+=abs(s0p) if s0n>=0: s0n-=(abs(s0n)+1) countn+=abs(s0n) for i in range(1,n): s1 = s0p+a[i] if s0p*s1>=0: if s1>0: a[i]-=(abs(s1)+1) countp+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) countp+=(abs(s1)+1) elif s1==0: if s0p>0: a[i]-=1 countp+=1 elif s0p<0: a[i]+=1 countp+=1 s0p += a[i] for i in range(1,n): s1 = s0n+b[i] if s0n*s1>=0: if s1>0: b[i]-=(abs(s1)+1) countn+=(abs(s1)+1) elif s1<0: b[i]+=(abs(s1)+1) countn+=(abs(s1)+1) elif s1==0: if s0n>0: b[i]-=1 countn+=1 elif s0n<0: b[i]+=1 countn+=1 s0n += b[i] print(countp if countp<=countn else(countn))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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
python3
n = int(input()) a = list(map(int, input().split())) a1 = [a[0]] * n b = a[0] ans = 0 def f(x): if x == 0: return 0 else: return x // abs(x) for i in range(1, n): if a1[i - 1] * a[i] >= 0: a1[i] = -a[i] else: a1[i] = a[i] if b * (b + a1[i]) >= 0: a1[i] = -f(a1[i - 1]) - b if b + a1[i] == 0: a1[i] += f(a1[i]) ans += abs(a1[i] - a[i]) b += a1[i] a2 = [a[0]] * n b1 = a2[0] ans1 = abs(-f(a2[0]) - a2[0]) a2[0] = -f(a2[0]) for i in range(1, n): if a2[i - 1] * a[i] >= 0: a2[i] = -a[i] else: a2[i] = a[i] if b * (b + a2[i]) >= 0: a2[i] = -f(a2[i - 1]) - b1 if b1 + a2[i] == 0: a2[i] += f(a2[i]) ans1 += abs(a2[i] - a[i]) b1 += a2[i] print(min(ans1, 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
n = gets.to_i arr = gets.split.map(&:to_i) pre = arr[0] if pre == 0 if arr[1] >= 0 pre = -(arr[1] + 1) else pre = -(arr[1] - 1) end end count = 0 # binding.pry (arr.size - 1).times do |i| pre2 = arr[i + 1] if pre > 0 if pre + pre2 >= 0 pre2 = -(pre + 1) end pre += pre2 count += (pre2 - arr[i + 1]).abs elsif pre < 0 if pre + pre2 <= 0 pre2 = -(pre - 1) end pre += pre2 count += (pre2 - arr[i + 1]).abs end end puts count
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int ans1, ans2, sum1, sum2; int n, i; long long int a[100005]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } if (a[1] >= 0) { sum1 = a[1]; ans2 = a[1] + 1; sum2 = -1; } else { sum1 = 1; ans1 = abs(a[1]) + 1; sum2 = a[1]; } for (i = 2; i < n; i++) { if (sum1 > 0) { if (a[i] + sum1 >= 0) { ans1 += a[i] + sum1 + 1; sum1 = -1; } else { sum1 += a[i]; } } else { if (a[i] + sum1 <= 0) { ans1 += abs(sum1 + a[i]) + 1; sum1 = 1; } else { sum1 += a[i]; } } } if (sum1 + a[n] == 0) { ans1++; } for (i = 2; i < n; i++) { if (sum2 > 0) { if (a[i] + sum2 >= 0) { ans2 += a[i] + sum2 + 1; sum2 = -1; } else { sum2 += a[i]; } } else { if (a[i] + sum2 <= 0) { ans2 += abs(sum2 + a[i]) + 1; sum2 = 1; } else sum2 += a[i]; } } if (sum2 + a[n] == 0) { ans2++; } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sums[n], sum = 0; for (int i = 0; i < n; i++) { long long num; cin >> num; sum += num; sums[i] = sum; } int cnt = 0; sum = sums[0]; for (int i = 1; i < n; i++) { int add = 0; if (sum < 0) { if (sums[i] == 0) { add = 1; cnt++; } else if (sums[i] < 0) { add = sums[i] * (-1) + 1; cnt += add; } } else if (sum > 0) { if (sums[i] == 0) { add = -1; cnt++; } else if (sums[i] > 0) { add = sums[i] * (-1) - 1; cnt += add * (-1); } } for (int j = i; j < n; j++) { sums[j] += add; } sum = sums[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
n=int(input()) a=list(map(int, input().split())) fusei = 0 cnt = 0 if a[0] > 0: cnt = -1 fusei += abs(a[0])+1 flag = 1 for i in range(1, n): if flag == 1: ai = a[i] if ai > 0: if ai + cnt <= 0: fusei += abs(cnt) + 1 cnt = 1 else: cnt += ai else: fusei += abs(cnt)+1 cnt = 1 else: ai = a[i] if ai < 0: if ai + cnt >= 0: fusei += abs(cnt) + 1 cnt = -1 else: cnt += ai else: fusei += abs(cnt) +1 cnt = -1 seifu = 0 cnt = 0 if a[0] < 0: cnt = 1 seifu += abs(a[0])+1 flag = -1 for i in range(1, n): if flag == 1: ai = a[i] if ai > 0: if ai + cnt <= 0: seifu += abs(cnt) + 1 cnt = 1 else: cnt += ai else: seifu += abs(cnt)+1 cnt = 1 else: ai = a[i] if ai < 0: if ai + cnt >= 0: seifu += abs(cnt) + 1 cnt = -1 else: cnt += ai else: seifu += abs(cnt) +1 cnt = -1 print(min(fusei, seifu))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 ans = 0, c, n, count = 0, b = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> c; if (i == 0) { count = c; if (c < 0) b = 1; } else { count += c; if (b == 0) { while (count >= 0) { ans++; count--; } b = 1; } else { while (count <= 0) { ans++; count++; } b = 0; } } } 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
fun main() { val n = readLine()!!.toInt() val a = readLine()!!.split(" ").map { it.toLong() } var answer = 0L var total = 0L for (i in 0 until n) { val tmp = total total = total + a[i] if (total == 0L) { if (tmp > 0) { answer += 1 total = -1 } else if (tmp < 0) { answer += 1 total = 1 } else if (tmp == 0L) { //i ==0 answer += 1 total = getTotal(a) } continue } if (tmp > 0 && total > 0) { answer += (total + 1) total = -1 } else if (tmp < 0 && total < 0) { answer += (-total + 1) total = 1 } } println(answer) } fun getTotal(a: List<Long>): Long { for (x in a) { if (x > 0) { return -1 } else if (x < 0) { return 1 } } return 1 }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[100005]; long long sum, ans = 100000000, res, n; int main() { bool flag = 0; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { sum += a[i]; if (!flag) { if (sum <= 0) { res += 1 - sum; sum = 1; } } else { if (sum >= 0) { res += sum + 1; sum = -1; } } flag ^= 1; } if (res < ans) ans = res; flag = 0, res = sum = 0; for (int i = 1; i <= n; i++) { sum += a[i]; if (!flag) { if (sum >= 0) { res += sum + 1; sum = -1; } } else { if (sum <= 0) { res += 1 - sum; sum = 1; } } flag ^= 1; } if (res < ans) ans = res; 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(void) { int n; cin >> n; int* a = new int[n]; bool flg = false; long long sum = 0; int cnt = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; if (i == 0) { if (a[0] < 0) flg = false; else flg = true; } sum += a[i]; if (flg == false && i % 2 == 0 && sum >= 0) { while (sum >= 0) { --sum; ++cnt; } } else if (flg == false && i % 2 == 1 && sum <= 0) { while (sum <= 0) { ++sum; ++cnt; } } else if (flg == true && i % 2 == 0 && sum <= 0) { while (sum <= 0) { ++sum; ++cnt; } } else if (flg == true && i % 2 == 1 && sum >= 0) { while (sum >= 0) { --sum; ++cnt; } } } 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 copy n = int(input()) a = list(map(int, input().split())) def judge_pm(a,b): if a*b<0: return True else: return False a1 = copy.deepcopy(a) tmp_sum = a1[0] operate_num1 = 0 for i in range(1, n): if judge_pm(tmp_sum, tmp_sum+a1[i]): pass elif tmp_sum<0: tmp_operate_num = - tmp_sum + 1 - a1[i] operate_num1 += tmp_operate_num a1[i] += tmp_operate_num else: tmp_operate_num = tmp_sum + 1 + a1[i] operate_num1 += tmp_operate_num a1[i] -= tmp_operate_num tmp_sum += a1[i] a2 = copy.deepcopy(a) operate_num2 = 0 if a2[0]<0: operate_num2 += min(-a2[0] - a[1] + 1, -a[0] + 1) a2[0] += operate_num2 else: operate_num2 += min(a2[0] + a[1] - 1, a[0] - 1) a2[0] -= operate_num2 tmp_sum = a2[0] for i in range(1, n): if judge_pm(tmp_sum, tmp_sum+a2[i]): pass elif tmp_sum<0: tmp_operate_num = - tmp_sum + 1 - a2[i] operate_num2 += tmp_operate_num a2[i] += tmp_operate_num else: tmp_operate_num = tmp_sum + 1 + a2[i] operate_num2 += tmp_operate_num a2[i] -= tmp_operate_num tmp_sum += a2[i] print(min(operate_num1, operate_num2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int VX[] = {0, 1, 0, -1}; const int VY[] = {1, 0, -1, 0}; const long long MOD = pow(10, 9) + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, cost = 0; cin >> n; vector<int> a(n); for (int i = (0); i < (n); i++) cin >> a[i]; bool sign = a[0] > 0; for (int i = (1); i < (n); i++) { sign = !sign; a[i] += a[i - 1]; if (sign) { if (a[i] <= 0) { while (a[i] != 1) { a[i]++; cost++; } } } else { if (a[i] >= 0) { while (a[i] != -1) { a[i]--; cost++; } } } } 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; cin >> n; int* all = new int[n]; for (int i = 0; i < n; i++) cin >> all[i]; long long ans = 0; int sum = 0; int last = 0; long long ans2 = 0; int sum2 = 0; int last2 = 0; bool flag = false; if (all[0] > 0) last = -1; else if (all[0] < 0) last = 1; else { last = -1; all[0] = 1; ans++; flag = true; } for (int i = 0; i < n; i++) { sum += all[i]; if (last == 1) { if (sum >= 0) { ans += sum + 1; sum = -1; } last = -1; } else if (last == -1) { if (sum <= 0) { ans += -1 * sum + 1; sum = 1; } last = 1; } } if (all[0] > 0) { if (flag) { last2 = -1; ans2++; all[0] = -1; } else { last2 = 1; ans2 += all[0] + 1; all[0] = -1; } } else if (all[0] < 0) { last2 = -1; ans2 += all[0] * -1 + 1; all[0] = 1; } for (int i = 0; i < n; i++) { sum2 += all[i]; if (last2 == 1) { if (sum2 >= 0) { ans2 += sum2 + 1; sum2 = -1; } last2 = -1; } else if (last2 == -1) { if (sum2 <= 0) { ans2 += -1 * sum2 + 1; sum2 = 1; } last2 = 1; } } if (ans < ans2) cout << ans; else cout << ans2; system("pause"); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int [n]; for(int i = 0;i < n;i++){ a[i] = sc.nextInt(); } int[] sum = new int[n]; sum[0] = a[0]; int count = 0; count = Math.min(solve1(sum,a,count),solve2(sum,a,count)); System.out.println(count); } public static int solve1(int[] sum,int[] a,int count){ if(sum[0] == 0){ count++; sum[0] = 1; } for(int i = 0;i < sum.length-1;i++){ sum[i+1] = sum[i] + a[i+1]; if((i+1) % 2 == 1){ if(sum[i+1] >= 0){ count += 1 + sum[i+1]; sum[i+1] = -1; } } if((i+1) % 2 == 0){ if(sum[i+1] <= 0){ count += 1 - sum[i+1]; sum[i+1] = 1; } } } return count; } public static int solve2(int[] sum,int[] a,int count){ if(sum[0] == 0){ count++; sum[0] = -1; } for(int i = 0;i < sum.length-1;i++){ sum[i+1] = sum[i] + a[i+1]; if((i+1) % 2 == 1){ if(sum[i+1] <= 0){ count += 1 - sum[i+1]; sum[i+1] = 1; } } if((i+1) % 2 == 0){ if(sum[i+1] >= 0){ count += 1 + sum[i+1]; sum[i+1] = -1; } } } 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
python3
n = int(input()) a = [int(i) for i in input().split()] cur = 0 ans1, ans2 = 0,0 for i, x in enumerate(a): cur += x if i%2 == 0: if cur >= 1: continue else: ans1 += abs(1-x) else: if cur <= -1: continue else: ans1 += abs(-1-x) for i, x in enumerate(a): cur += x if i%2 != 0: if cur >= 1: continue else: ans2 += abs(1-x) else: if cur <= -1: continue else: ans2 += abs(-1-x) print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a.at(i); int64_t sumi = 0, val1 = 0; int ne = 0; if (a.at(0) == 0) { while (a.at(ne) == 0) { if (ne == 0) val1++; else val1 += 2; ne++; if (ne == n) break; } } int64_t val2 = val1; for (int i = ne; i < n;) { if (a.at(0) == 0 && sumi == 0) { if (ne % 2 == 0) sumi = -1; else sumi = 1; i++; } if (i % 2 == 1) { if (sumi + a.at(i) < 0) sumi += a.at(i); else { val1 += (sumi + a.at(i) + 1); sumi = -1; } } else { if (sumi + a.at(i) > 0) sumi += a.at(i); else { val1 += (abs(sumi + a.at(i)) + 1); sumi = 1; } } } sumi = 0; for (int i = ne; i < n;) { if (a.at(0) == 0 && sumi == 0) { if (ne % 2 == 0) sumi = 1; else sumi = -1; i++; } if (i % 2 == 1) { if (sumi + a.at(i) > 0) sumi += a.at(i); else { val2 += (abs(sumi + a.at(i)) + 1); sumi = 1; } } else { if (sumi + a.at(i) < 0) sumi += a.at(i); else { val2 += (sumi + a.at(i) + 1); sumi = -1; } } } cout << min(val1, val2) << 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(); } sc.close(); // +-+-+...の場合 long sum1 = a[0]; long count1 = 0; for (int i = 1; i < n; i++) { sum1 += a[i]; if (i % 2 == 0) { if (sum1 <= 0) { count1 += 1 - sum1; sum1 = 1; } } else { if (0 <= sum1) { count1 += sum1 + 1; sum1 = -1; } } } // -+-+_...の場合 long sum2 = a[0]; long count2 = 0; for (int i = 1; i < n; i++) { sum2 += a[i]; if (i % 2 == 0) { if (0 <= sum2) { count2 += sum2 + 1; sum2 = -1; } } else { if (sum2 <= 0) { count2 += 1 - sum2; sum2 = 1; } } } System.out.println(Math.min(count1, count2)); } }