Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define int long long int solvePlus(vector<int> v) { int cnt = 0, sum = 0; for (int i = 0; i < v.size(); i++) { sum += v[i]; if (i % 2 == 1) { cnt = (sum < 0) ? cnt : (cnt + abs(sum) + 1); sum = (sum < 0) ? sum : -1; } else if (i % 2 == 0) { cnt = (sum > 0) ? cnt : (cnt + abs(sum) + 1); sum = (sum > 0) ? sum : 1; } } return cnt; } signed main() { int n; cin >> n; vector<int> a(n), b(n); for (auto&& w : a) cin >> w; copy(begin(a), end(a), begin(b)); for (auto&& w : b) w *= -1; int ans = min(solvePlus(a), solvePlus(b)); 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 <bits/stdc++.h> using namespace std; using ll = long long; int n, a[100000], s = 0; ll x = 0, y = 0; bool b = 0; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i, b = !b) { s += a[i]; if (b) { if (s <= 0) { x += 1 - s; s = 1; } } else if (s >= 0) { x += s + 1; s = -1; } } b = 1; s = 0; swap(x, y); for (int i = 0; i < n; ++i, b = !b) { s += a[i]; if (b) { if (s <= 0) { x += 1 - s; s = 1; } } else if (s >= 0) { x += s + 1; s = -1; } } cout << min(x, y) << 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 in = new Scanner(System.in); int n = in.nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long odd = f(a, n, 1); long even = f(a, n, -1); System.out.println(Math.min(odd, even)); } static long f(long a[], int n, int sign) { long total = 0; long man = 0; for (int i = 0; i < n; i++) { total += a[i]; if (total * sign <= 0) { long x = Math.abs(total) + 1; total += sign * x; man += x; } sign = -sign; } return man; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 posi(long long x){ if(x>0)return 1; if(x<0)return -1; return 0; } int main(){ int N; cin >> N; vector<long long> a(N); for(auto &i:a)cin >> i; long long ans=0,tmp=0; long long sum=a[0]; if(a[0]==0){ tmp=1; sum=-1; } for(int i=1;i<N;i++){ if(posi(sum+a[i])*posi(sum)!=-1 || sum+a[i]==0){ tmp+=abs(sum+a[i])+1; sum=(sum>0)?-1:1; } else sum+=a[i]; } ans=tmp; tmp=abs(a[0])+1; sum=(a[0]>0)?-1:1; for(int i=1;i<N;i++){ if(posi(sum+a[i])*posi(sum)!=-1 || sum+a[i]==0){ tmp+=abs(sum+a[i])+1; sum=(sum>0)?-1:1; } else sum+=a[i]; } ans=min(ans,tmp); 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
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.lang.Character.Subset; import java.math.BigDecimal; import java.text.DecimalFormat; import java.time.temporal.ValueRange; import java.util.AbstractMap; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.ForkJoinPool; import static java.util.Comparator.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyInput in = new MyInput(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(in, out); solver.solve(); out.close(); } // ====================================================================== static class Solver { MyInput in; PrintWriter out; public Solver(MyInput in, PrintWriter out) { this.in = in; this.out = out; } // ----------------------------------------- public void solve() { int N = ni(); long[] A = ndl(N); long cnt = 0; long total = 0; boolean f = true; for (int i = 0; i < N; i++) { total += A[i]; if(f && total <= 0) { cnt += (Math.abs(total) + 1); total = 1; } else if(!f && total >= 0){ cnt += (Math.abs(total) + 1); total = -1; } f = !f; } long ans = cnt; cnt = 0; total = 0; f = false; for (int i = 0; i < N; i++) { total += A[i]; if(f && total <= 0) { cnt += (Math.abs(total) + 1); total = 1; } else if(!f && total >= 0){ cnt += (Math.abs(total) + 1); total = -1; } f = !f; } ans = Math.min(ans, cnt); prn(ans); } // ----------------------------------------- // Integer のキーに対して、カウントを管理する(カウントを足したり、引いたりする) static class MapCounter { private Map<Long, Long> map = new TreeMap<>(); public MapCounter() {} // キーのカウントに値を足す public void add(long key) { add(key, 1); } public void add(long key, int cnt) { Long val = map.get(key); if(val == null) { map.put(key, (long)cnt); } else { map.put(key, val + cnt); } } // キーのカウントから値を引く public void sub(long key) { sub(key, 1); } public void sub(long key, int cnt) { Long val = map.get(key); if(val == null) { map.put(key, (long)-cnt); } else { map.put(key, val - cnt); } } // キーのカウントを取得する(なければ NULLを返す) public Long getCountwithNull(long key) { return map.get(key); } // キーのカウントを取得する(なければ 0 を返す) public Long getCount(long key) { Long val = map.get(key); if(val == null) return 0L; else return val; } public Set<Long> getKey() { return map.keySet(); } // 登録されているキーの数を返す public int getKeyCount() { return map.keySet().size(); } } // ----------------------------------------- // 配列のバイナリーサーチ 1 boolean isRightMin(int[] a, boolean f, int index, int key) { if (f && a[index] >= key) return true; // 以上 else if (!f && a[index] > key) return true; // より大きい else return false; } // 配列 a の中で key 以上(f=true)または、より大きく(f=false)、一番小さい値を返す int binarySearchRightMin(int[] a, boolean f, int key) { int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1 int ok = (int)a.length; // 「index = a.length-1」が条件を満たさないこともあるので、初期値は a.length() /* ok と ng のどちらが大きいかわからないことを考慮 */ while (Math.abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isRightMin(a, f, mid, key)) ok = mid; // 下半分を対象とする else ng = mid; // 上半分を対象とする } return ok; // ← ここで返すのは isOK() が true の時にセットする方(ok / ng) } // ----------------------------------------- // 配列のバイナリーサーチ 2 boolean isLeftMax(int[] a, boolean f, int index, int key) { if (f && a[index] <= key) return true; // 以下 else if (!f && a[index] < key) return true; // より小さい else return false; } // 配列 a の中で key 以下(f=true)または、より小さい(f=false)、一番大きい値を返す int binarySearchLeftMax(int[] a, boolean f, int key) { int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1 int ok = (int)a.length; // 「index = a.length-1」が条件を満たさないこともあるので、初期値は a.length() /* ok と ng のどちらが大きいかわからないことを考慮 */ while (Math.abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isLeftMax(a, f, mid, key)) ng = mid; // 上半分を対象とする else ok = mid; // 下半分を対象とする } return ng; // ← ここで返すのは isOK() が true の時にセットする方(ok / ng) } // ----------------------------------------- // オイラーツアー(部分木対応) static class EulerTour { Graph g; List<Integer> euler_tour = new ArrayList<>(); int[] begin, end; int k = 0, root = 0; void dfs(int v,int p, PrintWriter out) { out.println("v = " + v + " p = " + p); begin[v] = k; euler_tour.add(v); k++; if(!g.contains(v)) { return; } for(int i : g.get(v)) { if(i != p) { dfs(i, v, out); euler_tour.add(v); k++; } } end[v]=k; } // 初期化 public void init(int p_cnt, int root, Graph g, PrintWriter out) { begin = new int[p_cnt + 1]; end = new int[p_cnt + 1]; this.root = root; this.g = g; dfs(root, -1, out); } // 部分木の頂点を渡すと、オイラーツアーの部分木を返す public List getPartTour(int v) { return euler_tour.subList(begin[v], end[v]); } // 部分木の頂点を渡すと、頂点のリストを返す public List<Integer> getPartList(int v) { Set<Integer> set = new TreeSet<>(); set.addAll(getPartTour(v)); List<Integer> ans = new ArrayList<>(); for(Integer p : set) { ans.add(p); } return ans; } } // ----------------------------------------- // グラフのリンクリスト static class Graph { // 頂点に紐づく頂点のリスト private Map<Integer, List<Integer>> data = new HashMap<Integer, List<Integer>>(); // // 全ての頂点のセット // private Set<Integer> point = new TreeSet<>(); // 頂点と頂点の繋がりを追加する void add(int from, int to) { List<Integer> list = data.get(from); if(list == null) { list = new ArrayList<Integer>(); data.put(from, list); } list.add(to); // point.add(key); // point.add(value); } // 指定された頂点に紐づく、頂点のリストを返す List<Integer> get(int key) { return data.get(key); } // 頂点 key が登録されているか? boolean contains(int key) { return data.containsKey(key); } // 頂点のセットを返す Set<Integer> keySet() { return data.keySet(); } // 頂点 key_1 と 頂点 key_2 がつながっていれば true を返す boolean isConnect(int key_1, int key_2) { List<Integer> list = data.get(key_1); if(list == null) return false; else return list.contains(key_2); } // 指定された頂点から、すべての頂点への距離を返す List<PP> distList(int key) { List<PP> dist = new ArrayList<>(); // 頂点と距離のペアのリスト Set<Integer> mark = new HashSet<>(); // 処理したら入れる Stack<PP> stack = new Stack<>(); // スタックの宣言 stack.push(new PP(key, 0)); // スタートをスタックに保存 while(!stack.isEmpty()) { PP wk = stack.pop(); // スタックから次の頂点を取得 int pp = wk.getKey(); int dd = wk.getVal(); mark.add(pp); // 通過マーク dist.add(new PP(pp, dd)); // 距離を登録 List<Integer> list = get(pp); // つながっている頂点のリストを取得 for(int next : list) { if(mark.contains(next)) continue; stack.push(new PP(next, dd + 1)); } } return dist; } // ダンプ void dump(PrintWriter out) { for(int key : data.keySet()) { out.print(key + " : "); for(int val : data.get(key)) { out.print(val + " "); } out.println(""); } } } // ----------------------------------------- // 重さを持ったグラフのリンクリスト static class GraphWith { // キーに紐づくリストに、頂点番号と重さのペアを持つ private Map<Integer, List<PP>> data = new HashMap<Integer, List<PP>>(); // 指定された頂点に紐づく、頂点と重さのペアを追加する void add(int key, PP p) { List<PP> list = data.get(key); if(list == null) { list = new ArrayList<PP>(); data.put(key, list); } list.add(p); } // 頂点に紐づく、頂点と重さのペアのリストを返す List<PP> get(int key) { return data.get(key); } // 頂点 key が登録されているか? boolean contains(int key) { return data.containsKey(key); } // 頂点のセットを返す Set<Integer> keySet() { return data.keySet(); } // 頂点 key_1 と 頂点 key_2 がつながっていれば true を返す boolean isConnect(int key_1, int key_2) { List<PP> list = data.get(key_1); if(list == null) return false; boolean ans = false; for(PP p : list) { if(p.getKey() == key_2) { ans = true; break; } } return ans; } } // ----------------------------------------- // グラフのリンクリスト(Long) static class GraphLong { private Map<Long, List<Long>> G = new HashMap<Long, List<Long>>(); void add(long key, long value) { List<Long> list = G.get(key); if(list == null) { list = new ArrayList<Long>(); G.put(key, list); } list.add(value); } List<Long> get(long key) { return G.get(key); } } // ----------------------------------------- // 重さを持ったグラフのリンクリスト(Long) static class GraphLongWith { private Map<Long, List<PPL>> G = new HashMap<Long, List<PPL>>(); void add(long key, PPL p) { List<PPL> list = G.get(key); if(list == null) { list = new ArrayList<PPL>(); G.put(key, list); } list.add(p); } List<PPL> get(long key) { return G.get(key); } } // ----------------------------------------- void prn(String s) { out.println(s); } void prn(int i) { out.println(i); } void prn(long i) { out.println(i); } void prr(String s) { out.print(s); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } double nd() { return in.nextDouble(); } String ns() { return in.nextString(); } int[] ndi(int n) { int[] ans = new int[n]; for(int i=0; i < n; i++) { ans[i] = ni(); } return ans; } long[] ndl(int n) { long[] ans = new long[n]; for(int i=0; i < n; i++) { ans[i] = nl(); } return ans; } double[] ndd(int n) { double[] ans = new double[n]; for(int i=0; i < n; i++) { ans[i] = nd(); } return ans; } String[] nds(int n) { String[] ans = new String[n]; for(int i=0; i < n; i++) { ans[i] = ns(); } return ans; } int[][] nddi(int n, int m) { int[][] ans = new int[n][m]; for(int i=0; i < n; i++) { for(int j=0; j < m; j++) { ans[i][j] = ni(); } } return ans; } long[][] nddl(int n, int m) { long[][] ans = new long[n][m]; for(int i=0; i < n; i++) { for(int j=0; j < m; j++) { ans[i][j] = nl(); } } return ans; } } // Set に入れるなら PPKEY を使う! static class PP{ public int key, val; public PP(int key, int val) { this.key = key; this.val = val; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public int getVal() { return val; } public void setVal(int val) { this.val = val; } } static class PPL { public long key, val; public PPL(long key, long val) { this.key = key; this.val = val; } public long getKey() { return key; } public void setKey(long key) { this.key = key; } public long getVal() { return val; } public void setVal(long val) { this.val = val; } } static class PPDL { public long key; public long[] val; public PPDL(long key, long[] val) { this.key = key; this.val = val; } public long getKey() { return key; } public void setKey(long key) { this.key = key; } public long[] getVal() { return val; } public void setVal(long[] val) { this.val = val; } public void dump(PrintWriter out) { out.print("key = " + key + " val "); for(int i=0; i < val.length; i++) { out.print("[" + val[i] + "] "); } out.println(""); } } // HashMap のキーに使う → Set に入れるのもこれ(PPでは値での比較が行われない) static final class PPKEY{ private final int key, val; public PPKEY(int key, int val) { this.key = key; this.val = val; } public int getKey() { return key; } public int getVal() { return val; } @Override public boolean equals(Object obj) { if (obj instanceof PPKEY) { PPKEY dest = (PPKEY) obj; return this.key == dest.key && this.val == dest.val; } else { return false; } } @Override public int hashCode() { return Objects.hash(key, val); } } // HashMap のキーに使う → Set に入れるのもこれ(PPでは値での比較が行われない) static final class PPLKEY{ private final long key, val; public PPLKEY(long key, long val) { this.key = key; this.val = val; } public long getKey() { return key; } public long getVal() { return val; } @Override public boolean equals(Object obj) { if (obj instanceof PPKEY) { PPKEY dest = (PPKEY) obj; return this.key == dest.key && this.val == dest.val; } else { return false; } } @Override public int hashCode() { return Objects.hash(key, val); } } // ====================================================================== static class Pair<K, V> extends AbstractMap.SimpleEntry<K, V> { /** serialVersionUID. */ private static final long serialVersionUID = 6411527075103472113L; public Pair(final K key, final V value) { super(key, value); } } static class MyInput { private final BufferedReader in; private static int pos; private static int readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500 * 8 * 2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for (int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public MyInput(InputStream is) { in = new BufferedReader(new InputStreamReader(is)); } public int read() { if (pos >= readLen) { pos = 0; try { readLen = in.read(buffer); } catch (IOException e) { throw new RuntimeException(); } if (readLen <= 0) { throw new MyInput.EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public long nextLong() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0L; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public double nextDouble() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; double ret = 0; if (str[0] == '-') { i = 1; } int cnt = 0; for (; i < len; i++) { if(str[i] == '.') { cnt = 10; continue; } if(cnt == 0) { ret = ret * 10 + str[i] - '0'; } else { ret = ret + ((double)(str[i] - '0') / cnt); cnt *= 10; } } if (str[0] == '-') { ret = -ret; } return ret; } public String nextString() { String ret = new String(nextDChar()).trim(); return ret; } public char[] nextDChar() { int len = 0; len = reads(len, isSpace); char[] ret = new char[len + 1]; for (int i=0; i < len; i++) ret[i] = str[i]; ret[len] = 0x00; return ret; } public char nextChar() { while (true) { final int c = read(); if (!isSpace[c]) { return (char) c; } } } int reads(int len, boolean[] accept) { try { while (true) { final int c = read(); if (accept[c]) { break; } if (str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char) c; } } catch (MyInput.EndOfFileRuntimeException e) { } return len; } static class EndOfFileRuntimeException extends RuntimeException { } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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())) total = [0,0] count = [0,0] for i in range(2): for j in range(N): total[i] += A[j] if (i+j)%2 == 0: if total[i] >= 0: count[i] += abs(total[i]+1) total[i] = -1 else: if total[i] <= 0: count[i] += abs(total[i]-1) total[i] = 1 print(min(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
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class Main { void solve() throws IOException { int N = ni(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = ni(); } long csum = 0; long ans = 0; for (int i = 0; i < N; i++) { csum += a[i]; if(i%2==0 && csum>=0) { ans+= Math.abs(csum - (-1)); csum = -1; } else if (i%2==1 && csum<=0) { ans += Math.abs(csum - 1); csum = 1; } } csum = 0; long ans2 = 0; for (int i = 0; i < N; i++) { csum += a[i]; if(i%2==0 && csum<=0) { ans2 += Math.abs(csum - (1)); csum = 1; } else if (i%2==1 && csum>=0) { ans2 += Math.abs(csum - (-1)); csum = -1; } } out.println(Math.min(ans,ans2)); } final int mod = 1000000007; final BigInteger MOD = BigInteger.valueOf(mod); int upperBound(ArrayList<Long> list, Long target){ int i = Collections.binarySearch(list, target, new UpperBoundComparator<Long>()); return ~i; } class UpperBoundComparator<T extends Comparable<? super T>> implements Comparator<T>{ public int compare(T x, T y){ return (x.compareTo(y) > 0) ? 1 : -1; } } int lowerBound(ArrayList<Long> list, Long target){ int i = Collections.binarySearch(list, target, new LowerBoundComparator<Long>()); return ~i; } class LowerBoundComparator<T extends Comparable<? super T>> implements Comparator<T>{ public int compare(T x, T y){ return (x.compareTo(y) >= 0) ? 1 : -1; } } int mul(int x, int y){ int val = (int)((x * 1L * y) % mod); return val>=0 ? val : val+mod; } int add(int x, int y) { x += y; if(x < 0) x += mod; if(x>=mod) x -= mod; return x; } int sub(int x, int y){ x = add(x,mod-y); if(x < 0) x += mod; if(x>=mod) x -= mod; return x; } String ns() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine(), " "); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(ns()); } long nl() throws IOException { return Long.parseLong(ns()); } double nd() throws IOException { return Double.parseDouble(ns()); } String[] nsa(int n) throws IOException { String[] res = new String[n]; for (int i = 0; i < n; i++) { res[i] = ns(); } return res; } int[] nia(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = ni(); } return res; } long[] nla(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nl(); } return res; } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); tok = new StringTokenizer(""); Main main = new Main(); main.solve(); out.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.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] inputString = br.readLine().split(" "); long[] input = new long[n]; for(int i = 0 ; i < n ; i++) { input[i] = Integer.parseInt(inputString[i]); } long result = -1, keyValue = result; while(keyValue == -1){ keyValue = result; result = 0; long count = 0; for(int i = 0 ; i < n ; i++){ if(i % 2 == 0 && (count + input[i]) * Math.signum(keyValue) >= 0){ result += Math.abs(count + input[i]) + 1; count = (int)Math.signum(keyValue) * -1; }else if(i % 2 != 0 && (count + input[i]) * Math.signum(keyValue) <= 0){ result += Math.abs(count + input[i]) + 1; count = (int)Math.signum(keyValue); }else{ count += input[i]; } } result = (keyValue != -1)? Math.min(result, keyValue): result; } System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> using namespace std; int n; long long a[100005]; long long ans1, ans2, sum; void solver(){ 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; } cout << min(ans1, ans2) << endl; } int main(){ cin >> n; for(int i = 1; i <= n; ++i){ cin >> a[i]; } solver(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 cnt1 = 0; long add1 = 0; //一番目を奇数スタートにする。 for (int i = 0; i < n; i++) { add1 += a[i]; if (i % 2 == 0 && add1 <= 0) { cnt1 += (long) 1 - add1; add1 = 1; } else if (i % 2 == 1 && add1 >= 0) { cnt1 += (long) add1 + 1; add1 = -1; } } long cnt2 = 0; long add2 = 0; for (int i = 0; i < n; i++) { add2 += a[i]; if (i % 2 == 1 && add2 <= 0) { cnt2 += (long) 1 - add2; add2 = 1; } else if (i % 2 == 0 && add2 >= 0) { cnt2 += (long) add2 + 1; add2 = -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> using namespace std; typedef long long ll; const int MAX = 1e5; int n; int a[MAX]; ll solve(bool flag, ll sum) { ll res = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (flag) { if (sum >= 0) { res += sum + 1; sum = -1; } } else { if (sum <= 0) { res += -sum + 1; sum = 1; } } flag = !flag; } return res; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; // 貪欲法で操作回数の最小値を求める // プラスから始まるパターンとマイナスから始まるパターンの2パターン試して、その小さい方が答え cout << min(solve(true, 0), solve(false, 0)) << 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
import copy n=int(input()) a=[int(x) for x in input().rstrip().split()] now=0 ans1=0 for i in range(n): now+=a[i] if i%2==0: if now<=0: ans1+=abs(now)+1 now=1 else: if 0<=now: ans1+=abs(now)+1 now=-1 now=0 ans2=0 for i in range(n): now+=a[i] if i%2==0: if 0<=now: ans2+=abs(now)+1 now=-1 else: if now<=0: ans2+=abs(now)+1 now=1 print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; // ABC 6-C // http://abc006.contest.atcoder.jp/tasks/abc006_3 public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } long answer = solve(nums, 0, 0); if (nums[0] > 0) { answer = Math.min(solve(nums, nums[0], 1), nums[0] + 1 + solve(nums, -1, 1)); } else if (nums[0] < 0) { answer = Math.min(solve(nums, nums[0], 1), Math.abs(nums[0]) + 1 + solve(nums, 1, 1)); } else { answer = Math.min(1 + solve(nums, 1, 1), 1 + solve(nums, -1, 1)); } System.out.println(answer); // // long sum = 0; // long answer = 0; // // for (int i = 0; i < n; i++) { // int a = in.nextInt(); // // if (sum < 0 && sum + a < 0) { // answer += 1 + Math.abs(sum + a); // sum = 1; // } else if (sum > 0 && sum + a > 0) { // answer += 1 + sum + a; // sum = -1; // } else if (sum + a == 0) { // answer++; // if (sum < 0) { // sum = 1; // } else { // sum = -1; // } // } else { // sum += a; // } // } // System.out.println(answer); } public static long solve(int[] nums, long sum, int index) { if (index == nums.length) { return 0; } if (sum < 0 && sum + nums[index] < 0) { return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1); } else if (sum > 0 && sum + nums[index] > 0) { return 1 + sum + nums[index] + solve(nums, -1, index + 1); } else if (sum + nums[index] == 0) { if (sum > 0) { return 1 + solve(nums, -1, index + 1); } else { return 1 + solve(nums, 1, index + 1); } } else { return solve(nums, sum + nums[index], index + 1); } // else if (sum < 0 && sum + nums[index] > 0) { // return solve(nums, sum + nums[index], index + 1); // } else if (sum > 0 && sum + nums[index] < 0) { // return solve(nums, sum + nums[index], index + 1); // } else { // // sum == 0 or sum + nums[index] == 0 // if (sum == 0) { // return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); // } // // sum + nums[index] == 0 // else { // if (sum < 0) { // return Math.abs(sum) + 1 + solve(nums, 1, index + 1); // } else { // return Math.abs(sum) + 1 + solve(nums, -1, index + 1); // } // } // } // else if (sum + nums[index] == 0) { // if (sum < 0) { // return Math.abs(sum) + 1 + solve(nums, 1, index + 1); // } else if (sum > 0) { // return Math.abs(sum) + 1 + solve(nums, -1, index + 1); // } else { // return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); // } // } else if (sum == 0) { // return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); // } else { // return solve(nums, sum + nums[index], index + 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; int main(){ int n; cin>>n; vector<long long> a(n); for(int i=0;i<n;i++)cin>>a[i]; long long res1=0, res2=0, sum=0; for(int i=0;i<n;i++){ sum+=a[i]; if(i%2==0 && sum>=0){//奇数番目までの和を負に res1+= sum+1; sum=-1; } if(i%2==1 && sum<=0){//偶数番目までの和を正に res1+= 1-sum; sum=1; } } sum=0; for(int i=0;i<n;i++){ sum+=a[i]; if(i%2==0 && sum<=0){//奇数番目までの和を正に res2+= 1-sum; sum=1; } if(i%2==1 && sum>=0){//偶数番目までの和を負に res2+= sum+1; sum=-1; } } cout<<min(res1, res2)<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<bits/stdc++.h> typedef long long ll; int a[100010],n; ll s,cnt,ans=1e9; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]); if(a[1]>0)s=a[1]; else cnt=1-a[1],s=1; for(int i=2;i<=n;++i) if(s*(s+a[i])<0)s+=a[i]; else { cnt+=abs(s+a[i])+1; s=((s<0)<<1)-1; } ans=cnt; if(a[1]<0)cnt=0,s=a[1]; else cnt=a[1]+1,s=-1; for(int i=2;i<=n;++i) if(s*(s+a[i])<0)s+=a[i]; else { cnt+=abs(s+a[i])+1; s=((s<0)<<1)-1; } printf("%lld\n",std::min(ans,cnt)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> #define REP(i,n,N) for(int i=(n);i<(int)N;i++) #define p(s) cout<<(s)<<endl #define ck(n,a,b) (a)<=(n)&&(n)<(b) typedef long long ll; using namespace std; const int inf=1e9; int main(){ int n; cin>>n; int a[100010]; REP(i,0,n){ cin>>a[i]; } ll ans[2]; REP(hugou,0,2){ ans[hugou]=0; ll sum=0; REP(i,0,n){ sum+=a[i]; if(i%2==hugou&&sum>=0){ ans[hugou]+=sum+1; sum=-1; }else if(i%2!=hugou&&sum<=0){ ans[hugou]+=-sum+1; sum=1; } } } p(min(ans[0],ans[1])); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
import copy n=int(input()) A=list(map(int,input().split())) def solve(a,f): # print(f,a) ans=0 for i in range(n): if i!=0: a[i]+=a[i-1] if not f: if a[i]>=0: ans+=a[i]+1 a[i]=-1 else: if a[i]<=0: ans+=a[i]*-1+1 a[i]=1 # print(a,ans,f) f^=True return ans x=A.copy() print(min(solve(A,True),solve(x,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.*; import java.io.*; import java.math.*; public class Main { static class Graph0n { private ArrayList<Node0n> dt = new ArrayList<>(); Graph0n(int sz){ for(int i=0; i<sz; i++){ Node0n node1 = new Node0n(); dt.add(node1); } } public void add(int vn, int val){ dt.get(vn).add(val); } public int get(int vn, int index){ return dt.get(vn).get(index); } public ArrayList<Integer> get(int vn){ return dt.get(vn).getAll(); } public int sizeOf(int vn){ return dt.get(vn).size(); } public void clear(){ for(int i=0; i<dt.size(); i++){ dt.get(i).clear(); } } } static class Node0n { //重みなし無向・有向グラフの頂点 private ArrayList<Integer> next_vs = new ArrayList<>(); public void add(int val){ next_vs.add(val); } public int get(int ad){ return next_vs.get(ad); } public ArrayList<Integer> getAll(){ return next_vs; } public int size(){ return next_vs.size(); } public void clear(){ next_vs.clear(); } } static class Edge { int from=-1, v2=-1; long weight; public Edge(int vn, long w){ this.v2 = vn; this.weight = w; } public Edge(int cm, int vn, long w){ this.from = cm; this.v2 = vn; this.weight = w; } } static class Edge2 { int v2; long cost1,cost2; public Edge2(int vn, long w1, long w2){ this.v2 = vn; this.cost1 = w1; this.cost2 = w2; } } static class Comparator_Edge implements Comparator<Edge>{ public int compare(Edge a, Edge b){ if(a.weight>b.weight) return -1; else if(a.weight<b.weight) return 1; else return a.v2-b.v2; } } static class Vector { int x,y; public Vector(int sx, int sy){ this.x=sx; this.y=sy; } } //私が好きなアルゴリズム::累積和・UF木 public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n=sc.nexI(); long[] as = new long[n]; sc.ni(as); //+スタート boolean sum_plus = true; long sum_former = 0; long nhandle = 0; for(int i=0; i<n; i++){ sum_former += as[i]; if(sum_plus){ if(sum_former<=0){ nhandle += (1-sum_former); sum_former = 1; } }else{ if(sum_former>=0){ nhandle += sum_former+1; sum_former=-1; } } sum_plus = !sum_plus; } long ans =nhandle; sum_plus = false; sum_former = 0; nhandle = 0; for(int i=0; i<n; i++){ sum_former += as[i]; if(sum_plus){ if(sum_former<=0){ nhandle += (1-sum_former); sum_former = 1; } }else{ if(sum_former>=0){ nhandle += sum_former+1; sum_former=-1; } } sum_plus = !sum_plus; } ans = min(ans,nhandle); out.println(ans); out.flush(); } private static boolean triangle_inequality(int a, int b, int c){ if((a+b)<c) return false; if((b+c)<a) return false; if((c+a)<b) return false; return true; } private static int INF = (int)1e8; private static long INFL = (long)1e17; private static long e97 = (long)1e9 + 7; private static int abs(int a){ return (a>=0) ? a: -a; } private static long abs(long a){ return (a>=0) ? a: -a; } private static double abs(double a){ return (a>=0) ? a: -a; } private static int min(int a, int b){ return (a>b) ? b : a; } private static long min(long a, long b){ return (a>b) ? b : a; } private static double min(double a, double b){ return (a>b) ? b : a; } private static int max(int a, int b){ return (a>b) ? a : b; } private static long max(long a, long b){ return (a>b) ? a : b; } private static double max(double a, double b){ return (a>b) ? a : b; } private static int minN(int... ins){ int min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; } private static int maxN(int... ins){ int max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; } private static long minN(long... ins){ long min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; } private static long maxN(long... ins){ long max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; } private static int minExAd(int[] dt, int ad){ int min=INF; for(int i=0; i<dt.length; i++){ if((i != ad) && (dt[i] < min)) min = dt[i]; } return min; } private static long minExAd(long[] dt, int ad){ long min=INFL; for(int i=0; i<dt.length; i++){ if((i != ad) && (dt[i] < min)) min = dt[i]; } return min; } private static int minExVal(int[] dt, int ex_val){ int min=INF; for(int i=0; i<dt.length; i++){ if((dt[i] != ex_val) && (dt[i] < min)) min = dt[i]; } return min; } private static long minExVal(long[] dt, long ex_val){ long min=INFL; for(int i=0; i<dt.length; i++){ if((dt[i] != ex_val) && (dt[i] < min)) min = dt[i]; } return min; } private static int maxExAd(int[] dt, int ad){ int max=-INF; for(int i=0; i<dt.length; i++){ if((i != ad) && (dt[i] > max)) max = dt[i]; } return max; } private static long maxExAd(long[] dt, int ad){ long max=-INFL; for(int i=0; i<dt.length; i++){ if((i != ad) && (dt[i] > max)) max = dt[i]; } return max; } private static int maxExVal(int[] dt, int ex_val){ int max=-INF; for(int i=0; i<dt.length; i++){ if((dt[i] != ex_val) && (dt[i] > max)) max = dt[i]; } return max; } private static long maxExVal(long[] dt, long ex_val){ long max=-INFL; for(int i=0; i<dt.length; i++){ if((dt[i] != ex_val) && (dt[i] > max)) max = dt[i]; } return max; } private static boolean same3(long a, long b, long c){ if(a!=b) return false; if(b!=c) return false; if(c!=a) return false; return true; } private static boolean dif3(long a, long b, long c){ if(a==b) return false; if(b==c) return false; if(c==a) return false; return true; } private static double hypod(double a, double b){ return Math.sqrt(a*a+b*b); } private static long factorial(int n) { long ans=1; for(long i=n; i>0; i--){ ans*=i; } return ans; } private static long facP(int n, long p) { long ans=1; for(long i=n; i>0; i--){ ans*=i; ans %= p; } return ans; } private static long lcm(long m, long n){ long ans = m/gcd(m,n); ans *= n; return ans; } private static long gcd(long m, long n) { if(m < n) return gcd(n, m); if(n == 0) return m; return gcd(n, m % n); } private static boolean is_prime(long a){ if(a==1) return false; for(int i=2; i<=Math.sqrt(a); i++){ if(a%i == 0) return false; } return true; } private static long modinv(long a, long p) { //a|p, >1に注意 long b = p, u = 1, v = 0; while (b>0) { long t = a / b; long pe = a%b; a=b; b=pe; pe= u-t*v; u=v; v=pe; } u %= p; if (u < 0) u += p; return u; } private static long[] fac10E97 = null; private static long[] finv10E97 = null; private static void Cinit(int max_sz, long p){ fac10E97 = new long[max_sz+1]; finv10E97 = new long[max_sz+1]; fac10E97[0]=1; finv10E97[0]=1; for(int i=1; i<=max_sz; i++){ fac10E97[i] = (fac10E97[i-1]*i) % p; } finv10E97[max_sz] = modinv(fac10E97[max_sz], p); for(int i=max_sz; i>1; i--){ finv10E97[i-1] = (finv10E97[i]*i) % p; } } private static long C10e97(int n, int k, long p){ long ans = fac10E97[n]; ans *= finv10E97[k]; ans %= p; ans *= finv10E97[n-k]; ans %=p; if(ans<0) return ans+p; else return ans; } private static long C10e97LS(long n, int k, long p){ long ans =1; for(long i=n; i>(n-(long)k); i--){ ans *= i; ans %= e97; } ans *= modinv(facP(k,e97),e97); return ans%e97; } private static int pow(int n, int k){ int ans=1; for(int i=0; i<k; i++) ans *= n; return ans; } private static int pow2(int in){ return in*in; } private static long pow2(long in){ return in*in; } private static double pow2(double in){ return in*in; } private static int getDigit2(long num){ long cf = 1; int d=0; while(num >= cf){ d++; cf = 1<<d; } return d; //numはd桁の数で、2^dより小さい } private static int getDigit10(long num){ long cf = 1; int d=0; while(num >= cf){ d++; cf*=10; } return d; //numはd桁の数で、10^dより小さい } private static boolean isINF(int in){ if(((long)in*20)>INF) return true; else return false; } private static boolean isINFL(long in){ if((in*10000)>INFL) return true; else return false; } private static long pow10E97(long ob, long soeji, long p){ if(ob==0) return 0; if(soeji==0) return 1; if(soeji==2) return (ob*ob)%p; int d = getDigit2(soeji); long[] ob_pow_2pow = new long[d]; ob_pow_2pow[0] = ob; for(int i=1; i<d; i++){ ob_pow_2pow[i] = (ob_pow_2pow[i-1]*ob_pow_2pow[i-1])%p; } long ans=1; for(int i=d-1; i>=0; i--){ if(soeji >= (long)(1<<i)){ soeji -= (long)(1<<i); ans = (ans*ob_pow_2pow[i])%p; } } return ans; } private static int flag(int pos){ return (1<<pos); } private static boolean isFlaged(int bit, int pos){ if((bit&(1<<pos)) > 0) return true; else return false; } private static boolean isFlaged(long bit, int pos){ if((bit&(1<<pos)) > 0) return true; else return false; } private static int deflag(int bit, int pos){ return bit&~(1<<pos); } private static int countFlaged(int bit){ int ans=0; for(int i=0; i<getDigit2(bit); i++){ if((bit&(1<<i)) > 0) ans++; } return ans; } private static void showflag(int bit){ for(int i=0; i<getDigit2(bit); i++){ if(isFlaged(bit,i)) System.out.print("O"); else System.out.print("."); } System.out.println(); } public static int biSearch(int[] dt, int target){ int left=0, right=dt.length-1; int mid=-1; while(left<=right){ mid = (right+left)/2; if(dt[mid] == target) return mid; if(dt[mid] < target) left=mid+1; else right=mid-1; } return -1; } public static int biSearchMax(long[] dt, long target){ int left=-1, right=dt.length; int mid=-1; while((right-left)>1){ mid = left + (right-left)/2; if(dt[mid] <= target) left=mid; else right=mid; } return left;//target以下の最大のaddress } public static int biSearchMaxAL(ArrayList<Integer> dt, long target){ int left=-1, right=dt.size(); int mid=-1; while((right-left)>1){ mid = left + (right-left)/2; if(dt.get(mid) <= target) left=mid; else right=mid; } return left;//target以下の最大のaddress } private static int get_root_uf(int[] parent, int index){ if(parent[index] == index) return index; int root = get_root_uf(parent, parent[index]); parent[index] = root; return root; } private static boolean is_same_uf(int[] parent, int x, int y){ if(get_root_uf(parent,x) == get_root_uf(parent,y)) return true; else return false; } private static void unite_uf(int[] parent, int receiver, int attacker){ parent[get_root_uf(parent,attacker)] = get_root_uf(parent, receiver); } private static int[] Xdir4 = {1,0,0,-1}; private static int[] Ydir4 = {0,1,-1,0}; private static int[] Xdir8 = {1,1,1,0,0,-1,-1,-1}; private static int[] Ydir8 = {1,0,-1,1,-1,1,0,-1}; private static boolean is_in_area(int y, int x, int h, int w){ if(y<0) return false; if(x<0) return false; if(y>=h) return false; if(x>=w) return false; return true; } static void show2(boolean[][] dt, int lit_x, int lit_y){ PrintWriter out = new PrintWriter(System.out); for(int j=0; j<dt.length; j++){ for(int i=0; i<dt[j].length; i++){ if((i==lit_y) && (j==lit_x)) out.print("X"); else if(dt[j][i]) out.print("O"); else out.print("."); } out.println(); } out.flush(); } static void show2(int[][] dt, String cmnt){ PrintWriter out = new PrintWriter(System.out); for(int i=0; i<dt.length; i++){ for(int j=0; j<dt[i].length; j++){ out.print(dt[i][j]+","); } out.println("<-"+cmnt+":"+i); } out.flush(); } static void show2(long[][] dt, String cmnt){ PrintWriter out = new PrintWriter(System.out); for(int i=0; i<dt.length; i++){ for(int j=0; j<dt[i].length; j++){ out.print(dt[i][j]+","); } out.println("<-"+cmnt+":"+i); } out.flush(); } static void disp_que(ArrayDeque<Long> dt){ //上手くいかなかった時用 long a=0; while(dt.size()>0){ a=dt.removeFirst(); System.out.print(a); } System.out.println("\n"); } static void disp_list(List dt){ //上手くいかなかった時用 for(int i=0; i<dt.size(); i++){ System.out.print(dt.get(i)+","); } System.out.println("\n"); } private static void prtlnas(int[] as){ PrintWriter out = new PrintWriter(System.out); for(int i=0; i<as.length; i++){ out.println(as[i]); } out.flush(); } private static void prtlnas(long[] as){ PrintWriter out = new PrintWriter(System.out); for(int i=0; i<as.length; i++){ out.println(as[i]); } out.flush(); } private static void prtspas(int[] as){ PrintWriter out = new PrintWriter(System.out); out.print(as[0]); for(int i=1; i<as.length; i++){ out.print(" "+as[i]); } out.flush(); } private static void prtspas(long[] as){ PrintWriter out = new PrintWriter(System.out); out.print(as[0]); for(int i=1; i<as.length; i++){ out.print(" "+as[i]); } out.flush(); } private static void fill(boolean[] ob, boolean res){ for(int i=0; i<ob.length; i++){ ob[i] = res; }} private static void fill(int[] ob, int res){ for(int i=0; i<ob.length; i++){ ob[i] = res; }} private static void fill(long[] ob, long res){ for(int i=0; i<ob.length; i++){ ob[i] = res; }} private static void fill(char[] ob, char res){ for(int i=0; i<ob.length; i++){ ob[i] = res; }} private static void fill(double[] ob, double res){ for(int i=0; i<ob.length; i++){ ob[i] = res; }} private static void fill(boolean[][] ob,boolean res){for(int i=0;i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}} private static void fill(int[][] ob, int res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}} private static void fill(long[][] ob, long res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}} private static void fill(char[][] ob, char res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}} private static void fill(double[][] ob, double res){for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}} private static void fill(int[][][] ob,int res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}} private static void fill_parent(int[] ob){ for(int i=0; i<ob.length; i++) ob[i]=i; } static 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 nexL() { 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) || b == ':'){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nexI() { long nl = nexL(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nexD() { return Double.parseDouble(next());} public void ni(long[] array2){ for(int i=0; i<array2.length; i++){ array2[i] = nexL(); } return; } public void ni(int[] array2){ for(int i=0; i<array2.length; i++){ array2[i] = nexI(); } return; } public void ni(int[] as, int[] bs){ for(int i=0; i<as.length; i++){ as[i] = nexI(); bs[i] = nexI(); } return; } public void ni(int[] as, int[] bs, int[] cs){ for(int i=0; i<as.length; i++){ as[i] = nexI(); bs[i] = nexI(); cs[i] = nexI(); } return; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 z(s,l): for i in range(n-1): r=l+a[i+1] if r*l>=0: if l<=0: s+=1-r r=1 else: s+=1+r r=-1 l=r return s n=int(input()) a=list(map(int,input().split())) s1=0 l1=a[0] if a[0]<=0: s1=1-a[0] l1=1 s2=0 l2=a[0] if a[0]>=0: s2=a[0]+1 l2=-1 print(min(z(s1,l1),z(s2,l2)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 num, i = 0, a; long long sumT = 0, sumF = 0, ansT = 0, ansF = 0; bool sig = true; cin >> num; for (; i < num; i++) { scanf("%d", &a); sumT += a; sumF += a; if (sig && sumT >= 0) { ansT += sumT + 1; sumT = -1; } else if (!sig && sumT <= 0) { ansT += 1 - sumT; sumT = 1; } if (!sig && sumF >= 0) { ansF += sumF + 1; sumF = -1; } else if (sig && sumF <= 0) { ansF += 1 - sumF; sumF = 1; } sig = !sig; } cout << min(ansT, ansF) << "\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
cpp
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <map> #include <set> using namespace std; #define lli long long int #define REP(i,n) for(int i=0;i<n;i++) #define DEBUG 1 lli n,a[100100]; lli calc(lli s){ lli sum=0,res = 0; for(lli i=0;i<n;i++,s *= -1){ sum += a[i]; if(sum * s > 0)continue; res += abs(sum-s); sum += s*abs(sum-s); } return res; } int main(){ cin>>n; REP(i,n)cin>>a[i]; cout<<min(calc(1),calc(-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
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } int64_t min_ans = INT64_MAX; for (int s = -1; s < 2; s += 2) { int64_t ans = 0LLU; int64_t sum = 0LLU; int sign; for (int i = 0, sign = s; i < n; ++i, sign *= -1) { sum += a[i]; // cout << i << "| sign: " << sign << " sum: " << sum << "\n"; if (sum * sign <= 0) { ans += abs(sign - sum); sum = sign; } } min_ans = min(min_ans, ans); } cout << min_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
python3
n = int(input()) a = list(map(int, input().split())) def chk(a,first): count = 0 x = 0 # sum t = first # True : plus, False : minus for item in a: x += item if t == True and x<1: count += 1-x x = 1 elif t == False and x>-1: count += x+1 x = -1 t = not t return count 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
cpp
#include<bits/stdc++.h> using namespace std; #define all(a) (a).begin(),(a).end() typedef long long ll; ll mod=1000000007; int main() { int n;cin >>n; ll cnt_a=0; ll cnt_b=0; ll sum_a =0; ll sum_b =0; ll sign_a = 1; ll sign_b = -1; for(int i=0;i<n;++i){ ll t;cin >> t; sum_a +=t; sum_b +=t; if (sum_a * sign_a <= 0){ cnt_a += abs(sum_a) + 1; sum_a = sign_a; } if (sum_b * sign_b <= 0){ cnt_b += abs(sum_b) + 1; sum_b = sign_b; } sign_a *=-1; sign_b *=-1; } cout << min(cnt_a,cnt_b) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) A = map(int, input().split()) sum_b = 0 cnt_b = 0 sum_c = 0 cnt_c = 0 for i,a in enumerate(A): sum_b += a if not sum_b or ((sum_b > 0) != ((i % 2) > 0)): cnt_b += abs(sum_b) + 1 sum_b = 1 if ((i % 2) > 0) else -1 sum_c += a if not sum_c or ((sum_c > 0) == ((i % 2) > 0)): cnt_c += abs(sum_c) + 1 sum_c = -1 if ((i % 2) > 0) else 1 print(min(cnt_b, cnt_c))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = list(map(int, input().split())) def sol(S): ret = 0 for a in A[1:]: b = a if S * (S + b) > 0: b = (abs(S) + 1) * (1 if S < 0 else -1) if S + b == 0: b = b - 1 if S > 0 else b + 1 ret += abs(b - a) S += b return ret if A[0] == 0: ans = min(sol(1), sol(-1)) + 1 else: ans = min(sol(A[0]), sol(-A[0] // abs(A[0])) + abs(A[0]) + 1) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; 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 = Long.MAX_VALUE / 2; // + - + - + - long res = 0; long t = 0; for(int i=0;i<n;i++) { t += a[i]; if(i % 2 == 0) { if(t > 0) continue; res += 1 - t; t = 1; } else { if(t < 0) continue; res += t + 1; t = -1; } } ans = Math.min(ans, res); res = 0; t = 0; for(int i=0;i<n;i++) { t += a[i]; if(i % 2 == 1) { if(t > 0) continue; res += 1 - t; t = 1; } else { if(t < 0) continue; res += t + 1; t = -1; } } ans = Math.min(ans, res); 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
cpp
#include<bits/stdc++.h> using namespace std; long long int a[100005]={0}; long long int solve(long long int n, long long int sum){ long long int ans = 0; for(int i=1;i<n;++i){ long long int s = sum + a[i]; if((s^sum) >= 0 || s == 0){ ans += abs(s) + 1; s = (sum < 0 ? 1 : -1); } sum = s; } return ans; } int main(){ int n; cin >> n; for(int i=0;i<n;++i)cin >> a[i]; long long int ans = solve(n, 1) + abs(1-a[0]); ans = min(solve(n, -1) + abs(-1-a[0]), ans); if(a[0] != 0)ans = min(solve(n, a[0]), ans); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <functional> using namespace std; using Int = long long; #define REP(i, n) for (int i = 0; i < n; i++) int main() { int n; cin >> n; Int c1 = 0, c2 = 0, s1 = 0, s2 = 0; REP(i, n) { int a; cin >> a; s1 += a; s2 += a; if (i % 2 == 0) { if (s1 <= 0) c1 += 1 - s1, s1 = 1; if (s2 >= 0) c2 += s2 - (-1), s2 = -1; } else { if (s1 >= 0) c1 += s1 - (-1), 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
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[100010]; cin >> n; for (int i = 1; i <= n; i++)cin >> a[i]; for (int i = 1; i <= n; i++) { s1 += a[i]; s2 += a[i]; 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.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 java.util.function.Function; 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; int N; long calc(boolean f) { long ans = 0, sv = 0, wk; for (int i = 1; i <= N; i++) { wk = R[i] + sv; if(f && wk <= 0) { ans += (1 - wk); sv += (1 - wk); } else if(!f && wk >= 0) { ans += (1 + wk); sv -= (1 + wk); } f = !f; } return ans; } public void solve() { N = ni(); R = new long[N+1]; long a; for (int i = 0; i < N; i++) { a = nl(); R[i+1] = R[i] + a; } long ans1 = calc(true); long ans2 = calc(false); 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.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); long[] a = new long[n]; for (int i = 0; i < n; i++) { long temp = Long.parseLong(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
python3
from itertools import accumulate n, *A = map(int, open(0).read().split()) B = list(accumulate(A)) p0 = n0 = p1 = n1 = 0 for i, b in enumerate(B): if i % 2 == 0: if b+p0-n0 <= 0: p0 += abs(b+p0-n0)+1 if b+p1-n1 >= 0: n1 += abs(b+p1-n1)+1 else: if b+p0-n0 >= 0: n0 += abs(b+p0-n0)+1 if b+p1-n1 <= 0: p1 += abs(b+p1-n1)+1 print(min(p0+n0, p1+n1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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; typedef long long ll; int n; ll a[100000] = {}; ll solve(bool judge){ ll sum = 0, score = 0; for(int i = 0; i < n; i++){ sum += a[i]; if(judge){ if(sum <= 0){ score += 1-sum; sum = 1; } }else{ if(sum >= 0){ score += sum+1; sum = -1; } } judge = !judge; } return score; } int main(){ cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; ll first = solve(true); ll second = solve(false); cout << min(first, second) << 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { private static final int MOD = (int)Math.pow(10, 9); public static void main(String[] args) { FastReader sc = new FastReader(); int n = sc.nextInt(); int[] nums = new int[n]; for (int i = 0; i < nums.length; i++) { nums[i] = sc.nextInt(); } long firstDigitPostive = 0; long prefixSum = 0; for (int i = 0; i < nums.length; i++) { prefixSum += nums[i]; if (i % 2 == 0) { // odd is postive if (prefixSum == 0) { prefixSum++; firstDigitPostive++; } else if (prefixSum < 0) { firstDigitPostive += (1 - prefixSum); prefixSum += 1 - prefixSum; } } else { // even is negative if (prefixSum == 0) { prefixSum--; firstDigitPostive++; } else if (prefixSum > 0) { firstDigitPostive += (1 + prefixSum); prefixSum -= (prefixSum + 1) ; } } } prefixSum = 0; long firstDigitNegative = 0; for (int i = 0; i < nums.length; i++) { prefixSum += nums[i]; if (i % 2 == 0) { // odd is negative if (prefixSum == 0) { prefixSum--; firstDigitNegative++; } else if (prefixSum > 0) { firstDigitNegative += (1 + prefixSum); prefixSum -= (prefixSum + 1) ; } } else { // even is postive if (prefixSum == 0) { prefixSum++; firstDigitNegative++; } else if (prefixSum < 0) { firstDigitNegative += (1 - prefixSum); prefixSum += 1 - prefixSum; } } } System.out.println(Math.min(firstDigitPostive, firstDigitNegative)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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; using ll = long long; ll n, a, c[2], s[2]; int main() { cin >> n; for (int i = 0; i != n; ++i) { cin >> a; for (int j : {0, 1}) { s[j] += a; auto p = 1 - (i + j) % 2 * 2; if (s[j] * p <= 0) { c[j] += abs(p - s[j]); s[j] = p; } } } cout << min(c[0], c[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
java
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class Main implements Runnable { // クラス名はMain1 PrintWriter out = new PrintWriter(System.out); InputReader sc = new InputReader(System.in); public static void main(String[] args) { Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1)); new Thread(null, new Main(), "", 1024 * 1024 * 1024).start(); // 16MBスタックを確保して実行 } public void run() { try { int N = sc.nextInt(); long[] A = new long[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextLong(); } long[] wa = new long[N + 1]; long[] wa2 = new long[N + 1]; for (int i = 1; i <= N; i++) { wa[i] += wa[i - 1] + A[i - 1]; wa2[i] += wa2[i - 1] + A[i - 1]; } //out.println("A:" + Arrays.toString(A)); //out.println("W:" + Arrays.toString(wa)); boolean plus = wa[1] >= 0 ? true : false; long cntp = 0; long cntm = 0; for (int i = 1; i <= N; i++) { wa[i] += cntp - cntm; if (!plus && wa[i] >= 0) { long p = Math.abs(wa[i]) + 1; cntm += p; wa[i] -= p; A[i - 1] -= p; } else if (plus && wa[i] <= 0) { long m = Math.abs(wa[i]) + 1; cntp += m; wa[i] += m; A[i - 1] += m; } plus = !plus; } long ans = cntp + cntm; //out.println("W:" + Arrays.toString(wa)); for (int i = 1; i <= N; i++) { wa[i] = wa2[i]; } plus = wa[1] >= 0 ? false : true; cntp = 0; cntm = 0; for (int i = 1; i <= N; i++) { wa[i] += cntp - cntm; if (!plus && wa[i] >= 0) { long p = Math.abs(wa[i]) + 1; cntm += p; wa[i] -= p; } else if (plus && wa[i] <= 0) { long m = Math.abs(wa[i]) + 1; cntp += m; wa[i] += m; } plus = !plus; } ans = Math.min(ans, cntp + cntm); //out.println("A:" + Arrays.toString(A)); //out.println("WM:" + Arrays.toString(wa2)); out.println(ans); } catch (ArithmeticException ae) { //ae.printStackTrace(); throw new RuntimeException(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } finally { out.flush(); out.close(); } } // 高速なScanner static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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<(int)(n); i++) using namespace std; typedef long long LL; const LL INF=1e15; int main(){ int n; cin >> n; vector<LL> a(n); rep(i,n) cin >> a[i]; LL sum=0, sign=1; LL res1=0, res2=0; rep(i,n){ sum+=a[i]; if(sum*sign<=0){ res1+=abs(sum)+1; sum=sign; } sign*=-1; } sum=0, sign=-1; rep(i,n){ sum+=a[i]; if(sum*sign<=0){ res2+=abs(sum)+1; sum=sign; } sign*=-1; } cout << min(res1,res2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, sum1 = 0, sum2 = 0, b = 0, c = 0, a; cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (i%2) { if (sum1+a < 0) { sum1 += a; } else { b += 1+(sum1+a); sum1 = -1; } if (sum2+a > 0) { sum2 += a; } else { c += 1-(sum2+a); sum2 = 1; } } else { if (sum1+a > 0) { sum1 += a; } else { b += 1-(sum1+a); sum1 = 1; } if (sum2+a < 0) { sum2 += a; } else { c += 1+(sum2+a); sum2 = -1; } } } cout << min(b,c); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
// C - Sequence #include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ int N; cin>>N; vector<ll> A(N); for(int i=0; i<N; ++i) cin>>A[i]; vector<ll> C(2); for(int c=0; c<2; ++c){ int sign = c?1:-1; for(ll s=0, a=0; a<N; ++a, sign*=-1){ s += A[a]; if(sign*s<=0){ C[c]+=abs(s)+1; s=sign; } } } cout<< (C[0]<C[1]?C[0]:C[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
java
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); long ans1 = 0; long sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum <= 0) { ans1 += 1 - sum; sum = 1; } } else { if (sum >= 0) { ans1 += 1 + sum; sum = -1; } } } long ans2 = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum >= 0) { ans2 += 1 + sum; sum = -1; } } else { if (sum <= 0) { ans2 += 1 - sum; sum = 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
cpp
#include<bits/stdc++.h> using namespace std; int main(){ long N;cin>>N; vector<long>A(N),SP(N),SN(N); long ansp=0,ansn=0; for(int i=0;i<N;i++)cin>>A[i]; for(int i=0;i<N;i++){ SP[i]=(i==0?0:SP[i-1])+A[i]; if(i%2==0&&SP[i]<=0){ansp+=1-SP[i];SP[i]=1;} if(i%2==1&&SP[i]>=0){ansp+=SP[i]+1;SP[i]=-1;} } for(int i=0;i<N;i++){ SN[i]=(i==0?0:SN[i-1])+A[i]; if(i%2==0&&SN[i]>=0){ansn+=SN[i]+1;SN[i]=-1;} if(i%2==1&&SN[i]<=0){ansn+=1-SN[i];SN[i]=1;} } cout<<min(ansp,ansn); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 long N,sum,ans,ans2; cin>>N; sum=0; ans=0; ans2=0; vector<long long>x(N); for(int i=0;i<N;i++){ cin>>x[i]; } for(int i=0;i<N;i++){ sum+=x[i]; if(i%2==0&&sum<=0){ ans+=1-sum; sum=1; } if(i%2==1&&sum>=0){ ans+=1+sum; sum=-1; } ans2=ans; } ans=0; sum=0; for(int i=0;i<N;i++){ sum+=x[i]; if(i%2==1&&sum<=0){ ans+=1-sum; sum=1; } if(i%2==0&&sum>=0){ ans+=1+sum; sum=-1; } } cout<<min(ans,ans2)<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) def f(p): sa = 0 ans = 0 for ai in a: if p: sa += ai if sa >= 0: ans += sa + 1 sa = -1 p = False else: sa += ai if 0 >= sa: ans += 1 - sa sa = 1 p = True return ans def main(): print(min(f(True), f(False))) main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> #include<stdlib.h> using namespace std; int main(){ int n; cin>>n; long sum; long input; cin>>sum; long ans1,ans2,sum1,sum2; if(sum>0){ sum1=sum; sum2=-1; ans1=0; ans2=abs(sum)+1; } else if(sum<0){ sum1=1; sum2=sum; ans1=abs(sum)+1; ans2=0; } else{ sum1=1; sum2=-1; ans1=1; ans2=1; } for(long i=1;i<n;++i){ cin>>input; if(sum1*(sum1+input)>=0){ ans1+=abs(sum1+input)+1; if(sum1<0) sum1=1; else sum1=-1; } else sum1+=input; if(sum2*(sum2+input)>=0){ ans2+=abs(sum2+input)+1; if(sum2<0) sum2=1; else sum2=-1; } else sum2+=input; } 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
python3
n = int(input()) a_list = list(map(int,input().split())) ans = float('inf') for i in [1,-1]: ans_i = 0 sums = 0 for a in a_list: sums += a if sums*i <= 0: ans_i += abs(sums-i) sums = i i *= -1 ans = min(ans,ans_i) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<iostream> #include<algorithm> #include<string> #include<cstdlib> #include<map> #include<iomanip> #include<sstream> using namespace std; int main(void){ const long long int mod=1000000007; long long int n,m,a[200005],plsum=0,misum=0,pl=0,mi=0; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; plsum+=a[i]; misum+=a[i]; if(plsum<=0&&i%2==0){pl+=1-plsum;plsum=1;} if(plsum>=0&&i%2==1){pl+=plsum+1;plsum=-1;} if(misum>=0&&i%2==0){mi+=1+misum;misum=-1;} if(misum<=0&&i%2==1){mi+=1-misum;misum=1;} } cout<<min(mi,pl); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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<int> a(N); for(int j=0;j<N;j++){ cin>>a[j]; } long c1=0, c2=0; long sum=0; // a[0]>0 for(int i=0;i<N;i++){ sum += a[i]; if(i%2==1 && sum>=0){ c1 += sum+1; sum =-1; } if(i%2==0 && sum<=0){ c1 += -sum+1; sum = 1; } } // a[0]<0 sum=0; for(int i=0;i<N;i++){ sum += a[i]; if(i%2==1 && sum<=0){ c2 += -sum+1; sum =1; } if(i%2==0 && sum>=0){ c2 += sum+1; sum = -1; } // cout<<sum<<endl; } // cout<<c1<<" "<<c2<<endl; cout<<min(c1,c2)<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import static java.lang.Math.*; import static java.lang.String.format; public class Main { public static void main(String[] args) { solve(); } final static int INF = Integer.MAX_VALUE>>1; final static int MOD = 1_000_000_007; final static int[] dx4 = { 0, 1, 0, -1 }; final static int[] dy4 = { 1, 0, -1, 0 }; final static int[] dx8 = {0, 1, 1, 1, 0, -1, -1, -1}; final static int[] dy8 = {1, 1, 0, -1, -1, -1, 0, 1}; final static Runnable me=()->{}; public static void solve(){ //TODO:Solve problem like *** Scanner sc=new Scanner(); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } put(min(func(a, true), func(a, false))); } private static long func(int[] a,boolean firstIsPlus){ long ans=0; long sum=a[0]; if(firstIsPlus){ if(a[0]<=0){ ans+=abs(a[0])+1; sum=1; } }else{ if(a[0]>=0){ ans+=abs(a[0])+1; sum=-1; } } for (int i = 1; i < a.length; i++) { long nextAns=sum+a[i]; if(firstIsPlus){ if(i%2==0){ if(nextAns<=0){ //だめ sum=1; ans+=abs(nextAns)+1; continue; }else{ sum=nextAns; continue; } }else{ if(nextAns>=0){ //だめ sum=-1; ans+=abs(nextAns)+1; continue; }else{ sum=nextAns; continue; } } }else{ if(i%2==0){ if(nextAns>=0){ //だめ sum=-1; ans+=abs(nextAns)+1; continue; }else{ sum=nextAns; continue; } }else{ if(nextAns<=0){ //だめ sum=1; ans+=abs(nextAns)+1; continue; }else{ sum=nextAns; continue; } } } } return ans; } private static Runnable func(Object... objects){ try{ assert false; return me; }catch(AssertionError e){ return ()->{put(objects);}; } } private static void print(Object... objects){ if(objects.length==1){ System.out.print(objects[0]); }else{ System.out.print(Arrays.toString(objects)); } } private static void put(Object... objects) { print(objects); put(); } private static void put(){ System.out.println(); } private static void runWhenEA(Runnable runnable){ try{ assert false; }catch(AssertionError e){ PrintStream ps=System.out; PrintStream pse=System.err; System.setOut(pse); runnable.run(); System.setOut(ps); } } private static void putM(String name,char[][] mat){ put("---------------------"+name+"-----------------"); for (int i = 0; i < mat.length; i++) { put(Arrays.toString(mat[i])); } } private static void putM(String name,int[][] mat){ put("---------------------"+name+"-----------------"); for (int i = 0; i < mat.length; i++) { put(Arrays.toString(mat[i])); } } private static void putM(String name,long[][] mat){ put("---------------------"+name+"-----------------"); for (int i = 0; i < mat.length; i++) { put(Arrays.toString(mat[i])); } } private static void putM(String name,boolean[][] mat){ put("---------------------"+name+"-----------------"); for (int i = 0; i < mat.length; i++) { put(Arrays.toString(mat[i])); } } final static private class Scanner { 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 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()); } } final static private class FixedIntPair { final public int x, y; final static public FixedIntPair ZEROS=new FixedIntPair(0,0); FixedIntPair(int x, int y) { this.x = x; this.y = y; } public static double distance(FixedIntPair fip1,FixedIntPair fip2){ double x = (double) fip1.x - fip2.x; double y = (double) fip1.y - fip2.y; return Math.sqrt(x*x+y*y); } @Override public int hashCode() { return x*100000+y; } @Override public boolean equals(Object obj) { if(obj==null)return false; if(!(obj instanceof FixedIntPair)) return false; FixedIntPair pair=(FixedIntPair) obj; return this.x==pair.x&&this.y==pair.y; } @Override public String toString() { return format(FixedIntPair.class.getSimpleName()+":(%d,%d)", x, y); } } final static private class FixedLongPair { final public long x, y; final static public FixedLongPair ZEROS=new FixedLongPair(0,0); FixedLongPair(long x, long y) { this.x = x; this.y = y; } public static double distance(FixedLongPair flp1,FixedLongPair flp2){ double x = (double) flp1.x - flp2.x; double y = (double) flp1.y - flp2.y; return Math.sqrt(x*x+y*y); } @Override public int hashCode() { return (int)x+(int)y; } @Override public boolean equals(Object obj) { if(obj==null)return false; if(!(obj instanceof FixedLongPair)) return false; FixedLongPair pair=(FixedLongPair)obj; return this.x==pair.x&&this.y==pair.y; } @Override public String toString() { return format(FixedLongPair.class.getSimpleName()+":(%d,%d)", x, y); } } final static private class Binary{ public static String toZeroPadding(int i){ return format("%"+Integer.toBinaryString(-1).length()+"s",Integer.toBinaryString(i)).replace(' ','0'); } public static String toZeroPadding(long i){ return format("%"+Long.toBinaryString(-1).length()+"s",Long.toBinaryString(i)).replace(' ','0'); } } final static private class Util { static long gcd(long a,long b){ a= abs(a); b= abs(b); if(a<b){ long tmp=a; a=b; b=tmp; } while(b!=0){ long r=a%b; a=b; b=r; } return a; } static int gcd(int a,int b){ a= abs(a); b= abs(b); if(a<b){ int tmp=a; a=b; b=tmp; } while(b!=0){ int r=a%b; a=b; b=r; } return a; } static long lcm(long a,long b){ long gcd=gcd(a,b); long result=b/gcd; return a*result; } static boolean isValidCell(int i,int j,int h,int w){ return i>=0&&i<h&&j>=0&&j<w; } } } /* ...................................................................................................................................... ..................................... ________ ____ __________________________________________ ..................................... ..................................... / _____/| | \ \__ ___/\_ _____/\______ \..................................... ...................................../ \ ___| | / | \| | | __)_ | _/..................................... .....................................\ \_\ \ | / | \ | | \ | | \..................................... ..................................... \______ /______/\____|__ /____| /_______ / |____|_ /..................................... ..................................... \/ \/ \/ \/ ..................................... ...................................................................................................................................... .............................................................,;'';:................................................................... ........................................................+@@@@@@@@@@@@@@'.............................................................. .....................................................#@@@##############@@@:........................................................... ...................................................@@@####################@@,......................................................... .................................................@@#########################@@........................................................ ...............................................:@############################@@....................................................... ..............................................@@######@@@#';;'#@@@############@@:..................................................... .............................................@#####@@,````````````,@@###########@:.................................................... ............................................@####@;``````````````````+@##########@.................................................... ...........................................@###@:``````````````````````#@########@@................................................... ..........................................@####``````````````````````````@########@@.................................................. .........................................###@.````````````````````````````@########@+................................................. .........................................@#@```````````````````````````````#########@................................................. ........................................@#@`````````````````````````````````########@@................................................ .......................................,@@```````````````````````````````````@#######@:............................................... .......................................@@`````````````````````````````````````@#######@............................................... .......................................@:````````````````````#@@'``````````````@######@+.............................................. ......................................#@```````````````````@@@@@@@#````````````########@.............................................. ......................................@```````````````````@@@@@@@@@@````````````@######@+............................................. ......................................@``````````````````@@@@@@@+ +```````````+#######@............................................. .....................................;:``````````````````@@@@@@@ @````````````@######@'............................................ .....................................@``````````````````:@@@@@@@ @````````````@#######@............................................ .....................................@```,@@@#``````````;@@@@@@@ @````````````:#######@:........................................... .....................................@``@@@@@@@@````````.@@@@@@@# ,#`````````````@#######@........................................... .....................................@`@@@@@@@+'@````````@@@@@@@@@@@``````````````@#######@........................................... .....................................@,@@@@@@ ,```:+:``:@@@@@@@@@.``````````````@########@.......................................... .....................................#@@@@@@@ ;@@#;,,,@``:@@@@@@@````````````````#########@.......................................... .....................................+@@@@@@@@',,,,,,,,;,```.'+;``````````````````'########@;......................................... .....................................'@@@@',,,,,,,,,,,,,@`````````````````````````:#########@......................................... ....................................:@#,,,,,:;;;;;:,,,,,@`````````````````````````.#########@......................................... .................................:@#@@@@#++';;;;;;;;;;;;@``````````````````````````##########+........................................ ...............................#@#+;;;;;;;;;;;;;;;;;;;;':``````````````````````````##########@........................................ ....................................,@#@@@@@#+'';;;;;+@#```````````````````````````##########@........................................ .....................................@``````````.,,,.``````````````````````````````############....................................... .....................................@`````````````````````````````````````````````#######+'+#@....................................... .....................................@`````````````````````````````````````````````##########'@....................................... .....................................#`````````````````````````````````````````````############@#..................................... .....................................:.````````````````````````````````````````````##############@,................................... ......................................+```````````````````````````````````````````.###############@#.................................. ......................................@```````````````````````````````````````````.################@@................................. ......................................@```````````````````````````````````````````.###+##############@................................ ......................................@```````````````````````````````````````````.###+###############@............................... ......................................',``````````````````````````````````````````.####'##############@@.............................. .......................................@```````````````````````````````````````````#####+##############@:............................. .......................................@```````````````````````````````````````````#####'###############@............................. .......................................@```````````````````````````````````````````######'################............................ .......................................#,``````````````````````````````````````````#######'##############@............................ ........................................@``````````````````````````````````````````@######++##############+........................... ........................................@``````````````````````````````````````````@#######'##############@........................... ........................................@``````````````````````````````````````````@########'#############@........................... .......................................@#'`````````````````````````````````````````@#########'##############.......................... .......................................@#@`````````````````````````````````````````+#########+'############@.......................... ......................................@##@`````````````````````````````````````````.##########+'###########@.......................... ......................................@##@:`````````````````````````````````````````###########+'###########.......................... .....................................:@###@`````````````````````````````````````````@###########+'+#########,......................... .....................................@####@`````````````````````````````````````````@#############''########.......................... .....................................@####@.````````````````````````````````````````;##############+'######@.......................... .....................................@#####@`````````````````````````````````````````################@@@###+.......................... .....................................@#####@`````````````````````````````````````````@###############@..;;............................ ....................................,@#####@.````````````````````````````````````````+################'............................... ....................................:#######@`````````````````````````````````````````################@............................... ....................................:#######@`````````````````````````````````````````@###############@............................... ....................................,@#######,````````````````````````````````````````:###############@............................... .....................................@######@@`````````````````````````````````````````@##############@............................... .....................................@######@@`````````````````````````````````````````+##############@............................... .....................................@#####@,;;`````````````````````````````````````````@#############@............................... .....................................@####@@..@`````````````````````````````````````````+#############@............................... .....................................,####@...@``````````````````````````````````````````@############+............................... ......................................@##@.....@`````````````````````````````````````````:###########@,............................... .......................................@+......@``````````````````````````````````````````@##########@................................ ...............................................:#``````````````````````````````````````````##########@................................ ................................................@``````````````````````````````````````````+########@,................................ ................................................'+``````````````````````````````````````````@#######@................................. .................................................@```````````````````````````````````````````@#####@:................................. .................................................'#``````````````````````````````````````````.#####@.................................. ..................................................@```````````````````````````````````````````;###@................................... ...................................................@```````````````````````````````````````````+#@'................................... ...................................................'#```````````````````````````````````````````@#.................................... ....................................................##`````````````````````````````````````````@#..................................... .....................................................#@```````````````````````````````````````@+...................................... ......................................................:@;```````````````````````````````````;@,....................................... .......................................................;@@'```````````````````````````````:@@+;....................................... .......................................................@,,'@@'``````````````````````````@@@,,,@....................................... ......................................................@,,,,,,'@@@@;````````````````.+@@@;,,,,,@....................................... ......................................................#@+@,,,,,,,,+@@@@@@@@@@@@@@@@@;,,,,,'@@@........................................ .........................................................+,,,#',,@@..............@,,,,,,,,@........................................... ..........................................................@@@,#@@,...............:+,,,'@,,@........................................... ..................................................................................@,,,@.##............................................ ...................................................................................@;@................................................ ....................................................................................:................................................. ...................................................................................................................................... ...................................................................................................................................... */
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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())) #正/負いずれかから始める cost1=0 cost2=0 sum1=0 sum2=0 for i in range(n): sum1=sum1+a[i] sum2=sum2+a[i] if i%2==0: if sum1<1: cost1+=1-sum1 sum1=1 if sum2>-1: cost2+=1+sum2 sum2=-1 else: if sum1>-1: cost1+=1+sum1 sum1=-1 if sum2<1: cost2+=1-sum2 sum2=1 print(min(cost1,cost2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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.nextLong(); } long x = 0; long sum = a[0]; if(sum <= 0) { x = 1 - sum; sum = 1; } long x1 = seq(sum, x, a); x = 0; long sum2 = a[0]; if(sum2 >= 0) { x = sum2 + 1; sum2 = -1; } long x2 = seq(sum2, x, a); System.out.println(Math.min(x1, x2)); } public static long seq(long sum, long x, long[] a) { long count = 0; for(int i = 1; i < a.length; i++) { if(sum * (sum + a[i]) >= 0) { if(sum < 0) { count = -sum - a[i] + 1; } else { count = -sum - a[i] - 1; } } sum += a[i] + count; x += Math.abs(count); count = 0; } return x; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a= (list(map(int,input().split()))) sm1=sm2=0 cnt1=cnt2=0 for i ,num in enumerate(a): sm1+=num if sm1<=0 and i%2==0: cnt1+=1-sm1 sm1=1 elif sm1>=0 and i%2!=0: cnt1+=1+sm1 sm1=-1 sm2+=num if sm2<=0 and i%2!=0: cnt2+=1-sm2 sm2=1 elif sm2>=0 and i%2==0: cnt2+=1+sm2 sm2=-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
cpp
#include <bits/stdc++.h> using namespace std; #define int long long int solvePlus(vector<int> v) { int cnt = 0, sum = 0; for (int i = 0; i < v.size(); i++) { sum += v[i]; if (i % 2 == 1) { cnt = (sum < 0) ? cnt : (cnt + abs(sum) + 1); sum = (sum < 0) ? sum : -1; } else if (i % 2 == 0) { cnt = (sum > 0) ? cnt : (cnt + abs(sum) + 1); sum = (sum > 0) ? sum : 1; } } return cnt; } signed main() { int n; cin >> n; vector<int> a(n); for (auto&& w : a) cin >> w; int ans1, ans2; ans1 = solvePlus(a); for (auto&& w : a) w *= -1; ans2 = solvePlus(a); cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { // Scanner sc = new Scanner(System.in); FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); int a[]=sc.readArray(n); long sum = 0, pm = 0, mp = 0; for(int i=0; i<n; ++i){ sum += a[i]; if(i%2 == 0 && sum <= 0){ pm += 1-sum; sum = 1; } if(i%2 != 0 && sum >= 0){ pm += 1+sum; sum = -1; } } sum = 0; for(int i=0; i<n; ++i){ sum += a[i]; if(i%2 == 0 && sum >= 0){ mp += 1+sum; sum = -1; } if(i%2 != 0 && sum <= 0){ mp += 1-sum; sum = 1; } } System.out.println(Math.min(pm,mp)); out.flush(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sortReverse(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); // Collections.sort.(l); Collections.sort(l,Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(long n) { long[] a=new long[(int)n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(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
python3
input() l=list(map(int,input().split())) def f(b): c=s=0 for i in l: if b: if s+i>0: s+=i else: c-=s+i-1; s=1 else: if s+i<0: s+=i else: c+=s+i+1; s=-1 b=1-b return c print(min(f(0),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> using namespace std; int a[100005]; int n; long long calc(int MOD) { long long sum = 0, res = 0; for (int i = 1; i <= n; ++i) { sum += a[i]; if (i%2 == MOD && sum <= 0) { res += (1-sum); sum = 1; } else if (i%2 != MOD && sum >= 0) { res += (sum+1); sum = -1; } } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); cout << min(calc(0), 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.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = scan.nextLong(); } long sum1 = 0; long sum2 = 0; long ans1 = 0; long ans2 = 0; for (int i = 0; i < n; i++) {//偶数添字が正 sum1 += a[i]; if (i%2 == 0) { if (sum1 > 0) continue; else { ans1 += (1 + Math.abs(sum1)); sum1 = 1; } } else if (i%2 == 1) { if (sum1 < 0) continue; else { ans1 += (sum1 + 1); sum1 = -1; } } } for (int i = 0; i < n; i++) {//奇数添字が正 sum2 += a[i]; if (i%2 == 1) { if (sum2 > 0) continue; else { ans2 += (1 + Math.abs(sum2)); sum2 = 1; } } else if (i%2 == 0) { if (sum2 < 0) continue; else { ans2 += (sum2 + 1); sum2 = -1; } } } if (sum1 == 0) { ans1++; } if (sum2 == 0) { 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
python3
N = int( input()) A = list( map( int, input().split())) ans = 10**15 for i in [1, -1]: ansi, sums = 0, 0 for a in A: sums += a if sums*i <= 0: ansi += abs(sums-i) sums = i i *= -1 ans = min( ans, ansi) 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; /** * https://abc059.contest.atcoder.jp/tasks/arc072_a */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.next()); long[] a = new long[N]; for(int i=0; i<N; i++) a[i] = sc.nextLong(); sc.close(); long ans = 0; long ans2 = 0; long sum = 0; for(int i=0; i<N; i++){ if(i%2==0 && sum+a[i]>=0){ ans = ans + (a[i]+sum) +1; sum = -1; }else if(i%2==1 && sum+a[i]<=0){ ans = ans - (a[i]+sum) +1; sum = 1; }else{ sum = sum + a[i]; } } sum = 0; for(int i=0; i<N; i++){ if(i%2==1 && sum+a[i]>=0){ ans2 = ans2 + (a[i]+sum) +1; sum = -1; }else if(i%2==0 && sum+a[i]<=0){ ans2 = ans2 - (a[i]+sum) +1; sum = 1; }else{ sum = 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.PrintWriter; import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); //入力 int n = sc.nextInt(); long[] a = new long[n]; for(int i = 0; i < n; i++){ a[i] = sc.nextLong(); } sc.close(); //処理 long ans = -1; for(int pat = 0; pat < 2; pat++){ long sum = 0; long temp = 0; for(int i = 0; i < n; i++){ sum += a[i]; if(i % 2 == pat){ if(sum <= 0){ temp += Math.abs(sum) + 1; sum = 1; } }else{ if(sum >= 0){ temp += Math.abs(sum) + 1; sum = -1; } } } if(ans == -1){ ans = temp; }else{ ans = Math.min(ans, temp); } } //出力 out.println(ans); out.flush(); } static class Pair{ int w,v; public Pair(int a, int b){ this.w = a; this.v = b; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) a = list(map(int,input().split())) S = [0]*N S[0] = a[0] for i in range(1,N): S[i] = S[i-1] + a[i] count = [0]*N num = [0]*2 for i in range(2): value = 0 for j in range(N): if (S[j]+value)*((-1)**(i+j)) <= 0: num[i] += abs(S[j] + value - (-1)**(i+j)) value += (-1)**(i+j) - (S[j]+value) #print(i,num,value) #print(S) print(min(num))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; auto f = [&](int sign) { long long ans = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sign * sum <= 0) { ans += -1 * sign * sum + 1; sum = sign; } sign = sum < 0 ? 1 : -1; } return ans; }; cout << min(f(1), f(-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 <vector> #include <algorithm> using namespace std; const long long int inf = 1e18; int main(){ int n; cin >> n; vector<int> a(n); for(int i=0; i<n; i++){ cin >> a[i]; } long long int ans = inf; for(int r=0; r<2; r++){ long long int sum=0, t=0; for(int i=0; i<n; i++){ sum += a[i]; if(i%2==r && sum<=0){ t += -sum+1; sum = 1; }else if(i%2!=r && sum>=0){ t += sum+1; sum = -1; } } ans = min(ans, t); } 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
# -*- coding: utf-8 -*- N = int(input()) An = list(map(int, input().split())) ans = [0,0] asum = 0 for i in range(N): asum += An[i] if asum*(-1)**i < 1: ans[0] += 1 - asum*(-1)**i asum = (-1)**i asum = 0 for i in range(N): asum += An[i] if asum*(-1)**(i+1) < 1: ans[1] += 1 - asum*(-1)**(i+1) asum = (-1)**(i+1) 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
cpp
#include<iostream> using namespace std; int main(){ int n;cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; long long s1=0,s2=0,r1=0,r2=0,ans; for(int i=0;i<n;i++){ s1+=a[i]; if((i&1)&&s1<=0){ r1=r1+1-s1; s1=1; }else if(!(i&1)&&s1>=0){ r1=r1+s1+1; s1=-1; } s2+=a[i]; if(!(i&1)&&s2<=0){ r2=r2+1-s2; s2=1; }else if((i&1)&&s2>=0){ r2=r2+s2+1; s2=-1; } } ans=min(r1,r2); 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, *A = map(int, open(0).read().split()) def sgn(n): return 0 if n==0 else 1 if n>0 else -1 a = A[0] C = [0 if a>0 else -a+1, 0 if a<0 else a+1] S = [max(1, a), min(-1, a)] for a in A[1:]: for i, s in enumerate(S): sgn_sum = sgn(s) if sgn(s+a) == -sgn_sum: S[i] += a else: C[i] += abs(s+a+sgn_sum) S[i] = -sgn_sum print(min(C))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<long long> a(n); long long 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; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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(_) for _ in input().split()] def f(sign): ans = 0 s = 0 for i in range(N): a = A[i] s += a if s == 0: ans += 1 s = sign else: a_sign = s // abs(s) if a_sign != sign: ans += abs(s) + 1 s = sign sign *= -1 return ans 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<iostream> #include <algorithm> #include <string> using namespace std; int main() {int N; int C[200000]; cin >> N; for(int i=0; i<N; i++){ cin >> C[i];} long long cnt_a=0; long long cnt_b=0; int sum_a=0; int sum_b=0; for(int i=0; i<N; i++){ sum_a += C[i]; if((i%2==0)&&(sum_a<=0)){ cnt_a += 1 - sum_a; sum_a = 1;} if((i%2==1)&&(sum_a>=0)){ cnt_a += sum_a + 1; sum_a = -1;} } for(int i=0; i<N; i++){ sum_b += C[i]; if((i%2==0)&&(sum_b>=0)){ cnt_b += 1 + sum_b; sum_b = -1;} if((i%2==1)&&(sum_b<=0)){ cnt_b += 1 - sum_b; sum_b = 1;} } cout << min(cnt_a, cnt_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
#include<bits/stdc++.h> using namespace std; int main(){ int m,i; int64_t ans0=0,ans1=0,t=0; cin>>m; vector<int64_t> x(m); for(i=0;i<m;i++) cin>>x.at(i); for(i=0;i<m;i++){ t+=x.at(i); if(i%2==0&&t<=0){ ans0+=abs(t)+1; t=1; } else if(i%2==1&&t>=0){ ans0+=abs(t)+1; t=-1; } } t=0; for(i=0;i<m;i++){ t+=x.at(i); if(i%2==0&&t>=0){ ans1+=abs(t)+1; t=-1; } else if(i%2==1&&t<=0){ ans1+=abs(t)+1; t=1; } } cout<<min(ans0,ans1)<<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())) y = [0, 0] for k in range(2): s = 0 x = (-1) ** k for ai in a: si = s + ai if si * x > 0: s = si x *= -1 else: y[k] += abs(si) + 1 s = x x *= -1 print(min(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
python3
N = int(input()) A = [int(_n) for _n in input().split()] res0 = 0 s = 0 for i, a in enumerate(A): s += a if i % 2 == 0 and s >= 0: res0 += s + 1 s = -1 elif i % 2 == 1 and s <= 0: res0 += -s + 1 s = 1 res1 = 0 s = 0 for i, a in enumerate(A): s += a if i % 2 == 1 and s >= 0: res1 += s + 1 s = -1 elif i % 2 == 0 and s <= 0: res1 += -s + 1 s = 1 print(min(res0,res1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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
#abc059c n=int(raw_input()) a=map(int,raw_input().split()) def f(a,s): ac=0 cnt=0 for i in xrange(n): ac+=a[i] if i%2: if s*ac>=0: cnt+=1+s*ac ac=-s else: if s*ac<=0: cnt+=1-s*ac ac=s 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 = int(input()) A = list(map(int, input().split())) cnt = [0,0] for i in [0,1]: tmp_sum = 0 for j, a in enumerate(A): tmp_sum += a if (i + j) % 2 == 0 and tmp_sum <= 0: cnt[i] += abs(tmp_sum) + 1 tmp_sum += abs(tmp_sum) + 1 elif (i + j) % 2 == 1 and tmp_sum >= 0: cnt[i] += tmp_sum + 1 tmp_sum -= tmp_sum + 1 print(min(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
N = int(input()) A = list(map(int, input().split())) def sol(S): ret = 0 for a in A[1:]: b = a if S * (S + b) > 0: b = (abs(S) + 1) * (1 if S < 0 else -1) if S + b == 0: b = b - 1 if S < 0 else b + 1 ret += abs(b - a) S += b return ret if A[0] == 0: ans = min(sol(1), sol(-1)) + 1 else: ans = min(sol(A[0]), sol(-1 if A[0] > 0 else 1) + abs(A[0]) + 1) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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() { 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 == 1) { 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
cpp
#include <iostream> using namespace std; int N, a, i, x, y; long long X, Y; int main() { cin >> N; while (cin >> a && ++i) { x += a, y += a; if (i % 2) { if (x > -1) X += x + 1, x = -1; if (y < 1) Y += 1 - y, y = 1; } else { if (y > -1) Y += y + 1, y = -1; if (x < 1) X += 1 - x, x = 1; } } cout << min(X, Y) << "\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
python3
n=int(input()) a=list(map(int,input().split())) a1,a2,s=0,0,0 def cal(a,t): ans,x=0,0 for i in a: x+=i if t 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(cal(a,True),cal(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
python2
n = int(raw_input()) data = map(int, raw_input().split()) result = [] for i in range(2): sum = 0 count = 0 for j in range(n): sum += data[j] if (i+j) % 2 == 0: if sum <= 0: count += abs(1-sum) sum = 1 else: if sum >= 0: count += abs(-1-sum) sum = -1 result.append(count) print min(result)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) a = list(map(int, input().split())) ans1 = 0 s = 0 flg = 1 for ai in a: s += ai if s * flg <= 0: ans1 += abs(s) + 1 s = flg flg *= -1 ans2 = 0 s = 0 flg = -1 for ai in a: s += ai if s * flg <= 0: ans2 += abs(s) + 1 s = flg flg *= -1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) int main() { int n; cin >> n; vector<ll> A(n); ll a,now1=0,cnt1=0,now2=0,cnt2=0; rep(i,n) cin >> A[i]; rep(i,n) { now1 += A[i]; now2 += A[i]; if (now1<=0) { cnt1+=abs(now1)+1;now1=1; } if (now2>=0) { cnt2+=abs(now2)+1;now2=-1; } swap(now1,now2);swap(cnt1,cnt2); } 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
cpp
#include <iostream> #include<algorithm> using namespace std; typedef long long ll; ll s1, s2, c1, c2; int main() { ll n, a; scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &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
python3
n=int(input()) a=[int(i) for i in input().split()] if a[0]>=0: f=1 else: f=-1 s=0 ans=0 for i in range(n): s+=a[i] if s*f<0: ans+=abs(s)+1 s=f elif s==0: ans+=1 s=f f*=-1 if a[0]>=0: f=-1 else: f=1 s=0 ans2=0 for i in range(n): s+=a[i] if s*f<0: ans2+=abs(s)+1 s=f elif s==0: ans2+=1 s=f f*=-1 print(min(ans,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans1, ans2 = 0, 0 cnt1, cnt2 = 0, 0 cur = 1 for i in a: cnt1 += i if cnt1 * cur <= 0: ans1 += abs(cnt1 - cur) cnt1 = cur cur *= -1 cur = -1 for i in a: cnt2 += i if cnt2 * cur <= 0: ans2 += abs(cnt2 - cur) cnt2 = cur cur *= -1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = [int(x) for x in input().split()] def F(A, sgn): cnt_operation = 0 cum = 0 for a in A: sgn *= (-1) cum += a if cum * sgn > 0: continue if sgn > 0: cnt_operation += (1 - cum) cum = 1 else: cnt_operation += (cum + 1) cum = -1 return cnt_operation answer = min(F(A,1), F(A,-1)) 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
java
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] sa = br.readLine().split(" "); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } br.close(); long val = 0; long now = a[0]; if (a[0] <= 0) { val += 1 - a[0]; now = 1; } for (int i = 1; i < n; i++) { now += a[i]; if (i % 2 == 0) { if (now <= 0) { val += 1 - now; now = 1; } } else { if (now >= 0) { val += now + 1; now = -1; } } } long ans = val; val = 0; now = a[0]; if (a[0] >= 0) { val += a[0] + 1; now = -1; } for (int i = 1; i < n; i++) { now += a[i]; if (i % 2 != 0) { if (now <= 0) { val += 1 - now; now = 1; } } else { if (now >= 0) { val += now + 1; now = -1; } } } ans = Math.min(ans, val); 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
python3
N = int(input()) A = [int(_) for _ in input().split()] #positive pos_count = 0 sign = 1 cumsum = 0 for a in A: cumsum += a if cumsum * sign <= 0: pos_count += abs(sign - cumsum) cumsum = sign sign *= -1 #negative neg_count = 0 sign = -1 cumsum = 0 for a in A: cumsum += a if cumsum * sign <= 0: neg_count += abs(sign - cumsum) cumsum = sign sign *= -1 print(min(pos_count, neg_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 = [int(i) for i in input().split()] def judge(A,t): a_sum = 0 cnt = 0 for i in range(len(A)): a_sum += A[i] if i % 2 == 0: if t*a_sum <= 0: cnt += 1-t*a_sum a_sum = t else: if t*a_sum >= 0: cnt += 1 + t*a_sum a_sum = -t return cnt print(min(judge(A,1),judge(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.util.Scanner; public class Main { private static Scanner scan = new Scanner(System.in); public static void main(String[] args) { int N = Integer.parseInt(scan.next()); long a[] = new long[N]; for (int i = 0; i < N; i++) { a[i] = Long.parseLong(scan.next()); } long s1 = 0; long s2 = 0; long count1 = 0; long count2 = 0; for (int i = 0; i < N; i++) { s1 += a[i]; s2 += a[i]; if(i%2 == 0) { if(s1 <= 0) { count1 += (1-s1); s1 = 1; } if(s2 >= 0) { count2 += (s2+1); s2 = -1; } } else { if(s1 >= 0) { count1 += (s1+1); s1 = -1; } if(s2 <= 0) { count2 += (1-s2); s2 = 1; } } } System.out.println(count1 < count2 ? count1 : count2); scan.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,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; const lint INF=1LL<<61; int main(){ int n; cin>>n; vector<int> a(n); rep(i,n) cin>>a[i]; lint ans=INF; for(int a0:{1,-1,a[0]}) if(a0!=0) { lint tmp=abs(a0-a[0]),sum=a0; for(int i=1;i<n;i++){ sum+=a[i]; if(sum-a[i]>0){ if(sum>=0){ tmp+=sum+1; sum=-1; } } else if(sum-a[i]<0){ if(sum<=0){ tmp+=1-sum; sum=1; } } } ans=min(ans,tmp); } 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
java
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { static void solve() { int n = nextInt(); long[] a = nextLongArray(n); long ans; long sum; boolean expectPlus; // a1 > 0 の場合 ans = 0; sum = 0; expectPlus = true; for (int i = 0; i < n; i++) { sum += a[i]; if (expectPlus && sum <= 0) { ans += -sum + 1; sum = 1; } else if (!expectPlus && sum >= 0) { ans += sum + 1; sum = -1; } expectPlus = !expectPlus; } long ans1 = ans; // a1 < 0 の場合 ans = 0; sum = 0; expectPlus = false; for (int i = 0; i < n; i++) { sum += a[i]; if (expectPlus && sum <= 0) { ans += -sum + 1; sum = 1; } else if (!expectPlus && sum >= 0) { ans += sum + 1; sum = -1; } expectPlus = !expectPlus; } long ans2 = ans; ans = Math.min(ans1, ans2); out.println(ans); } static final int MOD = 1_000_000_007; static long[] fac, finv, inv; // 階乗(n!) static long factorial(long n) { long ans = 1; for (long i = n; i > 0; i--) { ans = ans * i % MOD; } return ans; } // nCkの初期化 static 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; } } // nCkの計算 static long com(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } static PrintWriter out; static Scanner sc; static int[][] newIntArray(int h, int w, int value) { int[][] ret = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { ret[i][j] = value; } } return ret; } static int nextInt() { return Integer.parseInt(sc.next()); } static long nextLong() { return Long.parseLong(sc.next()); } static String nextString() { return sc.next(); } static int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } static long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } static List<Integer> nextIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } static List<Double> nextDoubleList(int n) { List<Double> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add((double) nextInt()); } return list; } static List<Long> nextLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } static char[][] nextCharArray(int h, int w) { char[][] c = new char[h][w]; for (int i = 0; i < h; i++) { String str = nextString(); for (int j = 0; j < w; j++) { c[i][j] = str.charAt(j); } } return c; } static <T extends Comparable<? super T>> void sort(List<T> list) { Collections.sort(list); } // greatest common divisor // 最大公約数 static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } // least common multiple // 最小公倍数 static long lcm(long a, long b) { if (a >= b && a % b == 0) return a; if (b > a && b % a == 0) return b; if (a > b) { return (a / gcd(a, b)) * b; } else { return (b / gcd(a, b)) * a; } // return a * b / gcd(a, b); } // baseのn乗を計算を返す static int pow(int base, int n) { int ret = 1; for (int i = 0; i < n; i++) { ret *= base; } return ret; } // return n^k mod m static long powMod(long n, long k, long m) { if (k == 0) { return 1; } else if (k % 2 == 1) { return powMod(n, k - 1, m) * n % m; } else { long tmp = powMod(n, k / 2, m); return tmp * tmp % m; } } // intをlength桁のbit文字列に変換 static String toBitString(int length, int n) { StringBuilder sb = new StringBuilder(); for (int i = length - 1; i >= 0; i--) { if ((n >> i) % 2 == 1) { sb.append("1"); } else { sb.append("0"); } } return sb.toString(); } public static void main(String[] args) { out = new PrintWriter(System.out); sc = new Scanner(System.in); solve(); out.flush(); sc.close(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ansl = [0, 0] s = [0, 0] for i in range(2): for j in range(n): s[i] += a[j] if (i+j) % 2 == 0: if s[i] >=0: ansl[i] += abs(s[i]) + 1 s[i] = -1 else: if s[i] <=0: ansl[i] += abs(s[i]) + 1 s[i] = 1 print(min(ansl))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 long int sum1=0,prev1=0; long long int sum2=0,prev2=0; long long int ans1=0, ans2=0; for(int i=0;i<n;i++){ long long int a; cin >> a; sum1 += a; sum2 += a; if(i%2==0){ if(sum1 >= 0){ ans1 += abs(sum1)+1; sum1 = -1; } if(sum2 <= 0){ ans2 += abs(sum2)+1; sum2 = 1; } } else { if(sum1 <= 0){ ans1 += abs(sum1)+1; sum1 = 1; } if(sum2 >= 0){ ans2 += abs(sum2)+1; sum2 = -1; } } prev1 = sum1; prev2 = sum2; } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; vector<int>a(n); for (int i = 0; i < n; i++)cin >> a.at(i); ll ans = 1e18; bool x = true; for (int i = 0; i < 2; i++) { ll res = 0; ll sig = 0; for (int j = 0; j < n; j++) { if (x) { sig += a.at(j); x = false; if (sig > 0)continue; else { res += abs(1 - sig); sig = 1; } } else { sig += a.at(j); x = true; if (sig < 0)continue; else { res += abs(-1 - sig); sig = -1; } } } x = false; ans = min(ans, res); } 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> #include<sstream> #include<vector> #include<iterator> #include<cmath> using namespace std; long long f(int sign, const vector<long long>& a) { long long cnt = 0; long long sum = 0; for(int i=0; i<a.size(); i++) { sum += a[i]; if(sign*sum <= 0) { long long d = sign - sum; sum += d; cnt += abs(d); } sign *= -1; } return cnt; } int main(void) { int n; cin >> n; vector<long long> a(n); for(int i=0; i<n; i++) cin >> a[i]; long long sum[2]; sum[0] = f(1, a); sum[1] = f(-1, a); cout << min(sum[0], sum[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
cpp
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <math.h> #define MOD 1000000007 typedef long long ll; using namespace std; int main(){ int n; cin>>n; ll a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } ll ans1=0,ans2=0; ll s=0; for(int i=0;i<n;i++){ s+=a[i]; if(i%2==0&&s<=0){ ans1+=1-s; s=1ll; }else if(i%2==1&&s>=0){ ans1+=s+1; s=-1ll; } } s=0; for(int i=0;i<n;i++){ s+=a[i]; if(i%2==1&&s<=0){ ans2+=1-s; s=1ll; }else if(i%2==0&&s>=0){ ans2+=s+1; s=-1ll; } } 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
python3
n=int(input()) l=list(map(int,input().split())) count1=0 count2=0 sum1,sum2=0,0 now=1#一致させたい符号 for i in range(n):#第一項は正の時 sum1+=l[i] if sum1*now>0: pass else: count1+=abs(now-sum1) sum1=now now*=-1 now=-1 for i in range(n):#第一項はhuの時 sum2+=l[i] if sum2*now>0: pass else: count2+=abs(now-sum2) sum2=now now*=-1 print(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())) def calc(t): global a ans = 0 s = 0 for i in a: s += i if s * t <= 0: ans += abs(s-t) s = t t *= -1 return ans print(min(calc(1),calc(-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; using ll = long long; const int INF = 1e9; const int MOD = 1e9 + 7; int main() { int n; cin >> n; vector<ll> a(n); for (auto &e : a) cin >> e; ll ans = 1e16; for (int i = 0; i < 2; ++i) { ll cnt = 0, sum = 0; for (int j = 0; j < n; ++j) { sum += a[j]; if (j % 2 == i) { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { cnt += abs(sum) + 1; sum = -1; } } } ans = min(cnt, ans); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) A=list(map(int,input().split())) ans=10**15 for i in[-1,1]: ansi,sum=0,0 for a in A: sum+=a if sum*i<=0:ansi+=abs(sum-i);sum=i i*=-1 ans=min(ans,ansi) print(ans)