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
UNKNOWN
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace C { public class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } public static void Solve() { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); var answer = (long)1e18; var t = 1; for (var k = 0; k < 2; k++) { t ^= 1; var step = 0; var sum = 0; for (var i = 0; i < N; i++) { sum += A[i]; if (i % 2 == t) { if (sum <= 0) { step += 1 - sum; sum = 1; } } else { if (sum >= 0) { step += 1 + sum; sum = -1; } } } answer = Math.Min(answer, step); } Console.WriteLine(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, ans; cin >> n; vector<int> a(n); for (long long i = 0; i < (long long)(n); i++) { cin >> a[i]; } int cnt = 0; int b[n]; b[0] = a[0]; if (b[0] == 0 and a[1] > 0) { b[0] = -1; cnt += 1; } else if (b[0] == 0 and a[1] < 0) { b[0] = 1; cnt += 1; } int temp = b[0]; for (long long i = 1; i <= (long long)(n - 1); i++) { temp += a[i]; b[i] = temp; if (i != n and b[i] == 0 and signbit(b[i - 1]) == true) { b[i] += 1; cnt += 1; } else if (i != n and b[i] == 0 and signbit(b[i - 1]) == false) { b[i] += -1; cnt += 1; } if (i != n and b[i] != 0 and signbit(b[i]) == signbit(b[i - 1])) { cnt += abs(b[i]) + 1; if (b[i - 1] > 0) { b[i] = -1; } else { b[i - 1] = 1; } } temp = b[i]; } 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
python3
n = int(input()) a = input().split() a = [int(i) for i in a] sum = a[0] counter = 0 for i in range(1,n): if sum * (sum+a[i]) < 0: sum += a[i] continue else: while(sum * (sum+a[i]) >= 0): if sum > 0: a[i] -=1 counter += 1 else: a[i] +=1 counter += 1 sum += a[i] print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { static int ANS_POSI; static int ANS_NEGA; static int ANS; static int N; public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = Integer.parseInt(sc.nextLine()); long[] a = new long[N]; for (int i = 0; i < N; i++) { long elm = Long.parseLong(sc.next()); a[i] = elm; } solve(a); System.out.println(Math.min(ANS_POSI, ANS_NEGA)); } private static void solve(long[] a) { long posi = 0; long nega = 0; for (int i = 0; i < N; i++) { // 前回のループで変更があった場合は、1か-1になっている値に配列値を合算して合計値としている。 posi += a[i]; nega += a[i]; if (i % 2 == 0) { if (posi >= 0) { ANS_POSI += posi + 1; posi = -1; } if (nega <= 0) { ANS_NEGA -= nega - 1; nega = 1; } } else { if (posi <= 0) { ANS_POSI -= posi - 1; posi = 1; } if (nega >= 0) { ANS_NEGA += nega + 1; nega = -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; int main() { int n; cin >> n; int sum = 0, change = 0; vector<int> a(n, 0); for (int i = 0; i < n; i++) cin >> a[i]; sum = a[0]; if (sum <= 0) { change += -sum + 1; sum = 1; } for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum <= 0) { change += -sum + 1; sum = 1; } if (i % 2 == 1 && sum >= 0) { change += sum + 1; sum = -1; } } int ans = change; sum = a[0]; change = 0; if (sum >= 0) { change += sum + 1; sum = -1; } for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum >= 0) { change += sum + 1; sum = -1; } if (i % 2 == 1 && sum <= 0) { change += -sum + 1; sum = 1; } } ans = ans > change ? change : ans; cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); int ans = 0; int tmp = 0; for (int type = 0; type < 2; type++) { ans = 0; int wa = 0; for (int i = 0; i < n; i++) { wa += a[i]; if (i % 2 == type % 2) { if (wa >= 0) { ans += (wa + 1); wa = -1; } } else { if (wa <= 0) { ans -= (wa - 1); wa = 1; } } } if (type == 0) tmp = ans; } System.out.println(Math.min(ans, tmp)); } public static int[] arrayInt(Scanner sc, int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = sc.nextInt(); } return array; } public static long[] arrayLong(Scanner sc, int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = sc.nextLong(); } return array; } public static double[] arrayDouble(Scanner sc, int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = sc.nextDouble(); } return array; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 -*- ############# # Libraries # ############# import sys input = sys.stdin.readline import math #from math import gcd import bisect from collections import defaultdict from collections import deque from functools import lru_cache ############# # Constants # ############# MOD = 10**9+7 INF = float('inf') ############# # Functions # ############# ######INPUT###### def I(): return int(input().strip()) def S(): return input().strip() def IL(): return list(map(int,input().split())) def SL(): return list(map(str,input().split())) def ILs(n): return list(int(input()) for _ in range(n)) def SLs(n): return list(input().strip() for _ in range(n)) def ILL(n): return [list(map(int, input().split())) for _ in range(n)] def SLL(n): return [list(map(str, input().split())) for _ in range(n)] ######OUTPUT###### def P(arg): print(arg); return def Y(): print("Yes"); return def N(): print("No"); return def E(): exit() def PE(arg): print(arg); exit() def YE(): print("Yes"); exit() def NE(): print("No"); exit() #####Shorten##### def DD(arg): return defaultdict(arg) #####Inverse##### def inv(n): return pow(n, MOD-2, MOD) ######Combination###### kaijo_memo = [] def kaijo(n): if(len(kaijo_memo) > n): return kaijo_memo[n] if(len(kaijo_memo) == 0): kaijo_memo.append(1) while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD) return kaijo_memo[n] gyaku_kaijo_memo = [] def gyaku_kaijo(n): if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n] if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1) while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD) return gyaku_kaijo_memo[n] def nCr(n,r): if(n == r): return 1 if(n < r or r < 0): return 0 ret = 1 ret = ret * kaijo(n) % MOD ret = ret * gyaku_kaijo(r) % MOD ret = ret * gyaku_kaijo(n-r) % MOD return ret ######Factorization###### def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([n, 1]) return arr #####MakeDivisors###### def make_divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) return divisors #####MakePrimes###### def make_primes(N): max = int(math.sqrt(N)) seachList = [i for i in range(2,N+1)] primeNum = [] while seachList[0] <= max: primeNum.append(seachList[0]) tmp = seachList[0] seachList = [i for i in seachList if i % tmp != 0] primeNum.extend(seachList) return primeNum #####GCD##### def gcd(a, b): while b: a, b = b, a % b return a #####LCM##### def lcm(a, b): return a * b // gcd (a, b) #####BitCount##### def count_bit(n): count = 0 while n: n &= n -1 count += 1 return count #####ChangeBase##### def base_10_to_n(X, n): if X//n: return base_10_to_n(X//n, n)+[X%n] return [X%n] def base_n_to_10(X, n): return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X)))) #####IntLog##### def int_log(n, a): count = 0 while n>=a: n //= a count += 1 return count ############# # Main Code # ############# N = I() A = IL() s = 0 count1 = 0 for i in range(N): s += A[i] if i%2: if s>-1: count1 += s+1 s = -1 else: if s<1: count1 += 1-s s = 1 s = A[0] count2 = 0 for i in range(N): s += A[i] if i%2==0: if s>-1: count2 += s+1 s = -1 else: if s<1: count2 += 1-s s = 1 print(min(count1,count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { int n; cin >> n; long long cnt_a = 0; long long cnt_b = 0; long long sum_a = 0; long long sum_b = 0; for (int i = 0; i < n; ++i) { long long t; cin >> t; long long sign_t = (t > 0) ? 1 : -1; if (i == 0) { sum_a += t; sum_b += -1 * sign_t; cnt_b += abs(t) + 1; } else { long long tsum_a = sum_a + t; long long tsum_b = sum_b + t; long long sign_a = (sum_a > 0) ? 1 : -1; long long sign_b = (sum_b > 0) ? 1 : -1; if (tsum_a * sum_a >= 0) { cnt_a += abs(tsum_a) + 1; sum_a = -1 * sign_a; } else { sum_a = tsum_a; } if (tsum_b * sum_b >= 0) { cnt_b += abs(tsum_b) + 1; sum_b = -1 * sign_b; } else { sum_b = tsum_b; } } } cout << min(cnt_a, cnt_b) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, a[100001], b[100001]{}, ans1 = 0, ans2 = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] != 0) { b[0] += a[0]; } else { ans1++; int j = 1; while (j < n && a[j] == 0) j++; if (j != n) { b[0] = (a[j] / abs(a[j])); if (j % 2 == 1) b[0] *= -1; } else b[0] = 1; } for (int i = 1; i < n; i++) { b[i] += a[i] + b[i - 1]; if (b[i] * (b[i - 1] / abs(b[i - 1])) >= 0) { if (b[i - 1] > 0) { ans1 += abs(b[i]) + 1; b[i] = -1; } else { ans1 += abs(b[i]) + 1; b[i] = 1; } } } if (a[0] != 0) { b[0] += a[0]; ans2 += abs(a[0]) + 1; b[0] = -(a[0] / abs(a[0])); } else { ans2++; int j = 1; while (j < n && a[j] == 0) j++; if (j != n) { b[0] = (a[j] / abs(a[j])); if (j % 2 == 0) b[0] *= -1; } else b[0] = -1; } for (int i = 1; i < n; i++) { b[i] += a[i] + b[i - 1]; if (b[i] * (b[i - 1] / abs(b[i - 1])) >= 0) { if (b[i - 1] > 0) { ans1 += abs(b[i]) + 1; b[i] = -1; } else { ans1 += abs(b[i]) + 1; b[i] = 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; long long int MOD = 1000000007; const long long L_INF = 1LL << 60; const int INF = 2147483647; const double PI = acos(-1); 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; } template <class T> void debug(T v) { for (int i = 0; i < v.size(); ++i) cout << v[i] << " "; cout << endl; } const long long int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const long long int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; long long int n; vector<long long int> a, s; long long int solve(bool b) { long long int res = 0; if (a[0] == 0) { if (b) s[1] = a[0]; else s[1] = a[0]; } else if (a[0] > 0) { if (b) s[1] = a[0]; else { s[1] = -1; res += abs(a[0]) + 1; } } else { if (b) { s[1] = 1; res += abs(a[0]) + 1; } else s[1] = a[0]; } for (int i = 1; i < n; ++i) { if (s[i] < 0) { if (a[i] < 0) { res += abs(a[i]) + abs(s[i]) + 1; s[i + 1] = 1; } else { s[i + 1] = s[i] + a[i]; if (s[i + 1] <= 0) { res += abs(s[i + 1]) + 1; s[i + 1] = 1; } } } else { if (a[i] <= 0) { s[i + 1] = s[i] + a[i]; if (s[i + 1] >= 0) { res += s[i + 1] + 1; s[i + 1] = -1; } } else { res += abs(a[i]) + abs(s[i]) + 1; s[i + 1] = -1; } } } return res; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; a.resize(n); s.resize(n + 1); for (int i = 0; i < n; ++i) cin >> a[i]; long long int ans = solve(false); ans = min(solve(true), 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
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 if a[0] > 0: op = "+" elif a[0] < 0: op = "-" elif a[0] == 0: ans += 1 if a[1] > 0: a[0] = -1 op = "-" else: a[0] = 1 op = "+" total = a[0] for i in range(1, n): if (total+a[i]) >= 0 and op == "+": ans += abs((-1 - total) - a[i]) total = -1 op = "-" elif (total+a[i]) < 0 and op == "-": ans += abs((1 - total) - a[i]) total = 1 op = "+" elif total+a[i] == 0: ans += 1 if op == "+": total = -1 op = "-" else: total = 1 op = "+" else: total += a[i] if op == "+": op = "-" else: op = "+" 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; 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 sum = a[0]; long long ans = 0; for (int i = 1; i < n; ++i) { if (sum < 0) { if (a[i] > abs(sum)) { sum += a[i]; } else { ans += (1LL - sum - a[i]); sum = 1LL; } } else { if (a[i] < 0 && abs(a[i]) > sum) { sum += a[i]; } else { ans += abs((-1LL - sum - a[i])); sum = -1LL; } } } 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 <iostream> #include <algorithm> #include <string> using namespace std; #define int long long int main() { int n; int A[100001]; cin >> n; for(int i = 0; i < n; i++) { cin >> A[i]; } int sum = 0; int counter = 0; // 偶数番目が正 for(int i = 0; i < n; i++) { sum += A[i]; if(i % 2 == 0) { while(sum <= 0){ sum++; counter++; } } else { while(sum >= 0){ sum--; counter++; } } } int counterNeg = 0; sum = 0; for(int i = 0; i < n; i++) { sum += A[i]; if(i % 2 == 1) { while(sum <= 0){ sum++; counterNeg++; } } else { while(sum >= 0){ sum--; counterNeg++; } } } cout << min(counter, counterNeg) << 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 = 1e9 + 7; const int INF = 1 << 30; const int MAX_N = 100003; long long a[MAX_N]; int main() { int N; cin >> N; long long S = 0, ans = 0; int flag = 0; for (int i = 0; i < N; i++) cin >> a[i]; for (int i = N - 1; i >= 0; i--) { if (a[i] > 0) { if (i % 2) flag = -1; else flag = 1; break; } else if (a[i] < 0) { if (i % 2) flag = 1; else flag = -1; break; } } if (!flag) { cout << N << endl; return 0; } for (int i = 0; i < N; i++) { S += a[i]; if (flag == 1) { flag = -1; if (S > 0) continue; else { ans += 1 - S; S = 1; } } else { flag = 1; if (S < 0) continue; else { ans += S + 1; S = -1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) sum_a = [a[0]] sum_tmp = a[0] ans_v = 0 ans = 0 for i in range(1, len(a)): sum_tmp += a[i] sum_a.append(sum_tmp) if sum_a[i] * sum_a[i - 1] >= 0: if sum_a[i] > 0: diff = - 1 - sum_a[i] ans += abs(diff) sum_a[i] += diff sum_tmp += diff elif sum_a[i] < 0: diff = 1 - sum_a[i] ans += abs(diff) sum_a[i] += diff sum_tmp += diff else: if sum_a[i - 1] < 0: sum_a[i] = 1 ans += 1 sum_tmp += 1 elif sum_a[i - 1] > 0: sum_a[i] = -1 ans += 1 sum_tmp += -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
python3
n = int(input()) lis = list(map(int,input().split())) li = lis cou = 0 co = 0 for i in range(n): if i % 2 == 0: if sum(li[:i+1]) <= 0: cou += (1-sum(li[:i+1])) li[i] += (1-sum(li[:i+1])) else: if sum(li[:i+1]) >= 0: cou += sum(li[:i+1])+1 li[i] -= (sum(li[:i+1])+1) for i in range(n): if i % 2 != 0: if sum(lis[:i+1]) <= 0: co += (1-sum(lis[:i+1])) lis[i] += (1-sum(lis[:i+1])) else: if sum(lis[:i+1]) >= 0: co += sum(lis[:i+1])+1 lis[i] -= (sum(lis[:i+1])+1) print(min(co,cou))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(lambda x: int(x), input().split())) idx=0 ans=0 sum_last=0 for i in a: if i!=0: break idx+=1 if idx==len(a): ans=2*idx-1 elif idx>0: ans=2*idx-1 if a[idx]>0: sum_last=-1 else: sum_last=1 else: sum_last=a[0] ans=0 idx=1 for i in a[idx:]: sum_cur=sum_last+i if sum_cur*sum_last>=0: ans+=abs(sum_cur)+1 if sum_last>0: sum_last=-1 else: sum_last=1 else: sum_last=sum_cur print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 100005; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0, ans = INF, cnt = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i & 1) { if (sum >= 0) { cnt += abs(sum) + 1; sum = -1; } } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } } ans = min(ans, cnt); sum = 0; cnt = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (!(i & 1)) { if (sum >= 0) { cnt += abs(sum) + 1; sum = -1; } } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } } ans = min(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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector<ll>; const int INF = 1e9; int main() { int n; cin >> n; vll s(n, 0); for (int i = 0; i < n; i++) { ll a; cin >> a; if (i == 0) { s[i] = a; } else { s[i] = s[i - 1] + a; } } bool sgn; ll tmp = 0LL, cnt = 0LL; for (int i = 0; i < n; i++) { if (i == 0) sgn = (s[0] > 0) ? true : false; ll m = tmp + s[i]; if (sgn) { if (m <= 0) { tmp += 1 - m; cnt += abs(1 - m); } } else { if (m >= 0) { tmp += (-1) - m; cnt += abs((-1) - m); } } sgn = (sgn) ? false : true; } 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; long long a[1000000]; long long min(long long a, long long b) { int t = a; if (b <= t) t = b; return t; } int main() { int n; cin >> n; for (int t = 0; t < n; t++) cin >> a[t]; long long sum = 0LL; long long x = 0LL; for (int t = 0; t < n; t++) { sum += a[t]; if (t % 2 == 1 && sum >= 0) { long long s = sum + 1; sum = -1; x += s; } else if (t % 2 == 0 && sum <= 0) { long long s = 1 - sum; sum = 1; x += s; } } long long positive_x = x; x = 0LL; sum = 0LL; for (int t = 0; t < n; t++) { sum += a[t]; if (t % 2 == 0 && sum >= 0) { long long s = sum + 1; sum = -1; x += s; } else if (t % 2 == 1 && sum <= 0) { long long s = 1 - sum; sum = 1; x += s; } } long long negative_x = x; long long result = min(positive_x, negative_x); 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
cpp
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1e9 + 7; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); } int a, b; a = 0; b = 0; int sum = 0; for (int i = 0; i < n; i++) { if (!i) { if (v.at(i) >= 0) { if (!v.at(0)) { a++; sum = 1; } else { sum = v.at(i); } } else { sum = 1; a += abs(v.at(i)) + 1; } } else { int n = sum + v.at(i); if (i % 2) { if (n > 0) { a += abs(n) + 1; sum = -1; } else { if (!n) { a++; sum = -1; } else { sum = n; } } } else { if (n < 0) { a += abs(n) + 1; sum = 1; } else { if (!n) { a++; sum = 1; } else { sum = n; } } } } } for (int i = 0; i < n; i++) { if (!i) { if (v.at(i) <= 0) { if (!v.at(0)) { b++; sum = -1; } else { sum = v.at(i); } } else { sum = -1; b += abs(v.at(i)) + 1; } } else { int n = sum + v.at(i); if (i % 2) { if (n < 0) { b += abs(n) + 1; sum = 1; } else { if (!n) { b++; sum = 1; } else { sum = n; } } } else { int n = sum + v.at(i); if (n > 0) { b += abs(n) + 1; sum = -1; } else { if (!n) { b++; sum = -1; } else { sum = n; } } } } } cout << min(a, b) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; long long sum; cin >> sum; if (sum == 0) { long long delta1 = 1; sum = 1; long long ar[N]; for (int i = 1; i < N; ++i) { cin >> ar[i]; long long temp = ar[i]; if (sum > 0 && sum + temp >= 0) { sum = -1; delta1 += sum + temp + 1; } else if (sum < 0 && sum + temp <= 0) { sum = 1; delta1 += 1 - (sum + temp); } else { sum += temp; } } long long delta2 = 1; sum = -1; for (int i = 1; i < N; ++i) { long long temp = ar[i]; if (sum > 0 && sum + temp >= 0) { sum = -1; delta2 += sum + temp + 1; } else if (sum < 0 && sum + temp <= 0) { sum = 1; delta2 += 1 - (sum + temp); } else { sum += temp; } } if (delta1 < delta2) cout << delta1; else cout << delta2; } else { long long delta = 0; for (int i = 1; i < N; ++i) { int temp; cin >> temp; if (sum > 0 && sum + temp >= 0) { delta += sum + temp + 1; sum = -1; } else if (sum < 0 && sum + temp <= 0) { delta += 1 - (sum + temp); sum = 1; } else { sum += temp; } } cout << delta; } 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; namespace ABC059C { class Program { static void Main(string[] args) { int n = Int32.Parse(Console.ReadLine()); string[] bufs = Console.ReadLine().Split(' '); long res = 0; int sum = Int32.Parse(bufs[0]); if (sum == 0) { res++; } for (int i = 1; i < n; i++) { int tmp = sum + Int32.Parse(bufs[i]); if (sum == 0) { if (tmp == 0) { res += 2; } else if (tmp == 1 || tmp == -1) { sum = tmp; res++; } else if (tmp > 1) { sum = tmp - 1; } else { sum = tmp + 1; } } else if (sum > 0) { if (tmp >= 0) { res += tmp + 1; sum = -1; } else { sum = tmp; } } else { if (tmp <= 0) { res += -tmp + 1; sum = 1; } else { sum = tmp; } } } Console.WriteLine(res); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ulli = unsigned long long int; vector<lli> N, rN; lli in, n, d = 0, dp, pm; ulli ans = 0; int main() { cin >> n; for (lli l = 0; l < n; l++) { cin >> in; if (l == 0) { N.push_back(in); } else { N.push_back(N[l - 1] + in); } } for (lli l = 1; l < (lli)N.size(); l++) { dp = d; {}; {}; if (N[l - 1] + dp < 0) { if (N[l] + dp < 0) { d += 1 - N[l] - dp; ans += 1 - N[l] - dp; } else if (N[l] + dp == 0) { d += 1; ans += 1; } } else if (N[l - 1] + dp > 0) { if (N[l] + dp > 0) { d -= N[l] + dp + 1; ans += N[l] + dp + 1; } else if (N[l] + dp == 0) { d -= 1; ans += 1; } } else { for (lli m = l - 1; m < (lli)N.size(); m++) { if (N[m] > 0) { pm = (m - l) % 2; break; } else if (N[m] < 0) { pm = (m - l + 1) % 2; break; } if (m == (lli)N.size() - 1) { pm = (m + 1) % 2; break; } } if (pm == 1) { d += 1; ans += 1; } else if (pm == 0) { d -= 1; ans += 1; } dp = d; if (N[l] + dp < 0) { d += 1 - N[l] - dp; ans += 1 - N[l] - dp; } else if (N[l] + dp == 0) { d += 1; ans += 1; } else if (N[l] + dp > 0) { d -= N[l] + dp + 1; ans += N[l] + dp + 1; } else if (N[l] + dp == 0) { d -= 1; ans += 1; } } {}; {}; {}; {}; {}; } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; long long a[1000010]; int n; unsigned long long solve() { unsigned long long sum = 0; long long oo = 0, flag; if (a[0] > 0) flag = -1; else if (a[0] < 0) flag = 1; for (int i = 0; i < n; i++) { oo += a[i]; if (flag == 1) { if (oo >= 0) { sum += oo + 1; oo = -1; } } if (flag == -1) { if (oo <= 0) { sum += 0 - oo + 1; oo = 1; } } flag = -flag; } return sum; } int main() { while (scanf("%d", &n) != EOF) { unsigned long long sum; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } int t = a[0]; if (a[0] == 0) { a[0] = 1; unsigned long long sum1 = solve(); a[0] = -1; unsigned long long sum2 = solve(); sum = min(sum1, sum2) + 1; } else { a[0] = 1; unsigned long long sum1 = solve() + abs(t - 1); a[0] = -1; unsigned long long sum2 = solve() + abs(t + 1); sum = min(sum1, sum2); } printf("%lld\n", sum); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; // ABC 6-C // http://abc006.contest.atcoder.jp/tasks/abc006_3 public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); long sum = 0; long answer = 0; for (int i = 0; i < n; i++) { int a = in.nextInt(); if (i > 0) { if (sum < 0 && sum + a < 0) { answer += 1 + Math.abs(sum + a); sum = 1; } else if (sum > 0 && sum + a > 0) { answer += 1 + sum + a; sum = -1; } else if (sum + a == 0) { answer++; if (sum < 0) { sum = 1; } else { sum = -1; } } else { sum += a; } } else { sum += a; } } System.out.println(answer); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) sum=a[0] cnt=0 if a[0]==0: sum+=1 cnt+=1 for i in range(1,n): if sum<0: z=sum+a[i] if z>0: sum=z elif z<=0: cnt+=(1-z) sum=1 elif sum>0: z=sum+a[i] if z>=0: cnt+=(z+1) sum=-1 elif z<0: sum=z 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; using LL = long long; using VLL = std::vector<LL>; using VVLL = std::vector<VLL>; using VVVLL = std::vector<VVLL>; using LD = long double; using VLD = std::vector<LD>; using VVLD = std::vector<VLD>; using VVVLD = std::vector<VVLD>; using BL = bool; using VBL = std::vector<BL>; using VVBL = std::vector<VBL>; using VVVBL = std::vector<VVBL>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } template <class T> void PRINTSP(vector<T> v, size_t w = 3) { for (auto(a) : v) { cerr << setw(w) << a << " "; } cerr << endl; } struct mll { static LL MOD; LL val; mll(LL v = 0) : val(v % MOD) { if (val < 0) val += MOD; } mll operator-() const { return -val; } mll operator+(const mll &b) const { return val + b.val; } mll operator-(const mll &b) const { return val - b.val; } mll operator*(const mll &b) const { return val * b.val; } mll operator/(const mll &b) const { return mll(*this) /= b; } mll operator+(LL b) const { return *this + mll(b); } mll operator-(LL b) const { return *this - mll(b); } mll operator*(LL b) const { return *this * mll(b); } friend mll operator+(LL a, const mll &b) { return b + a; } friend mll operator-(LL a, const mll &b) { return -b + a; } friend mll operator*(LL a, const mll &b) { return b * a; } mll &operator+=(const mll &b) { val = (val + b.val) % MOD; return *this; } mll &operator-=(const mll &b) { val = (val + MOD - b.val) % MOD; return *this; } mll &operator*=(const mll &b) { val = (val * b.val) % MOD; return *this; } mll &operator/=(const mll &b) { LL c = b.val, d = MOD, u = 1, v = 0; while (d) { LL t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll &operator+=(LL b) { return *this += mll(b); } mll &operator-=(LL b) { return *this -= mll(b); } mll &operator*=(LL b) { return *this *= mll(b); } mll &operator/=(LL b) { return *this /= mll(b); } bool operator==(const mll &b) { return val == b.val; } bool operator!=(const mll &b) { return val != b.val; } bool operator==(LL b) { return *this == mll(b); } bool operator!=(LL b) { return *this != mll(b); } friend bool operator==(LL a, const mll &b) { return mll(a) == b.val; } friend bool operator!=(LL a, const mll &b) { return mll(a) != b.val; } friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; } friend istream &operator>>(istream &is, mll &a) { return is >> a.val; } static mll Combination(LL a, LL b) { chmin(b, a - b); if (b < 0) return mll(0); mll c = 1; for (LL(i) = 0; (i) < (b); (i)++) c *= a - i; for (LL(i) = 0; (i) < (b); (i)++) c /= i + 1; return c; } static mll Kumiawase(LL a, LL b) { chmin(b, a - b); if (b < 0) return mll(0); return Junretu(a, b) / Kaijou(b); } static mll Kaijou(LL a) { if (a < 0) return mll(0); mll c = 1; for (LL i = 1; i <= a; i++) { c *= i; } return c; } static mll Junretu(LL a, LL b) { if (a < b) { return mll(0); } mll c = 1; for (LL i = a; i > a - b; i--) { c *= i; } return c; } static mll _Junretu(LL a, LL b) { if (a < b) { return mll(0); } return Kaijou(a) / (Kaijou(a - b)); } LL get() { return val; } }; LL mll::MOD = (LL)(1e9 + 7); using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; LL modpow(LL a, LL n, LL mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } template <typename TTT> vector<LL> arg_sort(vector<TTT> A, bool ascend = true) { vector<LL> index(A.size()); iota(index.begin(), index.end(), 0); if (ascend) { std::sort(index.begin(), index.end(), [&A](TTT i1, TTT i2) { return A[i1] < A[i2]; }); } else { std::sort(index.begin(), index.end(), [&A](TTT i1, TTT i2) { return A[i1] > A[i2]; }); } return index; } template <typename _Iterator, typename _Compare> LL num_of_ika(_Iterator _first, _Iterator _last, _Compare key) { return (LL)(upper_bound(_first, _last, key) - _first); } template <typename _Iterator, typename _Compare> LL num_of_ookii(_Iterator _first, _Iterator _last, _Compare key) { return (LL)(_last - upper_bound(_first, _last, key)); } template <typename _Iterator, typename _Compare> LL num_of_chisai(_Iterator _first, _Iterator _last, _Compare key) { return (LL)(lower_bound(_first, _last, key) - _first); } template <typename _Iterator, typename _Compare> LL num_of_ijou(_Iterator _first, _Iterator _last, _Compare key) { return (LL)(_last - lower_bound(_first, _last, key)); } template <typename _Iterator, typename _Compare> LL num_of_onaji(_Iterator _first, _Iterator _last, _Compare key) { return (LL)(upper_bound(_first, _last, key) - lower_bound(_first, _last, key)); } struct UnionFindTree { private: vector<LL> UFT; public: UnionFindTree(LL N) { UFT.resize(N); for (LL i = 0; i < N; i++) { UFT[i] = -1; } } LL root(LL i) { if (UFT[i] < 0) { return i; } else { LL j = root(UFT[i]); UFT[i] = j; return j; } } bool same(LL i, LL j) { return (root(i) == root(j)); } bool unite(LL i, LL j) { if (same(i, j)) { return false; } LL root_i = root(i); LL root_j = root(j); if (root_i != root_j) { if (size(root_i) < size(root_j)) { swap(root_i, root_j); } UFT[root_i] += UFT[root_j]; UFT[root_j] = root_i; } return true; } LL size(LL i) { return -UFT[root(i)]; } LL get_root_num() { set<LL> roots; for (LL i = 0; i < (LL)UFT.size(); ++i) { roots.insert(root(i)); } return (LL)roots.size(); } map<LL, vector<LL>> get_root_child() { map<LL, vector<LL>> a; for (LL i = 0; i < (LL)UFT.size(); ++i) { LL j = root(i); a[j].push_back(i); } return a; } void print() { for (LL i = 0; i < (LL)UFT.size(); ++i) { cerr << root(i) << " "; } cerr << endl; } }; inline VLL cinvll(LL N, LL minus = 0) { VLL A(N); for (LL(i) = 0; (i) < (N); (i)++) { cin >> A[i]; A[i] -= minus; } return move(A); } inline VVLL zerosll(LL H, LL W, LL val = 0) { VVLL A(H, VLL(W, val)); return move(A); } inline VVVLL zerosll3(LL H, LL W, LL C, LL val = 0) { VVVLL A(H, VVLL(W, VLL(C, val))); return move(A); } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (LL i = 0; i < (LL)vec.size(); i++) { os << setw(5) << vec[i]; } os << endl; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { cerr << ("\033[36m"); cerr << head; if (sizeof...(Tail) > 0) { cerr << ", "; } dump_func(std::move(tail)...); cerr << "\033[m"; } struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; bool isPrime(LL N) { if (N < 2) { return false; } else if (N == 2) { return true; } else if (N % 2 == 0) { return false; } LD sqrtNum = sqrt(N); for (LL i = 3; i <= sqrtNum; i += 2) { if (N % i == 0) { return false; } } return true; } map<LL, LL> prime_factor(LL n) { map<LL, LL> ret; for (LL i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } LL GCD(LL m, LL n) { if (m < n) { swap(m, n); } while (n != 0) { LL n_new = m % n; m = n; n = n_new; } return m; } LL LCM(LL x, LL y) { return x / GCD(x, y) * y; } struct Furui { LL n; VLL f; VLL prime; Furui(LL n = 1) : n(n), f(n + 1) { f[0] = -1; f[1] = -1; for (LL i = 2; i <= n; i++) { if (f[i]) { continue; } prime.push_back(i); for (LL j = i; j <= n; j += i) { if (f[j] == 0) { f[j] = i; } } } } bool isPrime(LL x) { if (x <= 1) { return false; } return (f[x] == x); } VLL factorList(LL x) { VLL res; if (x >= 2 and x <= n) { while (x != 1) { res.push_back(f[x]); x /= f[x]; } } return res; } vector<pair<LL, LL>> factor(LL x) { VLL factors = factorList(x); vector<pair<LL, LL>> P; if (factors.size() == 0) { return P; } P.emplace_back(factors[0], 0); for (auto p : factors) { if (P.back().first == p) { P.back().second++; } else { P.emplace_back(p, 1); } } return P; } }; template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template <typename T> using Matrix = vector<vector<T>>; template <typename T> void warshall_floyd(Matrix<T> &g, T INF) { for (int k = 0; k < g.size(); k++) { for (int i = 0; i < g.size(); i++) { for (int j = 0; j < g.size(); j++) { if (g[i][k] == INF || g[k][j] == INF) continue; g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } } } } template <typename T> T kruskal(Edges<T> &edges, LL V) { sort(begin(edges), end(edges), [](const edge<T> &a, const edge<T> &b) { return (a.cost < b.cost); }); UnionFindTree tree(V); T ret = 0; for (auto &e : edges) { if (tree.unite(e.src, e.to)) ret += e.cost; } return (ret); } bool isOK(LL mid) { return true; } LL binary_search() { LL ng = 0; LL ok = 100000; while (abs(ok - ng) > 1) { LL mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } return ok; } LL powll(LL a, LL b) { LL c = 1LL; for (LL(i) = 0; (i) < (b); (i)++) { c *= a; } return c; } LL knapsack(vector<LL> v, vector<LL> w, LL W) { LL N = (LL)v.size(); vector<LL> dp(W + 1, 0); for (LL i = 0; i < N; ++i) { for (LL j = w[i]; j <= W; ++j) { dp[j] = max(dp[j], dp[j - w[i]] + v[i]); } } return dp[W]; } template <typename T> vector<T> dijkstra(WeightedGraph<T> &g, int s) { const auto INF = numeric_limits<T>::max(); vector<T> dist(g.size(), INF); using Pi = pair<T, int>; priority_queue<Pi, vector<Pi>, greater<Pi>> que; dist[s] = 0; que.emplace(dist[s], s); while (!que.empty()) { T cost; int idx; tie(cost, idx) = que.top(); que.pop(); if (dist[idx] < cost) continue; for (auto &e : g[idx]) { auto next_cost = cost + e.cost; if (dist[e.to] <= next_cost) continue; dist[e.to] = next_cost; que.emplace(dist[e.to], e.to); } } return dist; } template <typename T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz = 10) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } void resize(int sz) { data.assign(++sz, 0); } }; LL Combination(LL a, LL b) { chmin(b, a - b); if (b < 0) return 0; LL c = 1; for (LL(i) = 0; (i) < (b); (i)++) c *= a - i; for (LL(i) = 0; (i) < (b); (i)++) c /= i + 1; return c; } struct IntegralImage { VVLL I; LL H; LL W; IntegralImage(VVLL M) { I = M; H = M.size(); W = M[0].size(); bool isOK = true; for (LL(i) = 0; (i) < (H); (i)++) { if ((LL)I[i].size() != W) { isOK = false; } } if (not isOK) { cerr << "shape error :"; for (LL(i) = 0; (i) < (H); (i)++) { cerr << I[i].size() << " "; } cerr << endl; throw 0; } for (LL(i) = 0; (i) < (H); (i)++) { for (LL(j) = 0; (j) < (W - 1); (j)++) { I[i][j + 1] += I[i][j]; } } for (LL(j) = 0; (j) < (W); (j)++) { for (LL(i) = 0; (i) < (H - 1); (i)++) { I[i + 1][j] += I[i][j]; } } } LL get_sum(LL y, LL h, LL x, LL w) { if (y < 0 or x < 0 or h < 0 or w < 0 or y + h >= H or x + w >= W) { fprintf(stderr, "H=%lld, W=%lld, y=%lld, h=%lld, x=%lld, w=%lld\n", H, W, y, h, x, w); throw 1; } LL a1 = I[y + h][x + w]; LL a2 = 0; if (y > 0) { a2 = I[y - 1][x + w]; } LL a3 = 0; if (x > 0) { a3 = I[y + h][x - 1]; } LL a4 = 0; if (y > 0 && x > 0) { a4 = I[y - 1][x - 1]; } return (a1 + a4 - a2 - a3); } void print() { for (LL(i) = 0; (i) < ((LL)I.size()); (i)++) { PRINTSP(I[i]); } } }; bool isKaibun(string S) { bool ok = true; LL N = (LL)S.length(); for (LL(i) = 0; (i) < (N); (i)++) { if (S[i] != S[N - i - 1]) { ok = false; break; } } return ok; } VLL my_dycstra(VVVLL &R, LL start = 0) { LL S = start; LL N = R.size(); LL INFINF = 1e15; priority_queue<VLL, VVLL, greater<VLL>> Q; VLL C(N, INFINF); C[S] = 0; Q.push({0, S}); while (!Q.empty()) { VLL q = Q.top(); Q.pop(); LL cost = q[0]; LL no = q[1]; for (auto r : R[no]) { LL to = r[0]; LL cost_to = r[1]; if (chmin(C[to], (cost + cost_to))) { Q.push({cost + cost_to, to}); } } } return move(C); } VLL haba_yuusen_kyori(VVLL &V, LL n) { LL N = V.size(); VLL D(N, -1); queue<pair<LL, LL>> Q; Q.push({n, 0}); while (!Q.empty()) { auto q = Q.front(); Q.pop(); LL no = q.first; LL d = q.second; D[no] = d; for (auto v : V[no]) { if (D[v] != -1) { continue; } D[v] = d + 1; Q.push({v, d + 1}); } } return move(D); } struct SegmentTree { VLL D; LL N; LL DEFAULT; string mode; void init(LL N, string mode = "min") { this->mode = mode; if (mode == "min") { DEFAULT = LONG_LONG_MAX; } else if (mode == "max") { DEFAULT = LONG_LONG_MIN; } else if (mode == "sum") { DEFAULT = 0LL; } else { this->mode = "min"; DEFAULT = LONG_LONG_MAX; } LL K = 1; while (K < N) { K *= 2; } this->N = K; this->D.resize(2 * this->N, DEFAULT); ; } SegmentTree(LL N, string mode = "min") { init(N, mode); } SegmentTree(VLL &A, string mode = "min") { LL N = A.size(); this->init(N, mode); ; for (LL(i) = 0; (i) < (N); (i)++) { ; set(i, A[i]); }; build(); } void set(LL i, LL x) { D[i + N] = x; } void build() { for (LL i = N - 1; i > 0; i--) { if (mode == "min") { D[i] = min({D[2 * i], D[2 * i + 1]}); } else if (mode == "max") { D[i] = max({D[2 * i], D[2 * i + 1]}); } else if (mode == "sum") { D[i] = D[2 * i] + D[2 * i + 1]; } } } void update(LL i, LL x) { D[i + N] = x; LL j = i + N; while (j > 1) { LL k = j / 2; if (mode == "min") { chmin(D[k], D[j]); } else if (mode == "max") { chmax(D[k], D[j]); } else if (mode == "sum") { D[k] += D[j]; } j = k; } } void print() { ; for (LL(i) = 1; (i) < (2 * N); (i)++) { cerr << D[i] << " "; if (__builtin_popcountll(i + 1) == 1) { cerr << endl; } } } LL query(LL a, LL b, LL k = 1, LL l = 0, LL r = -1) { if (r == -1) { r = N; } if (b <= l || r <= a) { return DEFAULT; } if (a <= l && r <= b) { return D[k]; } LL vl = query(a, b, 2 * k, l, (l + r) / 2); LL vr = query(a, b, 2 * k + 1, (l + r) / 2, r); if (mode == "min") { return min({vl, vr}); } else if (mode == "max") { return max({vl, vr}); } else if (mode == "sum") { return vl + vr; } } }; void oira_tua(LL me, LL oya, VVLL &T, VLL &IO) { IO.push_back(me); ; for (LL i = 0; i < (LL)T[me].size(); i++) { LL ko = T[me][i]; if (ko == oya) { continue; } oira_tua(ko, me, T, IO); IO.push_back(me); } } VVLL cinvll2(LL H, LL W, LL bias = 0) { VVLL A = zerosll(H, W); for (LL(i) = 0; (i) < (H); (i)++) { for (LL(j) = 0; (j) < (W); (j)++) { cin >> A[i][j]; A[i][j] += bias; } } return move(A); } void print(vector<priority_queue<LL>> P) { for (LL(i) = 0; (i) < (P.size()); (i)++) { auto p = P[i]; cerr << i << " : "; while (!p.empty()) { LL f = p.top(); p.pop(); cerr << f << " "; } cerr << endl; } } void print(vector<multiset<LL, greater<LL>>> P) { for (LL(i) = 0; (i) < (P.size()); (i)++) { auto p = P[i]; cerr << i << " : "; for (auto f : p) { cerr << f << " "; } cerr << endl; } } void print(vector<map<LL, LL, greater<LL>>> P) { for (LL(i) = 0; (i) < (P.size()); (i)++) { auto p = P[i]; cerr << i << " : "; for (auto f : p) { cerr << f << " "; } cerr << endl; } } LL funcx(VLL A, bool flg) { LL N = A.size(); LL husai = 0; LL cnt = 0; for (LL(i) = 0; (i) < (N); (i)++) { A[i] += husai; if (i == 0) { if (flg) { if (A[0] <= 0) { husai += 1 - A[i]; cnt += abs(1 - A[i]); } } else { if (A[0] >= 0) { husai += -1 - A[i]; cnt += abs(-1 - A[i]); A[i] = -1; } } } else if (i > 0) { if (A[i - 1] > 0 and A[i] >= 0) { husai += -1 - A[i]; cnt += abs(-1 - A[i]); A[i] = -1; } else if (A[i - 1] < 0 and A[i] <= 0) { husai += 1 - A[i]; cnt += abs(1 - A[i]); A[i] = 1; } }; ; } return cnt; } void func() { LL N; cin >> N; VLL A = cinvll(N); LL cnt = 0; LL husai = 0; for (LL(i) = 0; (i) < (N - 1); (i)++) { A[i + 1] += A[i]; } LL p = funcx(A, true); LL m = funcx(A, false); ; bye(min({p, m})); } int main() { func(); 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()) l=list(map(int,input().split())) if l[0]!=0: count=0 suml=l[0] for i in range(1,n): if suml>0 and suml+l[i]<0: suml+=l[i] elif suml<0 and suml+l[i]>0: suml+=l[i] elif suml>0 and suml+l[i]>=0: count+=suml+l[i]+1 suml=-1 else: count+=-suml-l[i]+1 suml=1 print(count) else: count=1 suml1=1 for i in range(1,n): if suml1>0 and suml1+l[i]<0: suml1+=l[i] elif suml1<0 and suml1+l[i]>0: suml1+=l[i] elif suml1>0 and suml1+l[i]>=0: count+=suml1+l[i]+1 suml1=-1 else: count+=-suml1-l[i]+1 suml1=1 k1=count count=1 suml2=-1 for i in range(1,n): if suml2>0 and suml2+l[i]<0: suml2+=l[i] elif suml2<0 and suml2+l[i]>0: suml2+=l[i] elif suml2>0 and suml2+l[i]>=0: count+=suml2+l[i]+1 suml2=-1 else: count+=-suml2-l[i]+1 suml2=1 k2=count print(min(k1,k2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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(0L, N ).ToArray(); sum[0] = A[0]!=0?A[0]:A[1]>0?-1:1; long ans = A[0] != 0 ? 0 : 1; for(int i = 1; i < N; i++) { sum[i] = sum[i-1] + A[i]; if (sum[i - 1] * sum[i] >= 0) { if (sum[i-1] > 0) { ans += sum[i] + 1; sum[i] = -1; } else { ans += 1 - sum[i]; sum[i] = 1; } } } 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; 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> int main(void) { long long i, j, k, ans = 0, be, n, a; scanf("%d%d", &n, &be); for (i = 1; i < n; ++i) { scanf("%lld", &a); if (be > 0 && -1 < be + a) ans += be + a + 1, be = -1; else if (be < 0 && 1 > be + a) ans += 1 + -be - a, be = -1; else be += a; } printf("%lld", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def c(ints): for i in range(len(ints)): if ints[i] != 0: sig = 1 if ints[i] > 0 else -1 sig_ = -sig total = ints[i] total_ = -sig mov = i mov_ = i if i > 0: mov += 1 mov_ += abs(total) + 1 j = i break if i == len(ints) - 1: return i + 1 for i_ in ints[j+1:]: tmp = total + i_ tmp_ = total_ + i_ if tmp == 0: mov +=1 tmp = -sig elif sig * tmp > 0: mov += abs(tmp) + 1 tmp = -sig if tmp_ == 0: mov_ +=1 tmp_ = -sig_ elif sig_ * tmp_ > 0: mov_ += abs(tmp_) + 1 tmp_ = -sig_ sig *= -1 total = tmp sig_ *= -1 total_ = tmp_ return min(mov, mov_) _ = input() inp = input() inp = inp.split(' ') inp = [int(i_) for i_ in inp] print(c(inp))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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 int LINF = 2000000000000000000ll; const int INF = 1000000100; const long long int MOD = 1e9 + 7; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; int ans = INF; int ans_t, total, sign; for (int j = 0; j < (2); ++j) { ans_t = 0; total = 0; if (j == 0) sign = true; else sign = false; for (int i = 0; i < (n); ++i) { total += a[i]; if (sign) { while (total >= 0) { total--; ans_t++; } } else { while (total <= 0) { total++; ans_t++; } } sign = 1 - sign; } ans = min(ans, ans_t); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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)])) else:#頭0のとき;和0なので変える =>1,-1にした時両方やって良いほうに #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; template <typename T> void showvector(vector<T> v) { for (T x : v) cout << x << " "; cout << "\n"; } template <typename T> void showvector1(vector<T> v) { long long int n = v.size(); for (long long int i = 1; i <= n - 1; i++) cout << v[i] << "\n"; } template <typename T> void showset(set<T> s) { for (T x : s) cout << x << " "; cout << "\n"; } template <class T> void showvectorpair(vector<T> v) { for (auto it = v.begin(); it != v.end(); it++) cout << it->first << " " << it->second << "\n"; cout << "\n"; } template <typename T, typename P> void showmap(map<T, P> m) { for (auto it = m.begin(); it != m.end(); it++) cout << it->first << " " << it->second << "\n"; cout << "\n"; } template <typename T> bool comp(T a, T b) { return (a > b); } template <class T> bool comppair(T a, T b) { if (a.first == b.first) return (a.second > b.second); return (a.first > b.first); } bool sameparity(long long int a, long long int b) { return (a % 2 == b % 2); } bool difparity(long long int a, long long int b) { return !(a % 2 == b % 2); } bool isprime(long long int x) { if (x <= 1) return false; for (long long int i = 2; i <= sqrt(x); i++) { if (x % i == 0) return false; } return true; } bool iseven(long long int x) { return !(x % 2); } bool isodd(long long int x) { return (x % 2); } void vfun() { long long int n, k; cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int test = 1; while (test--) { long long int n; cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; long long int sum = v[0], psum = v[0], cnt = 0; for (long long int i = 1; i <= n - 1; i++) { sum += v[i]; if (psum > 0) { if (sum >= 0) { cnt += sum + 1; sum = -1; } } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } psum = sum; } cout << cnt << "\n"; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long op = 0LL; long long sum = 0LL; sum = a[0]; for (int i = 1; i < n; i++) { long long a; cin >> a; if (!(sum * (sum + a) < 0)) { long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1); op += abs(tmp_a - a); sum = sum + tmp_a; } else { sum += a; } } long long op_m = op; if (a[0] > 0) { sum = -1LL; op = a[0] + 1; } else { sum = 1LL; op = -1 * a[0] + 1; } for (int i = 1; i < n; i++) { long long a; cin >> a; if (!(sum * (sum + a) < 0)) { long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1); op += abs(tmp_a - a); sum = sum + tmp_a; } else { sum += a; } } op = min(op, op_m); cout << op << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; vector<long long int> a(100001); long long int s; for (int i = 0; i < (n); i++) { cin >> s; a[i] = 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
UNKNOWN
#include <bits/stdc++.h> int main(void) { int N; long long cnt = 0; long long sum = 0; scanf("%d%lld", &N, &sum); for (int i = 0; i < N - 1; i++) { int tmp; scanf("%d", &tmp); if (sum > 0 && sum + tmp >= 0) { cnt += sum + tmp + 1; sum = -1; } else if (sum < 0 && sum + tmp <= 0) { cnt += -(sum + tmp) + 1; sum = 1; } else { sum += tmp; } } 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 int MaxN = 1e5; bool flag, ok; long long sum, ans, anv, anw; int n; int a[MaxN + 5], b[MaxN + 5], c[MaxN + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; c[i] = a[i]; } sum = a[1]; if (a[1] < 0) flag = 1, ok = 1; if (a[1] != 0) { for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + a[i] <= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = 1 - sum; ans += (a[i] - t); sum += a[i]; } else sum += a[i]; flag = 0; } else { if (sum + a[i] >= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = -1 - sum; ans += (t - a[i]); sum += a[i]; } else sum += a[i]; flag = 1; } } } if (a[1] == 0) ans = 1LL << 62, ok = 1; int tr = b[1]; if (ok) b[1] = 1, flag = 0; else b[1] = -1, flag = 1; anv += (abs(b[1] - tr)); sum = b[1]; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + b[i] <= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = 1 - sum; anv += (b[i] - t); sum += b[i]; } else sum += b[i]; flag = 0; } else { if (sum + b[i] >= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = -1 - sum; anv += (t - b[i]); sum += b[i]; } else sum += b[i]; flag = 1; } } if (a[1] == 0) anw = 1LL << 62; int te = c[1]; if (!ok) c[1] = 1, flag = 0; else c[1] = -1, flag = 1; anw += (abs(c[1] - te)); sum = c[1]; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + c[i] <= 0) { long long ant = sum + c[i]; int t = c[i]; c[i] = 1 - sum; anw += (c[i] - t); sum += c[i]; } else sum += c[i]; flag = 0; } else { if (sum + c[i] >= 0) { long long ant = sum + c[i]; int t = c[i]; c[i] = -1 - sum; anw += (t - c[i]); sum += c[i]; } else sum += c[i]; flag = 1; } } if (anw < anv) anv = anw; if (anv < ans) ans = anv; printf("%lld\n", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; map<int, int> mpa, mpb; priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pque; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; int a[N + 2]; for (int i = 1; i <= N; i++) { cin >> a[i]; } int cnt1 = 0, cnt2 = 0; int sum = 0; for (int i = 1, s = 1; i <= N; i++, s *= -1) { sum += a[i]; if (sum * s <= 0) cnt1 += abs(sum - s), sum = s; } sum = 0; for (int i = 1, s = -1; i <= N; i++, s *= -1) { sum += a[i]; if (sum * s <= 0) cnt2 += abs(sum - s), sum = s; } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0, ans2 = 0, sum = a[0]; for (int i = 0; i < n; i++) { while (sum <= 0 && i % 2 == 0) { sum++; a[i]++; ans++; if (sum == 1) break; } while (sum >= 0 && i % 2 == 1) { sum--; a[i]--; ans++; if (sum == -1) break; } cout << sum << " "; if (i == n) break; sum += a[i + 1]; } for (int i = 0; i < n; i++) { while (sum <= 0 && i % 2 == 1) { sum++; a[i]++; ans2++; if (sum == 1) break; } while (sum >= 0 && i % 2 == 0) { sum--; a[i]--; ans2++; if (sum == -1) break; } cout << sum << " "; if (i == n) break; sum += a[i + 1]; } cout << min(ans, 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
import sys n = int(input()) l = list(map(int, input().split())) if all((x == 0 for x in l)): print(n * 2 - 1) sys.exit() ans = 0 last_sum = l[0] if last_sum == 0: for i in range(n - 1): if l[i + 1] != 0: if i + 1 % 2 == 1: ans = + 1 last_sum = -1 else: ans = + 1 last_sum = 1 for i in range(n - 1): # print(last_sum) if last_sum > 0: if last_sum + l[i + 1] < 0: last_sum += l[i + 1] else: a = -1 - last_sum - l[i + 1] ans += abs(a) last_sum += a + l[i + 1] else: if last_sum + l[i + 1] > 0: last_sum += l[i + 1] else: a = 1 - last_sum - l[i + 1] ans += abs(a) last_sum += a + l[i + 1] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int DIM = 1e5 + 5; int arr[DIM]; inline int sign(long long x) { return x > 0 ? 1 : -1; } inline long long solve(int n) { long long sum(arr[1]), ans(0); for (int i = 2; i <= n; i++) { long long aux(sum + arr[i]); if (aux == 0) { ans++; if (sum < 0) aux++; else aux--; } if (sign(sum) == sign(aux)) ans += abs(aux) + 1, sum = sign(-aux); else sum = aux; } return ans; } int main(void) { int n; cin >> n; long long ans(1e18); for (int i = 1; i <= n; i++) cin >> arr[i]; if (arr[1] != 0) ans = solve(n); else { arr[1] = -1; ans = min(ans, 1 + solve(n)); arr[1] = +1; ans = min(ans, 1 + solve(n)); } 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())) currentSum = 0 count3 = 0 count4 = 0 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count4 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count4 += abs(currentSum) + 1 currentSum = -1 elif A[i] >= 0 and restSum == 0: count4 += abs(currentSum) + 1 currentSum = -1 print(count4)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; long long a[N]; for (int i = 0; i < N; i++) cin >> a[i]; long long sum = a[0]; long long ans = 0; for (int i = 1; i < N; i++) { if (sum + a[i] >= 0 && sum > 0) { ans += abs(-sum - 1 - a[i]); sum = -1; } else if (sum + a[i] <= 0 && sum < 0) { ans += abs(-sum + 1 - a[i]); sum = 1; } else sum += 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
cpp
#include<bits/stdc++.h> #define FF ios_base::sync_with_stdio(0);cin.tie(0) #define binary(value, size) cout << bitset<size>(value) << '\n' #define PI acos(-1.0) //3.1416(180 degree to radian) #define PIby2 asin(1) //(3.1416/2) for angle(90 degree to radian) #define eps 1e-67 #define pf printf #define sf scanf #define sz size() #define rr read #define ww write #define clr(arr) memset((arr),0,(sizeof(arr))) #define rep(i,a,b) for(long long int i=a;i<b;i++) #define repb(i,a,b) for(long long int i=a;i>=b;i--) #define repa(i,a,b,c) for(long long int i=a;i<b;i=i+c) #define reps(i,a,b,c) for(long long int i=a;i>b;i=i-c) #define asort(a) sort(a.begin(),a.end()) #define asort2(a,comp) sort(a.begin(),a.end(),comp) #define arev(a) reverse(a.begin(),a.end()) #define all(v) (v).begin(),(v).end() #define all2(v,a,b) (v).begin()+a,(v).end()-b #define F first #define S second #define pb push_back #define eb emplace_back #define pbb pop_back #define mp make_pair #define mt make_tuple #define BS(v,x) binary_search(v.begin(),v.end(),x) //return true /false #define LB(v,x) lower_bound(v.begin(),v.end(),x) //found and that value and not found than greater value pos #define UB(v,x) upper_bound(v.begin(),v.end(),x) //found and greater value pos && not found also greater pos #define convertlower(c) towlower(c) #define root(x) sqrt(x) #define power(a,n) pow(a,n) #define tu(c) towupper(c) #define sq(a) ((a)*(a)) #define cube(a) ((a)*(a)*(a)) #define mx 1000 #define MX 100000 #define mod 1000000007 #define INF 2000000000 #define N 10000000 #define Ceil(n) (long long int)ceil(n) #define Floor(n) (long long int)floor(n) #define deb(x) std::cout << #x << " = " << x << std::endl; #define out(ans) cout<<ans<<endl #define outs(ans) cout<<ans<<" "<<endl using namespace std; typedef string str; typedef long long int ll; typedef double lf; typedef long double llf; typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pi; typedef pair<ll,ll> pll; typedef vector<pll> vpll; typedef char ch; typedef map<ll,ll> mpl; //vector<vector<int>> grid(n, vector<int>(n, 0)); 2D Array bool isLetter(char c) { return (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'Z'); } bool isUpperCase(char c) { return c >= 'A' and c <= 'Z'; } bool isLowerCase(char c) { return c >= 'a' and c <= 'z'; } bool isDigit(char c) { return c >= '0' and c <= '9'; } bool isVowel(char c) { return c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'; } bool isConsonant(char c) { return !isVowel(c); } template<class T> void Debug(T v) { for(int i=0; i<(int)v.size(); i++)cout << v[i] <<" "; cout<<endl; } template<class T> void Input(T &v) { for(int i=0; i<(int)v.size(); i++)cin >> v[i]; } template<class T> string toString(T n) { ostringstream ost; ost << n; ost.flush(); return ost.str(); } string intTobinary(int x) { std::string binary = std::bitset<32>(x).to_string(); return binary; } //BigMod (a^p)%mod template<typename A, typename P> int BigMod(A a, P p, const int MOD) { int ret = 1; while(p) { if(p & 1)ret = (1LL * ret * a) % MOD; a = (1LL * a * a) % MOD; p >>= 1LL; } return ret; } //Base (value,base) template<typename T> T toBase(T n, T base) { T ret = 0LL; while(n) { ret += n % base; ret *= 10LL; n /= base; } return ret; } // Divisor template<typename T> vector<T> Divisor(T value) { vector<T> v; for(int i = 1LL; i * i <= value; ++i) { if(value % i == 0) { v.push_back(i); if(i * i != value) v.push_back(value / i); } } return v; } //Primes template<typename T> bool prime(T n) { if (n % 2 == 0) return 0; for (T i = 3; i*i <= n; i += 2) { if (n % i == 0) return 0; } return 1; } //Sieve template<typename T> void SieveOfEratostheness(T n) { bool prime[n+1]; memset(prime, true, sizeof(prime)); for (T p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*p; i<=n; i += p) prime[i] = false; } } } //Math template<typename T> ll sum(std::vector<T> &v) { return std::accumulate(all(v), 0); } template<typename T> T minval(std::vector<T> &v) { return *std::min_element(all(v)); } template<typename T> T maxval(std::vector<T> &v) { return *std::max_element(all(v)); } template<typename T> void make_unique(std::vector<T> &v) { v.resize(unique(all(v)) - v.begin()); } template<typename T> void make_unique_sorted(std::vector<T> &v) { asort(v); v.resize(unique(all(v)) - v.begin()); } template<typename T> int lowerBound(std::vector<T> &v, T x) { return v.back() < x ? -1 : lower_bound(all(v), x) - v.begin(); } template<typename T> int upperBound(std::vector<T> &v, T x) { return v.back() < x ? -1 : upper_bound(all(v), x) - v.begin(); } double startTime = clock(); void showCurrentTime() { printf("%.6lf\n", ((double)clock() - startTime) / CLOCKS_PER_SEC); } //Graph /* std::vector<int> g[N], v, lev[N]; int par[N], vis[N]; void init(){ for(int i = 0; i < N; i++){ g[i].clear(); lev[i].clear(); par[i] = -1; vis[i] = 0; } } //DFS void dfs(int u, int p = -1, int d = 0){ lev[d].pb(u); par[u] = p; for( auto v : g[u]){ if(p == v) continue; dfs(v, u, d + 1); } } */ /* Graph representation: cin>>n>>ed; vi ar[n+1]; while(ed--) {cin>>a>>b) ar[a].pb(b); ar[b].pb(a); (if directed then comment out) } /* //DFS /* bool vis[MX]={0}; int graph[MX]; void DFS(int v) { vis[v]=1; rep(i,0,graph[v].size())//for(int child:graph[v]) { int child=graph[v][i]; if(vis[child]==0) { DFS(child); } } } */ //BFS /* vector<int> graph[10001]; bool vis[MX]={0}; int dist[MX]; int par[MX]; void BFS(int source) { queue<int> q; vis[source]=1; dist[source]=0; q.push(source); while(!q.empty()) { int node=q.front(); q.pop(); rep(i,0,graph[node].size()) { int next=graph[node][i]; if(vis[next]==0) { vis[next]=1; dist[next]=dist[node]+1; q.push(next); par[next]=node; } } } } */ //Bit Hacks ll MakeOneBit(ll decimalvalue, int pos) { return (decimalvalue | (1 << pos)); //make pos bit of value 1 } ll MakeZeroBit(ll decimalvalue, int pos) { return (decimalvalue & ~(1 << pos)); //make pos of value bit 0 } ll FlipBit(ll decimalvalue, int pos) { return (decimalvalue ^ (1 << pos)); //flip pos bit of value reverse } bool CheckBit(ll decimalvalue, int pos) { return (decimalvalue & (1 << pos)); } int MSB(ll k) { return ( 63 - __builtin_clzll(k)); // leftmost set bit } int LSB(ll k) { return __builtin_ffs(k)-1 ; // right most set bit } int Totalset(ll decimalvalue) { return __builtin_popcountll(decimalvalue); //total 1 in value } int Totolnotset(ll decimalvalue) { return MSB(decimalvalue) - Totalset(decimalvalue) + 1; //total 0 in value } bool ispow2(int i) { return i&&(i&-i)==i; } ll nC2(ll n) { return (n * (n - 1)) / 2; } ll nc3(ll n) { return (n * (n - 1) * (n - 2)) / 6; } ll apsum(ll n, ll a = 1, ll d = 1) { return (n * (a + a + (n - 1) * d) ) / 2; } ll LCM(ll a, ll b) { return (a / __gcd(a, b) ) * b; } ll OnetoNsum(ll n) { return (n*(n+1))/2; } //cout<<setprecision(decimal)<<value<<endl; //vector<vector<int>> grid(n, vector<int>(n, 0)); 2D Array int main() { ll tc; cin>>n; vll v; rep(i,0,n) { ll d; cin>>d; v.pb(d); } ll sign=1,sum=0,ans1=0,ans2=0; rep(i,0,n) { sum+=v[i]; if(sign*sum<=0) ans1+=labs(sign-sum),sum=sign; sign*=-1; } sign=-1,sum=0; rep(i,0,n) { sum+=v[i]; if(sign*sum<=0) ans2+=labs(sign-sum),sum=sign; sign*=-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; int main(void) { int N; cin >> N; vector<long long> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } long long sum = 0; long long cnt = 0; for (int i = 0; i < N; i++) { if (sum + a[i] == 0) { if (sum < 0) { cnt++; a[i]++; } else if (sum > 0) { cnt++; a[i]--; } else { if (a[i + 1] > 0) a[i]++; else if (a[i + 1] < 0) a[i]--; else a[i]++; cnt++; } } else if (sum < 0 && sum + a[i] < 0) { long long diff = abs(sum + a[i]) + 1; cnt += diff; a[i] += diff; } else if (sum > 0 && sum + a[i] > 0) { long long diff = abs(sum + a[i]) + 1; cnt += diff; a[i] -= diff; } sum += a[i]; } for (int i = 0; i < N; i++) { cerr << a[i] << " "; } cerr << endl; 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; int main() { int N; cin >> N; long long A[N]; for (int i = 0; i < N; i++) cin >> A[i]; bool loop = true; long long delta = 0; while (loop) { int sum[N]; bool sign = (A[0] > 0); sum[0] = A[0]; loop = false; for (int i = 1; i < N; i++) { sum[i] = sum[i - 1] + A[i]; sign = !sign; if (sign && sum[i] <= 0) { long long dd = 1 - sum[i]; delta += abs(dd); A[i] = A[i] + dd; sum[i] = 1; loop = true; } else if (!sign && sum[i] >= 0) { long long dd = -1 - sum[i]; delta += abs(dd); A[i] = A[i] + dd; sum[i] = -1; loop = true; } } } cout << delta << 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 sys input = sys.stdin.readline n=int(input()) a=list(map(int,input().split())) v=[] for s in (1,-1): x=a[::] hugou=[s*(-1)**i sor i in range(n)] total=0 for i in range(n): if (total+x[i])*hugou[i]<=0: x[i]=hugou[i]-total total+=xs[i] v.append(sum(absa[i]-x[i])for i in range(n)) print(min(v))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, count = 0; double a[100001], sum; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf", &a[i]); } if (a[0] == 0) { if (a[1] >= 0) a[0]--; else a[0]++; count++; } sum = a[0]; for (i = 1; i < n; i++) { if (a[i] == 0) { if (sum > 0) { while (sum + a[i] >= 0) { a[i]--; count++; } } else { while (sum + a[i] <= 0) { a[i]++; count++; } } } else if (sum > 0) { if (sum + a[i] >= 0) { count += sum + 1 + a[i]; a[i] = -sum - 1; } } else if (sum < 0) { while (sum + a[i] <= 0) { count += -sum + 1 - a[i]; a[i] = -sum + 1; } } sum += a[i]; } printf("%d\n", count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int ans = 0, sum = 0; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; if (a[0] > 0) { for (int i = 0, tmp = 1; i < N; i++, tmp *= -1) { if (a[i] * a[i + 1] > 0) { sum += a[i]; ans += abs(sum - tmp); sum = tmp; } } sum = 0; } else if (a[0] < 0) { for (int i = 0, tmp = -1; i < N; i++, tmp *= -1) { if (a[i] * a[i + 1] > 0) { sum += a[i]; ans += abs(sum - tmp); sum = tmp; } } } else { a[0] += 1; ans = 1; for (int i = 0, tmp = 1; i < N; i++, tmp *= -1) { if (a[i] * a[i + 1] > 0) { sum += a[i]; ans += abs(sum - tmp); sum = tmp; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int sum = 0, count = 0; cin >> n; vector<int> a(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } sum = a[0]; for (int i = 1; i < n; i++) { if (sum < 0) { sum += a[i]; if (sum < 0) { count += abs(sum) + 1; } continue; } else if (sum > 0) { sum += a[i]; if (sum > 0) { count += abs(sum) + 1; } continue; } } 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; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int main() { int n; cin >> n; int sump = 1; int sumn = -1; int ansp = 0; int ansn = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (sump > 0) { sump += x; if (sump >= 0) { ansp += sump + 1; sump = -1; } } else { sump += x; if (sump <= 0) { ansp += abs(sump) + 1; sump = 1; } } if (sumn > 0) { sumn += x; if (sumn >= 0) { ansn += sumn + 1; sumn = -1; } } else { sumn += x; if (sumn <= 0) { ansn += abs(sumn) + 1; sumn = 1; } } } cout << min(ansn, ansp) - 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
// AtCoder-Template.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include <iostream> #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; #define fst first #define snd second #define FOR(i,N) for(auto i=0; i<N; ++i) #define FORREV(i,N,_cnt) for(auto i=N-1,cnt=_cnt; cnt > 0; --i, --cnt) #define ALL(x) x.begin(), x.end() /* clang-format off */ template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; }; template <class T> struct _vec<T, 0> { using type = T; }; template <class T, size_t D> using vec = typename _vec<T, D>::type; template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); } template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); } template <class T> inline void chmin(T& a, const T& b) { if (b < a) a = b; } template <class T> inline void chmax(T& a, const T& b) { if (b > a) a = b; } /* clang-format on */ int main() { #ifdef DEBUG ifstream ifs("in.txt"); cin.rdbuf(ifs.rdbuf()); #endif ll N; cin >> N; vector<ll> A(N); FOR(i, N) cin >> A[i]; // RUISEKI vector<ll> Rui(N); Rui[0] = A[0]; ll ans = 0; FOR(i, N - 1) { Rui[i + 1] += Rui[i] + A[i + 1]; if (i + 1 >= 1 and Rui[i+1] * Rui[i] >= 0) { if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) { ans += 1; Rui[i + 1] = -1; } else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) { ans += 1; Rui[i + 1] = 1; } else { // 両方とも同じ符号になった場合 if (Rui[i + 1] > 0) { ans += Rui[i + 1] + 1; Rui[i + 1] = -1; } else { ans += - Rui[i + 1] + 1; Rui[i + 1] = 1; } } } } cout << ans << endl; //cout << 0 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) ans1, ans2 = 0, 0 f = a[0] if f <= 0: f = 1 ans1 += 1 - a[0] for i in range(1, N): if f * (f + a[i]) < 0: f += a[i] continue ans1 += abs(f + a[i]) + 1 if f > 0: f = -1 else: f = 1 f = -a[0] if f >= 0: f = -1 ans2 += 1 - a[i] for i in range(1, N): if f * (f + a[i]) < 0: f += a[i] continue ans2 += abs(f + a[i]) + 1 if f > 0: f = -1 else: f = 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<iostream> #include<string> #include<vector> #include<stdio.h> #include<algorithm> #include<math.h> #include<numeric> #include<iomanip> #include<deque> #include<tuple> #include<queue> #include<map> #include <cstdint> #include <boost/multiprecision/cpp_int.hpp> #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define vi vector<int> #define all(x) (x).begin(),(x).end() #define Endl endl #define F first #define S second namespace mp = boost::multiprecision; using cpp_int = mp::cpp_int; using ll = long long; using namespace std; int operation(int n,ll cnt,vector<ll>sum,vector<ll> a) { for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i - 1] > 0) { if (sum[i] >= 0) { cnt += sum[i] + 1; sum[i] = -1; } } else if (sum[i - 1] < 0) { if (sum[i] <= 0) { cnt += abs(sum[i]) + 1; sum[i] = 1; } } } return cnt; } int main() { int n; cin >> n; vector<ll>sum(n),a(n),sum2(n); rep(i, n) { cin >> a[i]; } ll count = 0; ll count2 = 0; sum[0] = a[0]; if (sum[0]>0) { sum2[0] = -1; count2 += sum[0] + 1; } else if (sum[0] < 0) { sum2[0] = 1; count2 += abs(sum[0])+1; } else { sum[0] = 1; count++; sum2[0] = -1; count2++; } cout<<min(operation(n, count, sum, a) ,operation(n, count2, sum2, 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
cpp
#include <bits/stdc++.h> using namespace std; int n; int a[100001]; int b[100001]; long long s[100001]; long long ans = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } s[0] = a[0]; if (s[0] == 0) { a[0] = 1; s[0] = 1; for (int i = 0; i < n - 1; i++) { int m = s[i] + a[i + 1]; if (s[i] * m >= 0) { if (s[i] < 0) { s[i + 1] = 1; int t = a[i + 1]; a[i + 1] = s[i + 1] - s[i]; ans += (a[i + 1] - t); } else if (s[i] > 0) { s[i + 1] = -1; int t = a[i + 1]; a[i + 1] = s[i + 1] - s[i]; ans += (t - a[i + 1]); } } } long long ans2 = 0; a[0] = -1; s[0] = -1; for (int i = 0; i < n - 1; i++) { int m = s[i] + a[i + 1]; if (s[i] * m >= 0) { if (s[i] < 0) { s[i + 1] = 1; int t = a[i + 1]; a[i + 1] = s[i + 1] - s[i]; ans2 += (a[i + 1] - t); } else if (s[i] > 0) { s[i + 1] = -1; int t = a[i + 1]; a[i + 1] = s[i + 1] - s[i]; ans2 += (t - a[i + 1]); } } } cout << min(ans, ans2) << endl; return 0; } else { for (int i = 0; i < n - 1; i++) { int m = s[i] + a[i + 1]; if (s[i] * m >= 0) { if (s[i] < 0) { s[i + 1] = 1; int t = a[i + 1]; a[i + 1] = s[i + 1] - s[i]; ans += (a[i + 1] - t); } else if (s[i] > 0) { s[i + 1] = -1; int t = a[i + 1]; a[i + 1] = s[i + 1] - s[i]; ans += (t - a[i + 1]); } } else { s[i + 1] = s[i] + a[i + 1]; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys from itertools import accumulate read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def solve(A): ans = 0 s = A[0] for a in A[1:]: prev, s = s, s + a if prev > 0 and s >= 0: ans += s + 1 s = -1 if prev < 0 and s <= 0: ans += -s + 1 s = 1 return ans def main(): N, *A = map(int, read().split()) a0 = A[0] ans1 = 0 if a0 <= 0: ans1 = -a0 + 1 A[0] = 1 ans1 = solve(A) A[0] = a0 ans2 = 0 if a0 >= 0: ans2 = a0 + 1 A[0] = -1 ans2 += solve(A) print(min(ans1, ans2)) return 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
python3
n = int(input()) a = list(map(int,input().split())) A = a[:] count_plus=0 count_minus=0 x = 0 y = 0 for i in range(n): if i%2==0: x+=a[i] if x<0: count_plus+=-1*x+1 a[i]+=-1*x+1 x+=-1*x+1 else: x+=a[i] if x>0: count_plus+=x+1 a[i]+=-1*x-1 x+=-1*x-1 for i in range(n): if i%2==0: y+=A[i] if y>0: count_minus+=y+1 A[i]+=-1*y-1 y+=-1*y-1 else: y+=A[i] if y<0: count_minus+=-1*y+1 A[i]+=-1*y+1 y+=-1*y+1 if x==0: count_plus+=1 if y==0: count_minus+=1 print(min(count_plus,count_minus))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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
// ABC 59 C #include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < n; ++i) using namespace std; typedef long long ll; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } const long long INF = 1LL<<60; ll foo(const vector<ll>& a, bool oddPositive) { size_t n = a.size(); vector<ll> b1(n); ll ans = 0; b1[0] = a[0]; if (oddPositive && a[0] <= 0) { b1[0] = 1; ans += abs(a[0] + 1); } if (!oddPositive && a[0] => 0) { b1[0] = -1; ans += abs(a[0] + 1); } for (int i = 1; i < n; i++) { if ((b1[i-1] + a[i]) * b1[i-1] < 0) { b1[i] = b1[i-1] + a[i]; continue; } if (a[i] + b1[i-1] == 0) { ans += 1; } else if (b1[i-1] * a[i] > 0) { // a[i], b[i-1] の符号が同じ ans += abs(a[i]) + abs(b1[i-1]) + 1; } else { // a[i], b[i-1] の符号が逆 ans += abs(b1[i-1]) - abs(a[i]) + 1; } b1[i] = b1[i-1] < 0 ? 1 : -1; } return ans; } int main() { int n; cin >> n; vector<ll> a(n); rep(i, n) { ll tmp = 0; cin >> tmp; a[i] = tmp; } ll ans1 = foo(a, true); // 奇数番目が正の数 ll ans2 = foo(a, false); // 奇数番目が負の数 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
n = int(input()) a = list(map(int, input().split())) count = 0 sum_i = a[0] for i in range(1, n): if sum_i < 0: sum_i += a[i] if sum_i <= 0: count += 1 - sum_i sum_i = 1 elif sum_i > 0: sum_i += a[i] if sum_i >= 0: count += sum_i + 1 sum_i = -1 print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 if a[0] > 0: op = "+" elif a[0] < 0: op = "-" elif a[0] == 0: ans += 1 if a[1] > 0: a[0] = -1 op = "-" else: a[0] = 1 op = "+" total = a[0] for i in range(1, n): if (total+a[i]) > 0 and op == "+": ans += abs((-1 - total) - a[i]) total = -1 op = "-" elif (total+a[i]) < 0 and op == "-": ans += abs((1 - total) - a[i]) total = 1 op = "+" elif total+a[i] == 0: ans += 1 if op == "+": total = -1 op = "-" else: total = 1 op = "+" else: total += a[i] if op == "+": op = "-" else: op = "+" 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; using ll = long long; int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); ll n; cin >> n; vector<ll> a(n); for (ll i = 0; i < n; i++) cin >> a[i]; vector<ll> sums(n + 1, 0); for (ll i = 0; i < n; i++) sums[i] = sums[i] + a[i]; ll ans = 0; for (ll i = 1; i <= n; i++) { if (a[i + 1] * a[i] >= 0) { ans += (a[i + 1] + 2); a[i + 1] -= (a[i + 1] + 2); } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) a_1 = a ans = 0 ans_2 = 0 o = 0 for i in range(n): if i == 0: if a[i] == 0: f = "+" a[i] = 1 elif a[0] > 0: f = "+" elif a[0] < 0: f = "-" else: o += a[i-1] if f == "+": if a[i] + o > 0: c = -1 - o ans += abs(c - a[i]) a[i] = c f = "-" else: if a[i] + o == 0: a[i] -= 1 ans += 1 f = "-" elif f == "-": if a[i] + o < 0: c = 1 - o ans += abs(c - a[i]) a[i] = c f = "+" else: if a[i] + o == 0: a[i] += 1 ans += 1 f = "+" a = a_1 for i in range(n): if i == 0: if a[i] == 0: f = "+" a[i] = 1 elif a[0] > 0: f = "-" c = -1 - a[0] ans_2 += abs(c - a[0]) a[i] = c elif a[0] < 0: c = 1 - a[0] ans_2 += abs(c - a[0]) a[i] = c f = "+" else: o += a[i-1] if f == "+": if a[i] + o > 0: c = -1 - o ans_2 += abs(c - a[i]) a[i] = c f = "-" else: if a[i] + o == 0: a[i] -= 1 ans += 1 f = "-" elif f == "-": if a[i] + o < 0: c = 1 - o ans_2 += abs(c - a[i]) a[i] = c f = "+" else: if a[i] + o == 0: a[i] += 1 ans += 1 f = "+" #print(a) print(min(ans,ans_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\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; vector<long long> cumulative; void reCalc(int index, long long value) { for (int i = index; i < n; ++i) cumulative[i] -= value; } int main() { cin >> n; vector<long long> input; for (int i = 0; i < n; ++i) { long long a; cin >> a; input.push_back(a); } auto itr = input.begin(); for (int i = 0; i < n; ++i) cumulative.push_back(accumulate(itr, itr + i + 1, 0)); int ans = 0; bool isPositive = cumulative[0] > 0; for (int i = 1; i < n; ++i) { if (isPositive) { if (cumulative[i] > 0) { ans += cumulative[i] + 1; reCalc(i, cumulative[i] + 1); } else if (cumulative[i] == 0) { ++ans; reCalc(i, 1); } } else { if (cumulative[i] < 0) { long long diff = (0 - cumulative[i]) + 1; ans += diff; reCalc(i, -diff); } else if (cumulative[i] == 0) { ++ans; reCalc(i, -1); } } isPositive = cumulative[i] > 0; } if (accumulate(cumulative.begin(), cumulative.end(), 0) == 0) cout << 1 << 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 main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long cnt = 0; if (sum == 0) { sum = (a[1] > 0 ? -1 : 1); cnt++; } for (int i = 1; i < n; i++) { long long nsum = sum + a[i]; if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) { sum = nsum; continue; } if (nsum == 0) { sum = (sum > 0 ? -1 : 1); cnt++; } else { if (sum > 0 && nsum > 0) sum = -1; else sum = 1; cnt += abs(nsum) + 1; } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Code3 { static void Main(string[] args) { string s1 = Console.ReadLine(); string s2 = Console.ReadLine(); Console.WriteLine(funcMain(s1,s2)); } static private string funcMain(string arg1, string arg2) { long ret = 0; long ret1 = 0; long ret2 = 0; long sum = 0; short sign = 0; for (int i = 0; i <= 1; i++) // 0はそのまま、1は逆符号 { sum = ret = 0; foreach (string buf in arg2.Split()) { if (sum == 0) { sum = long.Parse(buf); if (sum >= 0) sign = 1; else sign = -1; if (i == 1) { ret += Math.Abs(sum) + 1; sum = sign * -1; sign *= -1; } } else { sum += long.Parse(buf); if ((sum * sign) >= 0) { ret += Math.Abs(sum) + 1; sum = sign * -1; } sign *= -1; } } if (i == 0) ret1 = ret; else ret2 = ret; } ret = Math.Min(ret1, ret2); return ret.ToString(); } // 何のパターンを漏らしているのかがわからぬ・・・・・・ } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; long long A[n]={}; long long ans=0; for(int i=0;i<n;i++){ cin>>A[i]; } for(int i=1;i<n;i++){ if(A[i-1]*A[i]>0){ ans+=abs(A[i-1]+A[i])+1; A[i]=pow(-1,i+1); } else if((A[i-1]+A[i])*A[i]>0){ ans+=abs(A[i]+A[i-1]+1); A[i]=pow(-1,i+1); } else if(A[i-1]+A[i]==0){ ans+=1; A[i]=pow(-1,i+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(void) { int n; cin >> n; long long int a[n], sum = 0, count = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0) { if (a[i + 1] > 0) { a[i]--; count++; } else if (a[i + 1] < 0) { a[i]++; count++; } else if (a[i + 1] == 0) { a[i]++; a[i + 1] -= 2; count += 3; } continue; } else if (sum > 0) { if (sum + a[i + 1] > 0) { while (sum + a[i + 1] >= 0) { a[i + 1]--; count++; } continue; } } else if (sum < 0) { if (sum + a[i + 1] < 0) { while (sum + a[i + 1] <= 0) { a[i + 1]++; count++; } } } } cout << count; 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())) ans1 = 0 ans2 = 0 cur1 = a[0] cur2 = a[0] for i in range(1, n): cun = a[i] # iが偶数のときプラス if i % 2 == 0: # プラスじゃないとき if a[i] + cur1 <= 0: ans1 += -(a[i] + cur1) + 1 cun = -cur1 + 1 # iが奇数のときマイナス else: # マイナスじゃないとき if a[i] + cur1 >= 0: ans1 += a[i] + cur1 + 1 cun = -cur1 - 1 cur1 += cun for i in range(1, n): cun = a[i] # iが偶数のときマイナス if i % 2 == 0: # マイナスじゃないとき if a[i] + cur2 >= 0: ans2 += a[i] + cur2 + 1 cun = -cur2 - 1 # iが奇数のときプラス else: # プラスじゃないとき if a[i] + cur2 <= 0: ans2 += -(a[i] + cur2) + 1 cun = -cur2 + 1 cur2 += cun 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
using System; using static System.Console; using static System.Math; public class Hello{ public static void Main(){ int kazu = int.Parse(ReadLine()); string[] number = ReadLine().Split(' '); long wa = 0; long count1 = 0; long ans = 0; for(int i=0;i<kazu;i++){ long numi = int.Parse(number[i]); long temp = wa + numi; if(i%2==0){ if(temp > 0){ wa = temp; }else{ count1 += Abs(temp) +1; wa = 1; } }else{ if(temp < 0){ wa = temp; }else{ count1 += Abs(temp) +1; wa = -1; } } } long count2 = 0; for(int i=0;i<kazu;i++){ long numi = int.Parse(number[i]); long temp = wa + numi; if(i%2==1){ if(temp > 0){ wa = temp; }else{ count2 += Abs(temp) +1; wa = 1; } }else{ if(temp < 0){ wa = temp; }else{ count2 += Abs(temp) +1; wa = -1; } } } WriteLine(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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<long long> A; int j; bool is_plus; long long ans = 0; long long sum = 0; cin >> n; for (int i = 0; i < n; i++) { long long a; cin >> a; A.push_back(a); } for (int i = 0; i < n; i++) { if (!i) { if (A[i] == 0) { if (A[i + 1] >= 0) { sum = -1; } else { sum = 1; } ans++; } else { sum = A[i]; } continue; } bool is_plus = sum > 0; sum += A[i]; if (sum == 0) { ans += 1; sum = is_plus ? -1 : 1; } else if (is_plus == (sum > 0)) { ans += abs(sum) + 1; sum = is_plus ? -1 : 1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np N = int(input()) a_s = input().split() for i in range(N): a_s[i] = int(a_s[i]) a_s = np.array(a_s) #sum = 1,-1,1, ... ans1 = 0 sum0 = 0 for i ,a in enumerate(a_s): sum1 = sum0 + a if i==0: if sum1>0: pass else: ans1 += abs(1 - sum1) sum1 = 1 elif i%2==0: if sum1*sum0<0: pass else: ans1 += abs(1 - sum1) sum1 = 1 else: if sum1*sum0<0: pass else: ans1 += abs(-1 - sum1) sum1 = -1 sum0 = sum1 #sum = -1,1,-1, ... ans2 = 0 sum0 = 0 for i ,a in enumerate(a_s): sum2 = sum0 + a if i==0: if sum2<0: pass else: ans2 += abs(-1 - sum2) sum2 = -1 if i%2==0: if sum2*sum0<0: pass else: ans2 += abs(-1 - sum2) sum2 = -1 else: if sum2*sum0<0: pass else: ans2 += abs(1 - sum2) sum2 = 1 sum0 = sum2 ans = min(ans1,ans2) 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
python3
n = int(input()) a = list(map(int,input().split())) cnt = 0 sum = [0]*n sum[0] = a[0] for i in range(1,n): sum[i] = sum[i-1]+a[i] if sum[i]*sum[i-1]<0: continue if sum[i-1]*a[i]<0: cnt += abs(sum[i-1])-abs(a[i])+1 sum[i] = -sum[i-1]//abs(sum[i-1]) else: cnt += abs(sum[i-1])+abs(a[i])+1 sum[i] = -sum[i-1]//abs(sum[i-1]) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] s0 = a[0] count=0 if a[0]==0: s0+=1 count+=1 for i in range(1,n): s1 = s0+a[i] if s0*s1>=0: if s1>0: a[i]-=(s1+1) count+=(s1+1) elif s1<0: a[i]+=(s1+1) count+=(s1+1) elif s1==0: if s0>0: a[i]-=1 count+=1 elif s0<0: a[i]+=1 count+=1 s0 += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); long long sum = 0; long long tmp = 0; long long ans = 0; int sign = 1; int start = 0; for (int i = 0; i < n; i++) { cin >> a.at(i); } if (a.at(0) == 0) { for (int i = 0; i < n; i++) { if (a.at(i) != 0) { start = i; sign = -a.at(i) / abs(a.at(i)); ans += i; break; } } } else { sign = -a.at(0) / abs(a.at(0)); } for (int i = start; i < n; i++) { sum += a.at(i); if (sum == 0) { ans += 1; sum = -1 * sign; } else if (sign == sum / abs(sum)) { ans += abs(-1 * sign - sum); sum = -1 * sign; } sign *= -1; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int ans1 = 0; int sum = 0; for (int i = 0; i < n; i++) { int cur = a[i]; if (i % 2) { if (sum + cur >= 0) cur = -(sum + 1); } else { if (sum + cur <= 0) cur = -sum + 1; } sum += cur; ans1 += abs(a[i] - cur); } int ans2 = 0; sum = 0; for (int i = 0; i < n; i++) { int cur = a[i]; if (i % 2 == 0) { if (sum + cur >= 0) cur = -(sum + 1); } else { if (sum + cur <= 0) cur = -sum + 1; } sum += cur; ans2 += abs(a[i] - cur); } 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
n = int(input()) a = [int(i) for i in input().split()] s = [] sign = [] debug_total = [0 for i in range(n)] ans = 0 if a[0]==0: if a[1]<0: a[0]=1 else: a[0]=-1 ans+=1 if a[0]<0: sign.append(-1) else: sign.append(1) total = a[0] debug_total[0]=total op = 0 #print('total_in: ',total) #print('ans_in: ',ans) for i in range(1,n): total+=a[i] if total==0: sign.append(sign[i-1]*-1) total += sign[i-1]*-1 a[i] += sign[i-1]*-1 ans += 1 elif total<0: sign.append(-1) else: sign.append(1) if sign[i]==sign[i-1]: op = abs(total)+1 if total+op==0 or total-op==0: op+=1 if sign[i]==1: total -= op a[i] -= op else: total += op a[i] += op ans += op sign[i] *= -1 debug_total[i] = total #print('==========================') #print('i: ',i) #print('total: ',total) #print('ans: ',ans) #print('a: ',a) #print('total_list: ', debug_total) 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
python3
import copy n = int(input()) a = [int(i) for i in input().split()] b=a.copy() s0p = a[0] s0n = b[0] countp = 0 countn = 0 if a.count(0)==n: print(2*n-1) exit() if s0p<=0: s0p+=(abs(s0p)+1) countp+=abs(s0p) print(countp,abs(s0p)+1,s0p,a[0]) if s0n>=0: s0n-=(abs(s0n)+1) countn+=abs(s0n) for i in range(1,n): s1 = s0p+a[i] if s0p*s1>=0: if s1>0: a[i]-=(abs(s1)+1) countp+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) countp+=(abs(s1)+1) elif s1==0: if s0p>0: a[i]-=1 countp+=1 elif s0p<0: a[i]+=1 countp+=1 s0p += a[i] for i in range(1,n): s1 = s0n+b[i] if s0n*s1>=0: if s1>0: b[i]-=(abs(s1)+1) countn+=(abs(s1)+1) elif s1<0: b[i]+=(abs(s1)+1) countn+=(abs(s1)+1) elif s1==0: if s0n>0: b[i]-=1 countn+=1 elif s0n<0: b[i]+=1 countn+=1 s0n += b[i] print(countp if countp<=countn else(countn))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) count=0 tmp=A[1]-A[0] if tmp==0: count+=abs(-1-(A[1]+A[0])) tmp=-1 for i in range(2,n): if tmp<0: if tmp+A[i]<=0: count+= abs(1-(A[i]+tmp)) tmp=1 else: tmp+=A[i] continue else: if tmp+A[i]>=0: count+=abs(-1-(A[i]+tmp)) tmp=-1 else: tmp+=A[i] continue print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; ++i) { A[i] = sc.nextInt(); } sc.close(); int sum1 = 0; int sum2 = 0; int ans1 = 0; int ans2 = 0; for (int i = 0; i < N; ++i) { sum1 += A[i]; if (i % 2 == 0 && sum1 >= 0) { ans1 += sum1 +1; sum1 = -1; } else if (i % 2 != 0 && sum1 <= 0) { ans1 += Math.abs(sum1) + 1; sum1 = 1; } } for (int i = 0; i < N; ++i) { sum2 += A[i]; if (i % 2 == 0 && sum2 <= 0) { ans2 += Math.abs(sum1) +1; sum2 = 1; } else if (i % 2 != 0 && sum2 >= 0) { ans2 += sum2 + 1; sum2 = -1; } } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { long long n; cin >> n; vector<long long> a(n); vector<long long> sum(n, 0); for (long long i = 0; i < (n); i++) { cin >> a[i]; if (i == 0) sum[i] = a[i]; else sum[i] = a[i] + sum[i - 1]; } long long minus = 0; long long plus = 0; long long s = 0; for (long long i = 0; i < (n); i++) { bool p = i % 2 == 1; bool q = sum[i] + s > 0; if (p != q) { if (sum[i] + s == 0) { minus++; if (p) s++; else s--; } else { minus += abs(sum[i] + s) + 1; if (p) s += abs(sum[i] + s) + 1; else s -= abs(sum[i] + s) + 1; } } } s = 0; for (long long i = 0; i < (n); i++) { bool p = i % 2 == 0; bool q = sum[i] + s > 0; if (p != q) { if (sum[i] + s == 0) { plus++; if (p) s++; else s--; } else { plus += abs(sum[i] + s) + 1; if (p) s += abs(sum[i] + s) + 1; else s -= abs(sum[i] + s) + 1; } } } cout << min(minus, plus) << 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> const double PI = acos(-1); const double EPS = 1e-15; long long INF = (long long)1E17; long mod(long a) { long long c = a % (long long)(1E9 + 7); if (c >= 0) return c; return c + (long long)(1E9 + 7); } using namespace std; bool prime_(int n) { if (n == 1) { return false; } else if (n == 2) { return true; } else { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } } long long gcd_(long long a, long long b) { if (a < b) { swap(a, b); } if (a % b == 0) { return b; } else { return gcd_(b, a % b); } } long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; } class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } int size(int A) { return -Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long s = 0; long long ans = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { s += a[i]; if (s > 0) continue; ans += 1 - s; s = 1; } else { s += a[i]; if (s < 0) continue; ans += s + 1; s = -1; } } s = 0; long long temp = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { s += a[i]; if (s < 0) continue; temp += s + 1; s = -1; } else { s += a[i]; if (s > 0) continue; temp += 1 - s; s = 1; } } ans = min(ans, temp); 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 func(long long a[], int n) { long long cnt = 0; long long s = 0; for (int i = 1; i < n; i++) { s += a[i - 1]; long long t = 0, u; if (s > 0) { u = (-1) * s - 1; if (u < a[i]) { t = a[i] - u; a[i] = u; } } else { u = (-1) * s + 1; if (u > a[i]) { t = u - a[i]; a[i] = u; } } cnt += t; } return cnt; } int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < (n); i++) cin >> a[i]; long long cnt1 = func(a, n); int d; if (a[0] > 0) { d = a[0] + 1; a[0] = -1; } else { d = (-1) * a[0] + 1; a[0] = 1; } long long cnt2 = func(a, n); cout << min(cnt1, cnt1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] a_ = new int[n]; for(int i = 0; i < n; i++){ a_[i] = scan.nextInt(); } int count = 0; int[] sum_ = new int[n]; //i % 2 == 1 : sum_[i] <= -1 if(a_[0] > 0){ sum_[0] = a_[0]; for(int i = 1; i < n;){ sum_[i] = sum_[i-1] + a_[i]; if(i % 2 != 0){ if(sum_[i] < 0){ //OK i++; }else{ if(sum_[i-1] > 1){ sum_[i-1]--; }else{ a_[i]--; } count++; } }else{ if(sum_[i] > 0){ //OK i++; }else{ if(sum_[i-1] < -1){ sum_[i-1]++; }else{ a_[i]++; } count++; } }if(count == 10)break; } }else{ sum_[0] = a_[0]; for(int i = 1; i < n;){ sum_[i] = sum_[i-1] + a_[i]; if(i % 2 != 0){ if(sum_[i] > 0){ //OK i++; }else{ if(sum_[i-1] < -1){ sum_[i-1]++; }else{ a_[i]++; } count++; } }else{ if(sum_[i] < 0){ //OK i++; }else{ if(sum_[i-1] > 1){ sum_[i-1]--; }else{ a_[i]--; } count++; } } } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; int a, now, res = 0; cin >> now; for (int i = 0; i < N - 1; ++i) { cin >> a; if (now * (now + a) >= 0) { res += abs(a + now) + 1; if (now > 0) { now = -1; } else { now = 1; } } else { now += a; } } 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
java
import java.util.*; // ABC 6-C // http://abc006.contest.atcoder.jp/tasks/abc006_3 public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); long sum = 0; long answer = 0; for (int i = 0; i < n; i++) { int a = in.nextInt(); // if (i > 0) { if (sum < 0 && sum + a < 0) { answer += 1 + Math.abs(sum + a); sum = 1; } else if (sum > 0 && sum + a > 0) { answer += 1 + sum + a; sum = -1; } else if (sum + a == 0) { answer++; if (sum < 0) { sum = 1; } else { sum = -1; } } else { sum += a; } // } else { // sum += a; // } } System.out.println(answer); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using i_i = pair<int, int>; using ll_ll = pair<ll, ll>; using d_ll = pair<double, ll>; using ll_d = pair<ll, double>; using d_d = pair<double, double>; template <class T> using vec = vector<T>; static constexpr ll LL_INF = 1LL << 60; static constexpr int I_INF = 1 << 28; static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288); static constexpr double EPS = numeric_limits<double>::epsilon(); static map<type_index, const char* const> scanType = {{typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}}; template <class T> static void scan(vector<T>& v); [[maybe_unused]] static void scan(vector<string>& v, bool isWord = true); template <class T> static inline bool chmax(T& a, T b); template <class T> static inline bool chmin(T& a, T b); template <class T> static inline T gcd(T a, T b); template <class T> static inline T lcm(T a, T b); template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val); template <class T> T mod(T a, T m); template <class Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid& x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid& x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int& k) const { return seg[k + sz]; } template <class C> int find_subtree(int a, const C& check, Monoid& M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <class C> int find_first(int a, const C& check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <class C> int find_last(int b, const C& check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int main(int argc, char* argv[]) { ll n; cin >> n; vec<ll> a(n); scan(a); SegmentTree<ll> seg( n, [](ll x, ll y) { return x + y; }, 0LL); for (int i = (0); i < (n); i++) { seg.set(i, a[i]); } seg.build(); ll ans = 0; bool next_sign = (a[0] < 0) ? true : false; if (a[0] == 0) { seg.update(0, 1LL); ans++; } for (int i = (2); i < (n + 1); i++) { ll sum = seg.query(0, i); if ((next_sign && sum > 0) || (!next_sign && sum < 0)) { next_sign = !next_sign; continue; } ll to = (next_sign) ? 1 : -1; ll diff = abs(sum - to); ans += diff; seg.update(i - 1, seg[i - 1] + (to - sum)); next_sign = !next_sign; } ll ans2 = 0; for (int i = (0); i < (n); i++) { seg.update(i, a[i]); } next_sign = (a[0] < 0) ? true : false; if (a[0] == 0) { seg.update(0, -1LL); ans++; } for (int i = (2); i < (n + 1); i++) { ll sum = seg.query(0, i); if ((next_sign && sum > 0) || (!next_sign && sum < 0)) { next_sign = !next_sign; continue; } ll to = (next_sign) ? 1 : -1; ll diff = abs(sum - to); ans2 += diff; seg.update(i - 1, seg[i - 1] + (to - sum)); next_sign = !next_sign; } ((cout) << (min(ans, ans2)) << (endl)); return 0; } template <class T> static void scan(vector<T>& v) { auto tFormat = scanType[typeid(T)]; for (T& n : v) { scanf(tFormat, &n); } } static void scan(vector<string>& v, bool isWord) { if (isWord) { for (auto& n : v) { cin >> n; } return; } int i = 0, size = v.size(); string s; getline(cin, s); if (s.size() != 0) { i++; v[0] = s; } for (; i < size; ++i) { getline(cin, v[i]); } } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T gcd(T a, T b) { return __gcd(a, b); } template <class T> inline T lcm(T a, T b) { T c = min(a, b), d = max(a, b); return c * (d / gcd(c, d)); } template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) { std::fill((T*)arr, (T*)(arr + N), val); } template <class T> T mod(T a, T m) { return (a % m + m) % m; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long Num[1111111]; long long Sum[1111111]; long long Out; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &Num[i]); if (Num[1] == 0) Num[1] = 1, Out++; if (Num[1] > 0) { for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 0) { if (Sum[i] >= 0) { Out += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out += (1 - Sum[i]); Sum[i] = 1; } } } } else { for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 1) { if (Sum[i] >= 0) { Out += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out += (1 - Sum[i]); Sum[i] = 1; } } } } printf("%lld\n", Out); 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 long long int INF = 1e9; using namespace std; struct Point { int x, y; }; bool vector_finder(std::vector<int> vec, int number) { auto itr = std::find(vec.begin(), vec.end(), number); size_t index = std::distance(vec.begin(), itr); if (index != vec.size()) { return true; } else { return false; } } long long int factorial(long long int N) { long long int ans = 1; for (long long int i = 1; i <= (long long int)(N); i++) { ans *= i; } return ans; } vector<long long int> Eratosthenes(long long int N) { bool arr[N + 1]; arr[0] = false; arr[1] = false; for (long long int i = 2; i < N + 1; i++) { arr[i] = true; } for (long long int i = 2; i <= sqrt(N); i++) { if (arr[i]) { for (long long int j = 0; i * (j + 2) <= N; j++) { arr[i * (j + 2)] = false; } } } vector<long long int> prime; for (long long int i = 1; i <= (long long int)(N); i++) { if (arr[i] == true) { prime.push_back(i); } } return prime; } char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int main() { long long int n; cin >> n; long long int sum[n + 2]; long long int A[n + 2]; for (long long int i = 1; i <= (long long int)(n); i++) { cin >> A[i]; } sum[0] = 0; sum[1] = A[1]; long long int cnt = 0; if (A[1] > 0) { for (long long int i = 2; i <= n; i++) { sum[i] = sum[i - 1] + A[i]; if (i % 2 == 0) { if (sum[i] > -1) { long long int new_sum = -1; cnt += abs(new_sum - sum[i]); sum[i] = new_sum; } } else { if (sum[i] < 1) { long long int new_sum = 1; cnt += abs(new_sum - sum[i]); sum[i] = new_sum; } } } } else { for (long long int i = 2; i <= n; i++) { sum[i] = sum[i - 1] + A[i]; if (i % 2 == 1) { if (sum[i] > -1) { long long int new_sum = -1; cnt += abs(new_sum - sum[i]); sum[i] = new_sum; } } else { if (sum[i] < 1) { long long int new_sum = 1; cnt += abs(new_sum - sum[i]); sum[i] = new_sum; } } } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] def pura(a): A = a[:] ans = 0 check = 0 if A[0] <= 0: ans += abs(1-A[0]) A[0] = 1 check += A[0] for i in range(1, N): if i % 2 != 0: if check + A[i] >= 0: ans += abs(A[i] + 1 + check) A[i] = -1 + check else: if check + A[i] <= 0: ans += abs(A[i] - (1 + check)) A[i] = 1 - check check += A[i] return ans def mai(a): A = a[:] ans = 0 check = 0 if A[0] >= 1: ans +=1+abs(A[0]) A[0] = -1 check += A[0] for i in range(1, N): if i % 2 != 0: if check + A[i] <= 0: ans += abs(A[i] - (1 + check)) A[i] = 1 - check else: if check + A[i] >= 0: ans += abs(A[i] + 1 + check) A[i] = -1 + check check += A[i] return ans print(min(pura(a), mai(a)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int *a; int ans = 0; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0; int opr1 = 0, opr2 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum > 0) continue; opr1 += abs(sum) + 1; sum = 1; } else { if (sum < 0) continue; opr1 += abs(sum) + 1; sum = -1; } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum < 0) continue; opr2 += abs(sum) + 1; sum = -1; } else { if (sum > 0) continue; opr2 += abs(sum) + 1; sum = 1; } } cout << opr1 << " " << opr2 << endl; ans = min(opr1, opr2); cout << ans << endl; delete (a); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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) { k_ans += abs(sum) + 1; sum += k_ans; } flg = false; } else { if (sum >= 0) { k_ans += abs(sum) + 1; 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(sum) + 1; sum += g_ans; } flg = true; } else { if (sum >= 0) { g_ans += abs(sum) + 1; 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; int main() { int n; cin >> n; long long a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long cnt1 = 0; long long sum = a[0]; if (sum == 0) { sum = 1; cnt1++; } for (int i = 1; i < n; i++) { long long sumNext = sum + a[i]; if (sumNext * sum >= 0) { if (sum < 0) { cnt1 += 1 - sumNext; sumNext = 1; } else { cnt1 += 1 + sumNext; sumNext = -1; } } sum = sumNext; } long long cnt2 = 0; sum = a[0]; if (sum == 0) { sum = -1; cnt2++; } for (int i = 1; i < n; i++) { long long sumNext = sum + a[i]; if (sumNext * sum >= 0) { if (sum < 0) { cnt2 += 1 - sumNext; sumNext = 1; } else { cnt2 += 1 + sumNext; sumNext = -1; } } sum = sumNext; } 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
n = int(input()) a = list(map(int, input().split())) prv_total =0 cnt = 0 if a[0] == 0: if a[1]>= 0: a[0] = -1 else: a[0] = -1 cnt += 1 for i in range(n-1): total = prv_total + a[i] nxt_total = total+a[i+1] if total == 0 and i != 0: if prv_total > 0: cnt += 1 total -= 1 nxt_total -= 1 else: cnt += 1 total += 1 nxt_total += 1 if total > 0 and nxt_total >= 0: a[i+1] -= nxt_total+1 cnt += nxt_total+1 nxt_total -= nxt_total+1 elif total < 0 and nxt_total <=0: a[i+1] += abs(nxt_total)+1 cnt += abs(nxt_total)+1 nxt_total += abs(nxt_total)+1 prv_total = total total = prv_total + a[-1] if total == 0: cnt += 1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = scan.nextLong(); } long sum1[] = new long[n]; long sum2[] = new long[n]; sum1[0] = a[0]; sum2[0] = a[0]; long ans1 = 0; long ans2 = 0; for (int i = 1; i < n; i++) {//偶数添字が正 sum1[i] = sum1[i-1]+a[i]; if (i%2 == 0) { if (sum1[i] > 0) continue; else { ans1 += (1 + Math.abs(sum1[i])); sum1[i] = 1; } } else if (i%2 == 1) { if (sum1[i] < 0) continue; else { ans1 += (sum1[i] + 1); sum1[i] = -1; } } } for (int i = 1; i < n; i++) {//奇数添字が正 sum2[i] = sum2[i-1]+a[i]; if (i%2 == 1) { if (sum2[i] > 0) continue; else { ans2 += (1 + Math.abs(sum2[i])); sum2[i] = 1; } } else if (i%2 == 0) { if (sum2[i] < 0) continue; else { ans2 += (sum2[i] + 1); sum2[i] = -1; } } } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long res1 = 0, res2 = 0, res3, res4; long long som = v[0]; if (som > 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = v[0] + 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else if (som < 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = 1 - v[0]; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } if (v[0] == 0) { res1 = 1; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } res2 = 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long int abso(long int a) { if (a < 0) return -a; else return a; } bool isDifabso(long int a, long int b) { if (a * b < 0) return true; return false; } int main() { int n; long int sum, tmp, ttmp, ans = 0; cin >> n; if (n == 0) { cout << 0 << endl; return 0; } cin >> sum; for (int i = 1; i < n; i++) { cin >> tmp; if (!isDifabso(sum, sum + tmp)) { ttmp = tmp; if (sum < 0) tmp = abso(tmp) + 1; else if (sum > 0) tmp = -(abso(tmp) + 1); ans += abso(tmp - ttmp); } else if (sum + tmp == 0) { if (sum < 0) tmp++; if (sum > 0) tmp--; ans++; } sum += 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
java
import java.util.Arrays; import java.util.Scanner; public class Main { static final Scanner sc = new Scanner(System.in); static final int MOD = (int) 1E9 + 7; static final long INF_L = (long) 4E18; public static void main(String[] args) { int n = nint(); int[] a = nints(n); boolean a0was0 = a[0] == 0; if (a0was0) { a[0] = 1; } long ans = solve(a); a[0] = -a[0]; ans = min(solve(a) + 2*abs(a[0]), ans); if (a0was0) ans++; System.out.println(ans); } static long solve(int[] a) { if (a[0] == 0) throw new AssertionError(); int n = a.length; long currentSum = a[0]; long count = 0; for (int i = 1; i < n; i++) { long nextSumTemp = currentSum + a[i]; if (nextSumTemp == 0) { nextSumTemp = (currentSum > 0) ? -1 : 1; // いま正なら負になるように操作する count++; } else if (currentSum > 0 == nextSumTemp > 0) { nextSumTemp = currentSum > 0 ? -1 : 1; count += abs(-a[i] + nextSumTemp - currentSum); } currentSum = nextSumTemp; } return count; } @Deprecated static int nint() { return sc.nextInt(); } @Deprecated static int[] nints(int N) { return nints(N, 0, 0); } @Deprecated private static int[] nints(int N, int padL, int padR) { int[] a = new int[padL + N + padR]; for (int i = 0; i < N; i++) a[padL + i] = nint(); return a; } static long nlong() { return sc.nextLong(); } static long[] nlongs(int N) { return nlongs(N, 0, 0); } static long[] nlongs(int N, int padL, int padR) { long[] a = new long[padL + N + padR]; for (int i = 0; i < N; i++) a[padL + i] = nlong(); return a; } static double ndouble() { return sc.nextDouble(); } static double[] ndoubles(int N) { return ndoubles(N, 0, 0); } static double[] ndoubles(int N, int padL, int padR) { double[] d = new double[N + padL + padR]; for (int i = 0; i < N; i++) { d[padL + i] = ndouble(); } return d; } static String nstr() { return sc.next(); } static char[] nchars() { return sc.next().toCharArray(); } static char[] nchars(int padL, int padR) { char[] temp = sc.next().toCharArray(); char[] ret = new char[temp.length + padL + padR]; System.arraycopy(temp, 0, ret, padL, temp.length); return ret; } static char[][] nchars2(int H, int W) { return nchars2(H, W, 0, 0); } static char[][] nchars2(int H, int W, int padLU, int padRD) { char[][] a2 = new char[H + padLU + padRD][W + padLU + padRD]; for (int i = 0; i < H; i++) System.arraycopy(nchars(), 0, a2[padLU + i], padLU, W); return a2; } static long min(long... ls) { return Arrays.stream(ls).min().getAsLong(); } static long max(long... ls) { return Arrays.stream(ls).max().getAsLong(); } static long abs(long a) { return Math.abs(a); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a.at(i); bool fla = false; for (int i = 0; i < N; i++) { if (a.at(i) != 0) { if ((a.at(i) > 0) && (i % 2 == 0)) fla = true; else if (i % 2 == 1) fla = true; break; } } int t = 0, res1 = 0, res2 = 0; for (int i = 0; i < N; i++) { int b = a.at(i); if (fla) { if (t + b <= 0) { b = t * -1 + 1; res1 += b - a.at(i); } } else { if (t + b >= 0) { b = t * -1 - 1; res1 += abs(b - a.at(i)); } } t += b; fla = !fla; } for (int i = 0; i < N; i++) { int b = a.at(i); if (!fla) { if (t + b <= 0) { b = t * -1 + 1; res2 += b - a.at(i); } } else { if (t + b >= 0) { b = t * -1 - 1; res2 += abs(b - a.at(i)); } } t += b; fla = !fla; } int res = min(res1, res2); cout << res << endl; }