Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int N; cin >> N; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; ll ret = 0; ll sum = A[0]; for (int i = 1; i < N; i++) { ll after = sum + A[i]; if (after * sum < 0) { sum = after; } else { ret += abs(after) + 1; if (sum > 0) sum = -1; else sum = 1; } } ll ret2 = 0; ll sum2 = -A[0]; for (int i = 1; i < N; i++) { ll after = sum + A[i]; if (after * sum < 0) { sum = after; } else { ret += abs(after) + 1; if (sum > 0) sum = -1; else sum = 1; } } cout << min(ret, ret2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) X=list(map(int,input().split())) res=X[0] cnt=0 if X[0]==0: cnt=1 res=1 for i in range(1,n): if X[i]>0: tmp=i cnt+=2*(i-1) res=-1 elif X[i]<0: tmp=i cnt+=2*(i-1) res=1 for i in range(tmp,n): res+=X[i] if res>=0 and res-X[i]>0: cnt+=(1+res) res=-1 elif res<=0 and res-X[i]<0: cnt+=(1-res) res=1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n; long int a[100000] = {0}, b[100001] = {0}, fugo, dif, ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%ld", &a[i]); } if (a[0] < 0) fugo = 0; else fugo = 1; for (int i = 0; i < n; i++) { dif = 0; b[i + 1] = b[i] + a[i]; if ((i + 1) % 2 == fugo) { if (b[i + 1] <= 0) { dif += -1 - b[i]; b[i] += dif; dif += 1 - b[i + 1] - dif; b[i + 1] += dif; ans += dif; } } else { if (b[i + 1] >= 0) { dif += b[i] - 1; b[i] -= dif; dif += b[i + 1] + 1 - dif; b[i + 1] -= dif; ans += dif; } } } printf("%d\n", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) t = a[0] r, tmp, count = 0, 0, 0 for i in range(1, n): tmp = t + a[i] if t < 0 and tmp < 0: r = 1 - tmp elif t > 0 and tmp > 0: r = -tmp - 1 elif tmp == 0: if t < 0: r = 1 - t - a[i] else: r = -1 - t - a[i] # if a[i] < 0: # r = a[i] - 1 # elif a[i] > 0: # r = a[i] + 1 else: r = 0 count += abs(r) t = tmp + r print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int64_t n; int64_t i; int64_t sum; bool default_flag; bool plus_flag, minus_flag; int64_t ope_count; cin >> n; vector<int64_t> a(n); for (i = 0; i < n; i++) { cin >> a.at(i); } sum = 0; ope_count = 0; default_flag = true; plus_flag = false; minus_flag = false; for (i = 0; i < n; i++) { sum += a.at(i); if (default_flag == true) { default_flag = false; if (a.at(i + 1) > 0) { while (sum >= 0) { ope_count++; sum--; } minus_flag = true; } else if (a.at(i + 1) < 0) { while (sum <= 0) { ope_count++; sum++; } plus_flag = true; } } else if (plus_flag == true) { while (sum >= 0) { ope_count++; sum--; } plus_flag = false; minus_flag = true; } else if (minus_flag == true) { while (sum <= 0) { ope_count++; sum++; } plus_flag = true; minus_flag = false; } } cout << ope_count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; int s = a[0]; long long ans1 = 0, ans2 = 0; if (s == 0) ans1++; for (int i = (1); i < (int)(n); i++) { if (i % 2 && s + a[i] >= 0) { ans1 += s + a[i] + 1; s = -1; } else if (i % 2 == 0 && s + a[i] <= 0) { ans1 += -(s + a[i]) + 1; s = 1; } else s += a[i]; } s = a[0]; if (s == 0) ans2++; for (int i = (1); i < (int)(n); i++) { if (i % 2 == 0 && s + a[i] >= 0) { ans2 += s + a[i] + 1; s = -1; } else if (i % 2 && s + a[i] <= 0) { ans2 += -(s + a[i]) + 1; s = 1; } else s += a[i]; } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lint = long long; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); i++) cin >> a[i]; lint ans = 0, sum = 0; for (int i = 0; i < (n); i++) { sum += a[i]; if (sum - a[i] > 0) { if (sum >= 0) { ans += sum + 1; sum = -1; } } else if (sum - a[i] < 0) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] num,answer1= a[0],0 if a[0]==0: num,answer = 1,1 for i in range(1,n): if num*(num+a[i]) >= 0: x = (-num)//abs(num) answer1 += abs((x-num)-a[i]) num = x else: num += a[i] if a[0]==0: num,answer2 = -1,1 else: num,answer2 = -a[0]//abs(a[0]),abs(a[0]-(-a[0]//abs(a[0]))) for i in range(1,n): if num*(num+a[i]) >= 0: x = (-num)//abs(num) answer2 += abs((x-num)-a[i]) num = x else: num += a[i] print(min(answer1,answer2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(int a) { if (a > 0) return 1; else if (a < 0) return -1; else return 0; } int main(void) { int n; cin >> n; int sum = 0, s = 1; int num = 0; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { sum += a[i]; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * (-1); s *= -1; } else if (i == 0 && a[i] == 0) { num++; sum++; s = 1; } else { s = sign(sum); } } int m = num; sum = 0; num = 0; num += abs(a[0]) + 1; if (a[0] >= 0) sum = -1; else sum = 1; s = sum; for (int i = 1; i < n; i++) { sum += a[i]; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * -1; s *= -1; } else { s = sign(sum); } } m = min(num, m); cout << m << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; int main(){ int N; cin >> N; vector<long long int> A(N); for(int i = 0; i < N; i++) cin >> A[i]; long long unsigned int cnt, cnt1 ,cnt2 = 0; long long unsigned int M = 0; M = A[0]; for(int i = 1 ; i < N; i++){ if(i % 2 == 0){ cnt1 += abs(1 - M); }else{ cnt1 += abs(-1 - M); } M += A[i]; } M = A[0]; for(int i = 1 ; i < N; i++){ if(i % 2 == 1){ cnt2 += abs(1 - M); }else{ cnt2 += abs(-1 - M); } } cnt = min(cnt1, cnt2); cout << cnt; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "errors" "fmt" "math" "os" "strconv" "strings" ) var rdr = bufio.NewReaderSize(os.Stdin, 1000000) // readLine can read long line string (at least 10^5) func readLine() string { buf := make([]byte, 0, 1000000) for { l, p, e := rdr.ReadLine() if e != nil { panic(e) } buf = append(buf, l...) if !p { break } } return string(buf) } // NextLine reads a line text from stdin, and then returns its string. func NextLine() string { return ReadLine() } // NextIntsLine reads a line text, that consists of **ONLY INTEGERS DELIMITED BY SPACES**, from stdin. // And then returns intergers slice. func NextIntsLine() []int { ints := []int{} intsStr := NextLine() tmp := strings.Split(intsStr, " ") for _, s := range tmp { integer, _ := strconv.Atoi(s) ints = append(ints, integer) } return ints } // NextRunesLine reads a line text, that consists of **ONLY CHARACTERS ARRANGED CONTINUOUSLY**, from stdin. // Ant then returns runes slice. func NextRunesLine() []rune { return []rune(NextLine()) } // Max returns the max integer among input set. // This function needs at least 1 argument (no argument causes panic). func Max(integers ...int) int { m := integers[0] for i, integer := range integers { if i == 0 { continue } if m < integer { m = integer } } return m } // Min returns the min integer among input set. // This function needs at least 1 argument (no argument causes panic). func Min(integers ...int) int { m := integers[0] for i, integer := range integers { if i == 0 { continue } if m > integer { m = integer } } return m } // PowInt is integer version of math.Pow func PowInt(a, e int) int { if a < 0 || e < 0 { panic(errors.New("[argument error]: PowInt does not accept negative integers")) } fa := float64(a) fe := float64(e) fanswer := math.Pow(fa, fe) return int(fanswer) } // AbsInt is integer version of math.Abs func AbsInt(a int) int { fa := float64(a) fanswer := math.Abs(fa) return int(fanswer) } // DeleteElement returns a *NEW* slice, that have the same and minimum length and capacity. // DeleteElement makes a new slice by using easy slice literal. func DeleteElement(s []int, i int) []int { if i < 0 || len(s) <= i { panic(errors.New("[index error]")) } // appendのみの実装 n := make([]int, 0, len(s)-1) n = append(n, s[:i]...) n = append(n, s[i+1:]...) return n } // Concat returns a *NEW* slice, that have the same and minimum length and capacity. func Concat(s, t []rune) []rune { n := make([]rune, 0, len(s)+len(t)) n = append(n, s...) n = append(n, t...) return n } // sort package (snippets) //sort.Sort(sort.IntSlice(s)) //sort.Sort(sort.Reverse(sort.IntSlice(s))) //sort.Sort(sort.Float64Slice(s)) //sort.Sort(sort.StringSlice(s)) // copy function //a = []int{0, 1, 2} //b = make([]int, len(a)) //copy(b, a) /*******************************************************************/ var n int var A []int func main() { tmp := NextIntsLine() n = tmp[0] A = NextIntsLine() S := make([]int, len(A)) S[0] = A[0] for i := 1; i < len(A); i++ { sum := S[i-1] S[i] = sum + A[i] } // 最初を正とする場合と負とする場合の両方を試す answers := []int{} for _, firstSign := range []int{1, -1} { comp, answer := 0, 0 if (firstSign == 1 && S[0] <= 0) || (firstSign == -1 && S[0] >= 0) { comp = firstSign - S[0] answer = AbsInt(comp) } for i := 1; i < len(S); i++ { var befSign int if S[i-1]+comp < 0 { befSign = -1 } else { befSign = 1 } if (befSign == -1 && S[i]+comp > 0) || (befSign == 1 && S[i]+comp < 0) { continue } x := -befSign - (S[i] + comp) answer += AbsInt(x) comp += x } answers = append(answers, answer) } fmt.Println(Min(answers...)) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
;; -*- coding: utf-8 -*- (eval-when (:compile-toplevel :load-toplevel :execute) (sb-int:defconstant-eqx OPT #+swank '(optimize (speed 3) (safety 2)) #-swank '(optimize (speed 3) (safety 0) (debug 0)) #'equal) #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t) #-swank (set-dispatch-macro-character ;; enclose the form with VALUES to avoid being captured by LOOP macro #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t))))) #+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax) #-swank (disable-debugger) ; for CS Academy ;; BEGIN_INSERTED_CONTENTS (declaim (ftype (function * (values fixnum &optional)) read-fixnum)) (defun read-fixnum (&optional (in *standard-input*)) "NOTE: cannot read -2^62" (macrolet ((%read-byte () `(the (unsigned-byte 8) #+swank (char-code (read-char in nil #\Nul)) #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil)))) (let* ((minus nil) (result (loop (let ((byte (%read-byte))) (cond ((<= 48 byte 57) (return (- byte 48))) ((zerop byte) ; #\Nul (error "Read EOF or #\Nul.")) ((= byte #.(char-code #\-)) (setf minus t))))))) (declare ((integer 0 #.most-positive-fixnum) result)) (loop (let* ((byte (%read-byte))) (if (<= 48 byte 57) (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result)))) (return (if minus (- result) result)))))))) (defmacro dbg (&rest forms) #+swank (if (= (length forms) 1) `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms)) `(format *error-output* "~A => ~A~%" ',forms `(,,@forms))) #-swank (declare (ignore forms))) (defmacro define-int-types (&rest bits) `(progn ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits) ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits))) (define-int-types 2 4 7 8 15 16 31 32 62 63 64) (declaim (inline println)) (defun println (obj &optional (stream *standard-output*)) (let ((*read-default-float-format* 'double-float)) (prog1 (princ obj stream) (terpri stream)))) (defconstant +mod+ 1000000007) ;;; ;;; Body ;;; (defun main () (let* ((n (read)) (as (make-array n :element-type 'int32))) (dotimes (i n) (setf (aref as i) (read-fixnum))) (when (< (aref as 0) 0) (setq as (map '(simple-array int32 (*)) #'- as))) (let ((sum (aref as 0)) (res 0)) (loop for i from 1 below n do (incf sum (aref as i)) (if (oddp i) (when (>= sum 0) (incf res (+ sum 1)) (setq sum -1)) (when (<= sum 0) (incf res (- 1 sum)) (setq sum 1)))) (println res)))) #-swank (main) ;;; ;;; Test and benchmark ;;; #+swank (defun io-equal (in-string out-string &key (function #'main) (test #'equal)) "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if the string output to *STANDARD-OUTPUT* is equal to OUT-STRING." (labels ((ensure-last-lf (s) (if (eql (uiop:last-char s) #\Linefeed) s (uiop:strcat s uiop:+lf+)))) (funcall test (ensure-last-lf out-string) (with-output-to-string (out) (let ((*standard-output* out)) (with-input-from-string (*standard-input* (ensure-last-lf in-string)) (funcall function))))))) #+swank (defun get-clipbrd () (with-output-to-string (out) ;; (run-program "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" '("get-clipboard") :output out) (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t))) #+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname)) #+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*)) #+swank (defun run (&optional thing (out *standard-output*)) "THING := null | string | symbol | pathname null: run #'MAIN using the text on clipboard as input. string: run #'MAIN using the string as input. symbol: alias of FIVEAM:RUN!. pathname: run #'MAIN using the text file as input." (let ((*standard-output* out)) (etypecase thing (null (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd))) (main))) (string (with-input-from-string (*standard-input* (delete #\Return thing)) (main))) (symbol (5am:run! thing)) (pathname (with-open-file (*standard-input* thing) (main)))))) #+swank (defun gen-dat () (uiop:with-output-file (out *dat-pathname* :if-exists :supersede) (format out ""))) #+swank (defun bench (&optional (out (make-broadcast-stream))) (time (run *dat-pathname* out))) ;; To run: (5am:run! :sample) #+swank (it.bese.fiveam:test :sample (it.bese.fiveam:is (common-lisp-user::io-equal "4 1 -3 1 0 " "4 ")) (it.bese.fiveam:is (common-lisp-user::io-equal "5 3 -6 4 -5 7 " "0 ")) (it.bese.fiveam:is (common-lisp-user::io-equal "6 -1 4 3 2 -5 4 " "8 ")))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python2
# -*- coding:utf-8 -*- n = int(raw_input()) numlist = (raw_input()).split(' ') count = 0 if (int(numlist[0]) == 0): if (int(numlist[1]) > 0): numlist[0] = -1 else: numlist[0] = 1 sumlist = [int(numlist[0])] for i in range(1, n): sumlist.append(sumlist[i-1] + int(numlist[i])) while (True): if (sumlist[i-1] > 0 and sumlist[i] > 0): #i-1,i番目までのsumがともに正 #numlist[i] = int(numlist[i]) - 1 count += sumlist[i] + 1 sumlist[i] = -1 elif (sumlist[i-1] < 0 and sumlist[i] < 0): #i-1,i番目までのsumがともに負 #numlist[i] = int(numlist[i]) + 1 count += (-1)*sumlist[i] + 1 sumlist[i] = 1 elif (sumlist[i] == 0): #i番目までのsum=0 if (sumlist[i-1] > 0): #numlist[i] = int(numlist[i]) - 1 sumlist[i] -= 1 if (sumlist[i-1] < 0): #numlist[i] = int(numlist[i]) + 1 sumlist[i] += 1 count += 1 else: break #print numlist #print sumlist print count
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MOD = 1e9 + 7; const long long LINF = 1e18; long long n; vector<long long> a; long long count(long long sum) { long long cnt = 0; for (long long i = 1; i < n; ++i) { if (sum > 0) { sum += a[i]; if (sum >= 0) { cnt += abs(sum) + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } } return cnt; } int main() { cin >> n; long long ans = INF; a.resize(n); for (long long i = 0; i < n; ++i) { cin >> a[i]; } if (a[0] == 0) { ans = min(ans, count(1) + 1); ans = min(ans, count(-1) + 1); } else { long long sum = a[0]; ans = min(ans, count(sum)); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a; cin >> a; int sum = 0; int x = 0; if (a > 0) { sum += a; for (int t = 1; t < n; t++) { int temp; cin >> temp; sum += temp; if (t % 2 == 1 && sum >= 0) { int s = sum + 1; sum -= s; x += s; } else if (t % 2 == 0 && sum <= 0) { int s = 1 - sum; sum += s; x += s; } } } else { sum += a; for (int t = 1; t < n; t++) { int temp; cin >> temp; sum += temp; if (t % 2 == 0 && sum >= 0) { int s = sum + 1; sum -= s; x += s; } else if (t % 2 == 1 && sum <= 0) { int s = 1 - sum; sum += s; x += s; } } } cout << x << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N, temp; vector<int> a; scanf("%d", &N); int start = 0; bool v = false; for (int i = 0; i < N; i++) { scanf("%d", &temp); if (temp == 0) { if (!v) { start += 1; } } else if (!v) v = true; a.push_back(temp); } int sum = 0, cnt = 0; if (start != 0) { cnt = 2 * (start - 1) + 1; if (a[start] > 0) { if (a[start] > 1) { sum = a[start] - 1; } else { sum = 1; cnt += 1; } } else { if (a[start] < -1) { sum = a[start] + 1; } else { sum = -1; cnt += 1; } } } else { sum = a[start]; } start++; for (size_t i = start; i != a.size(); i++) { if (sum + a[i] >= 0 && sum > 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum + a[i] <= 0 && sum < 0) { cnt += 1 - sum - a[i]; sum = 1; } else { sum += a[i]; } } if (sum == 0) cnt += 1; printf("%d\n", cnt); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { uint n; cin >> n; vector<int> a(n, 0); vector<int> S(n, 0); for (size_t i = 0; i < a.size(); ++i) cin >> a[i]; int operation = 0; S[0] = a[0]; if (S[0] == 0) { size_t j = 0; for (size_t i = 1; i < a.size(); ++i) { if (a[i] != 0) { j = i; break; } } if (j == 0) { S[0] = 1; ++operation; } else if (j % 2 == 0) { S[0] = a[j] / abs(a[j]); ++operation; } else { S[0] = -a[j] / abs(a[j]); ++operation; } } for (size_t i = 1; i < a.size(); ++i) { S[i] = S[i - 1] + a[i]; if (S[i] == 0) { if (S[i - 1] < 0) { S[i] = 1; ++operation; } else if (S[i - 1] > 0) { S[i] = -1; ++operation; } } else { if (S[i - 1] * S[i] > 0) { if (S[i] < 0) { operation = operation - S[i] + 1; S[i] = 1; } else if (S[i] > 0) { operation = operation + S[i] + 1; S[i] = -1; } } } } cout << operation << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (n); ++i) using ll = long long; using P = pair<int, int>; int main(){ int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; int sum = 0; int sign = 1; int cout = 0; int ans = INT_MAX; // if 0 +1 rep(j, 2){ if (j == 0) sign = 1; else sign = -1; sum = 0; cout = 0; rep(i, n){ sum += a[i]; if ((sign > 0) && (sum <= 0)){ cout += (1 - sum); sum = 1; } else if ((sign < 0) && (sum >= 0)){ cout += abs(-1 - sum); sum = -1; } sign = sign == 1 ? -1 : 1; } if (ans > cout) ans = cout; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ans; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } for (int i = 0; i < n - 1; i++) { while (a.at(i) * a.at(i + 1) >= 0) { if (a.at(i) > 0) { a.at(i + 1)--; ans++; } if (a.at(i) < 0) { a.at(i + 1)++; ans++; } } int p = 0; for (int j = i + 1; j >= 0; j--) p += a.at(j); if (p == 0) { if (a.at(i) > 0) a.at(i + 1)++; else a.at(i + 1)--; ans++; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long total = 0; long man = 0; boolean iniZero = true; int zeroCount = 0; for (int i = 0; i < a.length; i++) { if (iniZero) { if (a[i] == 0) { zeroCount++; } else { iniZero = false; if (Math.abs(a[i]) == 1 && i != 0) { total += a[i] + a[i] > 0 ? 1 : -1; man += 1; } else { total += a[i]; } } } else { if (total * (total + a[i]) >= 0) { long x = Math.abs(total + a[i]) + 1; if (total > 0) { total += a[i] - x; } else { total += a[i] + x; } man += x; } else { total += a[i]; } } } if (zeroCount > 0) { man += (zeroCount - 1) * 2 + 1; } System.out.println(man); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[100001] = {}; long long s[100001] = {}; int main() { long long n; cin >> n; for (long long i = 1; i < n; i++) { long long l; cin >> l; a[i] = l; s[i] = s[i - 1] + l; } long long cnt1 = 0; for (long long i = 1; i <= n; i++) { if (i % 2 == 1) { if (s[i] <= 0) { cnt1 += 1 - s[i]; s[i] = 1; } } else { if (s[i] >= 0) { cnt1 += 1 + s[i]; s[i] = -1; } } } long long cnt2 = 0; for (long long i = 1; i <= n; i++) { if (i % 2 == 1) { } else { } } cout << cnt1 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { long long i, j, k, ans = 0, be, n, a; scanf("%lld%lld", &n, &be); for (i = 1; i < n; ++i) { scanf("%lld", &a); if (be > 0 && -1 < be + a) ans += be + a + 1, be = -1; else if (be < 0 && 1 > be + a) ans += 1 + -be - a, be = 1; else be += a; } printf("%lld", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAX = 1e6; int arr[MAX], n; int status(int a) { if (a < 0) return 1; else if (a > 0) return 0; else return 2; } long long int solve() { long long int cnt = 0; long long int sum = arr[0], f = 0; if (arr[0] < 0) f = 1; else f = 0; for (int i = 1; i < n; i++) { f ^= 1; int add = arr[i]; if (status(arr[i]) != f) add = 0, cnt += abs(arr[i]); sum += add; if (status(sum) != f) { cnt += abs(sum) + 1; if (f) sum = -1; else sum = 1; } } return cnt; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } if (!arr[0]) { arr[0] = 1; long long int x = solve() + 1; arr[0] = -1; long long int y = solve() + 1; cout << min(x, y) << endl; } else { long long int old = arr[0]; arr[0] = old; long long int x = solve(); arr[0] = old * -1; long long int y = solve(); if (old > 0) y += old * 2; else x += abs(old) * 2; cout << min(x, y) << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int(i) = 0; (i) < (n); ++(i)) { cin >> a[i]; } long long ans; long long sum[n]; bool plusSumFlg; for (int(i) = 0; (i) < (n); ++(i)) { sum[i] = 0; } ans = 0; sum[0] = a[0]; if (sum[0] == 0) { if (a[1] >= 0) { ans++; sum[0]--; } else { ans++; sum[0]++; } } plusSumFlg = sum[0] > 0 ? true : false; for (int i = 1; i < n; ++i) { sum[i] = sum[i - 1] + a[i]; if (plusSumFlg) { plusSumFlg = false; if (sum[i] > 0) { ans += 1 + sum[i]; sum[i] = -1; } else if (sum[i] == 0) { ans++; sum[i]++; } } else { plusSumFlg = true; if (sum[i] < 0) { ans += 1 - sum[i]; sum[i] = 1; } else if (sum[i] == 0) { ans++; sum[i]++; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = [map(int,input().split())] acm1 = 0 acm2 = 0 ans1 = 0 ans2 = 0 for i, a in enumerate(A): acm1 += a acm2 += a if(i%2==1): if(acm1>=0): ans1 += acm1 + 1 acm1 = -1 if(acm2<=0): ans2 += -acm2 + 1 acm2 = 1 else: if(acm1<=0): ans1 += -acm1 + 1 acm1 = 1 if(acm2>=0): ans2 += acm2 + 1 acm2 = -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": [] }
IN-CORRECT
UNKNOWN
use proconio::input; fn main() { input! { n: usize, a: [i64; n], }; let a_e = { let mut res = 0_i64; let mut sum = 0_i64; for (i, &a_i) in a.iter().enumerate() { sum += a_i; if i % 2 == 0 { if sum > 0 { // do nothing } else { res += 1 - sum; sum = 1; } } else { if sum > 0 { res += 1 + sum; sum = -1; } else { // do nothing } } } res }; let a_o = { let mut res = 0_i64; let mut sum = 0_i64; for (i, &a_i) in a.iter().enumerate() { sum += a_i; if i % 2 == 0 { if sum > 0 { res += 1 + sum; sum = -1; } else { // do nothing } } else { if sum > 0 { // do nothing } else { res += 1 - sum; sum = 1; } } } res }; let ans = std::cmp::min(a_e, a_o); println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i a = [nil] + gets.split.map(&:to_i) r, s = 0, a[1] (1 ... n).each do |i| if s > 0 t = -s - 1 r += [a[i + 1] - t, 0].max s += [a[i + 1], t].min else t = -s + 1 r += [t - a[i + 1], 0].max s += [a[i + 1], t].max end end puts r
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[100000]; for (int i = 0; i < n; i++) { cin >> a[i]; } int Mp = 0; int Sp = 0; int dif = 0; for (int i = 0; i < n; i++) { Sp += a[i]; if (i % 2 == 0) { if (Sp <= 0) { dif = 1 - Sp; Mp += dif; Sp += dif; } } else { if (Sp >= 0) { dif = Sp + 1; Mp += dif; Sp += -dif; } } } int Mn = 0; int Sn = 0; int di = 0; for (int i = 0; i < n; i++) { Sn += a[i]; if (i % 2 == 1) { if (Sn <= 0) { di = 1 - Sn; Mn += di; Sn += di; } } else { if (Sn >= 0) { di = Sn + 1; Mn += di; Sn += -di; } } } if (Mp < Mn) { cout << Mp; } else { cout << Mn; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <inttypes.h> #include <ctype.h> #include <stdint.h> #include <string.h> #include <wchar.h> #include <math.h> #define N_MAX (100) #define P_MAX (100) #define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define ABS(a) ((a) < 0 ? -(a) : (a)) #define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a)) int compare_sz_asc(const void* a, const void* b) { return *((size_t*)a) < *((size_t*)b) ? -1 : 1; } int compare_sz_desc(const void* a, const void* b) { return *((size_t*)a) > * ((size_t*)b) ? -1 : 1; } int compare_i64_asc(const void* a, const void* b) { return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1; } int compare_i64_desc(const void* a, const void* b) { return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1; } int compare_c_asc(const void* a, const void* b) { return *((char*)a) < *((char*)b) ? -1 : 1; } int compare_c_desc(const void* a, const void* b) { return *((char*)a) > * ((char*)b) ? -1 : 1; } static size_t powSz(const size_t base, const size_t exp) { if (exp == 0) { return 1; } if (exp == 1) { return base; } if (exp % 2 == 0) { return powSz(base * base, exp / 2); } else { return base * powSz(base, exp - 1); } } static size_t comb(const size_t n, const size_t r) { size_t result = 1; for (size_t i = 0; i < r; i++) { result *= n - i; result /= i + 1; } return result; } static uint64_t combU64(const uint64_t n, const uint64_t r) { uint64_t result = 1; for (uint64_t i = 0; i < r; i++) { result *= n - i; result /= i + 1; } return result; } static size_t gcdZu(size_t m, size_t n) { size_t temp; while (m % n != 0) { temp = n; n = m % n; m = temp; } return n; } static uint64_t gcdU64(uint64_t m, uint64_t n) { uint64_t temp; while (m % n != 0) { temp = n; n = m % n; m = temp; } return n; } static int64_t a[100000]; int main(void) { size_t n; scanf("%zu\n", &n); for (size_t i = 0; i < n; i++) { scanf("%"PRId64, &a[i]); } size_t cnt[2] = { 0,0 }; int64_t base[2] = { 1,-1 }; int64_t sum = 0; for (size_t i = 0; i < n; i++) { sum += a[i]; cnt[0] += (size_t)ABSS(base[0], a[i]); cnt[1] += (size_t)ABSS(base[1], a[i]); base[0] = -base[0]; base[1] = -base[1]; } printf("%zu", MIN(cnt[0], cnt[1])); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) count = 0 if A[0] == 0: A[0] = -1 if A[1]>0 else 1 count += 1 t = A[0] for a in A[1:]: if t*(t+a) >= 0: if t > 0: count += 1+(t+a) t = -1 else: count += 1-(t+a) t = 1 else: t = t+a print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) S = 0 C = 0 S = a[0] if S > 0: pm = 1 elif S < 0: pm = 0 else: S += 1 C += 1 pm = 1 for i in range(1, n): S += a[i] if pm == 1 and S >= 0: C += S + 1 S -= S + 1 elif pm == 0 and S <= 0: C += -S + 1 S += -S + 1 pm = 1 - pm T = 0 D = 0 T = a[0] if T > 0: D += T + 1 T -= T + 1 pm = 0 elif T < 0: D += -T + 1 T += -T + 1 pm = 1 else: T -= 1 C += 1 pm = 0 for i in range(1, n): T += a[i] if pm == 1 and T >= 0: D += T + 1 T -= T + 1 elif pm == 0 and T <= 0: D += -T + 1 T += -T + 1 pm = 1 - pm 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": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long ans = 0; if (sum == 0) { sum = 1; ans++; } for (int i = 1; i < n; i++) { long long tmp = sum + a[i]; if (sum > 0 && tmp > 0) { ans += tmp + 1; sum = -1; } else if (sum < 0 && tmp < 0) { ans += -tmp + 1; sum = 1; } else if (tmp == 0) { ans++; if (sum < 0) sum = 1; else sum = -1; } else sum = tmp; } sum = a[0]; long long ans2 = 0; if (sum == 0) { sum = 1; ans2++; } else { sum = -1 * a[0] / abs(a[0]); ans2 = abs(a[0]) + 1; } for (int i = 1; i < n; i++) { long long tmp = sum + a[i]; if (sum > 0 && tmp > 0) { ans2 += tmp + 1; sum = -1; } else if (sum < 0 && tmp < 0) { ans2 += -tmp + 1; sum = 1; } else if (tmp == 0) { ans2++; if (sum < 0) sum = 1; else sum = -1; } else sum = tmp; } cout << min(ans2, ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def m(): n = int(input()) a = list(map(int, input().split())) even = a[1::2] odd = a[::2] ever_total = 0 ans = 0 if sum(even) > sum(odd): # マイナススタート for i in range(n): ever_total += a[i] if (i+2) % 2 == 0: if ever_total <= 0: pass else: ans += abs(a[i-1]) + 1 else: if ever_total >= 0: pass else: ans += abs(a[i - 1]) + 1 else: # プラス for i in range(n): ever_total += a[i] if (i+2) % 2 == 0: if ever_total >= 0: pass else: ans += abs(a[i-1]) + 1 else: if ever_total <= 0: pass else: ans += abs(a[i - 1]) + 1 return ans print(m())
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a.at(i); int sco = a.at(0); int ans = 0; for (int i = 1; i < n; i++) { if (sco >= 0) { int math = a.at(i) + sco; while (math >= 0) { math--; ans++; } sco = math; } else { int math = a.at(i) + sco; while (math <= 0) { math++; ans++; } sco = math; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int count = 0; int sum = 0; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); int Ah = 0; int Bh = 0; int sumA = 0; int sumB = 0; if (a.at(0) == 0) { if (a.at(1) > 0) a.at(0) = -1; else a.at(0) = 1; count++; } for (int i = 0; i < n - 1; i++) { sumA += a.at(i); Ah = 0; Bh = 0; for (;;) { sumB = sumA + a.at(i + 1); if (sumA > 0) Ah = 1; else Ah = -1; if (sumB > 0) Bh = 1; else if (sumB < 0) Bh = -1; else Bh = 0; if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1)) break; else if (Ah == 1 && Bh != -1) { a.at(i + 1) -= abs(sumB) + 1; count += abs(sumB) + 1; break; } else if (Ah == -1 && Bh != 1) { a.at(i + 1) += abs(sumB) + 1; count += abs(sumB) + 1; break; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 sum=0; long count=0; for(int i=0;i<n-1;i++){ sum+=a[i]; if(sum>0){ if(sum+a[i+1]>=0){ count+=sum+a[i+1]+1; a[i+1]-=sum+a[i+1]+1; } }else if(sum<0){ if(sum+a[i+1]<=0){ count+=-1*(sum+a[i+1])+1; a[i+1]+=-1*(sum+a[i+1])+1; } } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 1e+5; int n; int a[MAX + 1]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; bool plus; int sum = a[0] + a[1]; long long ans = 0; if (sum < 0) plus = true; else if (sum > 0) plus = false; else { int cnt = 1; while (sum == 0) { sum += a[++cnt]; } if (sum < 0) { plus = true; sum = 1; } else { plus = false; sum = -1; } ans += (cnt - 2) * 2 + 1; } for (int i = 2; i < n; i++) { sum += a[i]; if (plus && sum <= 0) { ans += -sum + 1; sum = 1; } else if (!plus && sum >= 0) { ans += sum + 1; sum = -1; } plus = !plus; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ulli = unsigned long long int; vector<lli> N, rN; lli in, n, d = 0, dp, pm; ulli ans = 0; int main() { cin >> n; for (lli l = 0; l < n; l++) { cin >> in; if (l == 0) { N.push_back(in); } else { N.push_back(N[l - 1] + in); } } for (lli l = 1; l < (lli)N.size(); l++) { dp = d; std::cout << "N[l - 1] + dp" << ":" << N[l - 1] + dp << " "; ; std::cout << "N[l] + dp" << ":" << N[l] + dp << " "; ; if (N[l - 1] + dp < 0) { if (N[l] + dp < 0) { d += 1 - N[l] - dp; ans += 1 - N[l] - dp; } else if (N[l] + dp == 0) { d += 1; ans += 1; } } else if (N[l - 1] + dp > 0) { if (N[l] + dp > 0) { d -= N[l] + dp + 1; ans += N[l] + dp + 1; } else if (N[l] + dp == 0) { d -= 1; ans += 1; } } else { for (lli m = l - 1; m < (lli)N.size(); m++) { if (N[m] > 0) { pm = (m - l) % 2; break; } else if (N[m] < 0) { pm = (m - l + 1) % 2; break; } if (m == (lli)N.size() - 1) { pm = (m + 1) % 2; break; } } if (pm == 1) { d += 1; ans += 1; } else if (pm == 0) { d -= 1; ans += 1; } dp = d; if (N[l] + dp < 0) { d += 1 - N[l] - dp; ans += 1 - N[l] - dp; } else if (N[l] + dp == 0) { d += 1; ans += 1; } else if (N[l] + dp > 0) { d -= N[l] + dp + 1; ans += N[l] + dp + 1; } else if (N[l] + dp == 0) { d -= 1; ans += 1; } } std::cout << "N[l] + d" << ":" << N[l] + d << " "; ; std::cout << "dp" << ":" << dp << " "; ; std::cout << "d" << ":" << d << " "; ; std::cout << "ans" << ":" << ans << " "; ; std::cout << "\n"; ; } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main() { long long int n, a[100002], i, d[100002], e[100002], b, c; scanf("%lld", &n); for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); } b = 0; d[1] = a[1]; if (d[1] != 0) { for (i = 2; i <= n; i++) { d[i] = d[i - 1] + a[i]; if (d[i] * d[i - 1] >= 0) { if (d[i - 1] < 0) { b = b + 1 - d[i]; d[i] = 1; } if (d[i - 1] > 0) { b = b + 1 + d[i]; d[i] = -1; } } } printf("%lld\n", b); return 0; } if (a[1] == 0) { b = 1; d[1] = 1; for (i = 2; i <= n; i++) { d[i] = d[i - 1] + a[i]; if (d[i] * d[i - 1] >= 0) { if (d[i - 1] < 0) { b = b + 1 - d[i]; d[i] = 1; } if (d[i - 1] > 0) { b = b + 1 + d[i]; d[i] = -1; } } } e[1] = -1; c = 1; for (i = 2; i <= n; i++) { e[i] = e[i - 1] + a[i]; if (e[i] * e[i - 1] >= 0) { if (e[i - 1] < 0) { c = c + 1 - e[i]; e[i] = 1; } if (e[i - 1] > 0) { c = c + 1 + e[i]; e[i] = -1; } } } if (b <= c) printf("%lld\n", b); if (b > c) printf("%lld\n", c); return 0; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- n = int(input()) an = list(map(int, input().split())) sum = an[0] ans = 0 for i in range(1,n): if sum * (sum + an[i]) < 0: sum += an[i] else: if sum > 0: ans += abs(sum + an[i] + 1) sum = -1 else: ans += abs(sum + an[i] - 1) sum = 1 ans1 = ans if an[0] > 0: sum = -1 else: sum = 1 ans = abs(an[0])+1 for i in range(1, n): if sum * (sum + an[i]) < 0: sum += an[i] else: if sum > 0: ans += abs(sum + an[i] + 1) sum = -1 else: ans += abs(sum + an[i] - 1) sum = 1 # print(ans1, ans) print(min([ans1, ans]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) wa=a[0] ans1,ans2=0,0 for i in range(1,n): # print(wa) if wa>=0: if wa+a[i]<0: wa+=a[i] else: ans1+=abs(wa+a[i])+1 wa=-1 else: if wa+a[i]>0: wa+=a[i] else: ans1+=abs(wa+a[i])+1 wa=1 if a[0]>0: ans2+=a[0]+1 wa=-1 else: ans2+=-a[0]+1 wa=1 for i in range(1,n): if wa>0: if wa+a[i]<0: wa+=a[i] else: ans2+=abs(wa+a[i])+1 wa=-1 else: if wa+a[i]>0: wa+=a[i] else: ans2+=abs(wa+a[i])+1 wa=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": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using unsi = unsigned; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using pii = pair<int, int>; using db = double; using plex = complex<double>; using vs = vector<string>; template <class T> inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } aaaaaaa; const int INF = 1001001001; const ll LINF = 1001001001001001001ll; const int MOD = 1e9 + 7; const db EPS = 1e-9; const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; signed main() { int n; cin >> n; int odd{}; int ans{}; int even{}; vector<int> a(n); for (auto i = 0; i != n; ++i) { cin >> a.at(i); } if (a[0] < 0) { for (auto i = 0; odd < n; ++i) { odd = 2 * i + 1; while (a[odd] <= 0) { ++a[odd]; ++ans; } } for (auto i = 1; even < n; ++i) { even = 2 * i; while (a[even] >= 0) { --a[even]; ++ans; } } } if (a[0] = 0) { ++a[0]; } if (a[0] > 0) { for (auto i = 0; odd < n; ++i) { odd = 2 * i + 1; while (a[odd] >= 0) { --a[odd]; ++ans; } } for (auto i = 1; even < n; ++i) { even = 2 * i; while (a[even] <= 0) { ++a[even]; ++ans; } } } cout << ans; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, count = 0; cin >> n; long long a; cin >> a; long long sum = a, pre_sum; for (int i = 1; i < n; ++i) { cin >> a; pre_sum = sum; sum += a; while (sum * pre_sum >= 0) { if (pre_sum < 0) sum++; else sum--; ++count; } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int64_t n; cin >> n; int64_t Acount = 0; int64_t Bcount = 0; int64_t sum = 0; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); int Ah = 0; int Bh = 0; int64_t sumA = 0; int64_t sumB = 0; for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { sumA += a.at(i); if (sumA >= 1) ; else { sumA += abs(a.at(i)) + 1; Acount += abs(a.at(i)) + 1; } cout << i << endl; } else { sumA += a.at(i); if (sum <= -1) ; else { sumA -= abs(a.at(i)) + 1; Acount += abs(a.at(i)) + 1; } cout << i << endl; } } cout << Acount << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) n_list = list(map(int, input().split())) count = 0 prev_sum = n_list[0] for i in n_list[1:]: if prev_sum < 0: if -prev_sum >= i: count += abs((-prev_sum + 1) - i) i = -prev_sum + 1 else: if -prev_sum <= i: count += abs((-prev_sum - 1) - i) i = -prev_sum - 1 prev_sum += i print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> rev_a = a; int result = 0; bool isPlus = a[0] > 0 ? true : false; int sum = a[0]; for (int i = 1; i < n; i++) { int temp_sum = sum + a[i]; if (isPlus) { if (temp_sum >= 0) { result += temp_sum + 1; a[i] -= temp_sum + 1; } } else { if (temp_sum <= 0) { result += -temp_sum + 1; a[i] += -temp_sum + 1; } } isPlus = !isPlus; sum += a[i]; } sum = 0; int rev_result = 0; isPlus = rev_a[0] > 0 ? true : false; if (isPlus) { rev_result += rev_a[0] + 1; rev_a[0] -= rev_a[0] + 1; isPlus = !isPlus; } for (int i = 1; i < n; i++) { int temp_sum = sum + rev_a[i]; if (isPlus) { if (temp_sum >= 0) { rev_result += temp_sum + 1; rev_a[i] -= temp_sum + 1; } } else { if (temp_sum <= 0) { rev_result += -temp_sum + 1; rev_a[i] += -temp_sum + 1; } } isPlus = !isPlus; sum += rev_a[i]; } if (rev_result < result) cout << rev_result << endl; else cout << result << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; 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(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } 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) { if(a.length>=2&&sum+a[1]>0) sum--; else sum++; operations++; } 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+=a[i]-1; operations++; }else { long req=-1-1*sum; sum=-1; operations+=(-1*req+a[i]); } } }else { if(sum+a[i]>0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum+=a[i]+1; operations++; }else { long req=1+-1*sum; sum=1; operations+=(req-a[i]); } } } } System.out.println(operations); } } 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": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[100001]; long long sum1 = 0; long long sum2 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } int ans1 = 0; int ans2 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; if (i % 2 == 0 && sum1 <= 0) { ans1 += (1 - sum1); sum1 += (1 - sum1); if (sum1 == 0) { sum1++; ans1++; } } else if (i % 2 == 1 && sum1 >= 0) { ans1 += (sum1 + 1); sum1 -= (1 + sum1); if (sum1 == 0) { sum1--; ans1++; } } } for (int i = 0; i < n; i++) { sum2 += a[i]; if (i % 2 == 0 && sum2 >= 0) { ans2 += (sum2 + 1); sum2 -= (1 + sum2); if (sum2 == 0) { sum2--; ans2++; } } else if (i % 2 == 1 && sum2 <= 0) { ans2 += (1 - sum2); sum2 += (1 - sum2); if (sum2 == 0) { sum2++; ans2++; } } } if (sum2 == 0) ans2++; if (ans1 >= ans2) cout << ans2 << endl; else cout << ans1 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def resolve(SL): # L[0]!=0を起点とする cnt = 0 for i in range(len(SL)-1): s0 = SL[i] s1 = SL[i+1] if(s0>0 and s1>=0): SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]] cnt += (s1+1) elif(s0<0 and s1<=0): SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]] cnt += (-s1+1) print(SL) return cnt def ans(L): SL = [sum(L[:(i+1)]) for i in range(len(L))] c0,c1=0,0 if (L[0]>0): c0 = resolve(SL) c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL))) elif (L[0]<0): c0 = resolve(SL) c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL))) else: c0 = 1 + resolve(list(map(lambda x:x+1, SL))) c1 = 1 + resolve(list(map(lambda x:x-1, SL))) return(min(c0,c1)) N = int(input()) L = [int(x) for x in input().split(' ')] print(ans(L))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int &n, int b) { n |= two(b); } inline void unset_bit(int &n, int b) { n &= ~two(b); } const long long mod = 1e9 + 7; const int N = 1e6 + 9; long long a[N]; vector<long long> v[N]; long long modexp(long long a, long long n) { long long r = 1; while (n) { if (n & 1) r = (r * a) % mod; a = (a * a) % mod; n >>= 1; } return r; } bool cmp(const pair<double, long long> &a, const pair<double, int> &b) { if (a.first == b.first) { return a.second < b.second; } else return a.first > b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } long long sum = a[0], ans = 0; if (sum >= 0) { k = 1; } for (int i = 1; i < n; i++) { sum += a[i]; if (k == 1) { if (sum >= 0) { ans += sum + 1; sum = -1; } } else { if (sum <= 0) { ans += (-1 * sum) + 1; sum = 1; } } if (k == 0) k = 1; else k = 0; } long long kk = 1; long long su = a[0], an = 0; if (su >= 0) { kk = 0; an += su + 1; } for (int i = 1; i < n; i++) { su += a[i]; if (kk == 1) { if (su >= 0) { an += su + 1; su = -1; } } else { if (su <= 0) { an += (-1 * su) + 1; su = 1; } } if (kk == 0) kk = 1; else kk = 0; } cout << min(ans, an) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a_list = [int(i) for i in input().split(' ')] total = [a_list[0]] counter = 0 for a in a_list[1:]: total.append(total[-1] + a) if total[0]==0: total = map(lambda n: n-int(total[1]/abs(total[1])), total) for i in range(1,n): if total[i-1]<0 and total[i]<=0: counter += abs(total[i])+1 total[i:] = list(map(lambda n: n-(total[i])+1, total[i:])) elif total[i-1]>0 and total[i]>=0: counter += abs(total[i])+1 total[i:] = list(map(lambda n: n-(total[i])-1, total[i:])) else: continue print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [] * n a = list(map(int, input().split())) if a[0] > 0: symbol = "plus" elif a[0] < 0: symbol = "minus" sum1 = a[0] count = 0 for i in range(1, n): sum1 += a[i] if symbol == "plus" and sum1 >= 0: operation = sum1 + 1 count += operation sum1 = -1 elif symbol == "minus" and sum1 <= 0: operation = abs(sum1) + 1 count += operation sum1 = 1 if symbol == "plus": symbol = "minus" else: symbol = "plus" print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) seq = [int(x) for x in input().split()] a_sum = seq[0] op = 0 for a in seq[1:]: tmp = a_sum + a if tmp * a_sum < 0: a_sum = tmp elif a_sum < 0: diff = 1 - a_sum - a a_sum = 1 op += abs(diff) elif a_sum > 0: diff = -1 - a_sum - a a_sum = -1 op += abs(diff) print(op)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int dx[4] = {1, 0, 0, -1}; int dy[4] = {0, 1, -1, 0}; using namespace std; bool cmp_P(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return a.second < b.second; } int main() { long long int n, sum = 0, v, res = 0; cin >> n; vector<long long int> a(n + 1); for (int i = 0; i < (int)(n); i++) cin >> a[i]; v = abs(a[0]) / a[0]; sum = a[0]; for (int i = 1; i < n; i++) { if (v == -1) { if (a[i] + sum <= 0) { res += abs(1 - a[i] - sum); sum = 1; } else { sum += a[i]; } } else { if (a[i] + sum >= 0) { res += abs(-1 - a[i] - sum); sum = -1; } else { sum += a[i]; } } v = -v; } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100010]; int a1[10010]; int a2[10010]; long long sum1[100010] = {0}; long long sum2[100010] = {0}; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; a1[i] = a[i]; a2[i] = a[i]; } int ans1 = 0; int ans2 = 0; for (int i = 0; i < n; i++) { int j = i; while (j >= 0) { sum1[i] += a1[j]; j--; } if (i % 2 == 0 && sum1[i] <= 0) { a1[i] += 1 - sum1[i]; sum1[i] += 1 - sum1[i]; ans1 += 1 - sum1[i]; } else if (i % 2 == 1 && sum1[i] >= 0) { a1[i] -= (sum1[i] + 1); ans1 += (sum1[i] + 1); } } if (sum1[n - 1] == 0) ans1++; for (int i = 0; i < n; i++) { int j = i; while (j >= 0) { sum2[i] += a2[j]; j--; } if (i % 2 == 0 && sum2[i] >= 0) { a2[i] -= (1 + sum2[i]); sum2[i] -= (1 + sum2[i]); ans2 += (sum2[i] + 1); } else if (i % 2 == 1 && sum2[i] <= 0) { a2[i] += (1 - sum2[i]); sum2[i] += (1 - sum2[i]); ans2 += (1 - sum2[i]); } } if (sum2[n - 1] == 0) ans2++; int ans = ans1 >= ans2 ? ans2 : ans1; cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int j = 0; j < n; j++) { cin >> a[j]; } int ans = 0; int sum = a[0]; int ans1; int ans2; int ans3; int ans4; if (a[0] != 0) { for (int i = 1; i < n; i++) { int sum_old = sum; sum = sum + a[i]; if (sum * sum_old < 0) { } else { ans = ans + abs(sum) + 1; sum = (sum_old / abs(sum_old)) * -1; } } ans1 = ans; ans = abs(a[0]) + 1; sum = (a[0] / abs(a[0]) * -1); for (int i = 1; i < n; i++) { int sum_old = sum; sum = sum + a[i]; if (sum * sum_old < 0) { } else { ans = ans + abs(sum) + 1; sum = (sum_old / abs(sum_old)) * -1; } } ans2 = ans; ans = min(ans1, ans2); } else { sum = 1; ans = 1; for (int i = 1; i < n; i++) { int sum_old = sum; sum = sum + a[i]; if (sum * sum_old < 0) { } else { ans = ans + abs(sum) + 1; sum = (sum_old / abs(sum_old)) * -1; } } ans3 = ans; sum = -1; ans = 1; for (int i = 1; i < n; i++) { int sum_old = sum; sum = sum + a[i]; if (sum * sum_old < 0) { } else { ans = ans + abs(sum) + 1; sum = (sum_old / abs(sum_old)) * -1; } } ans4 = ans; ans = min(ans3, ans4); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) def f(first, m): now = first result = 0 for i, v in enumerate(a[1:]): tmp = now * -1 if m < 0: tmp += 1 if tmp < v: now += v else: result += tmp - v now = 1 else: tmp -= 1 if tmp > v: now += v else: result += v - tmp now = -1 m *= -1 return result m = a[0] < 0 and -1 or 1 print(min((f(a[0], m), f(m * -1, m * -1))))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = input() a = list(map(int, input().split())) def f(x): cnt = 0 cur = 0 for ai in a: cur += ai cur *= x if cur <= 0: cnt += 1 - cur cur = 1 x *= -1 return cnt print(min(f(1), f(-1)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(lambda x: int(x), input().split())) idx=0 ans=0 sum_last=0 for i in a: if i!=0: break idx+=1 ans=idx if idx==len(a): pass elif idx>0: if a[idx]>0: sum_last=-1 else: sum_last=1 else: sum_last=a[0] idx+=1 for i in a[idx:]: sum_cur=sum_last+i if sum_cur*sum_last>=0: ans+=abs(sum_cur)+1 if sum_last>0: sum_last=-1 else: sum_last=1 else: sum_last=sum_cur print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, flag, ans, sum, tt, MAX; int a[100005]; int main() { MAX = 100000000; tt = 0; sum = 0; ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); if (a[1] > 0) flag = 1; if (a[1] < 0) flag = 0; sum += a[1]; if (flag == 1) { for (int i = 2; i <= n; i++) { if (i % 2 == 0 && sum + a[i] >= 0) { a[i] -= abs(sum + a[i]) + 1; sum -= abs(sum + a[i]) + 1; ans += abs(sum + a[i]) + 1; } else if (i % 2 == 1 && sum + a[i] <= 0) { a[i] += abs(sum + a[i]) + 1; sum += abs(sum + a[i]) + 1; ans += abs(sum + a[i]) + 1; } } printf("%d\n", ans); } else if (flag == 0) { for (int i = 2; i <= n; i++) { if (i % 2 == 1 && sum + a[i] >= 0) { a[i] -= abs(sum + a[i]) + 1; sum -= abs(sum + a[i]) + 1; ans += abs(sum + a[i]) + 1; } else if (i % 2 == 0 && sum + a[i] <= 0) { a[i] += abs(sum + a[i]) + 1; sum += abs(sum + a[i]) + 1; ans += abs(sum + a[i]) + 1; } } printf("%d\n", ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a.at(i); bool fla = false; int t = 0, res1 = 0, res2 = 0; for (int i = 0; i < N; i++) { int b = a.at(i); if (fla) { if (t + b <= 0) { b = t * -1 + 1; res1 += b - a.at(i); } } else { if (t + b >= 0) { b = t * -1 - 1; res1 += abs(b - a.at(i)); } } t += b; fla = !fla; } t = 0; fla = false; for (int i = 0; i < N; i++) { int b = a.at(i); if (!fla) { if (t + b <= 0) { b = t * -1 + 1; res2 += b - a.at(i); } } else { if (t + b >= 0) { b = t * -1 - 1; res2 += abs(b - a.at(i)); } } t += b; fla = !fla; } int res = min(res1, res2); cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long ans = 0; long ai = sc.nextLong(); for (int i = 1; i < N; i++) { long a = sc.nextLong(); long total = ai + a; if (ai > 0) { if (total < 0) { ai = total; continue; } else { long tmp = total + 1; ans += Math.abs(tmp); ai = -1; } } else if(ai < 0) { if (total > 0) { ai = total; continue; } else { long tmp = total - 1; ans += Math.abs(tmp); ai = 1; } } } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] A = new int[N + 1]; for (int i = 1; i <= N; ++i) { A[i] = sc.nextInt(); } sc.close(); int sum1 = 0; int sum2 = 0; int ans1 = 0; int ans2 = 0; for (int i = 1; i <= N; ++i) { sum1 += A[i]; if (i % 2 == 0 && sum1 >= 0) { ans1 += sum1 +1; sum1 = -1; } else if (i % 2 != 0 && sum1 <= 0) { ans1 += Math.abs(sum1) + 1; sum1 = 1; } } for (int i = 1; i <= N; ++i) { sum2 += A[i]; if (i % 2 == 0 && sum2 <= 0) { ans2 += Math.abs(sum2) +1; sum2 = 1; } else if (i % 2 != 0 && sum2 >= 0) { ans2 += Math.abs(sum2) + 1; sum2 = -1; } } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline bool feq(const double& a, const double& b) { return fabs(a - b) < 1e-10; } inline int gcd(int a, int b) { if (b == 0) return a; return a < b ? gcd(b, a) : gcd(b, a % b); } long long mo = 1000000007; const long long INF = 1e18; bool f(pair<long long, long long> p1, pair<long long, long long> p2) { return p1.first < p2.first; } int main() { int n; cin >> n; long long cnt = 0; long long sum = 0; cin >> sum; for (int i = 0; i < n - 1; ++i) { long long a; cin >> a; a += sum; if ((sum < 0 && a > 0) || (sum > 0 && a < 0)) { sum = a; } else { cnt += (abs(a) + 1); sum = (-1) * (sum > 0 ? 1 : -1); } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(a) for a in input().split()] def f(times, prefix_sum): for i in range(1, n): if prefix_sum < 0: if prefix_sum + a[i] > 0: prefix_sum += a[i] else: times += 1 - (prefix_sum + a[i]) prefix_sum = 1 elif prefix_sum > 0: if prefix_sum + a[i] < 0: prefix_sum += a[i] else: times += abs(-1 - (prefix_sum + a[i])) prefix_sum = -1 return times t1 = 0 p1 = a[0] t1 = f(t1, p1) t2 = 0 if a[0] > 0: t2 += abs(-1 - a[0]) p2 = -1 else: t2 += 1 - a[0] p2 = 1 t2 = f(t2, p2) print(min(t1, t2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ulli = unsigned long long int; vector<lli> N, rN; lli in, n, d = 0, dp, pm; ulli ans = 0; int main() { cin >> n; for (lli l = 0; l < n; l++) { cin >> in; if (l == 0) { N.push_back(in); } else { N.push_back(N[l - 1] + in); } } for (lli l = 1; l < (lli)N.size(); l++) { dp = d; if (N[l - 1] + dp < 0) { if (N[l] + dp < 0) { d += 1 - N[l] - dp; ans += 1 - N[l] - dp; } else if (N[l] + dp == 0) { d += 1; ans += 1; } } else if (N[l - 1] + dp > 0) { if (N[l] + dp > 0) { d -= N[l] + dp + 1; ans += N[l] + dp + 1; } else if (N[l] + dp == 0) { d -= 1; ans += 1; } } else { for (lli m = l; m < (lli)N.size(); m++) { if (N[m] > 0) { pm = (m - l) % 2; break; } else if (N[m] < 0) { pm = (m - l + 1) % 2; break; } if (m == (lli)N.size() - 1) { pm = (m + 1) % 2; break; } } if (pm == 1) { d += 1; ans += 1; } else if (pm == 0) { d -= 1; ans += 1; } } } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random import itertools sys.setrecursionlimit(10**5) stdin = sys.stdin bisect_left = bisect.bisect_left bisect_right = bisect.bisect_right def LI(): return list(map(int, stdin.readline().split())) def LF(): return list(map(float, stdin.readline().split())) def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split())) def II(): return int(stdin.readline()) def IF(): return float(stdin.readline()) def LS(): return list(map(list, stdin.readline().split())) def S(): return list(stdin.readline().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): a = input().split() a = list(map(lambda x: x.capitalize(), a)) a,b,c = a print(a[0]+b[0]+c[0]) return #B def B(): a = II() b = II() if a > b: print("GREATER") if a < b: print("LESS") if a == b: print("EQUAL") return #C def C(): n = II() a = LI() if a[0] == 0: suma = 1 b = 1 else: suma = a[0] b = 0 for i in a[1:]: if (suma + i) * suma < 0: suma += i continue b += abs(suma + i) + 1 suma = -1 * (suma > 0) or 1 ans = b if a[0] == 0: suma = -1 b = 1 else: suma = -a[0] b = 2 * abs(a[0]) for i in a[1:]: if (suma + i) * suma < 0: suma += i continue b += abs(suma + i) + 1 suma = -1 * (suma > 0) or 1 print(min(ans, b)) return #D def D(): s = S() for i in range(len(s) - 1): if s[i] == s[i+1]: print(i + 1, i + 2) return for i in range(len(s) - 2): if s[i] == s[i + 2]: print(i + 1, i + 3) return print(-1, -1) return #Solve if __name__ == '__main__': C()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.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 q = 1; a[0] = sc.nextLong(); A = a[0]; if (a[0] < 0) { q = -1; } for (int i = 1; i < n; i++) { a[i] = sc.nextLong(); A += a[i]; q = q * -1; if (A <= 0 && q == 1) { ans += Math.abs(A - 1); A += Math.abs(A - 1); } else if (A >= 0 && q == -1) { ans += Math.abs(A + 1); A -= Math.abs(A + 1); } } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long dptemp[100010]; long long s1[100010], dp[100010]; int main() { long long mi = 9223372036854775807, n, a, sum, pri1, pri2, all; scanf("%lld", &n); dp[0] = 0; for (a = 1; a <= n; a++) { scanf("%lld", &s1[a]); dp[a] = s1[a] + dp[a - 1]; dptemp[a] = dp[a]; } if (dp[1] == 0) { dp[1]++; all = 1; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; for (a = 1; a <= n; a++) dp[a] = dptemp[a]; dp[1]--; all = 1; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } else if (dp[1] > 0) { all = 0; for (a = 1; a <= n; a++) dp[a] = dptemp[a]; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } else { sum = 0; all = 0; for (a = 1; a <= n; a++) dp[a] = dptemp[a]; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } printf("%lld\n", mi); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long res1 = 0, res2 = 0; long long som = v[0]; if (som > 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = som + 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = 1 - som; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace AtCorder { public class Program { const long MOD = 1000000007; static void Main(string[] args) { var cin = new Scanner2(); int n = cin.Int(); long[] a = cin.ArrayLong(n); long current = a[0]; if (current == 0) { current = 1; } long ans1 = 0; for (int i = 1; i < n; i++) { long next = current + a[i]; if (current * next < 0) { current = next; continue; } long temp = current * a[i]; if (temp > 0) { ans1 += Math.Abs(a[i]) + Math.Abs(current) + 1; current = current > 0 ? - 1 : 1; } else if (temp < 0) { ans1 += Math.Abs(current + a[i]) + 1; current = current > 0 ? -1 : 1; } else { ans1 += Math.Abs(current) + 1; current = current > 0 ? -1 : 1; } } current = a[0]; if (current == 0) { current = -1; } long ans2 = 0; for (int i = 1; i < n; i++) { long next = current + a[i]; if (current * next < 0) { current = next; continue; } long temp = current * a[i]; if (temp > 0) { ans2 += Math.Abs(a[i]) + Math.Abs(current) + 1; current = current > 0 ? -1 : 1; } else if (temp < 0) { ans2 += Math.Abs(current + a[i]) + 1; current = current > 0 ? -1 : 1; } else { ans2 += Math.Abs(current) + 1; current = current > 0 ? -1 : 1; } } Console.WriteLine(Math.Min(ans1, ans2)); } } public class Scanner2 { private readonly char[] delimiter_ = new char[] { ' ' }; private readonly string filePath_; private string[] buf_; private int index_; Func<string> reader_; public Scanner2(string file = "") { if (string.IsNullOrWhiteSpace(file)) { reader_ = Console.ReadLine; } else { filePath_ = file; var fs = new StreamReader(file); reader_ = fs.ReadLine; } buf_ = new string[0]; index_ = 0; } public string Next() { if (index_ < buf_.Length) { return buf_[index_++]; } string st = reader_(); while (st == "") { st = reader_(); } buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries); if (buf_.Length == 0) { return Next(); } index_ = 0; return buf_[index_++]; } public int Int() => int.Parse(Next()); public long Long() => long.Parse(Next()); public double Double() => double.Parse(Next()); public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = Int() + add; } return Array; } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = Long() + add; } return Array; } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = Double() + add; } return Array; } public void Save(string text) { if (string.IsNullOrWhiteSpace(filePath_)) { return; } File.WriteAllText(filePath_ + "_output.txt", text); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 999999999; const int MOD = 1e9 + 7; const double pi = 3.141592653589793238; long long n, k[100005], ans, cou, cou2; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> k[i]; } for (int i = 0; i < n; ++i) { ans += k[i]; if (i % 2 == 0 && cou <= 0) { cou += 1 - ans; ans = 1; } if (i % 2 == 1 && ans >= 0) { cou += ans + 1; ans = -1; } } ans = 0; for (int i = 0; i < n; ++i) { ans += k[i]; if (i % 2 == 1 && ans <= 0) { cou2 += 1 - ans; ans = 1; } if (i % 2 == 0 && ans >= 0) { cou2 += ans + 1; ans = -1; } } cout << min(cou, cou2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import java.util.* fun main(args: Array<String>) { val sc = Scanner(System.`in`) val n = sc.nextInt() val a = (0 until n).map { sc.next().toLong() } println(problem059c(n, a)) } fun problem059c(n: Int, a: List<Long>): Long { val count1 = compute(n, a) val a = a.toMutableList() var a0 = a[0] var count = 0L if (a0 > 0) { val tmp = a0 + 1 a[0] = a0 - tmp count += tmp } else { val tmp = a0 - 1 a[0] = a0 - tmp count -= tmp } val count2 = compute(n, a) + count return Math.min(count1, count2) } fun compute(n: Int, a: List<Long>): Long { val a = a.toMutableList() var count = 0L for (i in 0 until n) { val ai = a[i] val sum = a.take(i).sum() + ai if (sum == 0L) { break } if (i >= n - 1) { continue } val sum2 = sum + a[i + 1] if (sum * sum2 < 0) { continue } else { if (sum > 0) { val tmp = sum2 + 1 a[i + 1] = sum2 - tmp count += tmp } else { val tmp = sum2 - 1 a[i + 1] = sum2 - tmp count -= tmp } } } return count }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 1; i < n; i++) { a[i] += a[i - 1]; if (a[i] >= 0 && a[i - 1] > 0) { ans += abs(a[i] + 1); a[i] = -1; } else if (a[i] <= 0 && a[i - 1] < 0) { ans += abs(a[i] - 1); a[i] = 1; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long int; const ll INF = (1LL << 32); const ll MOD = (ll)1e9 + 7; const double EPS = 1e-9; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; ll n; ll solve(vector<ll> a) { ll sum = a[0]; ll ans = 0; for (ll i = (1); i < (n); i++) { if (sum >= 0 and (sum + a[i]) >= 0) { while (sum + a[i] != -1) { a[i]--; ans++; } } else if (sum < 0 and (sum + a[i]) < 0) { while (sum + a[i] != 1) { a[i]++; ans++; } } sum += a[i]; } if (sum == 0) ans++; return ans; } signed main() { ios::sync_with_stdio(false); cin >> n; vector<ll> a; for (ll i = 0; i < n; i++) { ll x; cin >> x; a.push_back(x); } ll start = a[0]; auto ac = a; ll fa1 = solve(a); ac[0] = ac[0] *= -1; ll fa2 = solve(ac); fa2 += start + 1; cout << min(fa1, fa2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> long calc(long *A, long N, long start) { long s = A[0]; long count = 0; if (s == 0) { count++; s = start; } for (int i = 1; i < N; i++) { s += A[i]; if (i % 2 == (start == 1 ? 0 : 1)) { if (s <= 0) { count += 1 - s; s = 1; } } else { if (s >= 0) { count += s + 1; s = -1; } } } return count; } int main() { long N; scanf("%ld", &N); long *A = (long *)malloc(N * sizeof(long)); long n; long i = 0; while (scanf("%ld", &n) != EOF) { A[i] = n; i++; } printf("%ld", ((calc(A, N, 1)) < (calc(A, N, -1)) ? (calc(A, N, 1)) : (calc(A, N, -1)))); free(A); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(int a) { if (a > 0) return 1; else if (a < 0) return -1; else return 0; } int main(void) { int n; cin >> n; int sum = 0, s = 1; int num = 0; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { sum += a[i]; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * -1; s *= -1; } else { s = sign(sum); } } int m = num; sum = 0; num = 0; num += abs(a[0]) + 1; if (a[0] > 0) sum = -1; else sum = 1; for (int i = 1; i < n; i++) { sum += a[i]; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * -1; s *= -1; } else { s = sign(sum); } } m = min(num, m); cout << m << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) s = list(map(int,input().split())) temp = s[0] ans = 0 #第一項が正ならば if temp > 0: for i in range(1,n): temp += s[i] if i%2 == 0:#正になってほしい if temp <= 0: ans += abs(temp-1) temp = 1 else:#負になって欲しい if temp >= 0: ans += abs(temp+1) temp = -1 if temp < 0: for i in range(1,n): temp += s[i] if i%2 == 0: if temp >= 0: ans += abs(temp+1) temp = -1 else: if temp <= 0: ans += abs(temp-1) temp = 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i digits = gets.split.map(&:to_i) sum = digits[0] cnt = 0 (1...digits.size).each do |i| sum1 = sum sum2 = sum1 + digits[i] if sum1 * sum2 >= 0 target = sum1 > 0 ? -1 : 1 diff = target - sum2 cnt += diff.abs sum += diff end sum += digits[i] end puts cnt
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3,no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") using namespace std; using Graph = vector<vector<int64_t>>; const double pi = M_PI; const int64_t MOD = 1000000007; int64_t calc(const vector<int64_t> &a, int64_t n, int64_t tem) { int64_t ans = 0; if (tem == 0) { if (0 <= a[1]) { tem = -1; ans++; } else { tem = 1; ans++; } } for (int i = 1; i < n; i++) { if ((0 < tem + a[i] && tem < 0) || (tem + a[i] < 0 && 0 < tem)) { tem += a[i]; } else { if (0 <= tem + a[i] && 0 <= tem) { ans += abs(-1 - (tem + a[i])); tem = -1; } else { ans += abs(1 - (tem + a[i])); tem = 1; } } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; cin >> n; vector<int64_t> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int64_t aa, bb, ansdelb, ansdela; if (abs(a[0]) != 1) { aa = 1; ansdela = abs(a[0]) - 1; } else { ansdela = 0; } if (0 <= a[0]) { bb = -1; } else { bb = 1; } ansdelb = abs(a[0]) + 1; int64_t ans = min(calc(a, n, aa) + ansdela, calc(a, n, bb) + ansdelb); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) now = a[0] m = now < 0 and -1 or 1 result = 0 for i, v in enumerate(a[1:]): tmp = now * -1 if m < 0: tmp += 1 if tmp < v: now += v else: result += tmp - v now = 1 else: tmp -= 1 if tmp > v: now += v else: result += v - tmp now = -1 m *= -1 print(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": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; long long sum = 0, ans = 0; bool isPlus; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; if (i == 0) { isPlus = a[i] > 0 ? true : false; } else { isPlus = !isPlus; } if (isPlus) { if (sum <= 0) { ans += abs(1 - sum); sum = 1; } } else { if (sum >= 0) { ans += abs(sum + 1); sum = -1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) lis=[] now=0 for num in a: now+=num lis.append(now) ans=10**20 cnt=0 sm=0 for i in range(len(lis)): if i%2==0: if lis[i]+sm >= 0: add = lis[i]+sm+1 cnt+= add sm=-add else: if lis[i]+sm <= 0: add = abs(1-lis[i]-sm) cnt+= add sm=add ans=min(ans,cnt) cnt=0 sm=0 for i in range(len(lis)): if i%2==1: if lis[i]+sm >= 0: add = lis[i]+sm+1 cnt+= add sm=-add else: if lis[i]+sm <= 0: add = abs(1-lis[i]-sm) cnt+= add sm=add 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": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1001001000; const int mINF = -1001001000; const long long LINF = 1010010010010010000; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) { cin >> a[i]; } long long ans = INF; int flip = 0; for (int si = 0; si < (2); ++si) { int f = flip; long long cnt = 0; long long sum = 0; for (int i = 0; i < (n); ++i) { if (f) { if (sum + a[i] <= 0) { cnt += 1 - (sum + a[i]); sum = 1; } else { sum = sum + a[i]; } } else { if (sum + a[i] >= 0) { cnt += sum + a[i] + 1; sum = -1; } else { sum = sum + a[i]; } } f = f ^ 1; } ans = min(ans, cnt); flip ^= 1; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int ans = 0, sum = 0; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; if (a[0] > 0) { for (int i = 0, tmp = 1; i < N; i++, tmp *= -1) { if (a[i] * a[i + 1] > 0) { sum += a[i]; ans += abs(sum - tmp); sum = tmp; } } sum = 0; } else if (a[0] < 0) { for (int i = 0, tmp = -1; i < N; i++, tmp *= -1) { if (a[i] * a[i + 1] > 0) { sum += a[i]; ans += abs(sum - tmp); sum = tmp; } } } else { a[0] += 1; for (int i = 0, tmp = 1; i < N; i++, tmp *= -1) { if (a[i] * a[i + 1] > 0) { sum += a[i]; ans += abs(sum - tmp); sum = tmp; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { 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]; } int sum = 0; int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum <= 0) { cnt1 += 1 - sum; sum = 1; } else if (i % 2 == 1 && sum >= 0) { cnt1 += 1 + sum; sum = -1; } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { cnt2 += 1 - sum; sum = 1; } else if (i % 2 == 0 && sum >= 0) { cnt2 += 1 + sum; sum = -1; } } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using ll = long long; int main() { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::vector<int> dp(n, 0); ll sum1 = 0, sum2 = a[0]; for (int i = 0; i < n - 1; i++) { sum1 = sum2; sum2 += a[i + 1]; if (sum1 * sum2 < 0) { dp[i + 1] = dp[i]; continue; } if (sum1 > 0) { dp[i + 1] = dp[i] + sum2 + 1; a[i + 1] -= sum2 + 1; sum2 = -1; } else { dp[i + 1] = dp[i] + 1 - sum2; a[i + 1] += 1 - sum2; sum2 = 1; } } std::cout << dp[n - 1] << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans=10**10 s=0 cnt=0 for i in range(n): s+=a[i] if i%2==0: if s>=0: cnt+=1+s s=-1 else: if s<=0: cnt+=1-s s=1 ans=min(ans,cnt) s=0 cnt=0 for i in range(n): s+=a[i] if i%2==1: if s>=0: cnt+=1+s s=-1 else: if s<=0: cnt+=1-s s=1 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": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, flag = 0; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long int ans = 0, sum = a[0]; if (sum > 0) { flag = 1; } else if (sum < 0) { flag = 0; } else if (sum == 0 && a[1] > 0) { sum = -1; ans++; flag = 0; } else if (sum == 0 && a[1] < 0) { sum = 1; ans++; flag = 1; } else if (sum == 0) { sum++; ans++; flag = 1; } for (int i = 1; i < n; i++) { sum += a[i]; if ((flag == 1 && sum < 0) || (flag == 0 && sum > 0)) { flag = 1 - flag; continue; } else if (sum == 0) { if (i + 1 == n) { ans++; } else if (a[i + 1] >= 0) { sum = -1; ans++; flag = 0; } else if (a[i + 1] < 0) { sum = 1; ans++; flag = 1; } } else { ans += (1 + abs(sum)); if (sum > 0) { sum = -1; flag = 0; } else { sum = 1; flag = 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 ans=0; //long ans2=0; int[] f = new int[n]; for(int i=0;i<n;i++) f[i] = sc.nextInt(); sc.close(); long sum1 = f[0]; long sum2 = f[0]; for(int i=0;i<n-1;i++) { sum2 += f[i+1]; if(sum1*sum2 > 0) { ans += Math.abs(sum2)+1; if(sum1 >0) sum2 = -1; else sum2 = 1; }else if(sum2 == 0) { ans += 1; if(sum1 > 0) sum2 = -1; else sum2 = 1; } sum1 = sum2; } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int x = 0, sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum < 1) { x += 1 - sum; sum = 1; } } else { if (sum > -1) { x += 1 + sum; sum = -1; } } } int y = 0, sum2 = 0; for (int i = 0; i < n; i++) { sum2 += a[i]; if (i % 2 == 0) { if (sum2 > -1) { y += (1 + sum2); sum2 = -1; } } else { if (sum2 < 1) { y += (1 - sum2); sum2 = 1; } } if (y >= x) { break; } } cout << min(x, y) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) l = len(a) cnt = 0 a_ = 0 x = -1 # -から for i in range(l): a_ += a[i] if x == -1: if a_ > x: cnt += a_ -x a_ = -1 else: if a_ < x: cnt += x-a_ a_ = 1 x *= -1 cnt1 = 0 x1 = 1 # +から for i in range(l): a_ += a[i] if x1 == -1: if a_ > x1: cnt += a_ -x a_ = -1 else: if a_ < x1: cnt += x1-a_ a_ = 1 x1 *= -1 print(min(x, x1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) arr = list(map(int, input().split())) c = 0 prev = 0 for i in range(n): flag = False s = sum(arr[:i+1]) if i > 0: if prev > 0 and s >= 0: diff = s + 1 c += diff arr[i] -= diff elif prev < 0 and s <= 0: diff = -1 * s + 1 c += diff arr[i] += diff else: flag = True prev = s if flag else sum(arr[:i+1]) print(c)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T = int> T in() { T x; cin >> x; return x; } int main() { int N = in(); vector<int> a(N); for (int i = 0; i < (N); i++) a[i] = in(); int prev = a[0]; int sum = 0; for (int i = 1; i < N; i++) { int curr = prev + a[i]; ; ; if (prev < 0 && curr < 0) { sum += abs(curr) + 1; curr = 1; } else if (prev > 0 && curr > 0) { sum += abs(curr) + 1; curr = -1; } else if (prev > 0 && curr == 0) { curr -= 1; sum++; } else if (prev < 0 && curr == 0) { curr += 1; sum++; } prev = curr; ; } cout << sum << '\n'; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N, a[100000]; cin >> N; for (int i = 0; i < N; ++i) cin >> a[i]; int counter = 0; long long sum = 0; if (a[0] >= 0) { for (int i = 0; i < N; ++i) { sum += a[i]; if (i % 2 == 0) { while (sum <= 0) { ++a[i]; ++sum; ++counter; } } else { while (sum >= 0) { --a[i]; --sum; ++counter; } } } } else { for (int i = 0; i < N; ++i) { sum += a[i]; if (i % 2 == 0) { while (sum >= 0) { --a[i]; --sum; ++counter; } } else { while (sum <= 0) { ++a[i]; ++sum; ++counter; } } } } cout << counter << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] sumi = a[0] sumi1 = a[0] ans = 0 for i in range(n-1): sumi1 = sumi + a[i+1] if sumi*sumi1 < 0: sumi += a[i+1] continue else: if sumi < 0: v = 1 - sumi ans += v-a[i+1] a[i+1] = v elif sumi > 0: v = - 1 - sumi ans += a[i+1] - v a[i+1] = v sumi += a[i+1] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a[1000000]; int even() { int res = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { res += -sum + 1; sum = 1; } } else { if (sum >= 0) { res += sum + 1; sum = -1; } } } return res; } int odd() { int res = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum >= 0) { res += sum + 1; sum = -1; } } else { if (sum <= 0) { res += -sum + 1; sum = 1; } } } return res; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } cout << min(even(), odd()) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) cnt = 0 s = 0 for i in range(n): if i == 0: s += a[i] if s == 0: s = 1 cnt += 1 else: if s > 0: if s < -a[i]: s += a[i] else: cnt += (s + a[i]) + 1 s = -1 else: if s + a[i] > 0: s += a[i] else: cnt += -(s + a[i]) + 1 s = 1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool debug = false; int main() { int n; long long a[100005]; long long cnt = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0] + a[1]; if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } bool plus = true; for (int i = 2; i < n; i++) { sum += a[i]; if (debug) cout << "sum:" << sum << endl; if (plus) { if (sum >= 0) { cnt += sum + 1; sum = -1; } plus = false; } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } plus = true; } } if (sum == 0) cnt += 1; int tmp = cnt; sum = a[0] + a[1]; cnt = 0; if (sum >= 0) { cnt += sum + 1; sum = -1; } plus = false; for (int i = 2; i < n; i++) { sum += a[i]; if (debug) cout << "sum:" << sum << endl; if (plus) { if (sum >= 0) { cnt += sum + 1; sum = -1; } plus = false; } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } plus = true; } } if (sum == 0) cnt += 1; cout << min(int(cnt), int(tmp)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; vector<int> A(N); for (long long i = 0; i < long long(N); i++) { cin >> A[i]; } int ansA = 0; int sumA = 0; for (long long i = 0; i < long long(N); i++) { sumA += A[i]; if (i % 2 == 0) { if (sumA <= 0) { sumA += (abs(sumA) + 1); ansA += (abs(sumA) + 1); } } else { if (sumA >= 0) { sumA -= (abs(sumA) + 1); ansA += (abs(sumA) + 1); } } } int ansB = 0; int sumB = 0; for (long long i = 0; i < long long(N); i++) { sumB += A[i]; if (i % 2 != 0) { if (sumB <= 0) { sumB += (abs(sumB) + 1); ansB += (abs(sumB) + 1); } } else { if (sumB >= 0) { sumB -= (abs(sumB) + 1); ansB += (abs(sumB) + 1); } } } cout << min(ansA, ansB) << endl; return 0; }