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
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using std::abs; using std::array; using std::cerr; using std::cin; using std::cout; using std::generate; using std::make_pair; using std::map; using std::max; using std::max_element; using std::min; using std::min_element; using std::pair; using std::reverse; using std::set; using std::sort; using std::string; using std::unique; using std::vector; template <typename T> T input() { T res; cin >> res; {}; return res; } template <typename IT> void input_seq(IT b, IT e) { std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>); } string solve(int a, int b, int c) { string base = "a"; while (c >= a) base += 'c', c -= a; while (b >= a) base += 'b', b -= a; if (b == 0 and c == 0) { string res = ""; for (int t = 0; t != a; ++t) res += base; return res; } if (b + c >= a) { vector<string> lst; int count = 0; while (c != 0) { c -= 1, count += 1; lst.push_back(base + 'c'); } while (count != a) { b -= 1, count += 1; lst.push_back(base + 'b'); } while (b != 0) lst.push_back("b"), b -= 1; std::sort(lst.begin(), lst.end()); string res = lst[0]; for (int i = int((lst).size()) - 1; i >= 0; --i) res += lst[i]; return res; } else { int num = (a + (b + c - 1)) / (b + c); int free = num * (b + c) - a; string tmp = ""; for (int i = 0; i != num - 1; ++i) tmp += base; vector<string> parts; for (int i = 0; i != (b + c - free); ++i) parts.push_back(tmp + base); for (int i = 0; i != free; ++i) parts.push_back(tmp); for (int i = 0; i != c; ++i) parts[i] += 'c'; for (int i = 0; i != b; ++i) parts[c + i] += 'b'; std::sort(parts.begin(), parts.end()); vector<string> cparts = parts; cparts.resize(std::unique(parts.begin(), parts.end()) - parts.begin()); while (cparts.size() < 3) cparts.push_back(""); assert(int((cparts).size()) == 3); string zzans = solve(std::count(parts.begin(), parts.end(), cparts[0]), std::count(parts.begin(), parts.end(), cparts[1]), std::count(parts.begin(), parts.end(), cparts[2])); string ans = ""; for (char ch : zzans) ans += cparts[ch - 'a']; return ans; } } int main() { std::iostream::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int a = input<int>(); int b = input<int>(); int c = input<int>(); std::map<char, char> back; if (a == 0 and b == 0) { back['a'] = 'c'; a = c; b = 0; c = 0; } else if (a == 0) { back['a'] = 'b'; back['b'] = 'c'; a = b; b = c; c = 0; } else { back['a'] = 'a'; back['b'] = 'b'; back['c'] = 'c'; } string ans = solve(a, b, c); for (int i = 0; i != int((ans).size()); ++i) cout << back[ans[i]]; cout << "\n"; return 0; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int x, y, z; pair<int, int> ans; string a, b, c; void calc(int x, int y, int z) { if (x == 0 && z == 0) { ans.first = 2, ans.second = y; return; } if (x == 0 && y == 0) { ans.first = 3, ans.second = z; return; } if (y == 0 && z == 0) { ans.first = 1, ans.second = x; return; } if (x <= z) { for (auto it : c) a.push_back(it); calc(x, y, z - x); } else { string tmp = c; c = b; b.clear(); for (auto it : a) b.push_back(it); for (auto it : tmp) b.push_back(it); calc(x - z, z, y); } } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); a.clear(), b.clear(), c.clear(); cin >> x >> y >> z; a.push_back('a'), b.push_back('b'), c.push_back('c'); calc(x, y, z); for (int i = 0; i < ans.second; i++) { if (ans.first == 1) cout << a; else if (ans.first == 2) cout << b; else cout << c; } cout << '\n'; return 0; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int MAXN = 4005; int x, y, z; string s; int dp[51][51][51][51]; int link[51][3]; int fail[51]; bool rend[MAXN]; bool f(int x, int y, int z, int m) { if (m == s.size()) m = fail[m]; if (x + y + z == 0) return rend[m]; if (~dp[x][y][z][m]) return dp[x][y][z][m]; int ret = 0; if (link[m][0] != -1 && x && f(x - 1, y, z, link[m][0])) ret = 1; if (link[m][1] != -1 && y && f(x, y - 1, z, link[m][1])) ret = 1; if (link[m][2] != -1 && z && f(x, y, z - 1, link[m][2])) ret = 1; return dp[x][y][z][m] = ret; } bool trial() { int p = 0; for (int i = 1; i < s.size(); i++) { while (p && s[i] != s[p]) p = fail[p]; if (s[i] == s[p]) p++; fail[i + 1] = p; } for (int i = 0; i < s.size(); i++) { for (int j = 0; j < 3; j++) { int pos = i; if (s[i] > j + 'a') { link[i][j] = -1; continue; } while (pos && s[pos] != j + 'a') pos = fail[pos]; if (s[pos] == j + 'a') pos++; link[i][j] = pos; } } for (int i = 0; i < s.size(); i++) { int pos = i; rend[i] = 1; for (int j = 0; j < s.size(); j++) { pos = link[pos][s[j] - 'a']; if (pos == -1) { rend[i] = 0; break; } } } memset(dp, -1, sizeof(dp)); return f(x, y, z, 0); } int main() { cin >> x >> y >> z; while (s.size() < x + y + z) { s.push_back('c'); if (trial()) continue; s.back() = 'b'; if (trial()) continue; s.back() = 'a'; } cout << s << endl; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.*; class Main { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = new String(in.readLine()); String[] abc = s.split(","); // 整数の入力 int a = Integer.parseInt(abc[0]); // スペース区切りの整数の入力 int b = Integer.parseInt(abc[1]); // スペース区切りの整数の入力 int c = Integer.parseInt(abc[2]); StringBuilder buff = new StringBuilder(); boolean flag = true; while(flag) { if(a!=0) { buff.append("a"); a--; }else if(c!=0) { buff.append("c"); }else if(b!=0) { buff.append("b"); } else { System.out.println(buff); flag = false; } } } }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int x, y, z, tmp, t; vector<char> V[55]; int main() { scanf("%d%d%d", &x, &y, &z); if (x == 0 && y == 0) { for (int i = 1; i <= z; i++) cout << 'c' << endl; return 0; } if (x == 0) { for (int i = 1; i <= y; i++) { V[i].push_back('b'); } tmp = y; for (int i = z; i >= 1; i--) { V[tmp--].push_back('c'); if (tmp == 0) tmp = y; } for (int i = 1; i <= y; i++) { for (int j = 0; j < V[i].size(); j++) { cout << V[i][j]; } } cout << endl; return 0; } for (int i = 1; i <= x; i++) { V[i].push_back('a'); } tmp = x; for (int i = z; i >= 1; i--) { V[tmp--].push_back('c'); if (tmp == 0) tmp = x; } t = tmp; for (int i = y; i >= 1; i--) { V[tmp--].push_back('b'); if (tmp == 0) tmp = t; } for (int i = 1; i <= x; i++) { for (int j = 0; j < V[i].size(); j++) { cout << V[i][j]; } } cout << endl; return 0; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int x, y, z; string s[100]; int main() { scanf("%d%d%d", &x, &y, &z); if (!x && !y) { for (int i = 1; i <= z; ++i) { printf("%c", 'c'); } } else if (!x) { for (int i = 1; i <= y; ++i) s[i] = 'b'; for (; z;) { for (int i = y; i; --i) if (z) { s[i] = s[i] + 'c'; --z; } } for (int i = 1; i <= y; ++i) for (int j = 0; j <= (int)s[i].length() - 1; ++j) { printf("%c", s[i][j]); } } else { for (int i = 1; i <= x; ++i) s[i] = 'a'; for (; z;) { for (int i = x; i; --i) if (z) { s[i] = s[i] + 'c'; --z; } } int k = x; for (int i = 1; i <= x - 1; ++i) if (s[i].length() != s[i + 1].length()) { k = i; break; } for (; y;) { for (int i = k; i; --i) if (y) { s[i] = s[i] + 'b'; --y; } } for (int i = 1; i <= x; ++i) for (int j = 0; j <= (int)s[i].length() - 1; ++j) { printf("%c", s[i][j]); } } return 0; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } long long a, b, c; long long t[3]; vector<long long> res; void licz(long long ak) { if (((long long)(res).size()) == a + b + c) { for (long long el : res) cout << char(el + 'a'); cout << "\n"; exit(0); } for (long long i = 2; i >= res[ak]; i--) { if (t[i]) { t[i]--; res.push_back(i); licz(i == res[ak] ? ak + 1 : 0); res.pop_back(); t[i]++; } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); if (0) cout << fixed << setprecision(6); cin >> a >> b >> c; t[0] = a; t[1] = b; t[2] = c; for (long long i = 0; i < 3; i++) { if (t[i]) { res.push_back(i); t[i]--; break; } } licz(0); }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; string slow(int ca, int cb, int cc) { int n = ca + cb + cc; string s = ""; for (int i = 0; i < ca; i++) s += 'a'; for (int i = 0; i < cb; i++) s += 'b'; for (int i = 0; i < cc; i++) s += 'c'; string ans = s; do { string mn = s; for (int i = 0; i < n; i++) { string t = s; rotate(t.begin(), t.begin() + i, t.end()); mn = min(mn, t); } ans = max(ans, mn); } while (next_permutation(s.begin(), s.end())); return ans; } string lexmin(string s) { string mn = s; for (int i = 0; i < (int)s.length(); i++) { string t = s; rotate(t.begin(), t.begin() + i, t.end()); mn = min(mn, t); } return mn; } string solve(vector<string> v) { sort(v.begin(), v.end()); int n = v.size(); if (v[0] == v[n - 1]) { string s = ""; for (auto t : v) s += t; return lexmin(s); } map<string, int> mp; for (auto s : v) mp[s]++; vector<pair<string, int> > b; for (auto it : mp) { b.push_back(it); } vector<string> nv; vector<string> cur(b[0].second, b[0].first); for (int i = (int)b.size() - 1; i >= 1; i--) { for (int j = 0; j < b[i].second; j++) { cur[j % (int)cur.size()] += b[i].first; } if (b[i].second % cur.size()) { int rem = b[i].second % cur.size(); while (cur.size() > rem) { nv.push_back(*cur.rbegin()); cur.pop_back(); } } } for (auto s : cur) nv.push_back(s); return solve(nv); } string fast(int ca, int cb, int cc) { bool f = 0; if (ca == 0) { f = 1; ca = cb; cb = cc; cc = 0; } if (ca == 0) { return string(cb, 'c'); } int n = ca + cb + cc; vector<string> a(ca, "a"); for (int i = 0; i < cc; i++) { a[i % ca] += "c"; } for (int i = 0; i < cb; i++) { a[(i + cc) % (ca - cc % ca) + cc % ca] += "b"; } sort(a.begin(), a.end()); string ans = solve(a); while (1) { string s = ""; random_shuffle(a.begin(), a.end()); for (string ss : a) s += ss; string mn = s; for (int i = 0; i < n; i++) { string t = s; rotate(t.begin(), t.begin() + i, t.end()); mn = min(mn, t); } ans = max(ans, mn); if (clock() / (double)CLOCKS_PER_SEC > 1.9) break; } if (f) { for (char &c : ans) { if (c == 'a') c = 'b'; else c = 'c'; } } return ans; } int main() { int ca, cb, cc; while (cin >> ca >> cb >> cc) { cout << fast(ca, cb, cc) << endl; } return 0; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 55; string res; bool vis[maxn][maxn][maxn][maxn]; int failure[maxn]; void push() { memset(failure, 0, sizeof(failure)); for (int s = 2; s <= res.size(); s++) { failure[s] = failure[s - 1]; while (failure[s] > 0 && res[failure[s]] != res[s - 1]) failure[s] = failure[failure[s]]; if (res[failure[s]] == res[s - 1]) ++failure[s]; } } bool solve(int qx, int qy, int qz, int pos) { if (qx + qy + qz == 0) { string pref; for (int e = 0; e < pos; e++) pref.push_back(res[e]); pref += res; for (int e = 0; e < res.size(); e++) { if (pref[e] < res[e]) return false; if (pref[e] > res[e]) return true; } return true; } if (vis[qx][qy][qz][pos]) return false; vis[qx][qy][qz][pos] = 1; if (qz > 0) { int nxt = pos; if (nxt == res.size()) nxt = failure[nxt]; while (nxt > 0 && 'c' != res[nxt]) nxt = failure[nxt]; if (res[nxt] == 'c') nxt++; if (solve(qx, qy, qz - 1, nxt)) return true; } if (res[pos] != 'c' && qy > 0) { int nxt = pos; if (nxt == res.size()) nxt = failure[nxt]; while (nxt > 0 && 'b' != res[nxt]) nxt = failure[nxt]; if (res[nxt] == 'b') nxt++; if (solve(qx, qy - 1, qz, nxt)) return true; } if (res[pos] != 'c' && res[pos] != 'b' && qx > 0) { int nxt = pos; if (nxt == res.size()) nxt = failure[nxt]; while (nxt > 0 && 'a' != res[nxt]) nxt = failure[nxt]; if (res[nxt] == 'a') nxt++; if (solve(qx - 1, qy, qz, nxt)) return true; } return false; } int M() { int x, y, z; if (!(cin >> x >> y >> z)) return 0; int len = x + y + z; res.clear(); for (int e = 0; e < len; e++) { { if (z > 0) { res.push_back('c'); push(); memset(vis, false, sizeof(vis)); if (solve(x, y, z - 1, 0)) { z--; continue; } res.pop_back(); } } { if (y > 0) { res.push_back('b'); push(); memset(vis, false, sizeof(vis)); if (solve(x, y - 1, z, 0)) { y--; continue; } res.pop_back(); } } { if (x > 0) { res.push_back('a'); push(); memset(vis, false, sizeof(vis)); if (solve(x - 1, y, z, 0)) { x--; continue; } res.pop_back(); } } assert(0); } cout << res << endl; return 1; } int main() { while (M()) ; return 0; }
p03582 CODE FESTIVAL 2017 qual B - Largest Smallest Cyclic Shift
For a string S, let f(S) be the lexicographically smallest cyclic shift of S. For example, if S = `babca`, f(S) = `ababc` because this is the smallest among all cyclic shifts (`babca`, `abcab`, `bcaba`, `cabab`, `ababc`). You are given three integers X, Y, and Z. You want to construct a string T that consists of exactly X `a`s, exactly Y `b`s, and exactly Z `c`s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T). Constraints * 1 \leq X + Y + Z \leq 50 * X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Examples Input 2 2 0 Output abab Input 1 1 1 Output acb
{ "input": [ "2 2 0", "1 1 1" ], "output": [ "abab", "acb" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } string solve(int na, int nb, int nc) { if (na == 0 && nb == 0 && nc == 0) return ""; if (na == 0) { string ret = solve(nb, nc, 0); for (int i = (0); i < (((int)(ret).size())); ++i) ++ret[i]; return ret; } vector<string> parts; string cur = "a"; int curcnt = na; na = 0; while (na + nb + nc > 0) { if (nc >= curcnt) { cur += "c"; nc -= curcnt; continue; } while (nc > 0) parts.push_back(cur + "c"), --curcnt, --nc; if (nb >= curcnt) { cur += "b"; nb -= curcnt; continue; } while (nb > 0) parts.push_back(cur + "b"), --curcnt, --nb; } while (((int)(parts).size()) > 0) { int rep = curcnt / ((int)(parts).size()), rem = curcnt % ((int)(parts).size()); if (rem == 0) --rep, rem = ((int)(parts).size()); string pref; for (int i = (0); i < (rep); ++i) pref += cur; string nxt = pref + cur + parts[rem - 1]; int nxtcnt = 1; while (nxtcnt < rem && parts[rem - 1] == parts[rem - 1 - nxtcnt]) ++nxtcnt; vector<string> nparts; for (int i = (0); i < (rem - nxtcnt); ++i) nparts.push_back(pref + cur + parts[i]); for (int i = (rem); i < (((int)(parts).size())); ++i) nparts.push_back(pref + parts[i]); parts = nparts; cur = nxt; curcnt = nxtcnt; } string ret; for (int i = (0); i < (curcnt); ++i) ret += cur; return ret; } void run() { int na, nb, nc; scanf("%d%d%d", &na, &nb, &nc); string s = solve(na, nb, nc); printf("%s\n", s.c_str()); } int main() { run(); 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": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) u=0 s=1 x=0 y=0 for i in a: u+=i if s*u<=0: x+=1-s*u u=s s=s*(-1) s=-1 u=0 for i in a: u+=i if s*u<=0: y+=1-s*u u=s s=-1*s print(min(x,y))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; int main(){ int n; cin >> n; vector<long> a(n); for(int i=0;i<n;i++) cin >> a[i]; long odd=0,even=0,sum=0; for(int i=0;i<n;i++){ //+- sum+=a[i]; if(i%2==0&&sum<=0){ odd+=abs(sum)+1; sum=+1; } else if(i%2==1&&sum>=0){ odd+=abs(sum)+1; sum=-1; } } sum=0; for(int i=0;i<n;i++){ //-+ sum+=a[i]; if(i%2==1&&sum<=0){ even+=abs(sum)+1; sum=+1; } else if(i%2==0&&sum>=0){ even+=abs(sum)+1; sum=-1; } } cout << min(odd,even) << 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": [] }
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<<mn<<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": [] }
CORRECT
python3
n=int(input()) A=list(map(int,input().split())) def chk(A,t): ans=0 s=0 for i in A: s+=i if t==True and s<1: ans+=1-s s=1 elif t==False and s>-1: ans+=1+s s=-1 t=not t return ans print(min(chk(A,True),chk(A,False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 10 ** 18 for z in range(2): if z == 0: sign = 1 else: sign = -1 c = 0 cost = 0 for x in a: c += x if c * sign <= 0: cost += 1 - c * sign c = sign sign *= -1 ans = min(ans, cost) 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": [] }
CORRECT
python3
N = int(input()) A = list(map(int, input().split())) s = max(A[0], +1) ans_p = abs(A[0]-s) for a in A[1:]: if s>0: b=min(a, -s-1) if s<0: b=max(a, -s+1) s += b ans_p += abs(a-b) s = min(A[0], -1) ans_n = abs(A[0]-s) for a in A[1:]: if s>0: b=min(a, -s-1) if s<0: b=max(a, -s+1) s += b ans_n += abs(a-b) print(min(ans_p, ans_n))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ ll n,ce=0,cf=0; cin>>n; ll a[n],e[n],f[n]; cin>>a[0]; e[0]=a[0]; f[0]=a[0]; if(e[0]<1){ e[0]=1; ce+=1-a[0]; } if(f[0]>-1){ f[0]=-1; cf+=a[0]+1; } for(int i=1;i<n;i++){ cin>>a[i]; e[i]=e[i-1]+a[i]; f[i]=f[i-1]+a[i]; if(i%2==1){ if(e[i]>-1){ ce+=e[i]+1; e[i]=-1; } if(f[i]<1){ cf+=1-f[i]; f[i]=1; } }else{ if(f[i]>-1){ cf+=f[i]+1; f[i]=-1; } if(e[i]<1){ ce+=1-e[i]; e[i]=1; } } } cout<<min(ce,cf); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.nio.charset.IllegalCharsetNameException; import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } long oddsum = 0; long oddcount = 0; for (int i = 0; i < n / 2; i++) { oddsum += a[2 * i]; if (oddsum <= 0) { oddcount += Math.abs(1 - oddsum); oddsum = 1; } oddsum += a[2 * i + 1]; if (oddsum >= 0) { oddcount += Math.abs(-1 - oddsum); oddsum = -1; } } long evensum = 0; long evencount = 0; for (int i = 0; i < n / 2; i++) { evensum += a[2 * i]; if (evensum >= 0) { evencount += Math.abs(-1 - evensum); evensum = -1; } evensum += a[2 * i + 1]; if (evensum <= 0) { evencount += Math.abs(1 - evensum); evensum = 1; } } if (n % 2 != 0) { oddsum += a[n - 1]; if (oddsum <= 0) { oddcount += Math.abs(1 - oddsum); oddsum = 1; } evensum += a[n - 1]; if (evensum >= 0) { evencount += Math.abs(-1 - evensum); evensum = -1; } } System.out.println(Math.min(oddcount, evencount)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
import sys input = sys.stdin.readline n=int(input()) a=list(map(int,input().split())) v=[] for s in (1,-1): x=a[::] hugou=[s*(-1)**i for i in range(n)] total=0 for i in range(n): if (total+x[i])*hugou[i]<=0: x[i]=hugou[i]-total total+=x[i] v.append(sum(abs(a[i]-x[i]) for i in range(n))) print(min(v))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) A=list(map(int,input().split())) def solve(a,t): ans,x=0,0 for i in a: x+=i if t==True and x<1: ans+=1-x x=1 elif t==False and x>-1: ans+=x+1 x=-1 t=not t return ans print(min(solve(A,True),solve(A,False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] a = new int[n]; for (int i = 0; i < n; i++) { int temp = Integer.parseInt(sc.next()); a[i] = temp; } long ans1 = 0; long ans2 = 0; long temp = 0; for (int i = 0; i < n; i++) { long num = a[i]; if (i % 2 == 0 && temp + num >= 0) { ans1 += temp + num + 1; num -= temp + num + 1; } if (i % 2 != 0 && temp + num <= 0) { ans1 += Math.abs(temp + num - 1); num += Math.abs(temp + num - 1); } temp += num; } temp = 0; for (int i = 0; i < n; i++) { long num = a[i]; if (i % 2 == 0 && temp + num <= 0) { ans2 += Math.abs(temp + num - 1); num += Math.abs(temp + num - 1); } if (i % 2 != 0 && temp + num >= 0) { ans2 += temp + num + 1; num -= temp + num + 1; } temp += num; } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
// C - Sequence #include <bits/stdc++.h> using namespace std; using ll = long long; #define rp(i,n) for(int i=0;i<(n);i++) ll counter(vector<ll> &A, int N, int sign){//sign: (+)start -> 1, (-)start -> -1 ll c = 0, s = 0; rp(i,N){ s += A[i]; if(sign*s<=0){ c += abs(s) + 1; s = sign; } sign *= -1; } return c; } int main(){ int N; cin>>N; vector<ll> A(N); rp(i,N) cin>>A[i]; ll a = counter(A,N, 1); ll b = counter(A,N,-1); cout<< (a<b?a:b) <<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": [] }
CORRECT
cpp
// C - Sequence #include <bits/stdc++.h> using namespace std; using ll = long long; #define up(i,s,e,d) for(int i=(s);i<(e);i+=(d)) int main(){ int N; cin>>N; vector<ll> A(N); ll csum = 0; ll a = 0; up(i,0,N,1){ cin>>A[i]; csum += A[i]; if(i%2==0 && csum<=0){//even + a += abs(csum) + 1; csum = 1; } if(i%2==1 && csum>=0){//odd - a += abs(csum) + 1; csum = -1; } } csum = 0; ll b = 0; up(i,0,N,1){ csum += A[i]; if(i%2==1 && csum<=0){//odd + b += abs(csum) + 1; csum = 1; } if(i%2==0 && csum>=0){//even - b += abs(csum) + 1; csum = -1; } } cout<< (a<b?a:b) <<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": [] }
CORRECT
python3
def F(list_A, sign): total = 0 cnt = 0 for a in list_A: total += a if sign==True and total < 1: cnt += 1- total total = 1 elif sign==False and total > -1: cnt += total + 1 total = -1 sign = not sign return cnt N = int(input()) A = list(map(int, input().split())) ans = min(F(A, True), F(A, False)) 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": [] }
CORRECT
java
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; long[] s = new long[n]; for(int i = 0; i < n; i++){ a[i] = sc.nextLong(); if(i == 0){ s[0] = a[0]; } else { s[i] = s[i-1]+a[i]; } } sc.close(); long cnt1 = 0; long cnt2 = 0; long diff1 = 0; long diff2 = 0; for(int i = 0; i < n; i++){ if(i%2 == 0){ if(s[i]+diff1 <= 0){ cnt1 += 1-s[i]-diff1; diff1 += 1-s[i]-diff1; } } else { if(s[i]+diff1 >= 0){ cnt1 += s[i] + diff1 + 1; diff1 += -1-s[i]-diff1; } } } for(int i = 0; i < n; i++){ if(i%2 == 0){ if(s[i]+diff2 >= 0){ cnt2 += s[i] + diff2 + 1; diff2 += -1-s[i]-diff2; } } else { if(s[i]+diff2 <= 0){ cnt2 += 1-s[i]-diff2; diff2 += 1-s[i]-diff2; } } } System.out.println(Math.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": [] }
CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] nums = new long[n]; for(int i = 0; i < n; i++) { nums[i] = sc.nextLong(); } long[] prefixSum = new long[n]; long minCost = 0; if(nums[0] == 0) { nums[0] = 1; minCost = 1 + count(nums); nums[0] = -1; minCost = Math.min(minCost, 1 + count(nums)); } else { minCost = count(nums); long org = nums[0]; if(org > 0) { nums[0] = -1; minCost = Math.min(minCost,count(nums) + org + 1); } else { nums[0] = 1; minCost = Math.min(minCost,count(nums) + 1 - org); } } System.out.println(minCost); } private static long count(long[] nums) { long cnt = 0; long prev = nums[0]; long cur = 0; for(int i = 1; i < nums.length; i++) { cur = nums[i] + prev; if(cur * prev < 0) { prev = cur; continue; } if(prev > 0) { cnt += cur + 1; prev = -1; } else { cnt += 1 - cur; prev = 1; } } return 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": [] }
CORRECT
python3
import copy n = int(input()) *A, = map(int, input().split()) def solve(p): r = 0 t = 0 for a in A: t += a if t * p <= 0: r += abs(p - t) t = p p *= -1 return r print(min(solve(1), solve(-1)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #if __has_include("print.hpp") #include "print.hpp" #endif #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() typedef long long ll; typedef pair<int, int> p; int n; vector<int> v; ll calc(ll s) { ll sum = 0, res = 0; for (int i = 0; i < n; ++i, s *= -1) { sum += v[i]; if (sum * s > 0) continue; res += abs(sum - s); sum += s * abs(sum - s); } return res; } int main(){ cin >> n; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; v.push_back(tmp); } cout << min(calc(1), calc(-1)) << 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": [] }
CORRECT
cpp
#include <iostream> #include <algorithm> using namespace std; int main(void){ int n,i,a[100010]; long s=0,h1=0,h2=0; cin >> n; for (i=0;i<n;i++) cin >> a[i]; for (i=0;i<n;i++){ s+=a[i]; if (i%2 && s>=0){ h1+=s+1; s=-1; } if (i%2==0 && s<=0){ h1+=1-s; s=1; } } s=0; for (i=0;i<n;i++){ s+=a[i]; if (i%2==0 && s>=0){ h2+=s+1; s=-1; } if (i%2 && s<=0){ h2+=1-s; s=1; } } cout << min(h1,h2) << 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": [] }
CORRECT
python3
n=int(input()) a=[int(x) for x in input().split()] def arrange(a0_is_plus): r=0 if a0_is_plus==True else 1 sumNum=0 ans=0 for i,an in enumerate(a): sumNum+=an if i%2==r and sumNum<=0: ans+=1-sumNum sumNum=1 elif i%2!=r and sumNum>=0: ans+=sumNum+1 sumNum=-1 return ans print(min(arrange(True),arrange(False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = list(map(int,input().split())) ans = 10**15 for s in [1,-1]: cos = 0 tot = 0 for a in A: tot+=a if s*tot<=0: cos+=abs(tot-s) tot=s s*=-1 ans=min(ans,cos) 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": [] }
CORRECT
cpp
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[100010],n; ll ans(int hugo){ ll sum=0,ANS=0; for(int i=0;i<n;i++){ sum+=a[i]; if(hugo==-1){ hugo=1; if(sum>=0){ ANS+=abs(sum)+1; sum=-1; } } else if(hugo==1){ hugo=-1; if(sum<=0){ ANS+=abs(sum)+1; sum=1; } } } return ANS; } int main() { cin>>n; for(int i=0;i<n;i++){ int z; cin>>z; a[i]=z; } cout << min(ans(-1),ans(1)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int[] ar = new int[n]; for (int i=0; i<n; i++) ar[i] = sc.nextInt(); //偶数項を正としたときの操作回数minA long minA = 0; long sumA = 0; for (int i=0; i<n; i++) { sumA += ar[i]; if (i%2 == 0) { if (sumA <= 0) { minA += Math.abs(sumA) + 1; sumA += Math.abs(sumA) + 1; } } else { //sumA - if (sumA >= 0) { minA += Math.abs(sumA) + 1; sumA -= Math.abs(sumA) + 1; } } } //奇数項を正としたときの操作回数minB long minB = 0; long sumB = 0; for (int i=0; i<n; i++) { sumB += ar[i]; if (i%2 != 0) { if (sumB <= 0) { minB += Math.abs(sumB) + 1; sumB += Math.abs(sumB) + 1; } } else { if (sumB >= 0) { minB += Math.abs(sumB) + 1; sumB -= Math.abs(sumB) + 1; } } } System.out.println(Math.min(minA, minB)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<bits/stdc++.h> using namespace std; int N; vector<int>a(100010); // 終了コード136が出る場合は配列を余分に取る。制約最大+αが望ましい long calc(long s){ long sum=0, res=0; for(int i=0; i<N; i++, s*=-1){ sum+=a[i]; if(sum*s>0){continue;} // 初項が+か―かで結果が出る問題である。sの変化を基準にしている。 res+=abs(sum-s); // abs(sum-s)を算出すると符号を変えるのに必要な操作回数の値がresに入る sum+=s*abs(sum-s); // sum*sが成り立たないという事は、 } // sumに新しく入れる数値がsの基準に合致していないためであり、sを掛ける必要がある return res; // 操作回数の最小値 } int main() { cin>>N; for(int i=0; i<N; i++){ cin>>a[i]; } cout<<min(calc(1), calc(-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": [] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn Agrawal coderbond007 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); long ret = Long.MAX_VALUE; for (int x = -1; x <= 1; x += 2) { long s = 0; long sg = x; long cost = 0; for (int i = 0; i < n; i++, sg = -sg) { s += a[i]; if (Long.signum(s) == sg) { } else if (sg == 1) { cost += 1 - s; s = 1; } else { cost += s - (-1); s = -1; } } ret = Math.min(ret, cost); } out.println(ret); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c == ',') { c = read(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) def pm(a, t): ans = 0 s = 0 for i in a: s += i if t == 1 and s<1: ans += -s+1 s = 1 elif t==-1 and s>-1: ans += s+1 s = -1 t *= -1 return ans print(min(pm(a, 1), pm(a, -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": [] }
CORRECT
java
import java.io.*; import java.math.BigInteger; public class Main implements Runnable { static ContestScanner in; static Writer out; public static void main(String[] args) { new Thread(null, new Main(), "", 16 * 1024 * 1024).start(); } public void run() { Main main = new Main(); try { in = new ContestScanner(); out = new Writer(); main.solve(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } void solve() throws IOException { n = in.nextInt(); s = new long[n]; for (int i = 0; i < n; i++) { s[i] = in.nextLong(); } System.out.println(Math.min(apply(-1), apply(1))); } int n; long[] s; long apply(long st) { long ans = 0; long sum = 0; long target = st; for (int i = 0; i < n; i++) { long a = s[i]; if ((a + sum) * target > 0) sum += a; else { ans += Math.abs(a + sum - target); sum = target; } target = -Long.signum(sum); } return ans; } } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException {super(System.out);} } class ContestScanner implements Closeable { private BufferedReader in;private int c=-2; public ContestScanner()throws IOException {in=new BufferedReader(new InputStreamReader(System.in));} public ContestScanner(String filename)throws IOException {in=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} public void close() throws IOException {in.close();} }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { private static long[] add(long[] a, long add) { long[] res = new long[a.length]; for (int i = 0; i < a.length; i++) { res[i] = a[i]; } res[0] += add; return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } boolean isZero = (a[0] == 0); long ans = Long.MAX_VALUE; if ( isZero ) { long[] ta = add(a, 1); ans = Math.min(ans, solve(ta) + 1); ta = add(a, -1); ans = Math.min(ans, solve(ta) + 1); } else { ans = Math.min(ans, solve(a)); long[] ta = add(a, -a[0] + (a[0] < 0 ? 1 : -1)); ans = Math.min(ans, solve(ta) + Math.abs(a[0]) + 1); } System.out.println(ans); } } private static long solve(long[] a) { long res = 0; long sum = a[0]; boolean isPositive = sum > 0; for (int i = 1; i < a.length; i++) { isPositive = !isPositive; sum += a[i]; if ( isPositive && sum <= 0 ) { res += Math.abs(sum) + 1; sum = 1; } else if ( !isPositive && sum >= 0 ) { res += Math.abs(sum) + 1; sum = -1; } } return res; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
#ABC059C N=int(input()) A=list(map(int,input().split())) ans=[0,0] i=[1,0] for c in range(2): summ=0 for a in A: summ+=a if summ==0: ans[c]+=abs(summ-(-1)**i[c]) summ=(-1)**i[c] elif (summ>0)==i[c]: ans[c]+=abs(summ-(-1)**i[c]) summ=(-1)**i[c] i[c]=(i[c]+1)%2 print(min(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": [] }
CORRECT
java
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.ConnectException; import java.rmi.dgc.Lease; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; import static java.util.Comparator.*; public class Main { public static void main(String[] args) { Main main = new Main(); main.solve(); main.out.close(); } // ====================================================================== long[] R; public void solve() { int N = ni(); R = new long[N+1]; long a; for (int i = 0; i < N; i++) { a = nl(); R[i+1] = R[i] + a; } boolean f = true; long ans1 = 0, sv = 0; for (int i = 1; i <= N; i++) { if(f) { if(R[i]+sv <= 0) { ans1 += (1-(R[i]+sv)); sv += (1-(R[i]+sv)); } } else { if(R[i]+sv >= 0) { ans1 += (1+(R[i]+sv)); sv -= (1+(R[i]+sv)); } } f = !f; } f = false; long ans2 = 0; sv = 0; for (int i = 1; i <= N; i++) { if(f) { if(R[i]+sv <= 0) { ans2 += (1-(R[i]+sv)); sv += (1-(R[i]+sv)); } } else { if(R[i]+sv >= 0) { ans2 += (1+(R[i]+sv)); sv -= (1+(R[i]+sv)); } } f = !f; } out.println(Math.min(ans1, ans2)); } // ------------------------------------------ // ライブラリ // ------------------------------------------ // Print private PrintWriter out = new PrintWriter(System.out); // Scanner private FastScanner scan = new FastScanner(); int ni() { return scan.nextInt(); } int[] ni(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } int[][] ni(int y, int x) { int[][] a = new int[y][x]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { a[i][j] = ni(); } } return a; } long nl() { return scan.nextLong(); } long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } long[][] nl(int y, int x) { long[][] a = new long[y][x]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { a[i][j] = nl(); } } return a; } String ns() { return scan.next(); } String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = ns(); } return a; } String[][] ns(int y, int x) { String[][] a = new String[y][x]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { a[i][j] = ns(); } } return a; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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(); long sum = 0, count1 = 0; for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum <= 0) { count1 += Math.abs(sum) + 1; sum = 1; } else if(i % 2 == 1 && sum >= 0) { count1 += sum + 1; sum = -1; } } sum = 0; long count2 = 0; for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum >= 0) { count2+= sum + 1; sum = -1; } else if(i % 2 == 1 && sum <= 0) { count2 += Math.abs(sum) + 1; sum = 1; } } System.out.println(Math.min(count1, count2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) A = list(map(int, input().split())) answer = 1e15 for s in range(2): # start from + or - prev_cumsum=0 num_man = 0 for i, a in enumerate(A): if (prev_cumsum + a)*(-1)**(i+s) > 0: prev_cumsum += a #print(prev_cumsum) else: num_man += abs((-1)**(i+s) - prev_cumsum -a) prev_cumsum = (-1)**(i+s) #print(prev_cumsum) answer = min(answer, num_man) #print(num_man) print(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": [] }
CORRECT
python3
n=input() a=list(map(int,input().split())) ans=10**18 for s in -1,1: c=0 if a[0]*s<0:c=abs(s-a[0]) elif a[0]*s>0:s=a[0] else:c=1 for i in a[1:]: if s<0: b=-s+1 c+=max(0,b-i) s+=max(i,b) else: b=-s-1 c+=max(0,i-b) s+=min(i,b) ans=min(ans,c) 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": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] A = new long[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } System.out.println( solve(N, A) ); } private static long solve(int N, long[] A) { long a0 = A[0]; if( a0 > 0 ) { long p = solve1(N, A, a0, 0); long m = solve1(N, A, -1, a0 + 1); return Math.min(p, m); } else if( a0 < 0 ) { long p = solve1(N, A, 1, Math.abs(a0) + 1); long m = solve1(N, A, a0, 0); return Math.min(p, m); } else { long p = solve1(N, A, 1, 1); long m = solve1(N, A, -1, 1); return Math.min(p, m); } } private static long solve1(int N, long[] A, long sum, long ans) { for (int i = 1; i < N; i++) { long a = A[i]; if( sum > 0 ) { // 次はminusになるのを期待 if( a + sum >= 0 ) { // sumが-1になるような値にまで変更する // a + sum が 5 の場合、6 だけ操作すると -1 にできる long diff = a + sum + 1L; ans += diff; sum = -1; } else { sum += a; } } else { if( a + sum <= 0 ) { long diff = Math.abs(a + sum) + 1L; ans += diff; sum = 1; } else { sum += a; } } } return ans; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) ans=0 tmp=0 for i in range(n): tmp+=a[i] if i%2==0: if tmp<=0: ans+=abs(tmp)+1 tmp=1 else: if tmp>=0: ans+=abs(tmp)+1 tmp=-1 tmp=0 tmp2=0 for i in range(n): tmp+=a[i] if i %2==0: if tmp>=0: tmp2+=abs(tmp)+1 tmp=-1 else: if tmp<=0: tmp2+=abs(tmp)+1 tmp=1 print(min(ans,tmp2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long a[] = new long[(int)n]; for(int i = 0; i < n; i++) { a[i] = sc.nextLong(); } long sum = a[0]; long ans1 = 0; if(sum == 0) { sum = 1; ans1++; } for(int i = 1; i < n; i++) { if(sum > 0) { if(sum + a[i] >= 0) { ans1 += sum + a[i] + 1; sum = -1; } else { sum = sum + a[i]; } } else { if(sum + a[i] <= 0) { ans1 += Math.abs(a[i] + sum) + 1; sum = 1; } else { sum = sum + a[i]; } } } long ans2 = Math.abs(a[0]) + 1; if(a[0] >= 0) { sum = -1; } else if(a[0] < 0){ sum = 1; } for(int i = 1; i < n; i++) { if(sum > 0) { if(sum + a[i] >= 0) { ans2 += sum + a[i] + 1; sum = -1; } else { sum = sum + a[i]; } } else { if(sum + a[i] <= 0) { ans2 += Math.abs(a[i] + sum) + 1; sum = 1; } else { sum = sum + a[i]; } } } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = [int(i) for i in input().split()] mi = 1e20 for sign in [-1, 1]: res, acc = 0, 0 for a in A: acc += a if sign * acc <= 0: res += abs(acc - sign) acc = sign sign *= -1 mi = min(mi, res) print(mi)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include <algorithm> #include <cstdlib> using namespace std; long long solver(bool pre_is_posi, long long a[], long long n) { long long total=0, ans=0; for (int i=0; i<n; i++){ total += a[i]; if (pre_is_posi && total >= 0){ ans += abs(total)+1; total = -1; } else if (!(pre_is_posi) && total <= 0){ ans += abs(total)+1; total = 1; } pre_is_posi = (total > 0); } return ans; } int main() { long long n, a[100000]; cin >> n; for (int i=0; i<n; i++) cin >> a[i]; cout << min(solver(true, a, n), solver(false, a, n)) << 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": [] }
CORRECT
cpp
#include<bits/stdc++.h> using namespace std; int main(){ long n; cin >> n; long a[n],sum = 0; for(long i = 0;i < n;i++){ cin >> a[i]; } long kekka = 0; for(long i = 0;i < n;i++){ sum += a[i]; if(i % 2 == 0 && sum >= 0){ kekka += sum + 1; sum = -1; } if(i % 2 == 1 && sum <= 0){ kekka += -sum + 1; sum = 1; } } long result = 0; sum = 0; for(long i = 0;i < n;i++){ sum += a[i]; if(i % 2 == 0 && sum <= 0){ result += -sum + 1; sum = 1; } if(i % 2 == 1 && sum >= 0){ result += sum + 1; sum = -1; } } cout << min(kekka,result) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int []a = new int [n]; for(int i = 0; i < n ;i++) { a[i] = sc.nextInt(); } sc.close(); long ans = Math.min(count(n,a,0),count(n,a,1)); System.out.println(ans); } private static long count(int n, int []a, int pat) { long count = 0; long sum = 0; int temp = (int)Math.pow(-1, 1+pat); for(int i = 0 ; i < n ; i++) { sum += a[i]; temp *= -1; if((long)temp * sum <= 0) { count += Math.abs(sum) + 1 ; sum = temp; } } return count; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define ll long long #ifdef PAPITAS #define DEBUG 1 #else #define DEBUG 0 #endif #define _DO_(x) if(DEBUG) x int main() { ios::sync_with_stdio(false);cin.tie(NULL); int n; cin >> n; int arr[n]; ll a = 0, b = 0, sa = 0, sb = 0; for(int i = 0; i< n; i++){ cin >> arr[i]; } for(int i = 0; i < n; i++){ sa += arr[i]; sb += arr[i]; if(i%2 == 0){ if(sa <= 0){ a += 1 - sa; sa = 1; } if(sb >= 0){ b += sb + 1; sb = -1; } }else{ if(sa >= 0){ a += sa + 1; sa = -1; } if(sb <= 0){ b += 1 - sb; sb = 1; } } } cout << min(a, b); 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": [] }
CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] s1 = s2 = su = 0 for i in range(n): su += a[i] if (i % 2 == 0 and su <= 0): s1 += abs(su) + 1 su = 1 elif(i % 2 == 1 and su >= 0): s1 += abs(su) + 1 su = -1 su = 0 for i in range(n): su += a[i] if (i % 2 == 0 and su >= 0): s2 += abs(su) + 1 su = -1 elif (i % 2 == 1 and su <= 0): s2 += abs(su) + 1 su = 1 print(min(s1,s2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
# solution import math import io data=int(input()) qwe = (list(map(int,input().split()))) score1=score2=0 cnt1=cnt2=0 for i ,num in enumerate(qwe): score1+=num if score1<=0 and i%2==0: cnt1+=1-score1 score1=1 elif score1>=0 and i%2!=0: cnt1+=1+score1 score1=-1 score2+=num if score2<=0 and i%2!=0: cnt2+=1-score2 score2=1 elif score2>=0 and i%2==0: cnt2+=1+score2 score2=-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": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) t = 1 cand1 = 0 tmp = 0 for ai in a: if (tmp + ai) * t <= 0: cand1 += abs(tmp + ai - t) tmp = t else: tmp += ai t *= -1 t = -1 cand2 = 0 tmp = 0 for ai in a: if (tmp + ai) * t <= 0: cand2 += abs(tmp + ai - t) tmp = t else: tmp += ai t *= -1 print(min(cand1, cand2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) A = [int(i) for i in input().split()] def f(A, op, acc = 0, cnt = 0): for i in range(n): acc += A[i] if i % 2 == 0 and op * acc <= 0: cnt += - op * acc + 1 acc = op if i % 2 == 1and op * acc >= 0: cnt += op * acc + 1 acc = -op if acc == 0: cnt += 1 return cnt print(min(f(A, 1), f(A, -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": [] }
CORRECT
python3
n=input() a = list(map(int,input().split())) def chk(a,t): ans=0 x=0 for i in a: x+=i if t and x<1: ans+=1-x x=1 elif not t and x>-1: ans+=x+1 x=-1 t= not t return ans print(min(chk(a,True),chk(a,False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.stream.LongStream; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CSequence solver = new CSequence(); solver.solve(1, in, out); out.close(); } static class CSequence { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); long[] a = in.readLongArray(n); long ans1 = calc(a); long ans2 = calc(LongStream.of(a).map(x -> -x).toArray()); out.printLine(Math.min(ans1, ans2)); } long calc(long[] a) { int n = a.length; long ret = 0; long sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { long need = Math.max(1 - (sum + a[i]), 0); ret += need; sum = Math.max(sum + a[i], 1); } else { long need = Math.max(sum + a[i] - (-1), 0); ret += need; sum = Math.min(sum + a[i], -1); } } return ret; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lli = int64_t; using vi = vector<lli>; lli f(vi& v, int n, int fl) { lli ans = 0; for(lli i = 0, s = 0; i < n; i++, fl = !fl) { s += v[i]; if(!fl && s >= 0) ans += s - (-1), s = -1; else if(fl && s <= 0) ans += 1 - s, s = 1; } return ans; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vi v(n); for(int i = 0; i < n; i++) cin >> v[i]; lli ans = min(f(v, n, 0), f(v, n, 1)); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) def solve(parity): wa=0 ans=0 for i in range(n): wa+=a[i] if i%2==parity: if wa>=0: ans+=wa+1 wa-=wa+1 else: if wa<=0: ans+=-wa+1 wa+=-wa+1 return ans print(min(solve(0),solve(1)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static double sequence(int a[], double start) { double count = 0.0, presum = start, sum = start; for(int i = 1; i < a.length; ++i) { sum += (double)a[i]; if(sum * presum > 0) { double min = Math.abs(sum) + 1; if(presum > 0)sum -= min; else sum += min; count += min; } if(sum == 0) { if(presum > 0)sum--; else sum++; ++count; } presum = sum; } return count; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, a[]; double count = 0.0, tmp2 = 0.0, tmp3 = 0.0; n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; ++i) a[i] = sc.nextInt(); sc.close(); if(a[0] == 0) { a[0]++; ++tmp3; } int tmp = Math.abs(a[0]) + 1; if(a[0] > 0) { tmp2 = tmp - tmp3; tmp = a[0] - tmp; } else { tmp2 = tmp + tmp3; tmp = a[0] + tmp; } count = Math.min((tmp3 + sequence(a, (double)a[0])),(tmp2 + sequence(a, (double)tmp))); System.out.printf("%.0f\n", count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = list(map(int,input().split())) def f(x): cnt = 0 tmp = 0 for a in A: tmp += a #print(tmp, x) if(tmp*x >= 0): cnt += abs(tmp)+1 tmp = -x x *= -1 #print(tmp, x) return cnt print(min(f(1), f(-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": [] }
CORRECT
cpp
#include<bits/stdc++.h> #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) (v).begin(),(v).end() #define int long long using namespace std; typedef vector<int> vint; typedef pair<int,int> pint; signed main() { int N; cin>>N; vint a(N); REP(i,N) cin>>a[i]; int cost1=0,S=0; REP(i,N){ S+=a[i]; if(i%2==0 and S<=0) cost1+=1-S,S=1; if(i%2==1 and S>=0) cost1+=S+1,S=-1; } int cost2=0; S=0; REP(i,N){ S+=a[i]; if(i%2==0 and S>=0) cost2+=S+1,S=-1; if(i%2==1 and S<=0) cost2+=1-S,S=1; } cout<<min(cost1,cost2)<<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": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) def cnt(p): s=d=0 for i in range(n): c=(s+a[i])*p if c<=0: d+=(1-c) s=p else: s+=a[i] p*=(-1) return d da=cnt(1) db=cnt(-1) print(min(da,db))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ Sequence solver = new Sequence(); solver.readInput(); solver.solve(); solver.writeOutput(); } static class Sequence { private int n; private long a[]; private long output; private Scanner scanner; public Sequence() { this.scanner = new Scanner(System.in); } public void readInput() { n = Integer.parseInt(scanner.next()); a = new long[n]; for(int i=0; i<n; i++) { a[i] = Integer.parseInt(scanner.next()); } } private int count(int sign) { int count=0; long sum=0; for(int i=0; i<n; i++) { sum += a[i]; if(i%2==sign) { // a[i]までの合計を正にするとき if(sum<=0) { count += 1-sum; sum = 1; } } else { // a[i]までの合計を負にするとき if(0<=sum) { count += 1+sum; sum = -1; } } } return count; } public void solve() { /* https://atcoder.jp/contests/abc059/submissions/8183898 */ long sum1 = 0; long count1 = 0; long sum2 = 0; long count2 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; sum2 += a[i]; if (i % 2 == 0) { if (sum1 <= 0) { count1 += 1 - sum1; sum1 = 1; } if (sum2 >= 0) { count2 += sum2 + 1; sum2 = -1; } } else { if (sum2 <= 0) { count2 += 1 - sum2; sum2 = 1; } if (sum1 >= 0) { count1 += sum1 + 1; sum1 = -1; } } } output = Math.min(count1,count2); } public void writeOutput() { System.out.println(output); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) def f(isPosi): total = 0 cnt = 0 for i in a: total += i if isPosi: if total >= 0: cnt += 1 + total total = -1 isPosi = False else: if total <= 0: cnt += 1 - total total = 1 isPosi = True return cnt print(min(f(True), f(False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.*; import java.util.*; class Solver { final int n; final int[] as; long[] bs; Solver(int n, int[] as) { this.n = n; this.as = as; } private long makePositive(int index, long sum) { if (index >= n) { return 0; } long v = 0; if (as[index] + sum <= 0) { v = 1 - (as[index] + sum); } return v + makeNegative(index + 1, sum + as[index] + v); } private long makeNegative(int index, long sum) { if (index >= n) { return 0; } long v = 0; if (as[index] + sum >= 0) { v = as[index] + sum - (-1); } return v + makePositive(index + 1, sum + as[index] - v); } public long solve() { return Math.min(makePositive(0, 0), makeNegative(0, 0)); } } public class Main { private static void execute(ContestReader reader, PrintWriter out) { int n = reader.nextInt(); int[] as = reader.nextInt(n); out.println(new Solver(n, as).solve()); } public static void main(String[] args) { ContestReader reader = new ContestReader(System.in); PrintWriter out = new PrintWriter(System.out); execute(reader, out); out.flush(); } } class ContestReader { private BufferedReader reader; private StringTokenizer tokenizer; ContestReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] next(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } public int[] nextInt(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLong(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public double[] nextDouble(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } public char[] nextCharArray() { return next().toCharArray(); } public int[][] nextInt(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextInt(); } } return matrix; } public long[][] nextLong(int n, int m) { long[][] matrix = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextLong(); } } return matrix; } public double[][] nextDouble(int n, int m) { double[][] matrix = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextDouble(); } } return matrix; } public char[][] nextCharArray(int n) { char[][] matrix = new char[n][]; for (int i = 0; i < n; i++) { matrix[i] = next().toCharArray(); } return matrix; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; long long cnt1 = 0, sum = 0; for (int i = 0; i < n; ++i) { long long _sum = sum + a[i], su = (i & 1 ? _sum : -_sum); if (su >= 0) { cnt1 += su + 1; sum = (i & 1 ? -1 : 1); } else sum = _sum; } long long cnt2 = 0; sum = 0; for (int i = 0; i < n; ++i) { long long _sum = sum + a[i], su = (i & 1 ? -_sum : _sum); if (su >= 0) { cnt2 += su + 1; sum = (i & 1 ? 1 : -1); } else sum = _sum; } 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": [] }
CORRECT
python3
input() *A,=map(int,input().split()) f=True s1=0;r1=0 s2=0;r2=0 for a in A: s1+=a;s2+=a if f and s1<=0:r1+=1-s1;s1=1 elif not(f) and s1>=0:r1+=s1+1;s1=-1 if not(f) and s2<=0:r2+=1-s2;s2=1 elif f and s2>=0:r2+=s2+1;s2=-1 f=not(f) print(min(r1,r2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; long[] b = new long[n]; for(int i = 0; i < n; i++) { long t = sc.nextLong(); a[i] = t; b[i] = t; } long min1 = 0; long min2 = 0; long[] s1 = new long[n]; s1[0] = a[0]; long[] s2 = new long[n]; s2[0] = b[0]; // 部分和を+-+-にする場合 for(int i = 0; i < n; i++) { if(i > 0) s1[i] = s1[i - 1] + a[i]; if(i % 2 == 0) { if(s1[i] <= 0) { min1 += (1 - s1[i]); s1[i] = 1; } } else { if(s1[i] >= 0) { min1 += (1 + s1[i]); s1[i] = -1; } } } // 部分和を-+-+にする場合 for(int i = 0; i < n; i++) { if(i > 0) s2[i] = s2[i - 1] + b[i]; if(i % 2 == 0) { if(s2[i] >= 0) { min2 += (1 + s2[i]); s2[i] = -1; } } else { if(s2[i] <= 0) { min2 += (1 - s2[i]); s2[i] = 1; } } } long ans = Math.min(min1, min2); System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.*; import java.util.*; class Main{ void solve(){ int N = ni(); long[] a = new long[N]; long[] s = new long[N + 1]; for(int i = 0; i < N; i++){ a[i] = ni(); } for(int i = 1; i <= N ; i++){ s[i] = s[i - 1] + a[i - 1]; } long pattern1 = 0; long control = 0; for(int i = 1; i <= N; i++){ if(i % 2 == 1){ // 正 if(s[i] + control <= 0){ pattern1 += Math.abs(s[i] + control) + 1; control += Math.abs(s[i] + control) + 1; } } else { // 負 if(s[i] + control >= 0){ pattern1 += Math.abs(s[i] + control) + 1; //control = -1 * (Math.abs(s[i] + control) + 1); control -= Math.abs(s[i] + control) + 1; } } } long pattern2 = 0; control = 0; for(int i = 1; i <= N; i++){ if(i % 2 == 0){ // 正 if(s[i] + control <= 0){ pattern2 += Math.abs(s[i] + control) + 1; control += Math.abs(s[i] + control) + 1; } } else { // 負 if(s[i] + control >= 0){ pattern2 += Math.abs(s[i] + control) + 1; control -= Math.abs(s[i] + control) + 1; } } } out.println(min(pattern1, pattern2)); out.flush(); } public static void main(String[] args){ Main m = new Main(); m.solve(); } Main(){ this.scan = new FastScanner(); this.out = new PrintWriter(System.out); } private FastScanner scan; private PrintWriter out; private final int MOD = 1_000_000_007; private final int INF = 2_147_483_647; private final long LINF = 9223372036854775807L; private long[] fac; private long[] finv; private long[] inv; // Scanner int ni(){ return scan.nextInt();} int[] ni(int n){int[] a = new int[n]; for(int i = 0; i < n; i++){a[i] = ni();} return a;} int[][] ni(int y, int x){int[][] a = new int[y][x]; for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = ni();}} return a;} long nl(){return scan.nextLong();} long[] nl(int n){long[] a = new long[n]; for(int i = 0; i < n; i++){a[i] = nl();} return a;} long[][] nl(int y, int x){long[][] a = new long[y][x]; for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = nl();}} return a;} String ns(){return scan.next();} String[] ns(int n){String[] a = new String[n]; for(int i = 0; i < n; i++){a[i] = ns();} return a;} String[][] ns(int y, int x){String[][] a = new String[y][x]; for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = ns();}} return a;} // Mathematics int max(int a, int b){return Math.max(a, b);} long max(long a, long b){return Math.max(a, b);} double max(double a, double b){return Math.max(a, b);} int max(int[] a){int max = a[0]; for(int value:a){max = max(max,value);} return max;} long max(long[] a){long max = a[0]; for(long value:a){max = max(max,value);} return max;} double max(double[] a){double max = a[0]; for(double value:a){max = max(max,value);} return max;} int min(int a, int b){return Math.min(a, b);} long min(long a, long b){return Math.min(a, b);} double min(double a, double b){return Math.min(a, b);} int min(int[] a){int min = a[0]; for(int value:a){min = min(min,value);} return min;} long min(long[] a){long min = a[0]; for(long value:a){min = min(min,value);} return min;} double min(double[] a){double min = a[0]; for(double value:a){min = min(min,value);} return min;} long sum(int[] a){long sum = 0; for(int value:a){sum += value;} return sum;} long sum(long[] a){long sum = 0; for(long value:a){sum += value;} return sum;} double sum(double[] a){double sum = 0; for(double value:a){sum += value;} return sum;} long mod(long n) { n %= MOD; return n + (n < 0 ? MOD : 0); } int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);} long gcd(long a, long b){return b == 0 ? a : gcd(b, a % b);} int lcm(int a, int b){return a / gcd(a, b) * b;} long lcm(long a, long b){return a / gcd(a, b) * b;} long fact(int n){ if(n == 0){ return 1; } long a = n; for(long i = n - 1; i >= 2; i--){ a = a % MOD * i; } return a; } long fact(long n){ if(n == 0){ return 1; } long a = n; for(long i = n - 1; i >= 2; i--){ a = a % MOD * i; } return a; } // nPr(int) long npr(int n, int r){ long a = 1; for(int i = n; i > n - r; i--){ a *= i; } return a; } // nPr(long) long npr(long n, long r){ long a = 1; for(long i = n; i > n - r; i--){ a *= i; } return a; } // 素数判定(int) boolean checkPrime(int n){ for(int i = 2; i * i <= n; i++){ if(n % i == 0){ return false; } } return true; } // 素数判定(long) boolean checkPrime(long n){ for(long i = 2; i * i <= n; i++){ if(n % i == 0){ return false; } } return true; } // エラトステネスの篩 long[] erathos(int n){ boolean[] flag = new boolean[n + 1]; TreeSet<Integer> nums = new TreeSet<>(); for(int i = 2; i <= n; i++){ if(flag[i]) continue; nums.add(i); for(int j = i * 2; j <= n; j += i){ flag[j] = true; } } long[] primes = new long[nums.size()]; int index = 0; for(int num : nums){ primes[index] = num; index++; } return primes; } // mod pにおける累乗 a^n long modpow(long a, long n, long p){ long res = 1; while(n > 0){ if((n & 1) == 1){ res = res * a % p; } a = a * a % p; n = n >> 1; } return res; } // mod pにおけるaの逆元a^-1 long modinv(long a, long p){ return modpow(a, p - 2, p); } // fac,finv,invの初期化 void comInit(int max){ fac = new long[max]; finv = new long[max]; inv = new long[max]; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i = 2; i < max; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数nCr long com(int n, int r){ if(n < r || (n < 0 || r < 0)){ return 0; } return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD; } // 二項係数nCr(nが10^9など巨大なとき用) long ncr(long n, long k){ long a = 1; long b = 1; for(int i = 1; i <= k; i++){ a = a * (n + 1 - i) % MOD; b = b * i % MOD; } return modinv(b, MOD) * a % MOD; } // 二次元上の二点間の距離 double distance(double x1, double y1, double x2, double y2){ double dist = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return dist; } // 三次元上の二点間の距離 double distance(double x1, double y1, double z1, double x2, double y2, double z2){ double dist = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); return dist; } // Array void sort(int[] a){ Arrays.sort(a);} void sort(long[] a){ Arrays.sort(a);} void sort(double[] a){ Arrays.sort(a);} void sort(String[] a){ Arrays.sort(a);} int[] reverse(int[] a){ int[] reversed = new int[a.length]; for(int i = 0; i < a.length; i++){ reversed[a.length - i - 1] = a[i]; } return reversed; } long[] reverse(long[] a){ long[] reversed = new long[a.length]; for(int i = 0; i < a.length; i++){ reversed[a.length - i - 1] = a[i]; } return reversed; } double[] reverse(double[] a){ double[] reversed = new double[a.length]; for(int i = 0; i < a.length; i++){ reversed[a.length - i - 1] = a[i]; } return reversed; } char[] reverse(char[] a){ char[] reversed = new char[a.length]; for(int i = 0; i < a.length; i++){ reversed[a.length - i - 1] = a[i]; } return reversed; } String[] reverse(String[] a){ String[] reversed = new String[a.length]; for(int i = 0; i < a.length; i++){ reversed[a.length - i - 1] = a[i]; } return reversed; } boolean[] reverse(boolean[] a){ boolean[] reversed = new boolean[a.length]; for(int i = 0; i < a.length; i++){ reversed[a.length - i - 1] = a[i]; } return reversed; } void fill(int[] array, int x) { Arrays.fill(array, x); } void fill(long[] array, long x) { Arrays.fill(array, x); } void fill(double[] array, double x) { Arrays.fill(array, x); } void fill(boolean[] array, boolean x) { Arrays.fill(array, x); } void fill(int[][] array, int x) { for(int a[] : array) { fill(a, x); } } void fill(long[][] array, long x) { for(long a[] : array) { fill(a, x); } } void fill(double[][] array, double x) { for(double a[] : array) { fill(a, x); } } void fill(boolean[][] array, boolean x) { for(boolean a[] : array) { fill(a, x); } } void fill(int[][][] array, int x) { for(int[][] ary : array) { for(int[] a : ary){ fill(a, x); } } } void fill(long[][][] array, long x) { for(long[][] ary : array) { for(long[] a : ary){ fill(a, x); } } } // Algorithm // 深さ優先探索 /* void dfs(Node[] nodes, int v, boolean[] seen){ seen[v] = true; for(Edge edge : nodes[v].edges){ if(seen[edge.to]) continue; dfs(nodes, edge.to, seen); } } */ // 幅優先探索 long[] bfs(Node[] nodes, int start){ Queue<Integer> queue = new ArrayDeque<>(); queue.add(start); long[] dist = new long[nodes.length]; fill(dist, -1); dist[start] = 0; while(!queue.isEmpty()){ int now = queue.poll(); for(Edge edge : nodes[now].edges){ if(dist[edge.to] != -1) continue; dist[edge.to] = dist[now] + 1; queue.add(edge.to); } } return dist; } // ダイクストラ法 long[] dijkstra(Node[] nodes, int start){ Queue<Edge> queue = new PriorityQueue<>(); long[] dist = new long[nodes.length]; fill(dist, LINF / 2); dist[start] = 0; queue.add(new Edge(start, start, dist[start])); while(!queue.isEmpty()){ Edge now = queue.poll(); if(dist[now.to] < now.cost) continue; for(Edge edge : nodes[now.to].edges){ if(dist[edge.to] > dist[edge.from] + edge.cost){ dist[edge.to] = dist[edge.from] + edge.cost; queue.add(new Edge(edge.from, edge.to, dist[edge.to])); nodes[edge.to].past = edge.from; } } } return dist; } // ソート済みint型配列でkey以上の値の最小indexを返す int lowerBound(int[] a, int key){ int ng = -1; int ok = a.length; while(Math.abs(ok - ng) > 1){ int mid = (ok + ng) / 2; if(a[mid] >= key){ ok = mid; } else { ng = mid; } } return ok; } // ソート済みlong型配列でkey以上の値の最小indexを返す int lowerBound(long[] a, long key){ int ng = -1; int ok = a.length; while(Math.abs(ok - ng) > 1){ int mid = (ok + ng) / 2; if(a[mid] >= key){ ok = mid; } else { ng = mid; } } return ok; } // 文字列sとtの最長共通部分列の長さを返す int lcs(String s , String t){ int[][] dp = new int[s.length() + 1][t.length() + 1]; for(int i = 0; i < s.length(); i++){ for(int j = 0; j < t.length(); j++){ if(s.charAt(i) == t.charAt(j)){ dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]); } dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]); } } return dp[s.length()][t.length()]; } } // ノード class Node{ int id; // ノード番号 int past; // 直前の頂点 List<Edge> edges; // 辺のリスト Node(int id, int past){ this.id = id; this.past = past; this.edges = new ArrayList<>(); } void addEdge(Edge edge){ edges.add(edge); } public boolean equals(Object obj){ if(obj instanceof Node){ Node node = (Node)obj; return this.id == node.id; } return false; } public int hashCode(){ return id; } public String toString(){ return "[id:" + id + " past: " + past + "]"; } } // 辺の情報を持つクラス class Edge implements Comparable<Edge>{ int from; // どの頂点から int to; // どの頂点へ long cost; // 辺の重み Edge(int from, int to, long cost){ this.from = from; this.to = to; this.cost = cost; } public int compareTo(Edge edge){ return Long.compare(this.cost, edge.cost); } public String toString(){ return "[" + from + " to " + to + " cost:" + cost + "]"; } } // 辺の重み比較のComparator class EdgeComparator implements Comparator<Edge>{ @Override public int compare(Edge e1, Edge e2){ long cost1 = e1.cost; long cost2 = e2.cost; if(cost1 < cost2){ return -1; } else if(cost1 > cost2){ return 1; } else { return 0; } } } // Union-Find class UnionFind{ int[] par; int[] size; UnionFind(int N){ par = new int[N]; size = new int[N]; for(int i = 0; i < N; i++){ par[i] = i; size[i] = 1; } } void init(int N){ for(int i = 0; i < N; i++){ par[i] = i; size[i] = 1; } } int root(int x){ if(par[x] == x){ return x; } else { return par[x] = root(par[x]); } } boolean same(int x, int y){ return root(x) == root(y); } void unite(int x, int y){ x = root(x); y = root(y); if(x == y) return; if(size[x] < size[y]){ int tmp = x; x = y; y = tmp; } size[x] += size[y]; par[y] = x; } int size(int x){ return size[root(x)]; } } // 順列を管理する class Permutation { private int number; private int listSize; private int searched; private int nextIndex; private int[][] permList; Permutation(int num) { this.number = num; this.listSize = this.fact(this.number); this.searched = 0; this.nextIndex = 0; this.permList = new int[this.listSize][this.number]; this.create(0, new int[this.number], new boolean[this.number]); } int[] nextPerm() { return permList[this.nextIndex++]; } boolean isNext() { if(this.nextIndex < this.listSize) { return true; } else { this.nextIndex = 0; return false; } } int fact(int n){ return n == 0 ? 1 : n * fact(n-1); } void create(int num, int[] list, boolean[] flag) { if(num == this.number) { copyArray(list, permList[this.searched]); this.searched++; } for(int i = 0; i < this.number; i++){ if(flag[i]) continue; list[num] = i; flag[i] = true; this.create(num+1, list, flag); flag[i] = false; } } void copyArray(int[] from, int[] to) { for(int i=0; i<from.length; i++) to[i] = from[i]; } void printNum(int[] nums) { for(int n : nums) System.out.print(n); System.out.println(); } } // 一点更新区間取得Segment Tree /* class SegTree{ int[] dat; int size; SegTree(int N){ this.dat = new int[N * 4]; this.size = N; } // [a, b]の最小値を返す // l,rにはノードkに対応する区間を与える int query(int a, int b, int k, int l, int r){ if(r < a || b < l) return Integer.MAX_VALUE; if(a <= l && r <= b) return dat[k]; int vl = query(a, b, k << 1, l, (l + r) / 2); int vr = query(a, b, (k << 1) + 1, (l + r) / 2 + 1, r); return Math.min(vl, vr); } // インデックスiの値をxに更新 void update(int i, int x){ i += this.size - 1; dat[i] = x; while(i > 0){ i = i >> 1; // 1ビットぶん右シフト dat[i] = Math.min(dat[i << 1], dat[(i << 1) + 1]); } } public String toString(){ StringBuilder sb = new StringBuilder("["); for(int i = size; i < 2 * size; i++){ sb.append(dat[i]).append(","); } sb.delete(sb.length() - 1, sb.length()); String output = sb.append("]").toString(); return output; } } */ // Range Sum Queryを実現するBinary Indexed Tree class BIT{ int[] tree; BIT(int N){ this.tree = new int[N + 1]; } int sum(int i){ int sum = 0; while(i > 0){ sum += this.tree[i]; i -= i & -i; } return sum; } void add(int i, int x){ while(i <= tree.length){ this.tree[i] += x; i += i & -i; } } public String toString(){ StringBuilder sb = new StringBuilder("["); for(int i = 0; i < tree.length; i++){ sb.append(tree[i] + ","); } sb.append("]"); String output = sb.toString(); return output; } } // 標準のScannerより高速に標準入力する class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] w = new long[n]; long sum1=0; long sum2=0; long ans1=0; long ans2=0; for(int i = 0; i < n; i++){ w[i] = sc.nextLong(); } for(int i = 0; i<n; i++){ sum1+=w[i]; sum2+=w[i]; if(sum1>0 && i%2==1){ ans1+=Math.abs(sum1)+1; sum1=-1; }else if(sum1<0 && i%2==0){ ans1+=Math.abs(sum1)+1; sum1=1; }else if(sum1==0){ ans1+=1; if(i%2==1){ sum1=-1; }else{ sum1=1; } } if(sum2>0 && i%2==0){ ans2+=Math.abs(sum2)+1; sum2=-1; }else if(sum2<0 && i%2==1){ ans2+=Math.abs(sum2)+1; sum2=1; }else if(sum2==0){ ans2+=1; if(i%2==1){ sum2=1; }else{ sum2=-1; } } } System.out.println(Math.min(ans1,ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python2
n = input() l = map(int, raw_input().split()) lastPos, lastNeg = l[0], l[0] countPos, countNeg = 0, 0 if(lastPos<=0): countPos = 1 - lastPos lastPos = 1 if(lastNeg>=0): countNeg = lastNeg+1 lastNeg = -1 for i in xrange(1,n): if(lastPos>0): #now it has to be negative if(lastPos+l[i] >= 0): diff = lastPos+l[i] + 1 countPos+=diff lastPos = -1 else: lastPos = lastPos + l[i] else: #now it has to be positive if(lastPos+l[i] <= 0): diff = 1 - (lastPos+l[i]) countPos+=diff lastPos = 1 else: lastPos = lastPos+l[i] if(lastNeg<0): #now it has to be positive if(lastNeg+l[i] <= 0): diff = 1 - (lastNeg+l[i]) countNeg+= diff lastNeg = 1 else: lastNeg = lastNeg + l[i] else: #now it has to be negative if(lastNeg+l[i] >= 0): diff = lastNeg+l[i]+1 countNeg+= diff lastNeg = -1 else: lastNeg = lastNeg + l[i] print min(countPos, countNeg)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt(); long sum = 0, cnt = 0; for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum <= 0) { // 正に変える cnt += Math.abs(sum) + 1; sum = 1; } else if(i % 2 == 1 && sum >= 0) { // 負に変える cnt += sum + 1; sum = -1; } } long sum1 = 0, cnt1 = 0; for(int i = 0 ; i < n ; i++) { sum1 += a[i]; if(i % 2 == 0 && sum1 >= 0) { // 負に変える cnt1 += sum1 + 1; sum1 = -1; } else if(i % 2 == 1 && sum1 <= 0) { // 正に変える cnt1 += Math.abs(sum1) + 1; sum1 = 1; } } System.out.println(Math.min(cnt, cnt1)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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() { long cnt1 = (a[0]>0)?0:(Math.abs(a[0])+1); int sign = 1; long sum = (a[0]>0)?a[0]:1; for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt1 += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } long cnt2 = (a[0]<0)?0:(Math.abs(a[0])+1); sign = -1; sum = (a[0]<0)?a[0]:-1; for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt2 += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } System.out.println(Math.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": [] }
CORRECT
cpp
#include <iostream> #include <algorithm> using namespace std; typedef long long int ll; int main(){ int n; cin >> n; ll a[n],sum=0,cnt1=0,cnt2=0; for(int i=0;i<n;i++) cin >> a[i]; for(int i=0;i<n;i++){//奇数番目が正 sum+=a[i]; if(i%2==0){ if(sum>=0) cnt1+=1+sum,sum=-1;//1にするまでの距離?を足す } else{ if(sum<=0) cnt1+=1-sum,sum=1;//sumは1か-1になってる } } sum=0; for(int i=0;i<n;i++){//奇数番目が負 sum+=a[i]; if(i%2==0){ if(sum<=0) cnt2+=1-sum,sum=1; } else{ if(sum>=0) cnt2+=1+sum,sum=-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": [] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Hamza Hasbi */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); C_Sequence solver = new C_Sequence(); solver.solve(1, in, out); out.close(); } static class C_Sequence { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); long[] a = new long[n + 1]; long[] pre = new long[n + 1]; Arrays.fill(a, 0); Arrays.fill(pre, 0); long[] pro = new long[n + 1]; Arrays.fill(pro, 0); long res = 0; long ans = 0; for (int i = 1; i <= n; i++) { a[i] = in.nextLong(); pre[i] = pre[i - 1] + a[i]; if ((i & 1) == 1 && pre[i] <= 0) { ans += Math.abs(1 - pre[i]); pre[i] = 1; continue; } if ((i & 1) == 0 && pre[i] >= 0) { ans += Math.abs(1 + pre[i]); pre[i] = -1; continue; } } for (int i = 1; i <= n; i++) { pro[i] = pro[i - 1] + a[i]; if ((i & 1) == 0 && pro[i] <= 0) { res += Math.abs(1 - pro[i]); pro[i] = 1; continue; } if ((i & 1) == 1 && pro[i] >= 0) { res += Math.abs(1 + pro[i]); pro[i] = -1; continue; } } out.printLine(Math.min(res, ans)); } } static class InputReader { BufferedReader reader; StringTokenizer st; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void printLine(long i) { writer.println(i); } public void close() { writer.close(); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<bits/stdc++.h> #define rep(i,s,t) for(int i=s;i<=t;i++) using namespace std; typedef long long LL; LL ans1,ans2,sum; int n; int a[100010]; int main(){ scanf("%d",&n); rep(i,1,n) scanf("%d",&a[i]); sum=0; for(int i=1,s=1;i<=n;i++,s*=-1){ sum+=a[i]; if(sum*s<=0) ans1+=abs(sum-s),sum=s; } sum=0; for(int i=1,s=-1;i<=n;i++,s*=-1){ sum+=a[i]; if(sum*s<=0) ans2+=abs(sum-s),sum=s; } printf("%lld\n",min(ans1,ans2)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { static String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] ary = new long[N]; for (int i = 0; i < N; i++) { ary[i] = sc.nextLong(); } long cstKisu = 0; long[] aryKisu = new long[N]; System.arraycopy(ary, 0, aryKisu, 0, ary.length); long sumKisu = 0; for (int i = 0; i < aryKisu.length; i++) { sumKisu += aryKisu[i]; if (i % 2 == 0) { if (sumKisu <= 0) { aryKisu[i] += 1 - sumKisu; cstKisu += 1 - sumKisu; sumKisu = 1; } } else { if (sumKisu >= 0) { aryKisu[i] -= sumKisu + 1; cstKisu += sumKisu + 1; sumKisu = -1; } } } long cstGusu = 0; long[] aryGusu = new long[N]; System.arraycopy(ary, 0, aryGusu, 0, ary.length); long sumGusu = 0; for (int i = 0; i < aryGusu.length; i++) { sumGusu += aryGusu[i]; if (i % 2 == 0) { if (sumGusu >= 0) { aryGusu[i] -= sumGusu + 1; cstGusu += sumGusu + 1; sumGusu = -1; } } else { if (sumGusu <= 0) { aryGusu[i] += 1 - sumGusu; cstGusu += 1 - sumGusu; sumGusu = 1; } } } System.out.println(Math.min(cstGusu, cstKisu)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n; cin >> n; vector<int>a(n); for (auto&& x : a)cin >> x; long long ans1 = 0, ans2 = 0, sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum >= 0) { ans1 += sum + 1; sum = -1; } else if (i % 2 == 1 && sum <= 0) { ans1 += -sum + 1; sum = 1; } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if (i % 2 == 0 && sum <= 0) { ans2 += -sum + 1; sum = 1; } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 1e5+10; int n; ll arr[ MAXN ]; int g( ll x ) { if( x < 0 ) return -1; if( x == 0 ) return 0; return 1; } ll f( ll sgn ) { ll r = 0, acc = 0; for( int i = 0; i < n; ++i ) { acc += arr[i]; if( g(acc) != sgn ) { r += abs(sgn-acc); acc = sgn; } sgn *= -1; } return r; } int main( ) { ios_base::sync_with_stdio( 0 ); cin.tie( 0 ); #ifdef LOCAL freopen( "input", "r", stdin ); #endif while( cin >> n ) { ll ans = 0; for( int i = 0; i < n; ++i ) { cin >> arr[i]; } ans = min( f(1), f(-1) ); cout << ans << '\n'; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt(); long sum = 0, ans = 0, ans2 = 0; // 偶数位置までの和:正、奇数位置までの和:負 for(int i = 0 ; i < n ; i++) { if(i % 2 == 0) { if(sum + a[i] >= 0) { ans += sum + a[i] + 1; sum = -1; } else { sum += a[i]; } } else { if(sum + a[i] <= 0) { ans += 1 - (sum + a[i]); sum = 1; } else { sum += a[i]; } } } sum = 0; // 偶数位置までの和:正、奇数位置までの和:負 for(int i = 0 ; i < n ; i++) { if(i % 2 == 1) { if(sum + a[i] >= 0) { ans2 += sum + a[i] + 1; sum = -1; } else { sum += a[i]; } } else { if(sum + a[i] <= 0) { ans2 += 1 - (sum + a[i]); sum = 1; } else { sum += a[i]; } } } System.out.println(Math.min(ans, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.File; import java.io.IOException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Deque; import java.util.List; import java.util.Scanner; public class Main { //ABC059 public static void main(String[] args) throws IOException { //File file = new File("input.txt"); //Scanner in = new Scanner(file); Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; long ans1 = 0; long ans2 = 0; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); } //+-+- long sum = 0; for(int i = 0; i < n; i++){ sum += a[i]; if(i % 2 == 0 && sum <= 0){ long diff = Math.abs(sum) + 1; sum += diff; ans1 += diff; }else if(i % 2 == 1 && sum >= 0){ long diff = - Math.abs(sum) - 1; sum += diff; ans1 += Math.abs(diff); } } //System.out.println(ans1); //-+-+ sum = 0; for(int i = 0; i < n; i++){ sum += a[i]; if(i % 2 == 1 && sum <= 0){ long diff = Math.abs(sum) + 1; sum += diff; ans2 += diff; }else if(i % 2 == 0 && sum >= 0){ long diff = - Math.abs(sum) - 1; sum += diff; ans2 += Math.abs(diff); } } //System.out.println(ans2); System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> #include<algorithm> #include<functional> #include<cmath> #include<string> #include<vector> #include<queue> using namespace std; const int mod = 1000000007; #define ll long long ll A[100010]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } ll sum = 0; ll ans1 = 0, ans2 = 0; int x = 1; for (int i = 0; i < N; i++) { sum += A[i]; if (sum * x >= 0) { ans1 += abs(sum + x); sum = -x; } x *= -1; } sum = 0; x = -1; for (int i = 0; i < N; i++) { sum += A[i]; if (sum * x >= 0) { ans2 += abs(sum + x); sum = -x; } x *= -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": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; std::cin >> n; std::array<int64_t, 2> rets{}; std::array<int64_t, 2> sums{}; for (int i = 0; i < n; i++) { int a; std::cin >> a; std::array<int64_t, 2> moves{}; for (int j = 0; j < 2; j++) { sums[j] += a; if (i % 2 == j) { if (sums[j] >= 0) { moves[j] = -sums[j] - 1; } } else { if (sums[j] <= 0) { moves[j] = -sums[j] + 1; } } sums[j] += moves[j]; rets[j] += abs(moves[j]); } } std::cout << std::min(rets[0], rets[1]) << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<bits/stdc++.h> #define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i) using namespace std; signed main(){ int N;cin>>N; vector<long long> A(N); REP(i, N) cin >> A[i]; long long ans=LLONG_MAX; int sig[2]={1,-1}; REP(j,2){ long long sum=0; long long count=0; REP(i,N){ sum+=A[i]; if(sig[(i^j)&1]*sum>=0){ count+=llabs(sum)+1; sum=-sig[(i^j)&1]; } } ans=min(ans,count); } cout<<ans<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> using namespace std; long long a[100001], res; int n; long long llabs(long long x) { return x < 0 ? -x : x; } void solve() { long long sum = 0; res = 0; for(int i = 0; i < n; ++i) { if((i%2)) { if(sum + a[i] <= 0) { res += llabs(sum + a[i]) + 1; sum = 1; }else { sum += a[i]; } }else { if(sum + a[i] >= 0) { res += llabs(sum + a[i]) + 1; sum = -1; }else { sum += a[i]; } } } } int main() { cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i]; } solve(); long long ress = res; for(int i = 0; i < n ; ++i) { a[i] = -a[i]; } solve(); cout << (ress < res ? ress : res) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int64_t i; int64_t n;cin >> n; vector<int64_t> a(n);for(int i=0;i<n;i++) cin >> a[i]; int64_t sum=0,ans0=0,ans1=0; for(i=0;i<n;i++){ sum += a[i]; if(i%2==0 && sum<=0){ ans0 += abs(sum) +1; sum = 1; } else if(i%2==1 && sum>=0){ ans0 += abs(sum) +1; sum = -1; } } sum=0; for(i=0;i<n;i++){ sum += a[i]; if(i%2==0 && sum>=0){ ans1 += abs(sum) +1; sum = -1; } else if(i%2==1 && sum<=0){ ans1 += abs(sum) +1; sum = 1; } } cout << min(ans0,ans1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = list(map(int, input().split())) s = 0 tmp1 = 0 for i in range(N): s += A[i] if i % 2 == 0: if s >= 0: tmp1 += s + 1 s = -1 else: if s <= 0: tmp1 += -s + 1 s = 1 #print(tmp1) s = 0 tmp2 = 0 for i in range(N): s += A[i] if i % 2 == 0: if s <= 0: tmp2 += -s + 1 s = 1 else: if s >= 0: tmp2 += s + 1 s = -1 #print(tmp2) print(min(tmp1, tmp2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> using namespace std; int N; int a[112345]; long long int ans[2]; int main () { cin >> N; for(int i=0; i<N; i++) cin >> a[i]; for(int i=0; i<2; i++) { long long int sum = 0; for(int j=0; j<N; j++) { sum += a[j]; if(j % 2 == i ) { if(sum <= 0) { ans[i] += 1 - sum; sum = 1; } } else { if(sum >= 0) { ans[i] += sum + 1; sum = -1; } } } } cout << min(ans[0],ans[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; class Main { int n; int[] a; long cnt1; long cnt2; 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() { Thread t1 = new Thread(()->{calc_run1((a[0]>0)?0:(Math.abs(a[0])+1),1,(a[0]>0)?a[0]:1);}); t1.start(); Thread t2 = new Thread(()->{calc_run2((a[0]<0)?0:(Math.abs(a[0])+1),-1,(a[0]<0)?a[0]:-1);}); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Math.min(cnt1, cnt2)); } void calc_run1(long cnt,int sign,long sum){ for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } cnt1 = cnt; } void calc_run2(long cnt,int sign,long sum){ for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } cnt2 = 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": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<long long int> a(n); long long int sum1=0, sum2=0, cost1=0, cost2=0; for(int i=0; i<n; i++){ cin >> a[i]; sum1 += a[i]; sum2 += a[i]; if(i%2 == 0 && sum1 <= 0){ cost1 += 1-sum1; sum1 = 1; } else if(i%2 == 1 && sum1 >= 0){ cost1 += sum1+1; sum1 = -1; } if(i%2 == 0 && sum2 >= 0){ cost2 += 1+sum2; sum2 = -1; } else if(i%2 == 1 && sum2 <= 0){ cost2 += 1-sum2; sum2 = 1; } } cout << min(cost1, cost2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) A = list(map(int,input().split())) s = max(A[0],1) cnt1 = abs(A[0]-s) #sを正にするまで for a in A[1:]: if s>0: b=min(a, -s-1) #aを何の値にするかがb if s<0: b=max(a,-s+1) s += b #bを足してs更新 cnt1 += abs(a-b) s = min(A[0],-1) cnt2 = abs(A[0]-s) for a in A[1:]: if s>0: b=min(a, -s-1) #aを何の値にするかがb if s<0: b=max(a,-s+1) s += b #bを足してs更新 cnt2 += abs(a-b) 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": [] }
CORRECT
cpp
#include <iostream> #include <string> #include<algorithm> using namespace std; typedef long long ll; typedef pair<ll, ll> P; ll s1, s2, c1, c2; int main() { ll n, a; cin >> n; for (int i = 1; i <= n; i++) { cin >> a; s1 += a; s2 += a; if (i % 2) { if (s1 <= 0) c1 += 1 - s1,s1 = 1; if (s2 >= 0) c2 += 1 + s2,s2 = -1; } else { if (s1 >= 0) c1 += 1 + s1,s1 = -1; if (s2 <= 0) c2 += 1 - s2,s2 = 1; } } cout << min(c1, c2) << 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": [] }
CORRECT
java
import java.io.PrintWriter; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); Task task = new Task(); task.solve(sc, out); out.flush(); sc.close(); } static class Task { public void solve(Scanner sc, PrintWriter out) { int n = nint(sc); List<Long> aList = getLongList(sc, n); long sum = 0; long result1 = 0; long result2 = 0; long margin = 0; // even +1 for (int i = 0; i < n; i++) { sum += aList.get(i); if (i%2 == 0){ if (sum>0){ continue; }else { margin = Math.abs(sum)+1; result1 += margin; sum = 1; } }else { if (sum<0){ continue; }else { margin = Math.abs(sum)+1; result1 += margin; sum = -1; } } } sum = 0; // even -1 for (int i = 0; i < n; i++) { sum += aList.get(i); if (i%2 == 0){ if (sum<0){ continue; }else { margin = Math.abs(sum)+1; result2 += margin; sum = -1; } }else { if (sum>0){ continue; }else { margin = Math.abs(sum)+1; result2 += margin; sum = 1; } } } out.println(Math.min(result1, result2)); } } static int nint(Scanner sc) { return Integer.parseInt(sc.next()); } static long nlong(Scanner sc) { return Long.parseLong(sc.next()); } static double ndouble(Scanner sc) { return Double.parseDouble(sc.next()); } static float nfloat(Scanner sc) { return Float.parseFloat(sc.next()); } static String nstr(Scanner sc) { return sc.next(); } static long[] longLine(Scanner sc, int size) { long[] lLine = new long[size]; for (int i = 0; i < size; i++) { lLine[i] = nlong(sc); } return lLine; } static int[] intLine(Scanner sc, int size) { int[] iLine = new int[size]; for (int i = 0; i < size; i++) { iLine[i] = nint(sc); } return iLine; } static String[] strLine(Scanner sc, int size) { String[] strLine = new String[size]; for (int i = 0; i < size; i++) { strLine[i] = nstr(sc); } return strLine; } static long maxFromList(List<Long> longList) { return longList.stream().max(Comparator.naturalOrder()).get(); } static long minFromList(List<Long> longList) { return longList.stream().min(Comparator.naturalOrder()).get(); } public static int sumDigits(int n) { int sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } public static long sumDigits(long n) { long sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } static List<Integer> getIntegerList(Scanner sc, int size) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < size; i++) { list.add(nint(sc)); } return list; } static List<Long> getLongList(Scanner sc, int size) { List<Long> list = new ArrayList<>(); for (int i = 0; i < size; i++) { list.add(nlong(sc)); } return list; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt(); long sum = 0, ans = 0, ans2 = 0; for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum <= 0) { ans += 1 - sum; sum = 1; } else if(i % 2 == 1 && sum >= 0) { ans += sum + 1; sum = -1; } } sum = 0; for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if(i % 2 == 1 && sum <= 0) { ans2 += 1 - sum; sum = 1; } } System.out.println(Math.min(ans, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) s1=c1=s2=c2=0 for i in range(n): if i%2: c1+=max(0,a[i]+s1+1); s1+=min(-s1-1,a[i]) c2+=max(0,-s2+1-a[i]); s2+=max(-s2+1,a[i]) else: c1+=max(0,-s1+1-a[i]); s1+=max(-s1+1,a[i]) c2+=max(0,a[i]+s2+1); s2+=min(-s2-1,a[i]) print(min(c1,c2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> using namespace std; int main() { int N; cin >> N; int a[100010]; for(int i=0; i<N; ++i) cin >> a[i]; long plus=0, minus=0; long psum=0, msum=0; for(int i=0; i<N; ++i){ psum += a[i]; msum += a[i]; if(i%2){ if(psum>=0){ plus += psum+1; psum=-1; } if(msum<=0){ minus += 1-msum; msum=1; } }else{ if(psum<=0){ plus += 1-psum; psum=1; } if(msum>=0){ minus += msum+1; msum=-1; } } } cout << min(plus, minus) << 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": [] }
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[] sum1 = new int[n]; int[] sum2 = new int[n]; sum1[0] = a[0]; sum2[0] = a[0]; long count = 0; //System.out.println(solve1(sum1,a,count)); //System.out.println(solve2(sum2,a,count)); count = Math.min(solve1(sum1,a,count),solve2(sum2,a,count)); System.out.println(count); } public static long solve1(int[] sum,int[] a,long count){ if(sum[0] <= 0){ count += 1 - sum[0]; 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 long solve2(int[] sum,int[] a,long count){ if(sum[0] >= 0){ count += 1 + sum[0]; 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": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { int n,t=0; ll r1=0,r2=0; cin >> n; vector<int> a(n); rep(i,n) cin >> a.at(i); rep(i,n) { t += a.at(i); if(i%2==0 && t<=0) { r1 += abs(-t)+1; t = 1; } else if(i%2 && t>=0) { r1 += t+1; t = -1; } } t = 0; rep(i,n) { t += a.at(i); if(i%2 && t<=0) { r2 += abs(-t)+1; t = 1; } else if(i%2==0 && t>=0) { r2 += t+1; t = -1; } } cout << min(r1,r2) << 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": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >>N; long S0=0; long S1=0; long T0=0; long T1=0; for (int i=0; i<N; i++) { long a; cin >>a; S0 +=a; S1 +=a; if (0==i%2) { if (0>=S0) { S0=1-S0; T0 +=S0; S0=1; } if (0<=S1) { S1=S1+1; T1 +=S1; S1=-1; } } else { if (0<=S0) { S0=S0+1; T0 +=S0; S0=-1; } if (0>=S1) { S1=1-S1; T1 +=S1; S1=1; } } } cout <<min(T0, T1) <<endl; return 0; };