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
java
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(); int[] a = na(n); int[] b = a.clone(); for (int i = 0; i < n; i++) { b[i] = -b[i]; } System.out.println(Math.min(f(a), f(b))); } public static long f(int[] a) { int n = a.length; long sum = 0; long ret = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { // + long d = Math.max(0, 1 - sum); sum += d; ret += d; } else { // - long d = Math.max(0, sum + 1); sum -= d; ret += d; } } return ret; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 t in [1,-1]: r=0 cnt=0 T=t for i in range(N): r+=A[i] if(T>0 and r<=0): cnt+=1-r r+=1-r if(T<0 and r>=0): cnt+=r+1 r-=r+1 T*=-1 ans=min(cnt,ans) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[]a = new int[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); long c1 = 0; long c2 = 0; long sign = 1; long sum = 0; for(int i = 0; i < n; i++) { sum += a[i]; if(sum * sign <= 0) { c1 += Math.abs(sum) + 1; sum = sign; } sign *= -1; } sign = -1; sum = 0; for(int i = 0; i < n; i++) { sum += a[i]; if(sum * sign <= 0) { c2 += Math.abs(sum) + 1; sum = sign; } sign *= -1; } System.out.println(Math.min(c1, c2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> using namespace std; int main(){ int n; long sum1 = 0; long sum2 = 0; long op1 = 0; long op2 = 0; cin >> n; for(int i = 0; i <n; i++){ int tmp; cin >> tmp; sum1 += tmp; sum2 += tmp; if(i%2){ if(sum1 >= 0) { op1 += sum1 + 1; sum1 = -1; } if(sum2 <= 0) { op2 += -sum2 + 1; sum2 = 1; } } else { if(sum1 <= 0) { op1 += -sum1 + 1; sum1 = 1; } if(sum2 >= 0) { op2 += sum2 + 1; sum2 = -1; } } } if(op1 < op2) cout << op1; else cout << op2; 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 = tuple(map(int, input().split())) def f(pm): ans, s = 0, 0 for a in A: s += a if s * pm <= 0: d = abs(s - pm) ans += d s += d * pm pm *= -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<vector> #include<cmath> using namespace std; int main(void) { long n; cin >> n; long a[n]; for(int i=0;i<n;++i) { cin >> a[i]; } long tmp1=0, tmp2=0; long ans1=0, ans2=0; for(int i=1;i<=n;++i){ tmp1 += a[i-1]; tmp2 += a[i-1]; if(i%2){ if(tmp1 <= 0){ ans1 += 1 - tmp1; tmp1 = 1; } if(tmp2 >= 0){ ans2 += 1 + tmp2; tmp2 = -1; } } else{ if(tmp1 >= 0){ ans1 += 1 + tmp1; tmp1 = -1; } if(tmp2 <= 0){ ans2 += 1 - tmp2; tmp2 = 1; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; import java.awt.*; import static java.lang.System.*; import static java.lang.Math.*; public class Main { public static void main(String[]$) { Scanner sc = new Scanner(in); int n=sc.nextInt(); long[] a=new long[n]; long ans1=0,ans2=0; for (int i = 0; i < n; i++) { a[i]=sc.nextLong(); } long temp1=0,temp2=0; for (int i = 0; i <n; i++) { //ans1:a[0]を負とする場合 temp1+=a[i]; temp2+=a[i]; if(i%2==0){ //負にする if(temp1>=0) { ans1 += temp1 + 1; temp1 = -1; } //正にする if(temp2<=0) { ans2 +=1-temp2; temp2 = 1; } }else{ //正にする if(temp1<=0){ ans1+=1-temp1; temp1=1; } //負にする if(temp2>=0){ ans2+=1+temp2; temp2=-1; } } } out.println(min(ans1,ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a_list=[int(i) for i in input().split()] ans=10**20 for flg in (-1,1): c=0 s=0 for a in a_list: s+=a if s==0: c+=1 s=flg if s//abs(s)!=flg: c+=abs(s)+1 s=flg flg*=-1 ans=min(ans,c) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) num = list(map(int, input().split())) def culc(pattern): sum=0 cost=0 for i in range(n): sum+=num[i] if i%2==pattern: if sum>=0: cost+=sum+1 sum=-1 else: if sum<=0: cost+=-sum+1 sum=1 #print("sum is ", sum, "cost is ", cost) #print("") return cost print(min(culc(0), culc(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
def sign(X): return [-1,0,1][(X>=0)+(X>0)] N = int(input()) A = [int(T) for T in input().split()] AFPCnt = 0 AFPNow = 0 for TFP in range(0,N): AFPNow += A[TFP] if sign(AFPNow)!=(-1)**TFP: AFPCnt += abs(AFPNow)+1 AFPNow = (-1)**TFP AFFCnt = 0 AFFNow = 0 for TFF in range(0,N): AFFNow += A[TFF] if sign(AFFNow)!=(-1)**(TFF+1): AFFCnt += abs(AFFNow)+1 AFFNow = (-1)**(TFF+1) print(min(AFPCnt,AFFCnt))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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
_,*l=map(int,open(0).read().split()) def f(s): c=t=0 for i in l: t+=i if s*t<=0: c+=abs(t-s); t=s s*=-1 return c 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 <vector> #include <cmath> #include <algorithm> #include <cstdint> int main() { int n; std::cin >> n; std::vector<int> vec(n); for (auto&& e : vec) std::cin >> e; auto solve = [&](bool positive) { int64_t ret = 0; int sum = 0; for (int i = 0; i < vec.size(); ++i) { sum += vec[i]; if (sum == 0 || positive != sum > 0) { ret += std::abs(sum) + 1; sum = (positive ? 1 : -1); } positive = !positive; } return ret; }; std::cout << std::min(solve(true), solve(false)) << std::endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) m=0 curr=1 ans=0 for i in range(n): m+=a[i] if curr*m<=0: ans+=1-curr*m m=curr curr=-1*curr ans2=0 m=0 curr=-1 for i in range(n): m+=a[i] if curr*m<=0: ans2+=1-curr*m m=curr curr=-1*curr 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
java
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { static final long MOD1=1000000007; static final long MOD2=998244353; public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int N=sc.nextInt(); long[] a=sc.nextLongArray(N); long sum=0; long ans=0; for (int i = 0; i < a.length; i++) { if (i%2==0) { if (sum+a[i]>0) { sum+=a[i]; }else { ans+=-(sum+a[i])+1; sum=1; } }else { if (sum+a[i]<0) { sum+=a[i]; }else { ans+=(sum+a[i])+1; sum=-1; } } } long ans1=0; sum=0; for (int i = 0; i < a.length; i++) { if (i%2==1) { if (sum+a[i]>0) { sum+=a[i]; }else { ans1+=-(sum+a[i])+1; sum=1; } }else { if (sum+a[i]<0) { sum+=a[i]; }else { ans1+=(sum+a[i])+1; sum=-1; } } } System.out.println(Math.min(ans, ans1)); } 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
python3
n = int(input()) a = list(map(int, input().split(" "))) def solve(s1, s2): "if start == true, assume the first of the sum is positive." res = 0 sum = 0 for i in range(n): sum += a[i] if sum <= 0 and i % 2 == s1: res += abs(sum) + 1 sum = 1 elif sum >= 0 and i % 2 == s2: res += abs(sum) + 1 sum = -1 return res print(min(solve(0, 1), solve(1, 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) { Main main = new Main(); main.run(); } public void run() { Scanner sc = new Scanner(System.in); long n= sc.nextInt(); long sum1=0; long sum2=0; long ans1=0; long ans2=0; for(long i=0; i<n; i++) { long a = sc.nextLong(); sum1 += a; sum2 += a; if(i%2==0) { if(sum1 <= 0) { ans1 += (1-sum1); sum1=1; } if(sum2 >= 0) { ans2 += sum2+1; sum2=-1; } }else { if(sum1 >= 0) { ans1 += sum1+1; sum1 = -1; } if(sum2 <= 0) { ans2 += (1-sum2); sum2 = 1; } } } System.out.println(Math.min(ans1, ans2)); 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())) def f(a, sgn): cnt = 0 s = 0 for i in range(n): s += a[i] sgn *= (-1) #判定する符号は交互に変わる if s * sgn <= 0: #sgn一致しなければその積は負 x = s + sgn*(-1) s -= x cnt += abs(x) return cnt ans = min(f(a, 1), f(a, -1)) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include<algorithm> using namespace std; typedef long long ll; ll s1, s2, c1, c2; int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, a; cin >> n; for (int i = 1; i <= n; i++) { cin >> a; s1 += a; s2 += a; if (i % 2) { if (s1 <= 0) c1 += 1 - s1,s1 = 1; if (s2 >= 0) c2 += 1 + s2,s2 = -1; } else { if (s1 >= 0) c1 += 1 + s1,s1 = -1; if (s2 <= 0) c2 += 1 - s2,s2 = 1; } } cout << min(c1, c2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num_count = sc.nextInt(); long[] array = new long[num_count]; for(int i = 0;i < num_count;i++){ array[i] = sc.nextInt(); } long ans1 = 0,sum = 0; for(int i = 0;i < num_count;i++){ if(i % 2 == 0 && (sum + array[i]) <= 0){ long cost = 1 - (sum + array[i]); ans1 += cost; sum = 1; }else if(i % 2 == 1 && (sum + array[i]) >= 0){ long cost = 1 + (sum + array[i]); ans1 += cost; sum = -1; }else{ sum += array[i]; } } long ans2 = 0; sum = 0; for(int i = 0;i < num_count;i++){ if(i % 2 == 1 && (sum + array[i]) <= 0){ long cost = 1 - (sum + array[i]); ans2 += cost; sum = 1; }else if(i % 2 == 0 && (sum + array[i]) >= 0){ long cost = 1 + (sum + array[i]); ans2 += cost; sum = -1; }else{ sum += array[i]; } } long min_ans = ans1 < ans2 ? ans1 : ans2; System.out.println(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
python3
n = int(input()) array = list(map(int, input().split())) total = 0 flag = -1 minus = 0 for tmp in range(len(array)): total += array[tmp] if total * flag <= 0: minus += abs(total*flag)+1 total = flag flag *= -1 total = 0 flag = 1 plus = 0 for tmp in range(len(array)): total += array[tmp] if total * flag <= 0: plus += abs(total*flag)+1 total = flag flag *= -1 print(min(minus, plus))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include <cmath> using namespace std; int main(){ int n;cin>>n; int a[n]; for(int i = 0; n > i; i++)cin>>a[i]; long long sh = 0; long long nw = 0; for(int i = 0; n > i; i++){ nw = (long long)nw+a[i]; if(i % 2 == 0)if(nw<=0)sh = (long long)sh+1-nw,nw=1; if(i % 2 == 1)if(nw>=0)sh = (long long)sh+nw+1,nw=-1; } long long hs = 0; nw = 0; for(int i = 0; n > i; i++){ nw = (long long)nw+a[i]; if(i % 2 == 1)if(nw<=0)hs = (long long)hs+1-nw,nw=1; if(i % 2 == 0)if(nw>=0)hs = (long long)hs+nw+1,nw=-1; } cout << min(hs,sh) << 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; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = sc.nextInt(); } int[] a_sum = new int[n]; a_sum[0] = a[0]; for(int i=1; i<n; i++){ a_sum[i] = a_sum[i-1] + a[i]; } int[] a_sum_copy = new int[n]; for(int i=0; i<n; i++){ a_sum_copy[i] = a_sum[i]; } //奇数番目までの和を正に、偶数番目までの和を負にする long cnt1 = 0; long cnt2 = 0; for(int i=0; i<n; i++){ a_sum[i] += cnt1 + cnt2; if(i%2==0){ if(a_sum[i]<0){ cnt1 += (-a_sum[i]) + 1; a_sum[i] = 1; }else if(a_sum[i]==0){ cnt1 += 1; a_sum[i] = 1; } }else if(i%2==1){ if(a_sum[i]>0){ cnt2 -= a_sum[i] + 1; a_sum[i] = -1; }else if(a_sum[i]==0){ cnt2 -= 1; a_sum[i] = -1; } } } //奇数番目までの和を負に、偶数番目までの和を正にする long cnt3 = 0; long cnt4 = 0; for(int i=0; i<n; i++){ a_sum_copy[i] += cnt3 + cnt4; if(i%2==1){ if(a_sum_copy[i]<0){ cnt3 += (-a_sum_copy[i]) + 1; a_sum_copy[i] = 1; }else if(a_sum_copy[i]==0){ cnt3 += 1; a_sum_copy[i] = 1; } }else if(i%2==0){ if(a_sum_copy[i]>0){ cnt4 -= a_sum_copy[i] + 1; a_sum_copy[i] = -1; }else if(a_sum_copy[i]==0){ cnt4 -= 1; a_sum_copy[i] = -1; } } } if(cnt1-cnt2>cnt3-cnt4){ System.out.print(cnt3-cnt4); }else{ System.out.print(cnt1-cnt2); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include <algorithm> using namespace std; long long n,no,ans,notw,anstw; int main(){ cin>>n; int a; for(int i=0;i<n;i++){ cin>>a; if(no>0){ no+=a; if(no>=0){ ans+=no+1; no=-1; } }else{ no+=a; if(no<=0){ ans+=no*-1+1; no=1; } } if(notw>=0){ notw+=a; if(notw>=0){ anstw+=notw+1; notw=-1; } }else{ notw+=a; if(notw<=0){ anstw+=notw*-1+1; notw=1; } } } cout<<min(ans,anstw)<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class Main { static final Scanner sc = new Scanner(System.in); static final int MOD = (int) 1E9 + 7; static final long INF_L = (long) 4E18; public static void main(String[] args) { int n = nint(); int[] a = nints(n); long ans = min(solve(a, false), solve(a, true)); System.out.println(ans); } static long solve(int[] a, boolean posSum_) { boolean posSum = posSum_; // trueなら偶数項目(0-indexed)までの和が正であるべき long sum = 0; long count = 0; for (int i = 0; i < a.length; i++) { long sumBuff = sum; sum += a[i]; if (posSum && sum > 0 || !posSum && sum < 0) { // do nothing } else { sum = posSum ? 1 : -1; count += abs(-a[i] + sum - sumBuff); } posSum = !posSum; } return count; } @Deprecated static int nint() { return sc.nextInt(); } @Deprecated static int[] nints(int N) { return nints(N, 0, 0); } @Deprecated private static int[] nints(int N, int padL, int padR) { int[] a = new int[padL + N + padR]; for (int i = 0; i < N; i++) a[padL + i] = nint(); return a; } static long nlong() { return sc.nextLong(); } static long[] nlongs(int N) { return nlongs(N, 0, 0); } static long[] nlongs(int N, int padL, int padR) { long[] a = new long[padL + N + padR]; for (int i = 0; i < N; i++) a[padL + i] = nlong(); return a; } static double ndouble() { return sc.nextDouble(); } static double[] ndoubles(int N) { return ndoubles(N, 0, 0); } static double[] ndoubles(int N, int padL, int padR) { double[] d = new double[N + padL + padR]; for (int i = 0; i < N; i++) { d[padL + i] = ndouble(); } return d; } static String nstr() { return sc.next(); } static char[] nchars() { return sc.next().toCharArray(); } static char[] nchars(int padL, int padR) { char[] temp = sc.next().toCharArray(); char[] ret = new char[temp.length + padL + padR]; System.arraycopy(temp, 0, ret, padL, temp.length); return ret; } static char[][] nchars2(int H, int W) { return nchars2(H, W, 0, 0); } static char[][] nchars2(int H, int W, int padLU, int padRD) { char[][] a2 = new char[H + padLU + padRD][W + padLU + padRD]; for (int i = 0; i < H; i++) System.arraycopy(nchars(), 0, a2[padLU + i], padLU, W); return a2; } static long min(long... ls) { return Arrays.stream(ls).min().getAsLong(); } static long max(long... ls) { return Arrays.stream(ls).max().getAsLong(); } static long abs(long a) { return Math.abs(a); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = [int(n) for n in input().split()] def calcSteps(sign): res = 0 s = 0 for i in range(n): if (s+a[i])*sign < 0: s += a[i] else: step = -s -sign -a[i] res += abs(step) s = -sign sign *= -1 return res print(min(calcSteps(-1),calcSteps(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<vector> using namespace std; int main(){ int n,i; cin>>n; vector<long long>a(n+1); for(i=1;i<=n;i++)cin>>a[i]; for(i=1;i<n;i++)a[i+1]+=a[i]; long o=0,oc=0,e=0,ec=0; for(i=1;i<=n;i++){ if(i%2){ if(a[i]+o<1)oc+=1-a[i]-o,o+=1-a[i]-o; if(a[i]+e>-1)ec+=1+a[i]+e,e-=1+a[i]+e; } else{ if(a[i]+o>-1)oc+=1+a[i]+o,o-=1+a[i]+o; if(a[i]+e<1)ec+=1-a[i]-e,e+=1-a[i]-e; } } cout<<min(oc,ec)<<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())) num1, num2 = 0, 0 s = 0 for i in range(n): s += a[i] if i % 2 == 0 and s <= 0: num1 += 1 - s s = 1 elif i % 2 == 1 and s >= 0: num1 += s + 1 s = -1 s = 0 for i in range(n): s += a[i] if i % 2 == 1 and s <= 0: num2 += 1 - s s = 1 elif i % 2 == 0 and s >= 0: num2 += s + 1 s = -1 print(min(num1, num2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) def solve(a, n, bool): # bool = True: 偶数項を足した時に正 change = 0 sum = 0 for i in range(n): sum+=a[i] if sum <= 0 and bool: # 和が正を仮定しているのに0以下なら change += abs(1-sum) sum=1 if sum >=0 and not bool: change += abs(-1-sum) sum=-1 bool = not bool return change print(min(solve(a,n,True),solve(a,n,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; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for(ll i = 0;i < n;i++) int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int a[100000]; rep(i, n) cin >> a[i]; ll ans = 1LL<<62; ll sum[100001] = {}; // parity rep(p, 2) { ll cnt = 0; rep(i, n) { int border = 1+(p+i)%2*-2; sum[i+1] = sum[i] + a[i]; if (border == 1 && sum[i+1] >= border) continue; if (border == -1 && sum[i+1] <= border) continue; cnt += abs(border-sum[i+1]); sum[i+1] = border; } ans = min(ans, cnt); } 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
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)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 = 0 cum = 0 for a in A: sgn *= -1 cum += a if cum * sgn > 0: continue if sgn > 0: cnt += 1 - cum cum = 1 else: cnt += cum + 1 cum = -1 return cnt 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
python3
n=int(input()) a=list(map(int,input().split())) c=0 d=0 e=0 f=0 for i in range(n): k=a[i] e+=k f+=k if i%2==0: if e<=0: c+=1-e e=1 if f>=0: d+=f+1 f=-1 else: if e>=0: c+=1+e e=-1 if f<=0: d+=1-f f=1 print(min(c,d))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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 sumi = 0; long sumi1 = 0; long countPlus = 0; long countMinus = 0; long[] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = Long.valueOf(scan.next()); } // 奇数がプラス for(int i = 0; i < n; i++) { sumi1 = sumi + a[i]; if((i + 1) % 2 == 1) { if(sumi1 <= 0) { countPlus += Math.abs(sumi1) + 1; sumi1 += Math.abs(sumi1) + 1; } } else if((i + 1) % 2 == 0) { if(sumi1 >= 0) { countPlus += Math.abs(sumi1) + 1; sumi1 -= Math.abs(sumi1) + 1; } } sumi = sumi1; } sumi = 0; sumi1 = 0; // 奇数がマイナス for(int i = 0; i < n; i++) { sumi1 = sumi + a[i]; if((i + 1) % 2 == 1) { if(sumi1 >= 0) { countMinus += Math.abs(sumi1) + 1; sumi1 -= Math.abs(sumi1) + 1; } } else if((i + 1) % 2 == 0) { if(sumi1 <= 0) { countMinus += Math.abs(sumi1) + 1; sumi1 += Math.abs(sumi1) + 1; } } sumi = sumi1; } if(countPlus < countMinus) { System.out.println(countPlus); } else { System.out.println(countMinus); } 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<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<climits> #include<iostream> #include<sstream> #include<utility> #include<map> #include<vector> #include<queue> #include<algorithm> #include<set> #include<stack> using namespace std; typedef long long ll; typedef pair<int,int>P; int A[100005],N; ll ch(int p) { ll t=0,r=0; for(int i=0;i<N;i++,p*=-1) { t+=A[i]; if(t*p<=0) { if(p==-1){r+=abs(-1-t);t=-1;} else {r+=abs(1-t);t=1;} } } return r; } int main() { scanf("%d",&N); for(int i=0;i<N;i++)scanf("%d",&A[i]); printf("%lld\n",min(ch(1),ch(-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
n=int(input()) A=list(map(int,input().split())) ans=10**30 for t in range(2): a=0 s=0 for i in range(n): a+=A[i] if t and a<=0: s+=-a+1 a=1 t=0 elif not(t) and a>=0: s+=a+1 a=-1 t=1 else: t=1-t ans=min(s,ans) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n = int(input()) a = list(map(int, input().split())) def solve(a, t): ans = 0 x = 0 for i in a: x += i if t==True and x<1: ans += 1-x x = 1 elif t==False and x>-1: ans += x+1 x = -1 t = not t return ans print(min(solve(a,True), solve(a,False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) nums = list(map(int, input().split())) ans = 10**14 for start in [-1, 1]: before = start cnt = 0 sum_n = 0 for num in nums: sum_n += num if before*sum_n >= 0: if before < 0: cnt += abs(1-sum_n) sum_n = 1 else: cnt += abs(-1-sum_n) sum_n = -1 before = sum_n ans = min(ans, cnt) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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 num1 = 0; long tmp1 = 0; //+-+- for(int i=0; i<n; i++) { tmp1 += a[i]; if(i % 2 == 0) { if(tmp1 <= 0) { num1 += Math.abs(tmp1) + 1; tmp1 += Math.abs(tmp1) + 1; } } else { if(tmp1 >= 0) { num1 += Math.abs(tmp1) + 1; tmp1 -= Math.abs(tmp1) + 1; } } } long num2 = 0; long tmp2 = 0; //-+-+ for(int i=0; i<n; i++) { tmp2 += a[i]; if(i % 2 == 0) { if(tmp2 >= 0) { num2 += Math.abs(tmp2) + 1; tmp2 -= Math.abs(tmp2) + 1; } } else { if(tmp2 <= 0) { num2 += Math.abs(tmp2) + 1; tmp2 += Math.abs(tmp2) + 1; } } } System.out.println(Math.min(num1,num2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <iostream> #include <vector> using namespace std; using ll = long long; int main() { ll n, ans1{}, ans2{}, s1{}, s2{}, t[2] = {1,-1};; cin >> n; vector<ll> a(n); for (ll &x: a) cin >> x; for (int i = 0; i != n; ++i) { s1 += a[i]; s2 += a[i]; auto p = t[i%2]; if (s1 * p <= 0) { ans1 += abs(p - s1); s1 = p; } if (s2 * -p <= 0) { ans2 += abs(p + s2); s2 = -p; } } cout << min(ans1,ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include<bits/stdc++.h> using namespace std; typedef long long lint; const int MAX_N = 1e5; lint N, a[MAX_N+5]; lint solve(lint next){ lint s = 0, cnt = 0; for(int i=0;i<N;i++){ s += a[i]; if(next==1 && s<=0){ cnt += next - s; s = 1; }else if(next==-1 && s>=0){ cnt += s - next; s = -1; } next *= -1; } return cnt; } int main(){ cin >> N; for(int i=0;i<N;i++) cin >> a[i]; cout << min(solve(1), solve(-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
python3
def k(n, a, p): x = 0 c = 0 for i in range(n): s = x + a[i] if p and s > 0 or not p and s < 0: x = s else: c += abs(s) + 1 if p: x = 1 else: x = -1 p = not p return c n = int(input()) a = [int(x) for x in input().split()] print(min(k(n, a, True), k(n, a, False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Arrays; import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int n = scanner.nextInt(); long[] a = new long[n]; IntStream.range(0, n).forEach(i -> a[i] = scanner.nextLong()); // sum1=[1,-1,...], sum2=[-1,1,...] int[] sum1 = new int[n], sum2 = new int[n]; Arrays.fill(sum1, 1); Arrays.fill(sum2, -1); IntStream.range(0, n / 2).forEach(i -> { sum1[2 * i + 1] = -1; sum2[2 * i + 1] = 1; }); System.out.println(Math.min(getResult(a, sum1), getResult(a, sum2))); } } /** * @param a 数値配列 * @param sum 変更したい合計値の配列 * @return 変更すべきステップ数 */ private static long getResult(final long[] a, final int[] sum) { int n = a.length; long now = 0, result = 0; for (int i = 0; i < n; i++) { now += a[i]; if (sum[i] * now <= 0) { result += Math.abs(sum[i] - now); now = sum[i]; } } return 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 = [int(i) for i in input().split()] b = a[::] ans1 = ans2 = 0 sign = 1 total = 0 for i in range(n): total += a[i] if total * sign <= 0: k = abs(total - sign) ans1 += k total = sign sign *= -1 sign = -1 total = 0 for i in range(n): total += b[i] if total * sign <= 0: k = abs(total - sign) ans2 += k total = sign sign *= -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 = list(map(int, input().split())) ans1 = 0 acc = 0 d = 1 for a in A: if (acc + a) * d > 0: acc += a else: ans1 += abs(1 - (acc + a) * d) acc = d d *= -1 ans2 = 0 acc = 0 d = -1 for a in A: if (acc + a) * d > 0: acc += a else: ans2 += abs(1 - (acc + a) * d) acc = d d *= -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.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+1]; long[] b = new long[n+1]; long ans1 =0, ans2 =0,total =0; //a[1]を+とするケース for (int i =1; i<=n; i++) { a[i] = Long.parseLong(sc.next()); b[i] = a[i]; total += a[i]; if (i % 2 == 1 && total <=0) { long p = 1 - total; ans1 += p; total -= a[i]; a[i] += p; total += a[i]; } if (i % 2 == 0 && total >=0) { long p = 1 + total; ans1 += p; total -= a[i]; a[i] -= p; total += a[i]; } } total =0; //a[1]を-とするケース for (int i =1; i<=n; i++) { total += b[i]; if (i % 2 == 0 && total <=0) { long p = 1 - total; ans2 += p; total -= b[i]; b[i] += p; total += b[i]; } if (i % 2 == 1 && total >=0) { long p = 1 + total; ans2 += p; total -= b[i]; b[i] -= p; total += b[i]; } } System.out.print(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
java
import java.io.*; import java.util.*; import java.lang.*; public class Main { 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 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()); } } public static void main(String[] args) { FastScanner fs = new FastScanner(); int n = fs.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; ++i) { a[i] = fs.nextLong(); } // a[0] = fs.nextLong(); // a[1] = fs.nextLong(); // long ans = 0L; // long sum = 0L; // if (a[0] == 0 && a[1] > a[0]) { // sum = -1L; // ans += 1; // } else if (a[0] == 0 && a[1] < a[0]) { // sum = 1L; // ans += 1; // } else { // sum = a[0]; // } // if (check(sum, sum + a[1])) { // sum += a[1]; // } else { // if (sum > 0) { // ans += (sum + a[1] + 1L); // sum = -1L; // } else { // ans += (1L - sum - a[1]); // sum = 1L; // } // } // for (int i = 2; i < n; ++i) { // a[i] = fs.nextLong(); // if (check(sum, sum + a[i])) { // sum += a[i]; // } else { // if (sum > 0) { // ans += (sum + a[i] + 1L); // sum = -1L; // } else { // ans += (1L - sum - a[i]); // sum = 1L; // } // } // } long ans = Math.min(helper(n, a, true), helper(n, a, false)); System.out.println(ans); } private static long helper(int n, long[] a, boolean positive) { long sum = 0L; long ans = 0L; for (int i = 0; i < n; ++i) { if (positive) { if (sum + a[i] <= 0) { ans += (1L - sum - a[i]); sum = 1L; } else { sum += a[i]; } } else { if (sum + a[i] >= 0) { ans += (sum + a[i] + 1L); sum = -1L; } else { sum += a[i]; } } positive = !positive; } return ans; } private static boolean check(long prev, long sum) { if (prev > 0 && sum < 0) { return true; } else if (prev < 0 && sum > 0) { return true; } return 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 <iostream> #include <cstdio> 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++) scanf("%d", 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
n = int(input()) a = list(map(int,input().split())) sum1 = sum2 = cost1 = cost2 = 0 for i in range(n): cur = a[i] sum1 += cur if (sum1<=0, sum1>=0)[i%2]: cost1 += 1+sum1*[-1,1][i%2] sum1 = [1,-1][i%2] sum2 += cur if (sum2>=0, sum2<=0)[i%2]: cost2 += 1+sum2*[1,-1][i%2] sum2 = [-1,1][i%2] 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
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100007]; long long solve() { long long res = 0; long long sum = 0; for(int i=0;i<n;i++) { if(i%2 == 0) { if(sum+a[i] <= 0) { res += llabs(sum+a[i]-1); sum = 1; } else sum += a[i]; } else { if(sum+a[i] >= 0) { res += llabs(sum+a[i]+1); sum = -1; } else sum += a[i]; } } return res; } int main() { cin >> n; for(int i=0;i<n;i++) cin >> a[i]; long long ans = solve(); for(int i=0;i<n;i++) a[i] *= -1; ans = min(ans,solve()); 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 <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> a(n); for(int i=0; i<n; ++i)cin>>a[i]; long long ans1=0LL, ans2=0LL, sum=0LL; for(int i=0; i<n; ++i){ sum+=a[i]; if(i%2==0 && sum<=0){ans1+=1-sum; sum=1;} else if(i%2==1 && sum>=0){ans1+=1+sum; sum=-1;} } sum=0; for(int i=0; i<n; ++i){ sum+=a[i]; if(i%2==1 && sum<=0){ans2+=1-sum; sum=1;} else if(i%2==0 && sum>=0){ans2+=1+sum; sum=-1;} } cout<<min(ans1, ans2)<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
# -*- coding: utf-8 -*- def calc(flag): s = res = 0 for i in range(N): flag = flag * -1 s += a[i] if flag == 1: if s <= 0: res += abs(s) + 1 s = 1 elif flag == -1: if s >= 0: res += abs(s) + 1 s = -1 return res N = int(input()) a = list(map(int, input().split())) ans = min(calc(1), calc(-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
python3
n = int(input()) a = list(map(int,input().split())) def chk(a,t): ans = 0 x = 0 for i in a: x += i if t==True and x<1: ans += 1-x x = 1 elif t==False and x>-1: ans += x+1 x = -1 t=not t return ans print(min(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
python2
n=input() a=map(int,raw_input().split()) #print n #print a a1_sum=0 a1=[] for i in a: a1_sum+=i a1.append(a1_sum) #print a1 #+,- b=[] chn_val=0 for i,val in enumerate(a1): val+=chn_val if i%2==0: if val>0: pass else: t_val=1-val chn_val+=t_val b.append(t_val) else: if val<0: pass else: t_val=-1-val chn_val+=t_val b.append(t_val) ans1=0 if len(b)==0: ans1=0 else: for i in b: ans1+=abs(i) #-,+ b=[] chn_val=0 for i,val in enumerate(a1): val+=chn_val if i%2==0: if val<0: pass else: t_val=-1-val chn_val+=t_val b.append(t_val) else: if val>0: pass else: t_val=1-val chn_val+=t_val b.append(t_val) ans2=0 if len(b)==0: ans2=0 else: for i in b: ans2+=abs(i) print min(ans1,ans2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; vector<ll> a(n); for(int i=0; i<n; i++) cin >> a[i]; ll sum = 0, ans1 = 0; for(int i=0; i<n; i++) { sum += a[i]; if(i%2 == 0 && sum <= 0) { ans1 += 1 - sum; sum = 1; } if(i%2 == 1 && sum >= 0) { ans1 += 1 + sum; sum = -1; } } sum = 0; ll ans2 = 0; for(int i=0; i<n; i++) { sum += a[i]; if(i%2 == 1 && sum <= 0) { ans2 += 1 - sum; sum = 1; } if(i%2 == 0 && sum >= 0) { ans2 += 1 + sum; sum = -1; } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); ArrayList<Integer> a = new ArrayList<>(); for(int i=0; i<n; i++){ a.add(Integer.parseInt(sc.next())); } long ans1 = 0; long sum1 = 0; long sign1 = 1; for(int i=0; i<n; i++){ sum1 += a.get(i); if (sum1 * sign1 <= 0){ ans1 += Math.abs(sum1) + 1; sum1 = sign1; } sign1 *= -1; } long ans2 = 0; long sum2 = 0; long sign2 = -1; for(int i=0; i<n; i++){ sum2 += a.get(i); if (sum2 * sign2 <= 0){ ans2 += Math.abs(sum2) + 1; sum2 = sign2; } sign2 *= -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
python3
n=int(input()) an=list(map(int, input().split())) def check(sign): a_cnt=0 a_sum=0 for a in an: a_sum+=a if a_sum*sign<=0: a_cnt+=abs(a_sum-sign) a_sum=sign sign*=-1 # print(a_sum, a_cnt) return a_cnt print(min(check(1),check(-1)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(System.in); int target = cin.nextInt(); long list[] = new long[target]; for(int i=0;i<target;i++){ list[i] = cin.nextLong(); } long sum_tmp_minus=0L; long sum_tmp_plus=0L; long count_minus=0; long count_plus=0; for(int k=0;k<target;k++){ sum_tmp_minus += list[k]; if(k%2==0){ if(sum_tmp_minus>=0){ count_minus += sum_tmp_minus+1; sum_tmp_minus = -1; } }else{ if(sum_tmp_minus<=0){ count_minus += 1-sum_tmp_minus; sum_tmp_minus=1; } } } for(int l=0;l<target;l++){ sum_tmp_plus += list[l]; if(l%2==0){ if(sum_tmp_plus<=0){ count_plus += 1-sum_tmp_plus; sum_tmp_plus = 1; } }else{ if(sum_tmp_plus>=0){ count_plus += sum_tmp_plus+1; sum_tmp_plus=-1; } } } System.out.println(Math.min(count_minus, count_plus)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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 sum1 = 0; long ans1 = 0; long sum2 = 0; long ans2 = 0; for (int i = 0; i < n; i++) { int a = sc.nextInt(); sum1 += a; sum2 += a; if(i % 2 == 0){ if(sum1 >= 0){ ans1 += sum1 + 1; sum1 = -1; } if(sum2 <= 0){ ans2 += Math.abs(sum2) + 1; sum2 = 1; } }else{ if(sum1 <= 0){ ans1 += Math.abs(sum1) + 1; sum1 = 1; } if(sum2 >= 0){ ans2 += sum2 + 1; sum2 = -1; } } } System.out.println(Math.min(ans1, ans2)); 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())) def tat(t,a,n): ans = 0 ssu = 0 for x in a: ssu += x if ssu*t <= 0: ans += abs(ssu-t) ssu = t t *= -1 return ans print(min(tat(1,a,n),tat(-1,a,n)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, s, t) for (int i = s; i <= t; i++) ll ans1, ans2, sum; int n; int a[100010]; int main() { cin >> n; rep(i, 0, n - 1) cin >> a[i]; sum = 0; for (int i = 0, 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 = 0, 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; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include "bits/stdc++.h" using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; const ll INF = 1LL << 60; const double PI = 3.141592653589793238; const double EPS = 1e-10; int a[100000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } ll ans = INF; for (int b = 0; b < 2; b++) { bool plus = b; ll cost = 0; ll sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (plus) { if (sum <= 0) cost += 1 - sum, sum = 1; } else { if (sum >= 0) cost += sum + 1, sum = -1; } plus ^= 1; } ans = min(ans, cost); } 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
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int N = scan.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = scan.nextInt(); } long[] s = new long[N+1]; s[0] = 0; for (int i = 0; i< N; i++) { s[i+1] = s[i] + a[i]; } // 正、負、正 long answer1 = solve(N, s, 1); // 負、正、負 long answer2 = solve(N, s, 0); System.out.println(Math.min(answer1, answer2)); } public static long solve(int N, long[] s, int pattern) { long answer = 0; long diff = 0; for (int i = 1; i <= N; i++) { if (i % 2 == pattern) { if (0 < s[i]+diff) { continue; } else { answer += Math.abs(1-(s[i]+diff)); diff += (1 - (s[i]+diff)); } } else { if (s[i]+diff < 0) { continue; } else { answer += Math.abs(s[i]+diff+1); diff -= (s[i]+diff+1); } } } return answer; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a=list(map(int,input().split())) b=a ans1=ans2=0 sign=1 total=0 for i in range(n): total+=a[i] if total*sign<=0: k=abs(total-sign) ans1+=k total=sign sign*=-1 sign=-1 total=0 for i in range(n): total+=b[i] if total*sign<=0: k=abs(total-sign) ans2+=k total=sign sign*=-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.*; public class Main { public static void main(String... args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] map = new long[N]; for (int i = 0; i < N; i++) { map[i] = sc.nextLong(); } long ans1 = calcAns(map, 1); long ans2 = calcAns(map, -1); System.out.println(Math.min(ans1, ans2)); } static long calcAns(long[] map, int type) { long ans = 0; long sum = 0; for (int i = 0; i < map.length; i++) { long a = map[i]; sum += a; if (type == -1) { if (sum <= type) { type = 1; continue; } else { ans += sum + 1; type = 1; sum = -1; } } else { if (sum >= type) { type = -1; continue; } else { ans += Math.abs(sum) + 1; type = -1; sum = 1; } } } return ans; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { private static final int mod =(int)1e9+7; public static void main(String[] args) throws Exception { 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(); } long sum=a[0]; long operations=0; if(a.length==1) { if(a[0]!=0) { System.out.println(0); }else { System.out.println(1); } }else { if(sum<=0) { sum=1; operations=Math.abs(a[0]-1); } for(int i=1;i<n;i++) { if(sum>0) { if(sum+a[i]<0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum=-1; operations++; }else { long req=(long)-1-1l*sum; sum=-1; operations+=(-1l*req+a[i]); } } }else { if(sum+a[i]>0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum=1; operations++; }else { long req=(long)1+-1l*sum; sum=1; operations+=(req-a[i]); } } } } sum=a[0]; long op1=0; if(sum>=0) { sum=-1; op1=Math.abs(a[0]+1); } for(int i=1;i<n;i++) { if(sum>0) { if(sum+a[i]<0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum=-1; op1++; }else { long req=(long)-1-1l*sum; sum=-1; op1+=(-1l*req+a[i]); } } }else { if(sum+a[i]>0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum=1; op1++; }else { long req=(long)1+-1l*sum; sum=1; op1+=(req-a[i]); } } } } System.out.println(Math.min(operations,op1)); } } static boolean vis[]=new boolean[10001]; static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Function to find gcd of array of // numbers static int f(int arr[], int n) { int result = n; int max=-1; int ans=0; for (int element: arr){ if(vis[element]==false) result = gcd(n, element); if(result>max) { max=result; ans=element; } } return ans; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
N = int(input()) A = list(map(int,input().split())) ret = 0 def count(nega): ret = 0 s = 0 for e in A: nega = not nega #print(e,ret,s,s+e) if nega and 0 <= s + e: ret += abs(s + e) + 1 s = -1 elif not nega and s + e <= 0: ret += abs(s + e) + 1 s = 1 else: s += e return ret print(min(count(True),count(False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
def sign(p): if p<0: return -1 elif p>0: return 1 else: return 0 N=int(input()) a=[int(i) for i in input().split()] m=[0,0] for sgn in [-1,1]: x=0 S=0 for i in range(N): S+=a[i] if sign(S)==sgn*(-1)**(i%2) or sign(S)==0: x+=abs(S)+1 S=sgn*(-1)**((i+1)%2) m[(sgn+1)//2]=x print(min(m))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) l=[int(i) for i in input().split()] chk1=ans1=chk2=ans2=0 for a,i in enumerate(l): chk1+=i chk2+=i if a%2: if chk1<=0: ans1+=1-chk1 chk1=1 if chk2>=0: ans2+=chk2+1 chk2=-1 else: if chk1>=0: ans1+=1+chk1 chk1=-1 if chk2<=0: ans2+=1-chk2 chk2=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> #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; using namespace std; int n, a[100010]; ll calc(ll s) { ll sum = 0, res = 0; for (int i = 0; i < n; ++i, s *= -1) { sum += a[i]; if (sum * s > 0) continue; if (s > 0) res += (-sum + s), sum += (-sum + s); else res += (sum - s), sum += -(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 <iostream> using namespace std; int main(void){ int n; long long a,b=0,c=0,d=0,e=0; cin>>n; for(int i=0;i<n;i++){ cin>>a; b+=a; c+=a; if(i%2==0){ if(b>=0){ d+=b+1; b=-1; } if(c<=0){ e+=-c+1; c=1; } }else{ if(b<=0){ d+=-b+1; b=1; } if(c>=0){ e+=c+1; c=-1; } } } cout<<min(d,e)<<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 calcOperation(base): sum = 0 ans = 0 sign = base for i in range(n): sum += a[i] if sum * sign >= 0: ans += abs(sum) + 1 sum = -sign sign *= -1 return ans posi = calcOperation(1) nega = calcOperation(-1) print(min(posi, nega))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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.StringTokenizer; public class Main { int n; int[] as; public static void main(String args[]) { new Main().run(); } void run() { FastReader sc = new FastReader(); n = sc.nextInt(); as = new int[n]; for (int i = 0; i < n; i++) { as[i] = sc.nextInt(); } solve(); } void solve() { long[] sums = new long[n]; sums[0] = as[0]; for (int i = 1; i < n; i++) { sums[i] = sums[i - 1] + as[i]; } long evenCount = 0; long evenChange = 0; long[] sumsForEven = sums.clone(); for (int i = 0; i < n; i++) { sumsForEven[i] += evenChange; if (i % 2 == 0 && sumsForEven[i] <= 0) { evenCount += -sumsForEven[i] + 1; evenChange += -sumsForEven[i] + 1; sumsForEven[i] = 1; } else if (i % 2 == 1 && sumsForEven[i] >= 0) { evenCount += sumsForEven[i] + 1; evenChange -= sumsForEven[i] + 1; sumsForEven[i] = -1; } } long oddCount = 0; long oddChange = 0; for (int i = 0; i < n; i++) { sums[i] += oddChange; if (i % 2 == 1 && sums[i] <= 0) { oddCount += -sums[i] + 1; oddChange += -sums[i] + 1; sums[i] = 1; } else if (i % 2 == 0 && sums[i] >= 0) { oddCount += sums[i] + 1; oddChange -= sums[i] + 1; sums[i] = -1; } } System.out.println(Math.min(evenCount, oddCount)); } 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
python2
# -*- coding: utf-8 -*- n = input() a = map(int, raw_input().split()) b = a asum = [0]*len(a) bsum = [0]*len(a) asum[0] = a[0] bsum[0] = b[0] cnt_a = 0 cnt_b = 0 if(a[0]==0): asum[0] = 1 cnt_a = 1 bsum[0] = -1 cnt_b = 1 if(a[0]<0): cnt_a += (-1)*a[0]+1 asum[0] = 1 for i in range(len(a)-1): asum[i+1] = a[i+1] + asum[i] if(i%2==0 and asum[i+1]>=0): cnt_a += asum[i+1]+1 asum[i+1] = -1 elif(i%2==1 and asum[i+1]<=0): cnt_a += (-1)*asum[i+1]+1 asum[i+1] = 1 if(b[0]>0): cnt_b += b[0]+1 bsum[0] = -1 for i in range(len(b)-1): bsum[i+1] = b[i+1] + bsum[i] if(i%2==0 and bsum[i+1]<=0): cnt_b += (-1)*bsum[i+1]+1 bsum[i+1] = 1 elif(i%2==1 and bsum[i+1]>=0): cnt_b += bsum[i+1]+1 bsum[i+1] = -1 print(min(cnt_a,cnt_b))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long c1 = 0, c2 = 0, s1 = 0, s2 = 0; for (int i = 0; i < n; i++) { s1 += a[i]; s2 += a[i]; if (i & 1) { if (s1 >= 0) {c1 += s1 + 1; s1 = -1;} if (s2 <= 0) {c2 += 1 - s2; s2 = 1;} } else { if (s1 <= 0) {c1 += 1 - s1; s1 = 1;} if (s2 >= 0) {c2 += s2 + 1; s2 = -1;} } } 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
cpp
#include<iostream> #define rep(i,n) for(int i=0;i<n;++i) using namespace std; int n, a[100000]; int64_t solve(bool plus) { int64_t ret = 0, sum = 0; rep(i, n) { sum += a[i]; if (plus && sum <= 0 || !plus && sum >= 0) { ret += abs(sum) + 1; sum = plus ? 1 : -1; } plus ^= true; } return ret; } int main() { cin >> n; rep(i, n) cin >> a[i]; cout << min(solve(true), solve(false)) << 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; #define ll long long constexpr ll inf = 1e9+7; ll solve(vector<ll> As, bool flag) { ll ans = 0; ll sum = 0; for (auto A : As) { sum += A; if (flag && sum <= 0) { ans += 1 - sum; sum = 1; } else if (!flag && sum >= 0) { ans += 1 + sum; sum = -1; } flag = !flag; } return ans; } int main () { cin.tie(0); ios::sync_with_stdio(false); ll N; cin>>N; vector<ll> A(N); for (ll n = 0; n < N; n++) cin>>A[n]; cout<<min(solve(A, false), solve(A, true))<<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
/* "It's okay if you hate me. I hate me too." */ #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
python2
def smaller(x, y): if x < y: return x else: return y def judge(a, posneg): cnt = 0 cur = posneg sum = 0 for i in range(0, len(a)): sum = sum + a[i] sumt = sum * cur if sumt < 1: cnt = cnt + abs(cur - sum) sum = cur cur = -1 * cur return cnt n = int(raw_input()) a = map(int, raw_input().split()) print smaller(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.lang.reflect.Array; import java.util.*; import java.util.function.*; import java.util.stream.Collectors.*; public class Main { public static void main(String[] args) { new Main().solve(); } IO io = new IO(); /// ********** 本体 ********** /// void solve() { int n = io.Int(); int[] a = new int[n]; REP(n,integer -> a[integer]=io.Int()); long cost1=0,cost2=0; long sum1=0,sum2=0; //sum is +-+... for(int i=0;i<n;++i){ if(i%2==0){ //sum is + sum1+=a[i]; long addVal = sum1<0?(-sum1)+1: sum1==0?1:0; sum1+=addVal; cost1+=Math.abs(addVal); }else{ //sum is - sum1+=a[i]; long addVal = sum1>0?(-sum1)-1: sum1==0?-1:0; sum1+=addVal; cost1+=Math.abs(addVal); } } //sum is -+-... for(int i=0;i<n;++i){ if(i%2!=0){ //sum is + sum2+=a[i]; long addVal = sum2<0?(-sum2)+1: sum2==0?1:0; sum2+=addVal; cost2+=Math.abs(addVal); }else{ //sum is - sum2+=a[i]; long addVal = sum2>0?(-sum2)-1: sum2==0?-1:0; sum2+=addVal; cost2+=Math.abs(addVal); } } //result long ans = Math.min(cost1,cost2); System.out.println(ans); } void FOR(int a, int b, Consumer<Integer>act) { for(int i = a; i < b; ++i) act.accept(i); } void REP(int a, Consumer<Integer>act) { FOR(0, a, act); } } class IO{ String[] nextBuff; int buffCnt; Scanner sc = new Scanner(System.in); public IO(){ nextBuff = new String[0]; buffCnt = 0; } String next() { if (buffCnt < nextBuff.length) return nextBuff[buffCnt++]; String line = sc.nextLine(); while (line == "") line = sc.nextLine(); nextBuff = line.split(" "); buffCnt = 0; return nextBuff[buffCnt++]; } public String String() { return next(); } public char Char() { return next().charAt(0);} public int Int() { return Integer.parseInt(next());} public long Long() { return Long.parseLong(next());} public double Double() { return Double.parseDouble(next());} }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; long[] ac = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } long answer = Long.MAX_VALUE; for (int i = 0; i < 2; i++) { long t = 0; ac[0] = a[0]; for (int j = 0; j < n; j++) { if ((i + j) % 2 == 0) { if (ac[j] >= 0) { t += ac[j] + 1; ac[j] = -1; } } else { if (ac[j] <= 0) { t += Math.abs(ac[j]) + 1; ac[j] = 1; } } if (j < n - 1) ac[j + 1] = ac[j] + a[j + 1]; } answer = Math.min(answer, t); } System.out.println(answer); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
n=int(input()) a=list(map(int, input().split())) def f(presum, x, fugo): if (presum+x)*fugo<=0: sa=fugo-presum-x prosum=fugo else: sa=0 prosum=presum+x return sa, prosum out1=0 y=0 for i in range(n): x, y=f(y, a[i], (-1)**i) out1+=abs(x) out2=0 y=0 for i in range(n): x, y=f(y, a[i], -(-1)**i) out2+=abs(x) print(min(out1, out2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 <map> #include <string> #include <math.h> typedef long long ll; using namespace std; int main(){ int n; cin >> n; ll a[100001]={0}; for (int i=0;i<n;i++) cin >> a[i]; bool sign=true; ll res=0,res2=0,sum=0; for (int x=0;x<2;x++){ res=0; sum=0; for (int i=0;i<n;i++){ sum+=a[i]; if (sign){ if (sum <= 0){ res+=1-sum; sum=1; } sign=false; } else { if (sum >= 0){ res+=1+sum; sum=-1; } sign=true; } } sign=false; if (x == 0) res2=res; } cout << min(res,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
python3
n = int(input()) A = list(map(int, input().split())) INF = float('inf') ans = INF for sign in (1, -1): res, total_a = 0, 0 for a in A: total_a += a if total_a * sign <= 0: res += abs(total_a - sign) total_a = sign sign *= -1 ans = min(ans, res) 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; 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(); solver.solve(1, in, out); out.close(); } // ====================================================================== static class Solver { long[] A = null; long[] S = null; long calc(boolean mflag, PrintWriter out) { long ans = 0; Arrays.fill(S, 0); for(int i=0; i < A.length; i++) { if(i == 0) S[0] = A[0]; else S[i] = S[i-1] + A[i]; // out.println("A[" + i + "] = " + A[i] + " S[" + i + "] = " + S[i]); if(mflag) { if(S[i] >= 0) { ans += Math.abs(S[i]) +1; S[i] = -1; // out.println("mflag = " + mflag + " ans = " + ans); } mflag = false; } else { if(S[i] <= 0) { ans += Math.abs(S[i]) + 1; S[i] = 1; // out.println("mflag = " + mflag + " ans = " + ans); } mflag = true; } } return ans; } public void solve(int testNumber, MyInput in, PrintWriter out) { int N = in.nextInt(); A = new long[N]; S = new long[N]; for(int i=0; i < N; i++) { A[i] = in.nextLong(); } long ans = Math.min(calc(true, out), calc(false, out)); out.println(ans); } } // ====================================================================== 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); } public String getString() { return "[" + getKey() + "] [" + getValue() + "]"; } } 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 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
java
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { new Main().solve(); } void solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; long A = 0; long ANS = 0; long ans = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { A += a[i]; if (i % 2 == 0 && A <= 0) { ans += Math.abs(A - 1); A = 1; } if (i % 2 == 1 && A >= 0) { ans += Math.abs(A + 1); A = -1; } } ANS = ans; A = 0; ans = 0; for (int i = 0; i < n; i++) { A += a[i]; if (i % 2 == 0 && A >= 0) { ans += Math.abs(A + 1); A = -1; } if (i % 2 == 1 && A <= 0) { ans += Math.abs(A - 1); A = 1; } } ANS = Math.min(ANS, ans); 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> #define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i) using namespace std; signed main(){ int N;cin>>N; vector<long long> A(N); REP(i, N) cin >> A[i]; long long ans=LLONG_MAX; int sig[2]={1,-1}; REP(j,2){ long long sum=0; long long count=0; REP(i,N){ sum+=A[i]; if(i%2==0&&sig[j]*sum<=0){ count+=llabs(sum-sig[j]*1); sum=sig[j]*1; } if(i%2==1&&sig[j]*sum>=0){ count+=llabs(sum+sig[j]*1); sum=-1*sig[j]; } } ans=min(ans,count); } cout<<ans<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
from itertools import accumulate n = int(input()) a = [int(i) for i in input().split()] def n_op(a, p): s = accumulate(a) x = 0 n = 0 for si in s: if p and si + x <= 0: n += 1 - (si + x) x += 1 - (si + x) if (not p) and si + x >= 0: n += 1 + (si + x) x -= 1 + (si + x) p = not p return n answer = min([n_op(a, True), n_op(a, False)]) 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
cpp
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using ll = long long; const ll INF = 1LL << 61; int main(){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; ll ans = INF; for(int a0 : {1,-1,a[0]} ) if(a0 != 0) { ll 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; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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())) B1 = [(-1)**i for i in range(n)] B2 = [(-1)**(i+1) for i in range(n)] def func(A, B): cum = 0 res = 0 for i in range(n): cum += A[i] if B[i] > 0 and cum <= 0: res += 1-cum cum = 1 elif B[i] < 0 and cum >= 0: res += cum+1 cum = -1 return res print(min(func(A, B1), func(A, B2)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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): sum = 0 count = 0 t = first for a in A: sum += a if t == True and sum < 1: count += 1-sum sum = 1 elif t == False and sum > -1: count += sum + 1 sum = -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
python3
_ = int(input()) A = list(map(int, input().split())) def chk(a_list, sgn): cum_n = 0 count = 0 for a in a_list: cum_n += a if sgn>0 and cum_n<=0: count += abs(cum_n-1) cum_n = 1 elif sgn<0 and cum_n>=0: count += abs(cum_n-(-1)) cum_n = -1 sgn = sgn*(-1) return(count) print(min(chk(A, 1), chk(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())) 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
cpp
#include<bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define pb push_back ll n,a[100005],sum,cnt1,cnt2; int main(){ cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ sum+=a[i]; if(i%2 && sum<=0){ cnt1+=1-sum; sum=1; } if(i%2==0 && sum>=0){ cnt1+=sum+1; sum=-1; } } sum=0; for(int i=1;i<=n;i++){ sum+=a[i]; if(i%2==0 && sum<=0){ cnt2+=1-sum; sum=1; } if(i%2 && sum>=0){ cnt2+=sum+1; sum=-1; } } cout<<min(cnt1,cnt2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
python3
def solve(f, A): r, s = 0, 0 for a in A: s += a if s == 0: r += 1 s = f elif s * f < 0: r += -(s * f) + 1 s = f f = -f return r def main(): N = int(input()) A = list(map(int, input().split())) return min(solve(1, A), solve(-1, A)) print(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
python3
n = int(input()) ls = list(map(int, input().split())) def calc(ls, sign): count = 0 acc = 0 for x in ls: y = acc + x if y == 0 or y // abs(y) != sign: z = sign - y count += abs(z) acc = sign else: acc = y sign *= -1 return count count = min(calc(ls, 1), calc(ls, -1)) print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> #define rep(i, n) REP(i, 0, n) #define REP(i, l, r) for (int i = l; i < r; ++i) #define int long long using namespace std; typedef pair<int, int> P; signed main() { int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; if (i) a[i] += a[i - 1]; } int out = LLONG_MAX; rep(i, 2) { int cnt = 0, ans = 0; rep(j, n) { if (j % 2 == i) { ans += max(1 - (a[j] + cnt), 0ll); cnt += max(1 - (a[j] + cnt), 0ll); } else { ans += max((a[j] + cnt) + 1, 0ll); cnt += min(-(a[j] + cnt) - 1, 0ll); } } out = min(out, ans); } cout << out << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int64_t> a(n); for(int i=0;i<n;++i){ cin>>a.at(i); } int64_t S1=0,S2=0,count1=0,count2=0; //odd>0,even<0 for(int i=0;i<n;++i){ S1 += a.at(i); if(i%2!=0 && S1<=0){ count1 += (1-S1); S1=1; } else if(i%2==0 && S1>=0){ count1 += (1+S1); S1=-1; } } //odd<0,even>0 for(int i=0;i<n;++i){ S2 += a.at(i); if(i%2!=0 && S2>=0){ count2 += (1+S2); S2=-1; } else if(i%2==0 && S2<=0){ count2 += (1-S2); S2=1; } } cout<<min(count1,count2)<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { int n; ll c = 0, d = 0, pm = 0, mp = 0; scanf("%d", &n); for(int i = 0; i < n; i++) { int a; scanf("%d", &a); pm += a; mp += a; if(pm * (i % 2 ? -1 : 1) <= 0) { c += abs(pm) + 1; pm = i % 2 ? -1 : 1; } if(mp * (i % 2 ? -1 : 1) >= 0) { d += abs(mp) + 1; mp = i % 2 ? 1 : -1; } } printf("%lld\n", min(c, d)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) using ll = long long; int main(){ int n; cin >> n; ll a[n]; rep(i,n) cin >> a[i]; ll c = 0, d = 0, s = 0, t = 0; rep(i,n) { s += a[i]; t += a[i]; if(i % 2 == 0) { if(s <= 0) { c += 1 - s; s = 1; } if(t >= 0) { d += t + 1; t = -1; } } else { if(s >= 0) { c += s + 1; s = -1; } if(t <= 0) { d += 1 - t; t = 1; } } } cout << min(c,d) << 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 # hello worldと表示する N=int(input()) s=list(map(int,input().split())) def f(N,s,t): ss=0 w=0 for i in range(N): ss+=s[i] if t==1: if ss<=0: w+=1-ss ss=1 t=-1 elif t==-1: if ss>=0: w+=1+ss ss=-1 t=1 return w t=1 a=f(N,s,t) t=-1 b=f(N,s,t) print(min(a,b))