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; int main() { int n; cin >> n; vector<long long int> a(n), a2(n); vector<long long int> sum(n), sum2(n); int num[2] = {0}; for (int i = 0; i < n; i++) { cin >> a[i]; } copy(a.begin(), a.end(), a2.begin()); sum[0] = a[0]; if (sum[0] < 0) { sum[0] = 1; num[0] += abs(1 - a[0]); a[0] = 1; } for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i % 2 == 1) { if (sum[i] > 0) { int tmp = -sum[i - 1] - 1; num[0] += abs(tmp - a[i]); a[i] = tmp; sum[i] = -1; } } else { if (sum[i] < 0) { int tmp = 1 - sum[i - 1]; num[0] += abs(tmp - a[i]); a[i] = tmp; sum[i] = 1; } } } sum2[0] = a2[0]; if (sum2[0] > 0) { sum2[0] = -1; num[1] += a2[0] + 1; a2[0] = -1; } for (int i = 1; i < n; i++) { sum2[i] = sum2[i - 1] + a2[i]; if (i % 2 == 1) { if (sum2[i] < 0) { int tmp = 1 - sum2[i - 1]; num[1] += abs(tmp - a2[i]); a2[i] = tmp; sum2[1] = sum2[i - 1] + a2[i]; } } else { if (sum2[i] > 0) { int tmp = -1 - sum2[i - 1]; num[1] += abs(tmp - a2[i]); a2[i] = tmp; sum2[i] = sum2[i - 1] + a2[i]; } } } int lsum = 0; for (int i = 0; i < n; i++) { lsum += a2[i]; } if (lsum == 0) cout << min(num[0], num[1]) + 1 << endl; else { cout << min(num[0], num[1]) << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
def solve(digits) if digits[0].zero? digits1 = digits.dup digits1[0] = 1 digits2[0] = digits.dup digits2[0] = -1 return [digits1, digits2].map{|dgt| solve(dgt)}.min end 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 cnt end n = gets.to_i digits = gets.split.map(&:to_i) puts solve(digits)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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; long long count = 0, sum = 0; long a[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { sum += a[i]; temp = a[i + 1]; if (sum >= 0 && sum + a[i + 1] >= 0) { a[i + 1] = sum * (-1) - 1; count += abs(a[i + 1] - temp); if (sum + a[i + 1] == 0) { a[i + 1] -= 1; count += 1; } } else if (sum <= 0 && sum + a[i + 1] <= 0) { a[i + 1] = 1 + sum * (-1); count += abs(a[i + 1] - temp); if (sum + a[i + 1] == 0) { a[i + 1] += 1; count += 1; } } } 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
UNKNOWN
package main import ( "bufio" "fmt" "os" "strconv" ) func main() { s := bufio.NewScanner(bufio.NewReader(os.Stdin)) s.Split(bufio.ScanWords) n := nextInt(s) a := make([]int, n) for i := 0; i < n; i++ { a[i] = nextInt(s) } fmt.Println(solve(a)) } func solve(a []int) int { if a[0] > 0 { return min(rec(a[0], a[1:], 0), rec(-1, a[1:], a[0]+1)) } return min(rec(a[0], a[1:], 0), rec(1, a[1:], 1-a[0])) } func rec(s int, a []int, r int) int { if len(a) == 0 { return r } if s < 0 { n := max(s+a[0], 1) return rec(n, a[1:], r+n-(s+a[0])) } n := min(s+a[0], -1) return rec(n, a[1:], r+(s+a[0])-n) } func max(a int, b int) int { if a > b { return a } return b } func min(a int, b int) int { if a < b { return a } return b } func nextInt(s *bufio.Scanner) int { s.Scan() n, _ := strconv.Atoi(s.Text()) return 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
java
import java.util.Scanner; /** * https://abc059.contest.atcoder.jp/tasks/arc072_a */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.next()); long[] a = new long[N]; for(int i=0; i<N; i++) a[i] = sc.nextLong(); sc.close(); long sum = a[0]; long ans = 0; for(int i=1; i<N; i++){ if(sum>0 && sum+a[i]>=0){ ans += Math.abs(a[i]+sum+1); sum = -1; }else if(sum<0 && sum+a[i]<=0){ ans += Math.abs(a[i]+sum-1); sum = 1; }else{ sum = sum + a[i]; } } 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
python3
# coding: utf-8 import math if __name__ == "__main__": n = int(input()) nums = list(map(int, input().split())) temp = 0 counts = 0 for num in nums: if temp * (temp+num)>0: counts += abs(temp+num)+1 temp = -temp//abs(temp) elif temp * (temp+num)<0: temp = temp+num else: if temp == 0: temp = temp+num else: counts += 1 temp = -temp//abs(temp) print(counts)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { int n; cin >> n; int suma = 0, sumb = 0; int ansa = 0, ansb = 0; for (int i = (int)0; i < (int)n; i++) { int x; cin >> x; suma += x; sumb += x; if (i % 2 == 0) { if (suma >= 0) { ansa += suma + 1; suma -= (suma + 1); } if (sumb <= 0) { ansb += abs(sumb) + 1; sumb += abs(sumb) + 1; } } else { if (suma <= 0) { ansa += abs(suma) + 1; suma += abs(suma) + 1; } if (sumb >= 0) { ansb += sumb + 1; sumb -= (sumb + 1); } } } cout << min(ansa, ansb) << 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 getCount(vector<int> a) { long long int sum = a[0]; long long int count = 0; bool pos = true; if (sum < 0) pos = false; for (int i = 1; i < a.size(); i++) { if (!pos) { sum += a[i]; if (sum <= 0) { count += abs(sum) + 1; sum = 1; } pos = true; } else { sum += a[i]; if (sum >= 0) { count += sum + 1; sum = -1; } pos = false; } } return count; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { a[0] = -1; long long int count1 = getCount(a); a[0] = 1; long long int count2 = getCount(a); cout << min(count1, count2); } else { cout << getCount(a) << 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
n = gets.to_i A = gets.split.map(&:to_i) x = A[0] answer = 0 for i in 0..n-2 s = x + A[i+1] if x * s >= 0 if x > 0 answer = answer - s + 1 A[i+1] = A[i+1] - s - 1 else answer = answer + s + 1 A[i+1] = A[i+1] - s - 1 end end x = x + A[i+1] end puts answer
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector<long long> S1(N); vector<long long> S2(N); S1[0] = A[0]; S2[0] = A[0]; int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < N; i++) { if (i) { S1[i] = S1[i - 1] + A[i]; S2[i] = S2[i - 1] + A[i]; } if (!(i % 2)) { if (S1[i] <= 0) { cnt1 += 1 - S1[i]; S1[i] = 1; } if (S2[i] >= 0) { cnt2 += S2[i] + 1; S2[i] = -1; } } else { if (S1[i] >= 0) { cnt1 += S1[i] + 1; S1[i] = -1; } if (S2[i] <= 0) { cnt2 += 1 - S2[i]; S2[i] = 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
python3
import numpy as np n = int(input()) a_array = np.array([int(elem) for elem in input().split(' ')]) def calculate_cumulative_sum(a_array, n): cumul_sum_array = np.zeros(n, dtype=np.int32) cumul_sum_array[0] = a_array[0] for i in range(1, n): cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i] return cumul_sum_array def check_1(cumul_sum_array): result = (cumul_sum_array != 0).all() return result def check_2(cumul_sum_array): odd = np.sign(cumul_sum[::2]) even = np.sign(cumul_sum[1::2]) odd_sign = np.unique(odd) even_sign = np.unique(even) return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0]) cumul_sum = calculate_cumulative_sum(a_array, n) count = {'plus': 0, 'minus': 0} def calc_num_manipulation(cumul_sum, start, n): cumul_sum = cumul_sum.copy() count = 0 if check_1(cumul_sum) and check_2(cumul_sum): return count for i in range(n): if i == 0: diff = cumul_sum[i] - start count += abs(diff) cumul_sum[i:] -= diff else: c_back, c_just = cumul_sum[i - 1], cumul_sum[i] if c_back < 0 and c_just <= 0: diff = c_just - 1 count += abs(diff) cumul_sum[i:] -= diff elif c_back > 0 and c_just >= 0: diff = c_just - (-1) count += abs(diff) cumul_sum[i:] -= diff else: continue return count count['plus'] = calc_num_manipulation(cumul_sum, 1, n) count['minus'] = calc_num_manipulation(cumul_sum, -1, n) print(min(count.values()))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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() { auto& in = cin; size_t n; in >> n; vector<int> a(n); for (int i = 0; i < n; ++i) in >> a[i]; int ans = 0; if (a[0] == 0) { if (0 < a[1]) a[0]--; else a[0]++; ans++; } int sum_prev = a[0]; for (int i = 1; i < n; ++i) { int sum_curr = sum_prev + a[i]; if (sum_curr == 0) { ans++; } else { if (0 < sum_curr * sum_prev) { ans += abs(sum_curr) + 1; if (0 < sum_curr) a[i] -= abs(sum_curr) + 1; else a[i] += abs(sum_curr) + 1; } } sum_prev = sum_prev + a[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 if a[0] == 0: a[0] += 1 ans += 1 elif a[0] < 0: a = list(map(lambda x: x * (-1), a)) product = a[0] for i in range(1, n): product += a[i] if i % 2 == 0: if product <= 0: ans += abs(product) + 1 product = 1 elif i % 2 == 1: if product >= 0: ans += abs(product) + 1 product = -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
java
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ Sequence solver = new Sequence(); solver.readInput(); solver.solve(); solver.writeOutput(); } static class Sequence { private int n; private long a[]; private int output; private Scanner scanner; public Sequence() { this.scanner = new Scanner(System.in); } public void readInput() { n = Integer.parseInt(scanner.next()); a = new long[n]; for(int i=0; i<n; i++) { a[i] = Integer.parseInt(scanner.next()); } } private int count(boolean sign) { int count=0; long sum=0; for(int i=0; i<n; i++) { sum += a[i]; if((i%2==0) == sign) { // a[i]までの合計を正にするとき if(sum<=0) { count += Math.abs(sum)+1; sum = 1; } } else { // a[i]までの合計を負にするとき if(0<=sum) { count += Math.abs(sum)+1; sum = -1; } } } return count; } public void solve() { output = Math.min(count(true), count(false)); } public void writeOutput() { System.out.println(output); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100010]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int total = 0, count_f = 0, count_l = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { total += a[i]; if (total <= 0) { while (total <= 0) { total++; count_f++; } } } else { total += a[i]; if (total >= 0) { while (total >= 0) { total--; count_f++; } } } } total = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { total += a[i]; if (total >= 0) { while (total >= 0) { total--; count_l++; } } } else { total += a[i]; if (total <= 0) { while (total <= 0) { total++; count_l++; } } } } cout << min(count_f, count_l) << 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; vector<int> T; cin >> N; for (int i = 0; i < N; i++) { int tmp; cin >> tmp; T.push_back(tmp); } int ans = 0; int sum = 0; bool pre_pm; sum = T.at(0); if (sum > 0) { pre_pm = true; } else { pre_pm = false; } for (int i = 1; i < N; i++) { if (pre_pm) { sum += T.at(i); while (0 <= sum) { sum--; ans++; } pre_pm = false; } else { sum += T.at(i); while (sum <= 0) { sum++; ans++; } pre_pm = true; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = LLONG_MAX; int a[100010]; int rui[100010]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; rui[0] = a[0]; for (int i = 1; i < n; i++) { rui[i] += a[i] + rui[i - 1]; } int temp = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && rui[i] + temp == 0) { temp--; ans++; continue; } if (i % 2 == 1 && rui[i] + temp == 0) { temp++; ans++; continue; } if (i % 2 == 0 && rui[i] + temp < 0) { temp = 1 - (rui[i] + temp); ans += abs(temp); } if (i % 2 == 1 && rui[i] + temp > 0) { temp = 1 + (rui[i] + temp); ans += abs(temp); } } if (ans < 0) ans = 1e9; temp = 0; int ans2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && rui[i] + temp == 0) { temp++; ans2++; continue; } if (i % 2 == 1 && rui[i] + temp == 0) { temp--; ans2++; continue; } if (i % 2 == 0 && rui[i] + temp > 0) { temp = (-1 - rui[i] + temp); ans2 += abs(temp); } if (i % 2 == 1 && rui[i] + temp < 0) { temp = -(-1 + rui[i] + temp); ans2 += abs(temp); } } if (ans2 < 0) ans2 = 1e9; cout << min(ans, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int ch_sign(int n) { if (n == 0) return 0; return (n > 0) - (n < 0); } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int sign1 = (a[0] > 0) - (a[0] < 0), sign2 = -1 * sign1; int s1 = 0, s2 = 0; int ans1 = 0, ans2 = 0; for (int i = 0; i < n; ++i) { s1 += a[i]; s2 += a[i]; sign1 *= -1; sign2 *= -1; if (ch_sign(s1) != sign1) { ans1 += abs(s1 - sign1); s1 = sign1; } if (ch_sign(s2) != sign2) { ans2 += abs(s2 - sign2); s2 = sign2; } } cout << ((ans1 < ans2) ? ans1 : ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[100010] = {}; long long calc(long long *, long long); int get_sign(long long); int main() { long long n, ans; cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; } ans = calc(a, n); cout << ans << endl; return 0; } long long calc(long long *a, long long n) { long long cnt = 0; int sign = 0; long long tmp; if (a[0] > 0) { sign = 1; } tmp = a[0]; for (long long i = 0; i < n - 1; i++) { tmp = tmp + a[i + 1]; if (sign == 1) { if (tmp >= 0) { cnt += tmp + 1; tmp = -1; } } else { if (tmp <= 0) { cnt += (-1 * tmp) + 1; tmp = 1; } } sign = get_sign(tmp); } return cnt; } int get_sign(long long tmp) { if (tmp < 0) return 0; else return 1; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; ll N; vector<ll> a; const ll inf = 1 << 30; int main() { cin >> N; a.resize(N); for (int i = 0; i < N; i++) scanf("%Ld", &a[i]); ll ans = inf; ll sum = 0; ll tempans = 0; for (int i = 0; i < N; i++) { if (i % 2) { if (a[i] + sum >= 0) { tempans += a[i] + sum + 1; sum = -1; } else { sum += a[i]; } } else { if (a[i] + sum <= 0) { tempans += abs(a[i] + sum) + 1; sum = 1; } else { sum += a[i]; } } } ans = min(ans, tempans); sum = 0; tempans = 0; for (int i = 0; i < N; i++) { if (!(i % 2)) { if (a[i] + sum >= 0) { tempans += a[i] + sum + 1; sum = -1; } else { sum += a[i]; } } else { if (a[i] + sum <= 0) { tempans += abs(a[i] + sum) + 1; sum = 1; } else { sum += a[i]; } } } ans = min(ans, tempans); 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; vector<int64_t> a(n); for (int i = 0; i < n; i++) cin >> a.at(i); int64_t ans = 0, s = 0; for (int i = 0; i < n; i++) { if (s > 0) { if (s + a.at(i) >= 0) { ans += (s + a.at(i) - (-1)); s = -1; continue; } } else if (s < 0) { if (s + a.at(i) <= 0) { ans += (1 - (s + a.at(i))); s = 1; continue; } } s += a.at(i); if (s == 0) { ans++; if (i < n - 1) { for (int j = i + 1; j < n; j++) { if (a.at(j) > 0) { s = -1; break; } else if (a.at(j) < 0) { s = 1; break; } } } } } 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 n, a[100000]; int func(int b, int c) { for (int i = 1; i < n; i++) { if (((a[i] + b > 0) == (b > 0) || !a)) { if (b > 0) { c += a[i] + b + 1; a[i] = -b - 1; } else if (b < 0) { c -= a[i] + b - 1; a[i] = -b + 1; } } b += a[i]; } return c; } int main() { int a1, a2, b1 = 0, b2 = 0, c1 = 0, c2 = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; a1 = a[0]; if (!a1) { a1++; a2--; c1++; c2++; } else if (a1 > 0) { c2 += a1 + 1; a2 = -1; } else { c2 -= a1 - 1; a2 = 1; } int ans1 = func(a1, c1), ans2 = func(a2, c2); if (ans1 < ans2) cout << ans1 << endl; else cout << ans2 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[10002]; long long N; int main() { long long A = 0; long long B = 0; int R = 0; cin >> N; for (long long i = 1; i < N + 1; i++) { cin >> a[i]; } B = a[1]; for (int i = 1; i < N; i++) { A += a[i]; B += a[i + 1]; if (A < 0 && B < 0) { B += (1 - B); R += (1 - B); } if (A > 0 && B > 0) { B += (0 - 1 - B); R += (0 - 1 - B); } } cout << R << 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; #define rep(i, c) for (int i = 0; i < (int)c; i++) int main() { int n; scanf("%d", &n); vector<int> a(n); rep(i, n) scanf("%d", &a[i]); ll temp = 0; //+- ll pm = 0; rep(i, n) { temp = temp + a[i]; if(i % 2 == 0) { if(temp <= 0) { pm = pm + 1 - temp; temp = 1; } } else { if(temp >= 0) { pm = pm + 1 + temp; temp = -1; } } } //-+ ll mp = 0; temp = 0; rep(i, n) { temp = temp + a[i]; if(i % 2 == 1) { if(temp <= 0) { mp = mp + 1 - temp; temp = 1; } } else { if(temp >= 0) { mp = mp + 1 + temp; temp = -1; } } } printf("%lld\n", min(pm, mp)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool 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]; cnt = 0; 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; cnt = 0; sum = a[0] + a[1]; 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; if (tmp < cnt) cout << tmp << endl; else cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long now = (a[0] == 0) ? 1LL : a[0]; long long tmp_ans1 = (a[0] == 0) ? abs(a[0]) + 1LL : 0; for (int i = 1; i < n; ++i) { if (now >= 0 && now + a[i] >= 0) { tmp_ans1 += abs(now + a[i]) + 1LL; now = -1LL; } else if (now < 0 && now + a[i] <= 0) { tmp_ans1 += abs(now + a[i]) + 1LL; now = 1LL; } else { now += a[i]; } } now = (a[0] < 0) ? 1LL : -1LL; long long tmp_ans2 = abs(a[0]) + 1LL; for (int i = 1; i <= n; ++i) { if (now >= 0 && now + a[i] >= 0) { tmp_ans2 += abs(now + a[i]) + 1LL; now = -1LL; } else if (now < 0 && now + a[i] <= 0) { tmp_ans2 += abs(now + a[i]) + 1LL; now = 1LL; } else { now += a[i]; } } cout << min(tmp_ans1, tmp_ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class 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() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long cnt = a[0]; long long ans = 0ll; long long p = -1ll; if (a[0] <= 0ll) p = 1ll; cout << ("ok") << " " << (cnt) << "\n"; for (int i = 1; i < n; ++i) { cnt += a[i]; long long g = 0; if (cnt * p <= 0ll) { g = cnt * -1ll + p; ans += (g * p); cnt = cnt + g; } cout << (ans) << " " << (g) << " " << (cnt) << "\n"; g = 0; p *= -1ll; } cout << (ans) << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vb = vector<bool>; using vvb = vector<vb>; using mii = map<int, int>; using pqls = priority_queue<long long>; using pqlg = priority_queue<long long, vector<long long>, greater<long long>>; using mll = map<long long, long long>; using pll = pair<long long, long long>; using sll = set<long long>; long long divup(long long a, long long b); long long kaijou(long long i); long long P(long long n, long long k); long long C(long long n, long long k); long long GCD(long long a, long long b); long long LCM(long long a, long long b); bool prime(long long N); double distance(vector<long long> p, vector<long long> q, long long n); void press(vector<long long> &v); void ranking(vector<long long> &v); void erase(vector<long long> &v, long long i); void unique(vector<long long> &v); void printv(vector<long long> v); vector<ll> keta(ll x); long long modpow(long long a, long long n, long long mod); long long modinv(long long a, long long mod); vector<long long> inputv(long long n); vector<long long> yakusuu(int n); map<long long, long long> soinsuu(long long n); vector<vector<long long>> maze(long long i, long long j, vector<string> &s); vector<long long> eratos(long long n); set<long long> eraset(long long n); long long divup(long long a, long long b) { long long x = abs(a); long long y = abs(b); long long z = (x + y - 1) / y; if ((a < 0 && b > 0) || (a > 0 && b < 0)) return -z; else if (a == 0) return 0; else return z; } long long kaijou(long long i) { if (i == 0) return 1; long long j = 1; for (long long k = 1; k <= i; k++) { j *= k; } return j; } long long P(long long n, long long k) { if (n < k) return 0; long long y = 1; for (long long i = 0; i < k; i++) { y *= (n - i); } return y; } long long C(long long n, long long k) { if (n < k) return 0; return P(n, k) / kaijou(k); } long long GCD(long long a, long long b) { if (a < b) swap(a, b); long long d = a % b; if (d == 0) { return b; } return GCD(b, d); } long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; } bool prime(long long N) { if (N == 1) { return false; } if (N < 0) return false; long long p = sqrt(N); for (long long i = 2; i <= p; i++) { if (N % i == 0) { return false; } } return true; } double distance(vector<long long> p, vector<long long> q, long long n) { double x = 0; for (long long i = 0; i < n; i++) { x += pow((p.at(i) - q.at(i)), 2); } return sqrt(x); } void press(vector<long long> &v) { long long n = v.size(); vector<long long> w(n); map<long long, long long> m; for (auto &p : v) { m[p] = 0; } long long i = 0; for (auto &p : m) { p.second = i; i++; } for (long long i = 0; i < n; i++) { w.at(i) = m[v.at(i)]; } v = w; return; } void ranking(vector<long long> &v) { long long n = v.size(); map<long long, long long> m; long long i; for (i = 0; i < n; i++) { m[v.at(i)] = i; } vector<long long> w(n); i = 0; for (auto &p : m) { v.at(i) = p.second; i++; } return; } void erase(vector<long long> &v, long long i) { long long n = v.size(); if (i > n - 1) return; for (long long j = i; j < n - 1; j++) { v.at(j) = v.at(j + 1); } v.pop_back(); return; } void unique(vector<long long> &v) { long long n = v.size(); set<long long> s; long long i = 0; while (i < n) { if (s.count(v.at(i))) { erase(v, i); n--; } else { s.insert(v.at(i)); i++; } } return; } void printv(vector<long long> v) { cout << "{ "; for (auto &p : v) { cout << p << ","; } cout << "}" << endl; } vector<ll> keta(ll x) { if (x == 0) return {0}; ll n = log10(x) + 1; vll w(n, 0); for (ll i = 0; i < n; i++) { ll p; p = x % 10; x = x / 10; w[n - 1 - i] = p; } return w; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } vector<long long> inputv(long long n) { vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; } return v; } vector<long long> yakusuu(long long n) { vector<long long> ret; for (long long i = 1; i <= sqrt(n); ++i) { if (n % i == 0) { ret.push_back(i); if (i * i != n) { ret.push_back(n / i); } } } sort(ret.begin(), ret.end()); return ret; } map<long long, long long> soinsuu(long long n) { map<long long, long long> m; long long p = sqrt(n); while (n % 2 == 0) { n /= 2; if (m.count(2)) { m[2]++; } else { m[2] = 1; } } for (long long i = 3; i * i <= n; i += 2) { while (n % i == 0) { n /= i; if (m.count(i)) { m[i]++; } else { m[i] = 1; } } } if (n != 1) m[n] = 1; return m; } vector<vector<long long>> maze(ll i, ll j, vector<string> &s) { ll h = s.size(); ll w = s[0].size(); queue<vector<long long>> q; vector<vector<long long>> dis(h, vll(w, -1)); q.push({i, j}); dis[i][j] = 0; while (!q.empty()) { auto v = q.front(); q.pop(); if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) { dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1; q.push({v[0] - 1, v[1]}); } if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) { dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1; q.push({v[0], v[1] - 1}); } if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) { dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1; q.push({v[0] + 1, v[1]}); } if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) { dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1; q.push({v[0], v[1] + 1}); } } return dis; } long long modC(long long n, long long k, long long mod) { if (n < k) return 0; long long p = 1, q = 1; for (long long i = 0; i < k; i++) { p = p * (n - i) % mod; q = q * (i + 1) % mod; } return p * modinv(q, mod) % mod; } long long POW(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } vector<long long> eratos(long long n) { if (n < 2) return {}; vll v(n - 1); for (long long i = 0; i < n - 1; i++) { v[i] = i + 2; } ll i = 0; while (i < n - 1) { ll p = v[i]; for (ll j = i + 1; j < n - 1; j++) { if (v[j] % p == 0) { v.erase(v.begin() + j); n--; } } i++; } v.resize(n - 1); return v; } set<long long> eraset(long long n) { set<long long> s; vll v = eratos(n); for (auto &t : v) { s.insert(t); } return s; } vll line(ll x1, ll y1, ll x2, ll y2) { vector<ll> v(3); v[0] = y1 - y2; v[1] = x2 - x1; v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2); return v; } double dis(vll v, ll x, ll y) { double s = sqrt(v[0] * v[0] + v[1] * v[1]); return (double)abs(v[0] * x + v[1] * y + v[2]) / s; } ll const mod = 1e9 + 7; int main() { ll n; cin >> n; auto a = inputv(n); ll l = 0; ll res = 0; for (long long i = 0; i < n; i++) { if (i == 0 && a[0] == 0) { for (long long j = 0; j < n - 1; j++) { if (a[j + 1]) { a[0] = -a[j + 1] / abs(a[j + 1]); break; } } if (!a[0]) a[0] = 1; res++; } else if (l < 0) { if (a[i] < -l + 1) { res += -l + 1 - a[i]; a[i] = -l + 1; l = 1; } else { l += a[i]; } } else if (l > 0) { if (a[i] > -l - 1) { res += abs(a[i] - (-l - 1)); a[i] = -l - 1; l = -1; } else { l += a[i]; } } else if (i == 0) { l = a[0]; } } 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> List(n); for (int i = 0; i < n; i++) { cin >> List.at(i); } int cnt, Sign; cnt = Sign = 0; for (int i = 0; i < n; i++) { if (Sign == 0) { if (List.at(i) > 0) { Sign = List.at(i); } else if (List.at(i) < 0) { Sign = List.at(i); } continue; } if (Sign > 0) { if (Sign + List.at(i) >= 0) { cnt += abs(Sign + List.at(i)) + 1; Sign = -1; } else { Sign += List.at(i); } continue; } if (Sign < 0) { if (List.at(i) + Sign <= 0) { cnt += abs(Sign + List.at(i)) + 1; Sign = 1; } else { Sign += List.at(i); } continue; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[100002] = {}; int b[100002] = {}; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { b[i] = a[i]; } int eve = 0, sum = 0; for (int j = 0; j < n; j++) { if (j % 2 == 0 && sum + a[j] <= 0) { eve += abs(a[j] + sum) + 1; a[j] = abs(sum) + 1; } if (j % 2 == 1 && sum + a[j] >= 0) { eve += a[j] + sum + 1; a[j] = -abs(sum) - 1; } sum += a[j]; } sum = 0; int odd = 0; for (int k = 0; k < n; k++) { if (k % 2 == 0 && sum + b[k] >= 0) { odd += abs(b[k] + sum) + 1; b[k] = -abs(sum) - 1; } if (k % 2 == 1 && sum + b[k] <= 0) { odd += abs(sum + b[k]) + 1; b[k] = abs(sum) + 1; } sum += b[k]; } cout << min(odd, eve) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long f(vector<long long>& sum, vector<long long>& pm) { int tmp; tmp = 0; for (int i = 0; i < (n - 1); i++) { sum[i + 1] += pm[i]; if (sum[i] * sum[i + 1] >= 0) { if (sum[i + 1] == 0) { if (sum[i] < 0) sum[i + 1] = pm[i + 1] = 1; else sum[i + 1] = pm[i + 1] = -1; } else if (sum[i + 1] < 0) { pm[i + 1] = 1 - sum[i + 1]; sum[i + 1] = 1; } else if (sum[i + 1] > 0) { pm[i + 1] = -1 - sum[i + 1]; sum[i + 1] = -1; } tmp += abs(pm[i + 1]); } } return tmp; } signed main(void) { cin >> n; vector<long long> s(n), t, pm(n, 0); long long ans, tmp; for (int i = 0; i < (n); i++) { int a; cin >> a; s[i] = a; } for (int i = 0; i < (n - 1); i++) s[i + 1] += s[i]; ans = 1e18; copy(s.begin(), s.end(), back_inserter(t)); tmp = 0; if (t[0] <= 0) { pm[0] = 1 - t[0]; t[0] = 1; tmp += abs(pm[0]); } tmp += f(t, pm); ans = min(ans, tmp); for (int i = 0; i < (n); i++) pm[i] = 0; tmp = 0; if (s[0] >= 0) { pm[0] = -1 - s[0]; s[0] = -1; tmp += abs(pm[0]); } tmp += f(s, pm); ans = min(ans, tmp); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 300000000; const long long MOD = 1000000007; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n; cin >> n; long long a[100100]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long ans = INF; for (int i = 0; i < 2; ++i) { long long count = 0; long long su = 0; for (int j = 0; j < n; ++j) { su += a[j]; if (i == 0) { if (j % 2 == 0 && su <= 0) { count += -su + 1; su = 1; } else if (j % 2 == 1 && su >= 0) { count += su + 1; su = -1; } } if (i == 1) { if (j % 2 == 0 && su >= 0) { count += su + 1; su = -1; } else if (j % 2 == 1 && su <= 0) { count += -su + 1; su = 1; } } } ans = min(ans, count); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC59C { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str = Console.ReadLine().Split(' '); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = int.Parse(str[i]); } int x = 0; int y = 0; int f = 0; int sum = 0; for(int i = 0; i < n; i++) { sum += a[i]; if (f == 1 && sum >= 0) { x += (sum + 1); f = 0; sum = -1; }else if (f == 0 && sum <= 0) { x += (1 - sum); f = 1; sum = 1; }else { f = 1 - f; } } sum = 0; f = 1; for (int i = 0; i < n; i++) { sum += a[i]; if (f == 1 && sum >= 0) { y += (sum + 1); f = 0; sum = -1; } else if (f == 0 && sum <= 0) { y += (1 - sum); f = 1; sum = 1; } else { f = 1 - f; } } Console.WriteLine(Math.Min(x,y)); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> using namespace std; typedef long long int lli int main() { int n; cin >> n; int a[100000]; for (int i = 0;i<n;i++) { cin >> a[i]; } //最初が正のケース lli 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; } } } //最初が負のケース lli 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 - Sp; 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
cpp
#include <bits/stdc++.h> using namespace std; signed main() { int n; cin >> n; int a[n]; for (size_t i = 0; i < n; i++) { cin >> a[i]; } int s = a[0]; int res = 0; for (size_t i = 1; i < n; i++) { if (s > 0) { int s2 = s + a[i]; if (s2 <= -1) { s = s2; } else { s = -1; res += (s2 + 1); } } else { int s2 = s + a[i]; if (s2 >= 1) { s = s2; } else { s = 1; res += (-s2 + 1); } } } s = a[0] > 0 ? -1 : 1; int res2 = abs(a[0]) + 1; for (size_t i = 1; i < n; i++) { if (s > 0) { int s2 = s + a[i]; if (s2 <= -1) { s = s2; } else { s = -1; res2 += (s2 + 1); } } else { int s2 = s + a[i]; if (s2 >= 1) { s = s2; } else { s = 1; res2 += (-s2 + 1); } } } cout << min(res, res2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int mod = 1e9 + 7; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; bool posi; long long sum[n]; sum[0] = a[0]; if (sum[0] > 0) posi = true; else posi = false; for (int i = 1; i < n; i++) { long long sum_t = (long long)a[i] + sum[i - 1]; if (posi) { posi = false; if (sum_t >= 0) { long long t = a[i] - sum_t - 1; ans += abs(a[i] - t); a[i] = t; sum_t = a[i] + sum[i - 1]; } } else { posi = true; if (sum_t <= 0) { long long t = a[i] - sum_t + 1; ans += abs(a[i] - t); a[i] = t; sum_t = a[i] + sum[i - 1]; } } sum[i] = sum_t; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, a[200005], sum, ans; int main() { cin >> n; for (long long i = (long long)0; i < (long long)n; i++) { cin >> a[i]; if (i == 0) { sum = a[i]; continue; } if (sum > 0 && sum + a[i] < 0 || sum < 0 && sum + a[i] > 0) { sum += a[i]; } else { ans += abs(sum + a[i]) + 1; if (sum > 0) sum = -1; else 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
UNKNOWN
function Main(s) { var s = s.split("\n"); var n = parseInt(s[0], 10); var a = s[1].split(" ").map(e => parseInt(e, 10)); var acc = 0, cnt = 0, arr = []; for (var i = 0; i < n; i++) { acc += a[i]; if (i === 0) { if (acc === 0) { acc++; cnt++; } } else { if (arr[i - 1] > 0) { if (acc >= 0) { cnt += (acc + 1); acc -= (acc + 1); } } else { if (acc <= 0) { cnt += (Math.abs(acc) + 1); acc += (Math.abs(acc) + 1); } } } arr.push(acc); } console.log(cnt); } Main(require("fs").readFileSync("/dev/stdin", "utf8"));
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!/usr/bin/env python3 import sys INF = float("inf") def solve(n: int, a: "List[int]"): # 正スタート tot = 0 countA = 0 for i, x in enumerate(a): tot += x if i % 2 == 0: if tot <= 0: countA += -tot+1 tot = 1 elif i % 2 == 1: if tot >= 0: countA += tot+1 tot = -1 tot = 0 countB = 0 for i, x in enumerate(a): tot += x if i % 2 == 1: if tot <= 0: countB += -tot+1 tot = 1 elif i % 2 == 0: if tot >= 1: countB += tot+1 tot = -1 print(min(countA, countB)) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() n = int(next(tokens)) # type: int a = [int(next(tokens)) for _ in range(n)] # type: "List[int]" solve(n, a) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int f(int s, vector<int> v) { int ans = 0, x, t = 0, y; if (v[0] == 0) { x = s * 1 - t; y = abs(x - v[0]); v[0] = x; ans += y; } t = t + v[0]; for (int i = 1; i < n; i++) { s *= (-1); if ((s == -1 && (t + v[i]) >= 0) || (s == 1 && (t + v[i]) <= 0)) { x = s * 1 - t; y = abs(x - v[i]); v[i] = x; ans += y; } t = t + v[i]; } return ans; } int main() { while (scanf("%d", &n) == 1) { vector<int> arr(100010); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } int x = f(-1, arr); int y = f(1, arr); int ans = min(x, y); printf("%d\n", ans); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) al = list(map(int, input().split())) ans1 = 0 curr_sum = al[0] next_sign = 1 for a in al[1:]: next_sum = curr_sum + a if next_sum >= 0 and next_sign == -1: ans1 += next_sum+1 next_sum = -1 elif next_sum <= 0 and next_sign == 1: ans1 += (1-next_sum) next_sum = 1 curr_sum = next_sum next_sign *= -1 ans2 = 0 curr_sum = al[0] next_sign = -1 for a in al[1:]: next_sum = curr_sum + a if next_sum >= 0 and next_sign == -1: ans2 += next_sum+1 next_sum = -1 elif next_sum <= 0 and next_sign == 1: ans2 += (1-next_sum) next_sum = 1 curr_sum = next_sum next_sign *= -1 print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
"use strict"; const main = arg => { arg = arg.trim().split("\n"); const N = parseInt(arg[0]); const A = arg[1].split(" ").map(n=>parseInt(n)); let totalSum = A[0]; let answer = 0; for(let i=1; i<N; i++) { // 累積和が+かつ、A[i]を足しても+ while(totalSum >= 0 && totalSum + A[i] >= 0) { A[i]--; answer++; } // 累積和が-かつ、A[i]を足しても- while(totalSum <= 0 && totalSum + A[i] <= 0) { A[i]++; answer++; } totalSum += A[i]; } console.log(answer); } main(require('fs').readFileSync('/dev/stdin', 'utf8'));
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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; vector<long long int> vec; vector<vector<long long int> > vec2; long long int MOD = 1000000007; int main() { long long int N; cin >> N; vector<long long int> vec(N, 0); for (long long int i = 0; i < N; i++) { cin >> vec[i]; } long long int ans = 0; long long int g_ans = 0; long long int k_ans = 0; long long int sum = 0; bool flg = true; for (long long int i = 0; i < N; i++) { sum += vec[i]; if (flg == true) { if (sum <= 0) { cout << i << " " << sum << " " << abs(1 - sum) << endl; k_ans += abs(1 - sum); sum += k_ans; } flg = false; } else { if (sum >= 0) { cout << i << " " << sum << " " << abs(-1 - sum) << endl; k_ans += abs(-1 - sum); sum += -k_ans; } flg = true; } } flg = true; sum = 0; for (long long int i = 0; i < N; i++) { sum += vec[i]; if (flg == false) { if (sum <= 0) { g_ans += abs(1 - sum); sum += g_ans; } flg = true; } else { if (sum >= 0) { g_ans += abs(-1 - sum); sum += -g_ans; } flg = false; } } ans = min(k_ans, g_ans); 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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; long long prevArraySum, currentArraySum; long long res, res1 = 1e15; for (int first = 0; first < (int)(2); first++) { if (first % 2 && a[0] == 0) { res = 1; prevArraySum = -1; currentArraySum = -1; } else if (first % 2 == 1 && a[0] == 0) { res = 1; prevArraySum = 1; currentArraySum = 1; } else if (first % 2 == 1) { res = 0; prevArraySum = a[0]; currentArraySum = a[0]; } else if (a[0] > 0) { res = a[0] + 1; prevArraySum = -1; currentArraySum = -1; } else { res = -a[0] + 1; prevArraySum = -1; currentArraySum = -1; } for (int i = (1); i < (n); ++i) { if (prevArraySum > 0) { currentArraySum = prevArraySum + a[i]; if (currentArraySum >= 0) { res += abs(-1 - currentArraySum); prevArraySum = -1; } else { prevArraySum = currentArraySum; } } else { currentArraySum = prevArraySum + a[i]; if (currentArraySum <= 0) { res += abs(1 - currentArraySum); prevArraySum = 1; } else { prevArraySum = currentArraySum; } } } res1 = min(res, res1); } cout << res1 << 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()) b = [int(x) for x in input().split()] temp = 0 count1 = 0 count2 = 0 a = b if a[0] == 0: a[0] = 1 count1 = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: if sum > 0: temp = -1 * abs(sum) - 1 count1 += abs(temp - a[i]) else: temp = abs(sum) + 1 count1 += abs(temp - a[i]) a[i] = temp sum += a[i] count2 = abs(a[0]) + 1 a = b if a[0] > 0: a[0] = -1 else: a[0] = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count2 += abs(sum - a[i]) + 1 if sum > 0: temp = -1 * abs(sum) - 1 count2 += abs(temp - a[i]) else: temp = abs(sum) + 1 count2 += abs(temp - a[i]) a[i] = temp sum += a[i] print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from functools import lru_cache n = int(input()) s = list(map(int, input().split())) @lru_cache(maxsize=None) def cost_e(): res = 0 sum = 0 for j, y in enumerate(s, 1): tmp = sum + y if j & 1 and tmp >= 0: sum = -1 res += abs(tmp) + 1 elif not j & 1 and tmp <= 0: sum = 1 res += abs(tmp) + 1 else: sum = tmp return res @lru_cache(maxsize=None) def cost_o(): res = 0 sum = 0 for j, y in enumerate(s, 1): tmp = sum + y if j & 1 and tmp <= 0: sum = -1 res += abs(tmp) + 1 elif not j & 1 and tmp >= 0: sum = 1 res += abs(tmp) + 1 else: sum = tmp return res print(min(cost_e(), cost_o()))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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 ans = 0, sum = 0; for (int i = 0; i < n; i++) { int presum = sum, cnt = 0; sum += a[i]; if (presum < 0) { if (sum <= 0) { while (sum <= 0) { sum++; cnt++; } } } else if (presum > 0) { if (sum >= 0) { while (sum >= 0) { sum--; cnt++; } } } else continue; ans += cnt; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text.RegularExpressions; using System.Diagnostics; //var input = Console.ReadLine().Split().Select(int.Parse).ToArray(); namespace AtCoderSolve { class Solve { const int mod = 1000000007; static void Main(string[] args) { //var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; //Console.SetOut(sw); int N = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(long.Parse).ToArray(); long[] sum = new long[N]; long[] ans = new long[2]; long c = 1; for (var j = 0; j < 2; j++) { for (var i = 0; i < N; i++) { if (i == 0) { sum[i] = a[i]; } else { sum[i] = sum[i - 1] + a[i]; } if (sum[i] * c <= 0) { ans[j] += 1 + Math.Abs(sum[i]); sum[i] = c; } c *= -1; } c *= -1; } Console.WriteLine($"{ans[0]} {ans[1]}"); //Console.Out.Flush(); } } public class Calculation { } public class Graph { } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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
#region using using System; using System.Collections.Generic; using System.Linq; using IEnumerable = System.Collections.IEnumerable; using IEnumerator = System.Collections.IEnumerator; using BitArray = System.Collections.BitArray; using BigInteger = System.Numerics.BigInteger; using TextReader = System.IO.TextReader; using System.Text; #endregion namespace AtCoderProject { public class Program { public object Calc() { var N = consoleReader.Int; var a = consoleReader.Split.Int; long count = 0; long sum = 0; bool isPositive = a[0] >= 0; for (int i = 0; i < a.Length; i++) { sum += a[i]; if (isPositive && sum <= 0) sum += count += 1 - sum; else if (!isPositive && sum >= 0) sum -= count += sum + 1; isPositive = !isPositive; } return count; } struct Mod : IEquatable<Mod> { public const long mod = 1000000007; public readonly long val; public Mod(long val) { this.val = val; } public override bool Equals(object obj) => (obj is Mod) ? this == ((Mod)obj) : false; public bool Equals(Mod obj) => this == obj; public override int GetHashCode() => val.GetHashCode(); public override string ToString() => val.ToString(); public static implicit operator Mod(long x) => new Mod(x); public static Mod operator +(Mod x, Mod y) => (x.val + y.val) % mod; public static Mod operator -(Mod x, Mod y) => x.val >= y.val ? (x.val - y.val) % mod : (x.val - y.val) % mod + mod; public static Mod operator *(Mod x, Mod y) => (x.val * y.val) % mod; public static Mod operator /(Mod x, Mod y) => x * y.Inverse(); public static bool operator ==(Mod x, Mod y) => x.val == y.val; public static bool operator !=(Mod x, Mod y) => x.val != y.val; public Mod Inverse() { long a = val, b = mod, u = 1, v = 0; while (b > 0) { long t = a / b; var b2 = a - t * b; a = b; b = b2; var v2 = u - t * v; u = v; v = v2; } u %= mod; if (u < 0) u += mod; return u; } public static Mod Pow(Mod x, int y) { Mod res = 1; for (; y > 0; y >>= 1) { if ((y & 1) == 1) res *= x; x *= x; } return res; } public static Factor CreateFactor(int max) => new Factor(max); public class Factor { private readonly Mod[] fac, finv; public Factor(int max) { ++max; var inv = new Mod[max]; fac = new Mod[max]; finv = new Mod[max]; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < max; i++) { fac[i] = fac[i - 1] * i; inv[i] = mod - inv[mod % i].val * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i]; } } // 二項係数計算 public Mod Combine(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } public Mod Factorial(int n) => fac[n]; public Mod FactorialInvers(int n) => finv[n]; } } #region いつもの #pragma warning disable private ConsoleReader consoleReader; public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; } static void Main() => Console.WriteLine(new Program(new ConsoleReader(Console.In)).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source); } static class Ext { public static Dictionary<TKey, int> GroupCount<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) => source.GroupBy(keySelector).ToDictionary(g => g.Key, g => g.Count()); public static Dictionary<TKey, int> GroupCount<TKey>(this IEnumerable<TKey> source) => source.GroupCount(i => i); } public class ConsoleReader { private string[] ReadLineSplit() => textReader.ReadLine().Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries); private string[] line = Array.Empty<string>(); private int linePosition; private TextReader textReader; public ConsoleReader(TextReader tr) { textReader = tr; } public int Int => int.Parse(String); public long Long => long.Parse(String); public double Double => double.Parse(String); public string String { get { if (linePosition >= line.Length) { linePosition = 0; line = ReadLineSplit(); } return line[linePosition++]; } } public class SplitLine { private string[] splited; public SplitLine(ConsoleReader cr) { splited = cr.ReadLineSplit(); cr.line = Array.Empty<string>(); } public int[] Int => String.Select(x => int.Parse(x)).ToArray(); public int[] Int0 => String.Select(x => int.Parse(x) - 1).ToArray(); public long[] Long => String.Select(x => long.Parse(x)).ToArray(); public double[] Double => String.Select(x => double.Parse(x)).ToArray(); public string[] String => splited; } public SplitLine Split => new SplitLine(this); public class RepeatReader : IEnumerable<ConsoleReader> { ConsoleReader cr; int count; public RepeatReader(ConsoleReader cr, int count) { this.cr = cr; this.count = count; } public IEnumerator<ConsoleReader> GetEnumerator() => Enumerable.Repeat(cr, count).GetEnumerator(); System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); public IEnumerable<string> String => this.Select(cr => cr.String); public IEnumerable<int> Int => this.Select(cr => cr.Int); public IEnumerable<int> Int0 => this.Select(cr => cr.Int - 1); public IEnumerable<long> Long => this.Select(cr => cr.Long); public IEnumerable<double> Double => this.Select(cr => cr.Double); } public RepeatReader Repeat(int count) => new RepeatReader(this, count); } #endregion }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 sum; int input; cin >> sum; int ans1, ans2, sum1, sum2; if (sum > 0) { sum1 = sum; sum2 = -1; ans1 = 0; ans2 = abs(sum) + 1; } else if (sum < 0) { sum1 = 1; sum2 = sum; ans1 = abs(sum) + 1; ans2 = 0; } else { sum1 = 1; sum2 = -1; ans1 = 1; ans2 = 1; } for (int i = 1; i < n; ++i) { cin >> input; if (sum1 * (sum1 + input) >= 0) { ans1 += abs(sum1 + input) + 1; if (sum1 < 0) sum1 = 1; else sum1 = -1; } else sum1 += input; if (sum2 * (sum2 + input) >= 0) { ans2 += abs(sum2 + input) + 1; if (sum2 < 0) sum2 = 1; else sum2 = -1; } else sum2 += input; } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) check = '' if a[0] > 0: check = '+' else: check = '-' ans = 0 for i in range(1, n): a[i] += a[i-1] if check == '+': if a[i] >= 0: ans += abs(a[i]) + 1 a[i] -= abs(a[i]) + 1 check = '-' else: if a[i] <= 0: ans += abs(a[i]) + 1 a[i] += abs(a[i]) + 1 check = '+' print(a) 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 maxn = 200010; int n, m, md, ans; int a[maxn], pre[maxn]; ; long long read() { long long s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } int main() { md = 0, ans = 0; memset(pre, 0, sizeof(pre)); n = read(); for (int i = 1; i <= n; i++) { a[i] = read(); pre[i] = a[i]; pre[i] += pre[i - 1]; } if (pre[1] != 0) { for (int i = 1; i < n; i++) { int tmp = md; if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) { if ((pre[i] + tmp) < 0) { md += (1 - (pre[i + 1] + tmp)); ans += (1 - (pre[i + 1] + tmp)); } else { md -= ((pre[i + 1] + tmp) + 1); ans += (1 + (pre[i + 1] + tmp)); } } } } else { int ans1 = 0, ans2 = 0; md = 0, ans = 0; pre[0] = -1; for (int i = 0; i < n; i++) { int tmp = md; if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) { if ((pre[i] + tmp) < 0) { md += (1 - (pre[i + 1] + tmp)); ans1 += (1 - (pre[i + 1] + tmp)); } else { md -= ((pre[i + 1] + tmp) + 1); ans1 += (1 + (pre[i + 1] + tmp)); } } } md = 0, ans = 0; pre[0] = 1; for (int i = 0; i < n; i++) { int tmp = md; if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) { if ((pre[i] + tmp) < 0) { md += (1 - (pre[i + 1] + tmp)); ans2 += (1 - (pre[i + 1] + tmp)); } else { md -= ((pre[i + 1] + tmp) + 1); ans2 += (1 + (pre[i + 1] + tmp)); } } } ans = min(ans1, ans2); } printf("%d\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def resolve(): N = int(input()) A = list(map(int, input().split())) def solve(positive, A): total = A[0] ope = 0 for i in range(1, len(A)): # print("####") # print(total) # print(ope) if positive: # 次は負の数=正の数になるなら補正 if total + A[i] >= 0: ope += total + A[i] + 1 total = total + A[i] - (total + A[i]) - 1 else: total += A[i] else: # 次は正の数=負の数になるなら補正 if total + A[i] <= 0: ope += abs(total + A[i]) + 1 total = total + A[i] + abs(total + A[i]) + 1 else: total += A[i] positive = (not positive) return ope print(min(solve(True, A), solve(False, A))) if '__main__' == __name__: resolve()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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
#![allow(unused_mut)] #![allow(non_snake_case)] #![allow(unused_imports)] use std::collections::HashSet; use std::collections::HashMap; use std::collections::BTreeSet; use std::collections::VecDeque; use std::cmp::{max, min}; use std::io::prelude::*; fn input<T>() -> T where T: std::str::FromStr { let stdin = std::io::stdin(); let token: String = stdin .lock() .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().unwrap() } fn main() { let n: usize = input(); let mut a_s: Vec<i32> = (0..n).map(|_| input()).collect(); let mut p0 = 0; let mut p1 = 0; for i in 0..n { if i % 2 == 0 { p0 += a_s[i]; } else { p1 += a_s[i]; } } let mut cnt = 0; if p0 < p1 { for i in 0..n { if a_s[i] == 0 { cnt += 1; } if i % 2 == 0 { if a_s[i] > 0 { cnt += a_s[i] + 1 } } else { if a_s[i] < 0 { cnt += a_s[i] + 1 } } } } else { for i in 0..n { if a_s[i] == 0 { cnt += 1; } if i % 2 == 1 { if a_s[i] > 0 { cnt += a_s[i] + 1 } } else { if a_s[i] < 0 { cnt += a_s[i] + 1 } } } } println!("{}", cnt); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] a = new long[N]; long[] aNegative = new long[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextLong(); aNegative[i] -= a[i]; } long cnt = Math.min(cntCal(a), cntCal(aNegative)); System.out.println(cnt); } public static long cntCal(long[] a) { long sum = 0; int cnt = 0; for (int i = 0; i < a.length; i++) { long tmpSum = sum + a[i]; if (sum > 0 && tmpSum >= 0) { // sum > a[i]にしたい long n = -sum - 1; cnt += a[i] - n; a[i] = n; } else if (sum <= 0 && tmpSum <= 0) { long n = -sum + 1; cnt += n - a[i]; a[i] = n; } sum = sum + a[i]; } return cnt; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np import copy N=int(input()) l=list(map(int, input().split())) #リスト入力 cp = copy.copy(l) if l[0]!=0: for k in range(1,N): if sum(l[:k])*sum(l[:k+1])>0: l[k]=-np.sign(sum(l[:k]))-sum(l[:k]) if sum(l)==0: print(1+sum([abs(l[n]-cp[n]) for n in range(N)])) else: print(sum([abs(l[n]-cp[n]) for n in range(N)])) exit() else: #1 sei_l=copy.copy(l) sei_l[0]=1 for k in range(1,N): if sum(sei_l[:k])*sum(sei_l[:k+1])>0: sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k]) if sum(sei_l)==0: c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)]) else: c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)]) #-1 fu_l=copy.copy(l) sei_l[0]=-1 for k in range(1,N): if sum(fu_l[:k])*sum(fu_l[:k+1])>0: fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k]) if sum(fu_l)==0: c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)]) else: c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)]) print(max(c1,c2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int64_t> data(n + 1); for (int i = 1; i <= n; i++) { cin >> data.at(i); } int answer = 10000000; int count = 0; int64_t sum_a = 0; for (int i = 1; i <= n; i++) { sum_a += data.at(i); if (i % 2 != 0 && sum_a <= 0) { while (sum_a != 1) { sum_a++; count++; } } if (i % 2 == 0 && sum_a >= 0) { while (sum_a != -1) { sum_a--; count++; } } } answer = min(answer, count); count = 0; sum_a = 0; for (int i = 1; i <= n; i++) { sum_a += data.at(i); if (i % 2 != 0 && sum_a >= 0) { while (sum_a != -1) { sum_a--; count++; } } if (i % 2 == 0 && sum_a <= 0) { while (sum_a != 1) { sum_a++; count++; } } } answer = min(answer, count); cout << answer << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; ll sum[n]; for (int i1 = 0; i1 < n; ++i1) { ll a; cin >> a; sum[i1] = a + (i1 == 0 ? 0 : sum[i1 - 1]); } ll f = sum[0]; ll s = sum[1]; ll ope = 0; if (f == 0) { ope++; int d = s < 0 ? 1 : -1; for (int i = 0; i < n; ++i) { sum[i] += d; } } for (int i = 1; i < n - 1; ++i) { ll f = sum[i]; ll s = sum[i + 1]; if (f * s < 0) { continue; } ll d = abs(s) + 1; ope += d; if (0 < f && 0 < s) { d *= -1; } for (int j = i + 1; j < n; ++j) { sum[j] += d; } } ll p = sum[n - 2]; ll t = sum[n - 1]; if (p * t > 0) { ll d = abs(t) + 1; ope += d; if (0 < p && 0 < t) { d *= -1; } sum[n - 1] += d; } cout << ope << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e18; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int ary[n]; for (int i = (int)(0); i < (int)(n); ++i) cin >> ary[i]; long long ans = 0; long long tmp = 0; int plus = ary[0] / abs(ary[0]); for (int i = (int)(0); i < (int)(n); ++i) { tmp += ary[i]; if (tmp == 0 || plus != tmp / abs(tmp)) { ans += abs(tmp - plus); tmp = plus; } plus = -plus; } 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; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; double EPS = 1e-8; const double PI = acos(-1); int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; int n; long long rp(long long *a, long long sum, long long cnt) { for (int j = 1; j < n; j++) { if (sum > 0 && sum + a[j] < 0) { sum += a[j]; continue; } if (sum < 0 && sum + a[j] > 0) { sum += a[j]; continue; } if (sum < 0 && sum + a[j] <= 0) { long long dt = 1 - (sum + a[j]); cnt += dt; a[j] += dt; sum += a[j]; continue; } if (sum > 0 && sum + a[j] >= 0) { long long dt = (sum + a[j]) - (-1); cnt += dt; a[j] -= dt; sum += a[j]; continue; } } return cnt; } int main() { cin >> n; long long a[int(1e5) + 5]; long long b[int(1e5) + 5]; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } long long cnt = 0; long long sum = 0; long long result; if (a[0] > 0) { cnt += a[0] + 1; sum = -1; long long ans1 = rp(a, sum, cnt); sum = b[0]; cnt = 0; long long ans2 = rp(b, sum, cnt); result = min(ans1, ans2); } else { sum = a[0]; cnt = 0; result = rp(a, sum, cnt); } cout << result << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count3 = 0 count4 = 0 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count3 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count3 += abs(currentSum) + 1 currentSum = -1 elif A[i] <= 0 and restSum == 0: count3 += abs(currentSum) + 1 currentSum = 1 print(count3)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 = tuple(map(int, input().split(' '))) cs = A[0] ans1 = 0 for na in A[1:]: if cs >= 0: cs += na if cs < 0: continue ans1 += cs + 1 cs = -1 else: cs += na if cs > 0: continue ans1 += -cs + 1 cs = 1 if A[0] > 0: cs = -1 else: cs = 1 ans2 = A[0] + 1 for na in A[1:]: if cs >= 0: cs += na if cs < 0: continue ans2 += cs + 1 cs = -1 else: cs += na if cs > 0: continue ans2 += -cs + 1 cs = 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; int change_num(int p[], int N) { int res = 0; int sum = p[0]; for (int i = 1; i < N; i++) { if (sum * (sum + p[i]) < 0) { sum += p[i]; continue; } if (sum > 0 && sum + p[i] >= 0) { sum += p[i]; while (sum >= 0) { res++; sum--; } continue; } if (sum < 0 && sum + p[i] <= 0) { sum += p[i]; while (sum <= 0) { res++; sum++; } continue; } } return res; } int main() { int N; cin >> N; int a[N]; for (int i = 0; i < N; i++) cin >> a[i]; int ans = 0; int sum = a[0]; if (a[0] == 0) { int plus_ans; a[0] = 1; plus_ans = change_num(a, N) + 1; int minus_ans = 1; a[0] = -1; minus_ans = change_num(a, N) + 1; if (plus_ans < minus_ans) { ans = plus_ans; } else { ans = minus_ans; } } else { ans = change_num(a, N); } 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; static const long long INF = 1000000000000000000; int main() { int n; cin >> n; long long A[n]; long long sum[n]; long long ans = 0; for (int i = 0; i < n; i++) { cin >> A[i]; sum[i] = 0; } long long minans = INF; long long temp = 0; int p[2] = {1, -1}; if (A[0] == 0) { for (int k = 0; k < 2; k++) { ans = 0; ans++; A[0] = p[k]; sum[0] = A[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); } } ans = 0; sum[0] = A[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); cout << minans << 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> int n, a[100000]; int main() { scanf("%d", &n); for (long long i = 0; i < n; i++) scanf("%d", &a[i]); int ans = 0, sum = a[0]; for (long long i = 1; i <= n - 1; i++) { int tmpsum = sum + a[i]; if (tmpsum == 0 || (tmpsum == 0 ? 0 : (tmpsum < 0 ? -1 : 1)) == (sum == 0 ? 0 : (sum < 0 ? -1 : 1))) { int tmp = -(sum == 0 ? 0 : (sum < 0 ? -1 : 1)) - tmpsum; a[i] += tmp; ans += abs(tmp); } sum += a[i]; } printf("%d\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1LL << 60; int main() { ll n; cin >> n; ll a[n]; for (int i = 0; i < (int)(n); i++) { cin >> a[i]; } ll sum = a[0]; ll ans = 0; for (int i = 1; i < n; i++) { ll tmp_sum = sum + a[i]; if (sum > 0) { while (tmp_sum >= 0) { --tmp_sum; ++ans; } } else if (sum < 0) { while (tmp_sum <= 0) { ++tmp_sum; ++ans; } } sum = tmp_sum; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { partial class Program { static long mod = 1000000007; static void Main() { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); Solve(); Console.Out.Flush(); Console.ReadKey(); } //ここから static void Solve() { int N = GetInt(); var A = GetLongArray(); var sum = Enumerable.Repeat(0, N).Select(s=>Enumerable.Repeat(0L,2).ToArray()).ToArray(); var cnt = Enumerable.Repeat(0L, 2).ToArray(); sum[0][0] = A[0] > 0 ? A[0] : 1; cnt[0] = A[0] > 0 ? 0 : 1 - A[0]; sum[0][1] = A[0] < 0 ? A[0] : -1; cnt[1] = A[1] < 0 ? 0 : 1 + A[0]; for(int i = 1; i < N; i++) { for (int j = 0; j < 2; j++) { sum[i][j] = sum[i - 1][j] + A[i]; if (sum[i - 1][j] * sum[i][j] >= 0) { if (sum[i - 1][j] > 0) { cnt[j] += sum[i][j] + 1; sum[i][j] = -1; } else { cnt[j] += 1 - sum[i][j]; sum[i][j] = 1; } } } } var ans = cnt.Min(); Console.WriteLine(ans); } } //拡張メソッド public static class Ex { public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); } public static string yesno(this bool b) { return b ? "yes" : "no"; } public static string YesNo(this bool b) { return b ? "Yes" : "No"; } public static string YESNO(this bool b) { return b ? "YES" : "NO"; } } partial class Program { static public string GetStr() { return Console.ReadLine().Trim(); } static public char GetChar() { return Console.ReadLine().Trim()[0]; } static public int GetInt() { return int.Parse(Console.ReadLine().Trim()); } static public long GetLong() { return long.Parse(Console.ReadLine().Trim()); } static public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); } static public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); } static public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); } static public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); } static public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); } static public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); } static public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H).Select(s => Enumerable.Repeat(value, W).ToArray()).ToArray(); } static public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetIntArray().ToArray()).ToArray(); } static public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetLongArray().ToArray()).ToArray(); } static public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetStr().ToCharArray()).ToArray(); } static public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); } static public void WriteObjects<T>(IReadOnlyCollection<T> values) { var array = values.ToArray(); var num = array.Length; if (num == 0) return; Console.Write(array[0]); for (int i = 1; i < num; i++) { Console.Write(" "+array[i]);} Console.WriteLine(); } static bool eq<T, U>() => typeof(T).Equals(typeof(U)); static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T)); static T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s)) : eq<T, long>() ? ct<T, long>(long.Parse(s)) : eq<T, double>() ? ct<T, double>(double.Parse(s)) : eq<T, char>() ? ct<T, char>(s[0]) : ct<T, string>(s); static void Multi<T>(out T a) => a = cv<T>(GetStr()); static void Multi<T, U>(out T a, out U b) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); } static void Multi<T, U, V>(out T a, out U b, out V c) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); } static void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); } static void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); } static void Multi<T, U, V, W, X,Y>(out T a, out U b, out V c, out W d, out X e,out Y f) { var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]);f = cv<Y>(ar[5]); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; bool code(long long int n) { if (n < 0) return 1; else if (n > 0) return 0; } int main() { int n; long long int sum = 0; long long int ans = 0; long long int ans2 = 0; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } if (a.at(0) != 0) { sum = a.at(0); for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += llabs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } cout << ans << endl; return 0; } else if (a.at(0) == 0) { sum = -1; ans = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += llabs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } long long int sum2 = 1; ans2 = 1; for (int i = 1; i < n; i++) { if (sum2 + a.at(i) == 0) { ans2++; if (sum2 > 0) sum2 = -1; else if (sum2 < 0) sum2 = 1; } else if (code(sum2 + a.at(i)) == code(sum2)) { ans2 += llabs(sum2 + a.at(i)) + 1; if (sum2 > 0) sum2 = -1; else if (sum2 < 0) sum2 = 1; } else { sum2 = a.at(i) + sum2; } } if (ans > ans2) cout << ans2 << endl; else { 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 n; int flag[100005], k[100005]; long long a[100005], sum[100005], ans, b[100005], tot[100005], ant; int main() { int m = 0; scanf("%d", &n); scanf("%lld", &a[1]); b[1] = a[1]; sum[1] = a[1]; tot[1] = sum[1]; if (sum[1] > 0) flag[1] = 1; if (sum[1] < 0) flag[1] = 0; if (sum[1] == 0) m = 1; if (m == 0) { for (int i = 2; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = a[i] + sum[i - 1]; if (sum[i] > 0) flag[i] = 1; if (sum[i] < 0) flag[i] = 0; if (flag[i - 1] == 1) { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; flag[i] = 0; } } else { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; flag[i] = 1; } } } printf("%lld\n", ans); } else { for (int i = 2; i <= n; i++) { scanf("%lld", &a[i]); flag[1] = 0; b[i] = a[i]; sum[i] = a[i] + sum[i - 1]; if (sum[i] > 0) flag[i] = 1; if (sum[i] < 0) flag[i] = 0; if (flag[i - 1]) { if (sum[i] >= 0) ans += sum[i] + 1; sum[i] = -1; flag[i] = 0; } else { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; flag[i] = 1; } } } k[1] = 1; for (int i = 2; i <= n; i++) { tot[i] = b[i] + tot[i - 1]; if (tot[i] > 0) k[i] = 1; if (tot[i] < 0) k[i] = 0; if (k[i - 1]) { if (tot[i] >= 0) ant += tot[i] + 1; tot[i] = -1; k[i] = 0; } else { if (tot[i] <= 0) { ant += 1 - tot[i]; tot[i] = 1; k[i] = 1; } } } printf("%lld\n", min(ant, 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; int sum = 0; int total = 0; cin >> n; vector<int> vect; bool positive = false; while (n--) { int a; cin >> a; vect.push_back(a); } if (vect[0] + vect[1] == 0) { if (vect.size() > 2) { int found = 0; for (int i = 2; i < vect.size(); i++) { if (vect[i] > 0 || vect[i] < 0) { if (vect[i] > 0) { if (i % 2 == 1) { vect[1]++; total++; positive = true; } else { vect[1]--; total++; positive = false; } } else { if (i % 2 == 1) { vect[1]--; total++; positive = false; } else { vect[1]++; total++; positive = true; } } found = 1; break; } } if (found == 0) { vect[1]++; total++; positive = true; } } else { cout << "1" << endl; } } sum = vect[0] + vect[1]; if (sum < 0) positive = false; else positive = true; for (int i = 2; i < vect.size(); i++) { sum += vect[i]; if (positive) { if (sum >= 0) { total += abs(sum) + 1; sum = -1; } positive = false; } else if (!positive) { if (sum <= 0) { total += abs(sum) + 1; sum = 1; } positive = true; } } cout << total << 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; using pint = pair<int, int>; using pll = pair<ll, ll>; template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); }; const int MOD = 1000000007; ll N, a[100010]; signed main() { cin >> N; for (int(i) = 0; (i) < (N); ++(i)) cin >> a[i]; if (a[0] < 0) { for (int(i) = 0; (i) < (N); ++(i)) a[i] *= -1; } ll sum = 0, ans = 0; for (int(i) = 0; (i) < (N); ++(i)) { if (i % 2 == 0) { if (sum + a[i] > 0) { sum += a[i]; } else { ans += 1 - (sum + a[i]); sum = 1; } } else { if (sum + a[i] < 0) { sum += a[i]; } else { ans += 1 + (sum + a[i]); sum = -1; } } } cout << (ans) << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC059Sequence { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); long[] a = new long[n]; string[] vals = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) a[i] = long.Parse(vals[i]); if(a[0] != 0) { long num = calc(a); Console.WriteLine(num); } else { a[0] = 1; long num1 = calc(a) + 1; a[0] = -1; long num2 = calc(a) + 1; Console.WriteLine(Math.Min(num1, num2)); } } static long calc(long[] a) { long num = 0; int n = a.Length; long[] cum = new long[n + 1]; for (int i = 1; i < n; i++) { cum[i] = cum[i - 1] + a[i - 1]; long t; if (cum[i] > 0) { t = cum[i] * -1 - 1; } else { t = cum[i] * -1 + 1; } //Console.WriteLine("target: {0}", t); long u; if (t > 0) { if (a[i] < t) { u = t - a[i]; //Console.WriteLine("u={0}", u); a[i] += u; num += u; } } else { if (a[i] > t) { u = a[i] - t; //Console.WriteLine("u=-{0}", u); a[i] -= u; num += u; } } } return num; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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 = -1; x += s; } else if (t % 2 == 0 && sum <= 0) { int s = 1 - sum; sum = 1; 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 = 1; x += s; } else if (t % 2 == 1 && sum <= 0) { int s = 1 - sum; sum = 1; 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() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; long long sum = 0; long long ans = 0; for (int i = 0; i < N; ++i) { long long t; cin >> t; if (i == 0) sum = t; else { if (sum < 0 && sum + t <= 0) { ans += 1 - sum - t; sum = 1; } else if (sum > 0 && sum + t >= 0) { ans += abs(-1 - sum - t); sum = -1; } else { sum += t; } } } cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long res1 = 0, res2 = 0; long long sum1[n], sum2[n]; sum1[0] = (a[0] <= 0) ? (-a[0] + 1) : a[0]; sum2[0] = (a[0] >= 0) ? (-a[0] - 1) : a[0]; for (int i = 1; i < n; i++) { sum1[i] = sum1[i - 1] + a[i]; long long sum = sum1[i]; if (sum1[i] <= 0 && sum1[i - 1] < 0) { sum1[i] += -sum + 1; res1 += abs(-sum + 1); } else if (sum1[i] >= 0 && sum1[i - 1] > 0) { sum1[i] += -sum - 1; res1 += abs(-sum - 1); } sum2[i] = sum2[i - 1] + a[i]; sum = sum2[i]; if (sum2[i] <= 0 && sum2[i - 1] < 0) { sum2[i] += -sum + 1; res2 += abs(-sum + 1); } else if (sum2[i] >= 0 && sum2[i - 1] > 0) { sum2[i] += -sum - 1; res2 += abs(-sum - 1); } } cout << min(res1, res2) << 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; long long count = 0; long a[100000]; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum > 0 && sum + a[i + 1] > 0) { temp = a[i + 1]; a[i + 1] = sum * (-1) - 1; count += abs(a[i + 1] - temp); } else if (sum < 0 && sum + a[i + 1] < 0) { temp = a[i + 1]; a[i + 1] = 1 + sum * (-1); count += abs(a[i + 1] - temp); } if (sum + a[i + 1] == 0) { if (sum > 0) { a[i + 1] -= 1; } else { a[i + 1] += 1; } count += 1; } } for (int i = 0; i < n; i++) { cout << a[i] << endl; } cout << count << endl; } string toUpper(string str) { transform(str.begin(), str.end(), str.begin(), ::toupper); return str; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); long ans = 0l; long sum = 0l; boolean fg= false; for(int i=0;i<n;i++){ long tw = Long.parseLong(sc.next()); sum += tw; if(i>0){ if(fg && sum>=0l){ ans += sum+1; sum = -1; }else if(!fg && sum<0){ ans += Math.abs(sum)+1; sum = 1; } fg = !fg; }else{ fg = sum>=0l; } } System.out.println(ans+(sum==0?1:0)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) al = list(map(int, input().split())) if al[0] == 0: ans1 = 0 ans1 += 1 curr_sum = 1 for a in al[1:]: next_sum = curr_sum + a if next_sum >= 0 and curr_sum > 0: ans1 += next_sum+1 next_sum = -1 elif next_sum <= 0 and curr_sum < 0: ans1 += (1-next_sum) next_sum = 1 ans2 = 0 ans2 += 1 curr_sum = 1 for a in al[1:]: next_sum = curr_sum + a if next_sum >= 0 and curr_sum > 0: ans2 += next_sum+1 next_sum = -1 elif next_sum <= 0 and curr_sum < 0: ans2 += (1-next_sum) next_sum = 1 print(min(ans1,ans2)) else: ans3 = 0 curr_sum = al[0] for a in al[1:]: next_sum = curr_sum + a if next_sum >= 0 and curr_sum > 0: ans3 += next_sum+1 next_sum = -1 elif next_sum <= 0 and curr_sum < 0: ans3 += (1-next_sum) next_sum = 1 curr_sum = next_sum print(ans3)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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<long long int> a; long long int s; for (int i = 0; i < (n); i++) { cin >> s; a.push_back(s); } long long int oddcount = 0, evencount = 0; long long int oddsum = 0, evensum = 0; bool odd = true, even = false; for (int i = 0; i < (n); i++) { oddsum += a[i]; evensum += a[i]; if (odd && oddsum <= 0) { oddcount += 1 - oddsum; oddsum = 1; } if (even && oddsum >= 0) { oddcount += 1 + oddsum; oddsum = -1; } if (even && evensum <= 0) { evencount += 1 - evensum; evensum = 1; } if (odd && evensum >= 0) { evencount += 1 + evensum; evensum = -1; } odd = !odd; even = !even; } cout << fmin(oddcount, evencount) << 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; vector<ll> a; vector<ll> ca; int main() { int n; cin >> n; a.resize(n); ca.resize(n); cin >> a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; ca[i] = a[i] + ca[0]; } ll sum1 = 0, n1 = 0; ll sum2 = 0, n2 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; sum2 += a[i]; if (i % 2 == 0 && sum1 <= 0) { n1 += 1 + abs(sum1); sum1 += 1 + abs(sum1); } if (i % 2 == 1 && sum1 >= 0) { n1 += 1 + abs(sum1); sum1 -= 1 + abs(sum1); } if (i % 2 == 0 && sum2 >= 0) { n2 += 1 + abs(sum2); sum2 -= 1 + abs(sum1); } if (i % 2 == 1 && sum2 <= 0) { n2 += 1 + abs(sum2); sum2 += 1 + abs(sum2); } } cout << min(n1, n2) << 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 count = 0; vector<int> a(100000); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> sum(100000); sum[0] = a[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i] * sum[i - 1] >= 0) { if (sum[i - 1] < 0) { while (sum[i] * sum[i - 1] >= 0) { sum[i]++; count++; } } else { while (sum[i] * sum[i - 1] >= 0) { sum[i]--; count++; } } } } cout << 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 <iostream> #include <cmath> #include <map> #include <queue> #include <string> #include <string> #include <algorithm> using namespace std; int ch_sign(int n){ if(n == 0)return 0; return (n>0)-(n<0); } int main(){ int n; cin >> n; int a[n]; for(int i = 0; i<n; ++i) cin >> a[i]; int sign1 = (a[0] > 0) - (a[0] < 0), sign2 = -1*sign1; int s = a[0]; int ans1 = 0, ans2 = 0; for(int i = 1; i<n; ++i){ //cout << a[i] << ',' << s << ',' << sign << endl; //while(abs(a[i]) <= abs(s)){ ++ans; a[i] -= sign;} //int dis = abs(s) - abs(a[i]) + 1; //cout << dis << endl << endl; //if(dis > 0){ans += dis; a[i] -= sign*dis;} //s += a[i]; sign *= -1; s += a[i]; sign1 *= -1; sign2 *= -1; if(ch_sign(s) != sign1){ ans1 += abs(s-sign1); s = sign1;} if(ch_sign(s) != sign2){ ans2 += abs(s-sign2); s = sign2;} } cout << ans1>ans2 ? ans1 : ans2 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <vector> #include <string> #include <cstring> #include <math.h> #include <limits.h> #include <map> #include <algorithm> #include <functional> using namespace std; int main() { int n; vector<long long> A; long long ans1 = 0; long long ans2 = 0; long long sum = 0; cin >> n; for ( int i = 0; i < n; i++ ) { long long a; cin >> a; A.push_back(a); } is_plus = true; for ( int i = 0; i < n; i++ ) { if ( i ) { is_plus = sum > 0; } sum += A[i]; if ( sum == 0 ) { ans1++; sum = is_plus ? -1 : 1; } else if ( is_plus == (sum > 0) ) { ans1 += abs(sum)+1; sum = is_plus ? -1 : 1; } } is_plus = false; for ( int i = 0; i < n; i++ ) { if ( i ) { is_plus = sum > 0; } sum += A[i]; if ( sum == 0 ) { ans2++; sum = is_plus ? -1 : 1; } else if ( is_plus == (sum > 0) ) { ans2 += abs(sum)+1; sum = is_plus ? -1 : 1; } } cout << min( ans1, ans2 ) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; int n; int main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; int a[n]; for (int i = (0); i < (n); ++i) cin >> a[i]; bool sign; if (a[0] >= 0) sign = true; else sign = false; int sum; int count = 0; int reminder; sum = a[0]; for (int i = (1); i < (n); ++i) { if (sign) { sum += a[i]; if (sum >= 0) sign = true; else sign = false; if (sign) { reminder = abs(-1 - sum); count += reminder; sum = -1; sign = false; } } else { sum += a[i]; if (sum >= 0) sign = true; else sign = false; if (!sign) { reminder = abs(1 - sum); count += reminder; sum = 1; sign = true; } } } if (sum == 0) 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; const int MOD = (int)1e9 + 7; const int INF = 100100100; const double PI = 3.14159265358979323846; int main() { int n, a[100001] = {0}; int res = 0; cin >> n; for (long long i = 0; i < (n); ++i) cin >> a[i]; int minPlus = 0, minMinus = 0; long long sumPlus = 0, sumMinus = 0; for (long long i = 0; i < (n); ++i) { sumPlus += a[i]; if (i % 2 == 0 && sumPlus <= 0) { minPlus += 1 - sumPlus; sumPlus = 1; } else if (i % 2 == 1 && sumPlus >= 0) { minPlus += sumPlus + 1; sumPlus = -1; } } for (long long i = 0; i < (n); ++i) { sumMinus += a[i]; if (i % 2 == 0 && sumMinus >= 0) { minMinus += sumMinus + 1; sumMinus = -1; } else if (i % 2 == 1 && sumMinus <= 0) { minMinus += 1 - sumMinus; sumMinus = 1; } } res = min(minPlus, minMinus); 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; long long max(long long a, long long b) { return (a > b) ? a : b; } long long min(long long a, long long b) { return (a < b) ? a : b; } long long abss(long long a) { return (a < 0) ? -a : a; } long long gcd(long long a, long long b) { if (b > a) { long long tmp = b; b = a; a = tmp; } if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { long long gcdi = gcd(a, b); return a / gcdi * (b); } int a[100001]; int sum[100001]; int main() { long long N; scanf("%lld", &N); for (int i = 0; i < N; i++) { scanf("%d", a + i); } long long cnt = 0; char sign; if (a[0] == 0) { a[0] = 1; cnt++; } if (a[0] > 0) sign = 1; else sign = -1; sign = -sign; long long sum = a[0]; for (int i = 1; i < N; i++) { sum += a[i]; if (sign * sum > 0) { } else { cnt += abss(sum - sign); sum = sign; } sign = -sign; } printf("%lld\n", 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; const long long MOD = 1e9 + 7; const long long MOD2 = 998244353; const long long MOD3 = 1812447359; const long long INF = 1ll << 62; const double PI = 2 * asin(1); void yes() { printf("yes\n"); } void no() { printf("no\n"); } void Yes() { printf("Yes\n"); } void No() { printf("No\n"); } void YES() { printf("YES\n"); } void NO() { printf("NO\n"); } int N; long long A[int(1e5 + 5)], sum; int main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; long long ans = 0, sum = A[0]; for (int i = 1; i < N; i++) { if (sum > 0) { if (sum + A[i] < 0) { sum += A[i]; continue; } ans += sum + A[i] + 1; sum = -1; } else { if (sum + A[i] > 0) { sum += A[i]; continue; } ans += 1 - sum - A[i]; 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(9); int N; long long a[100000]; cin >> N; for (int i = 0; i < N; i++) cin >> a[i]; long long ans = 0; long long sum = a[0]; for (int i = 1; i < N; i++) { if (sum > 0) { sum += a[i]; if (sum >= 0) { ans += sum + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { ans += -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
java
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ Sequence solver = new Sequence(); solver.readInput(); solver.solve(); solver.writeOutput(); } static class Sequence { private int n; private long a[]; private long output; private Scanner scanner; public Sequence() { this.scanner = new Scanner(System.in); } public void readInput() { n = Integer.parseInt(scanner.next()); a = new long[n]; for(int i=0; i<n; i++) { a[i] = Integer.parseInt(scanner.next()); } } private int count(int sign) { int count=0; long sum=0; for(int i=0; i<n; i++) { sum += a[i]; if(i%2==sign) { // a[i]までの合計を正にするとき if(sum<=0) { count += 1-sum; sum = 1; } } else { // a[i]までの合計を負にするとき if(0<=sum) { count += 1+sum; sum = -1; } } } return count; } public void solve() { int plus = count(1); int minus = count(0); output = Math.min(plus,minus); } public void writeOutput() { System.out.println(output); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) S = A[0] cnt = 0 if S!=0: for i in range(1,N): K = S+A[i] if S>0 and K>=0: cnt += 1+K S = -1 elif S<0 and K<=0: cnt += 1-K S = 1 else: S=K else: S,cnt = 1,1 for i in range(1,N): K = S+A[i] if S>0 and K>=0: cnt += 1+K S = -1 elif S<0 and K<=0: cnt += 1-K S = 1 else: S=K S,cnt1 = -1,1 for i in range(1,N): K = S+A[i] if S>0 and K>=0: cnt1 += 1+K S = -1 elif S<0 and K<=0: cnt1 += 1-K S = 1 else: S=K cnt = min(cnt,cnt1) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, temp; long long count = 0; long a[100000]; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum > 0 && sum + a[i + 1] > 0) { temp = a[i + 1]; a[i + 1] = sum * (-1) - 1; count += abs(a[i + 1] - temp); } else if (sum < 0 && sum + a[i + 1] < 0) { temp = a[i + 1]; a[i + 1] = 1 + sum * (-1); count += abs(a[i + 1] - temp); } if (sum + a[i + 1] == 0) { if (sum > 0) { a[i + 1] -= 1; } else { a[i + 1] += 1; } count += 1; } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { uint64_t N; cin >> N; long long total; cin >> total; long long sign = total / abs(total); unsigned long long count = 0; for (uint64_t i = 1; i < N; i++) { sign *= -1; long long val; cin >> val; total += val; if ((total == 0) || (sign * total < 0)) { count += abs(sign - total); total = sign; } } 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> const int MOD = 1000000007; using namespace std; int main() { int n, a[100000], sum, ans = 0, m = 1; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { a[0]++; ans++; } if (a[0] < 0) m = -1; sum = a[0]; for (int i = 1; i < n; i++) { m *= -1; sum += a[i]; if (m > 0 && sum <= 0) { ans += 1 - sum; sum = 1; } if (m < 0 && 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> A(n), B(n); for (int i = 0; i < n; ++i) { int a; cin >> a; A[i] = a; B[i] = a; } int ans1 = 0; long long sum = 0; for (int i = 0; i < n; ++i) { sum += A[i]; if (i % 2 == 0) { if (sum <= 0) { ans1 += abs(1 - sum); sum = 1; } } else { if (sum >= 0) { ans1 += abs(-1 - sum); sum = -1; } } } int ans2 = 0; sum = 0; for (int i = 0; i < n; ++i) { sum += A[i]; if (i % 2 == 1) { if (sum <= 0) { ans2 += abs(1 - sum); sum = 1; } } else { if (sum >= 0) { ans2 += abs(-1 - sum); sum = -1; } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n = I() a = LI() ans = 0 s = a[0] if s <= 0: ans += 1-a[0] s = 1 f = 1 for i in a[1:]: s += i if f and (s >= 0): ans += s+1 s = -1 if not f and (s <= 0): ans += 1-s s = 1 f ^= 1 m = ans ans = 0 s = a[0] if s >= 0: ans += a[0]+1 s = 1 f = 0 for i in a[1:]: s += i if f and (s >= 0): ans += s+1 s = -1 if not f and (s <= 0): ans += 1-s s = 1 f ^= 1 print(min(m,ans)) return #Solve if __name__ == "__main__": solve()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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.*; object MainKt { @JvmStatic fun main(args:Array<String>) { val N=readLine()!!.toInt() var A=readLine()!!.split(" ").map{it.toInt()} var sum=A[0] var plmi:Boolean var count=0 for(i in (1..(N-1))) { assert(sum!=0) plmi =sum>0 sum= if(sum+A[i]>0 == plmi) { val min=if(plmi) {-1} else{1} count+=Math.abs(min-(sum+A[i]) ) min } else {sum+A[i] } // println("${count} ${sum}") } // println("") // if(valid) // { // println("0") // return // } // sum=0 // for(i in (1..(N-1))) // { // } 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; template <typename T> void print_array(T* arr, int num) { for (int(i) = (0); (i) < (num); (i)++) cout << arr[i] << ' '; cout << endl; } template <typename T> void print_vector(vector<T> vec) { for (int(i) = (0); (i) < (vec.size()); (i)++) cout << vec[i] << ' '; cout << endl; } int n, arr[100010]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int(i) = (0); (i) < (n); (i)++) cin >> arr[i]; int sum = 0, ans = 0; for (int(i) = (0); (i) < (n); (i)++) { sum += arr[i]; if (i % 2 == 0) { if (sum < 0) { ans += -(sum) + 1; sum = 1; } else if (sum == 0) { ans++; sum = 1; } } else { if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum == 0) { ans++; sum = -1; } } } sum = 0; int ans2 = 0; for (int(i) = (0); (i) < (n); (i)++) { sum += arr[i]; if (i % 2) { if (sum < 0) { ans2 += -(sum) + 1; sum = 1; } else if (sum == 0) { ans2++; sum = 1; } } else { if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum == 0) { ans2++; sum = -1; } } } cout << min(ans, ans2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int min(int x, int y) { return x < y ? x : y; } int main(void) { int n, i, o = 0, e = 0; scanf("%d", &n); long long a, sum = 0, osum = 0, esum = 0; for (i = 0; i < n; i++) { scanf("%lld", &a); osum += a; esum += a; if (i % 2 == 0) { if (osum <= 0) { o += 1 - osum; osum = 1; } if (esum >= 0) { e += 1 + esum; esum = -1; } } else { if (osum >= 0) { o += 1 + osum; osum = -1; } if (esum <= 0) { e += 1 - esum; esum = 1; } } } o = min(o, e); printf("%d", o); 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<int> sum(N, 0); int now; cin >> now; sum[0] = now; for (int i = 1; i < N; i++) { cin >> now; sum[i] = sum[i - 1] + now; } int change = 0; int ansp = 0; int i = 0; while (i < N) { ansp += max(1 - (sum[i] + change), 0); change += max(1 - (sum[i] + change), 0); i++; if (i == N) { break; } ansp += max((sum[i] + change) + 1, 0); change -= max((sum[i] + change) + 1, 0); i++; } change = 0; int ansm = 0; i = 0; while (i < N) { ansm += max((sum[i] + change) + 1, 0); change -= max((sum[i] + change) + 1, 0); i++; if (i == N) { break; } ansm += max(1 - (sum[i] + change), 0); change += max(1 - (sum[i] + change), 0); i++; } cout << min(ansp, ansm) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; int i; long a[n], su, cnt, cnt2; su = 0; cnt = 0; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { su += a[i]; if (a[0] >= 0) { if (i % 2 == 0) { if (su <= 0) { cnt += 1 - su; su = 1; } } else { if (su >= 0) { cnt += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt += su + 1; su = -1; } } else { if (su <= 0) { cnt += 1 - su; su = -1; } } } } su = 0; for (i = 0; i < n; i++) { su += a[i]; if (a[0] > 0) { if (i % 2 == 0) { if (su <= 0) { cnt += 1 - su; su = 1; } } else { if (su >= 0) { cnt2 += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt2 += su + 1; su = -1; } } else { if (su <= 0) { cnt2 += 1 - su; su = -1; } } } } cout << min(cnt, cnt2) << endl; return 0; }