Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = (1e+9) + 7; const long long INF = 2e+9 + 10; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long res1 = 0, sum1 = 0; for (int i = 0, sign = 1; i < n; i++, sign *= -1) { sum1 += a[i]; if (sign == 1) while (sum1 <= 0) { sum1++; res1++; } else while (sum1 >= 0) { sum1--; res1++; } } long long res2 = 0, sum2 = 0; for (int i = 0, sign = -1; i < n; i++, sign *= -1) { sum2 += a[i]; if (sign == 1) while (sum2 <= 0) { sum2++; res2++; } else while (sum2 >= 0) { sum2--; res2++; } } int res = min(res1, res2); cout << res << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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(); } int[] sum = new int[n+1]; for (int i = 1; i <= n; i++) { sum[i] = sum[i-1] + a[i-1]; } int count1 = count(n, true, sum, 0 ); int count2 = count(n, false, sum, 0); System.out.println(Math.min(count1, count2)); } private static int count(int n , boolean pastPlus, int[] sum, long carry){ int count2 = 0; for (int i = 1; i <= n; i++) { long cur = sum[i] + carry; if (pastPlus && cur >= 0) { // minus nisinaito count2 += cur + 1; carry = - cur - 1; } if (!pastPlus && cur <= 0) { count2 += -cur + 1; carry = -cur + 1; } pastPlus = !pastPlus; } return 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() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; long long sum1 = 0, sum2 = 0; long long ans1 = 0, ans2 = 0; for (int i = 0; i < N; ++i) { long long t; cin >> t; if (i == 0) { sum1 = t; sum2 = t > 0 ? -1 : 1; ans2 += t + 1; } else { if (sum1 < 0 && sum1 + t <= 0) { ans1 += 1 - sum1 - t; sum1 = 1; } else if (sum1 > 0 && sum1 + t >= 0) { ans1 += abs(-1 - sum1 - t); sum1 = -1; } else { sum1 += t; } if (sum2 < 0 && sum2 + t <= 0) { ans2 += 1 - sum2 - t; sum2 = 1; } else if (sum2 > 0 && sum2 + t >= 0) { ans2 += abs(-1 - sum2 - t); sum2 = -1; } else { sum2 += t; } } } cout << min(ans1, ans2) << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long N, ans, sum; vector<int> a; void calc() { for (int i = 0; i < (N - 1); i++) { sum += a[i]; if (!(signbit(sum) ^ signbit(sum + a[i + 1]))) { ans += abs(sum + a[i + 1]) + 1; if (sum < 0) a[i + 1] = 1; else a[i + 1] = -1; } } } void solve() { cin >> N; a.resize(N); for (int i = 0; i < (N); i++) cin >> a[i]; if (a[0] == 0) { ans = 1; a[0] = 1; calc(); a[0] = -1; calc(); cout << ans << endl; return; } calc(); cout << ans << endl; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); solve(); 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); cin >> a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; a[i] += a[i - 1]; } long long ans1 = 0, def1 = 0; if (a[0] == 0) { ans1++; def1++; } for (int i = 1; i < n; i++) { if (i % 2 == 0 && a[i] + def1 <= 0) { ans1 += 1 - (a[i] + def1); def1 += 1 - (a[i] + def1); } else if (i % 2 == 1 && a[i] + def1 >= 0) { ans1 += a[i] + def1 + 1; def1 -= a[i] + def1 + 1; } } long long ans2 = 0, def2 = 0; if (a[0] == 0) { ans2++; def2++; } for (int i = 1; i < n; i++) { if (i % 2 == 1 && a[i] + def2 <= 0) { ans2 += 1 - (a[i] + def2); def2 += 1 - (a[i] + def2); } else if (i % 2 == 0 && a[i] + def2 >= 0) { ans2 += a[i] + def2 + 1; def2 -= a[i] + def2 + 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() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } int ans0 = 0; { int sum0 = 0; for (int i = 0; i < N; ++i) { sum0 += A[i]; if (0 == i % 2) { if (sum0 <= 0) { ans0 += (1 - sum0); sum0 = 1; } } else { if (sum0 >= 0) { ans0 += (sum0 - (-1)); sum0 = -1; } } } } int ans1 = 0; { int sum1 = 0; for (int i = 0; i < N; ++i) { sum1 += A[i]; if (1 == i % 2) { if (sum1 <= 0) { ans1 += (1 - sum1); sum1 = 1; } } else { if (sum1 >= 0) { ans1 += (sum1 - (-1)); sum1 = -1; } } } } cout << min(ans0, ans1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, a, s = 0, count1 = 0, count2 = 0, i, news; cin >> n; vector<int> as(n); for (int i = 0; i < n; i++) { cin >> a; s += a; as[i] = s; } i = 0; s = 0; while (i < n) { news = as[i] + s; if (news > -1) { s -= news + 1; count1 += news + 1; } if (++i >= n) break; news = as[i] + s; if (news < 1) { s += 1 - news; count1 += 1 - news; } i++; } i = 0; s = 0; while (i < n) { news = as[i] + s; if (news < 1) { s += 1 - news; count2 += 1 - news; } if (++i >= n) break; news = as[i] + s; if (news > -1) { s -= news + 1; count2 += news + 1; } i++; } cout << (count1 < count2 ? count1 : count2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; int main() { int n; cin >> n; int a[100010]; for (int i = (0); i < (int)n; i++) { cin >> a[i]; } long long ans = 0; long long sum = a[0]; for (int i = (1); i < (int)n; i++) { long long tmp; tmp = sum; sum += a[i]; if (sum >= 0 && tmp > 0) { ans += sum + 1; sum = -1; } else if (sum <= 0 && tmp < 0) { ans += -sum + 1; sum = 1; } } cout << (ans) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, sum = 0, tmp = 0, c = 0, tmp2 = 0, c2 = 0; bool chg = false; cin >> n; vector<long long> a(n); vector<long long> b(n); for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; b[i] = sum; } vector<long long> bb(b); for (int i = 0; i < n; i++) { b[i] += tmp; if (i % 2 == 0 && b[i] <= 0) { tmp = abs(b[i]) + 1; chg = true; } if (i % 2 == 1 && b[i] >= 0) { tmp = -(abs(b[i]) + 1); chg = true; } if (chg) c += abs(tmp); chg = false; } chg = false; for (int i = 0; i < n; i++) { bb[i] += tmp2; if (i % 2 == 0 && bb[i] >= 0) { tmp2 = -(abs(bb[i]) + 1); chg = true; } if (i % 2 == 1 && bb[i] <= 0) { tmp2 = abs(bb[i]) + 1; chg = true; } if (chg) c2 += abs(tmp2); chg = false; } cout << min(c, 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
UNKNOWN
import std.stdio, std.algorithm, std.conv, std.array, std.string; long check(long op, long sum, long[] as) { foreach (a; as) { if (sum < 0) { if ((sum + a) <= 0) { op += (1 - (sum + a)); sum = 1; } else { sum += a; } } else { if ((sum + a) >= 0) { op += sum + a + 1; sum = -1; } else { sum += a; } } } return op; } void main() { readln; auto as = readln.chomp.split(" ").map!(to!long).array; auto op1 = check(0, as[0], as[1..$]); auto op2 = check(as[0] < 0 ? 1 - as[0] : as[0] - 1, 1, as[1..$]); auto op3 = check(as[0] < 0 ? -as[0] - 1 : 1 + as[0], -1, as[1..$]); writeln(min(op1, op2, op3)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python2
import sys from collections import deque import copy import math def get_read_func(fileobject): if fileobject == None : return raw_input else: return fileobject.readline def calc(A, N): s = 0L count = 0 pre_sign = 0 for i in range(N): s += A[i] if s == 0: if pre_sign == 1: s -= 1L elif pre_sign == -1: s += 1L count += 1 elif pre_sign == s/abs(s): if s < 0L: ope = 1L - s else: ope = -1L - s s += ope count += abs(ope) pre_sign = s/abs(s) return count def main(): if len(sys.argv) > 1: f = open(sys.argv[1]) else: f = None read_func = get_read_func(f); input_raw = read_func().strip().split() [N] = [int(input_raw[0])] input_raw = read_func().strip().split() A = [int(input_raw[i]) for i in range(N)] s = 0 count = 0 pre_sign = 0 if A[0] != 0: count = calc(A, N) else: A[0] = -1 count_minus =calc(A, N) + 1 A[0] = 1 count_plus =calc(A, N) + 1 count = min(count_minus, count_plus) print count 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()) L = list(map(int,input().split())) accum = 0 cnt = 0 for i in range(N): accum += L[i] if i % 2 == 0 and accum <= 0: cnt += 1 - accum accum += cnt if i % 2 == 1 and accum >= 0: cnt += accum - (-1) accum -= cnt ans = cnt accum = 0 cnt = 0 for i in range(N): accum += L[i] if i % 2 == 1 and accum <= 0: cnt += 1 - accum accum += cnt if i % 2 == 0 and accum >= 0: cnt += accum - (-1) accum -= cnt ans = min(ans,cnt) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int t; if (a[0] >= 0) t = 1; else t = -1; long long sum = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { long long sum2 = sum + a[i]; if (sum > 0 && sum2 > 0) { ans += sum2 + 1; sum = -1; } else if (sum < 0 && sum2 < 0) { ans += -sum2 + 1; sum = 1; } else if (sum2 == 0) { ans++; if (sum < 0) sum = 1; else sum = -1; } else sum = sum2; } 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
UNKNOWN
#include <bits/stdc++.h> int main() { int n, a[100010], i, ans1 = 0, ans2 = 0, sum = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { ans1 += 1 - sum; sum = 1; } else if (i % 2 == 0 && sum >= 0) { ans1 += sum + 1; sum = -1; } } sum = 0; for (i = 1; i <= n; i++) { sum += a[i]; if (i % 2 == 1 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if (i % 2 == 0 && sum <= 0) { ans2 += 1 - sum; sum = 1; } } if (ans1 > ans2) { printf("\n%d\n\n", ans2); } else { printf("\n%d\n\n", ans1); } 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, os f = lambda:list(map(int,input().split())) if 'local' in os.environ : sys.stdin = open('./input.txt', 'r') def solve(): n = f()[0] a = f() suma = [0] * n minop = 1e9 for greater in [True, False]: oper = 0 greater0 = greater for i in range(n): if i == 0: suma[i] = a[i] else: suma[i] = a[i] + suma[i-1] greater0 = not greater0 if greater0 and suma[i]<=0: oper += 1 - suma[i] suma[i] = 1 continue if (not greater0) and suma[i]>=0: oper += 1 + suma[i] suma[i] = -1 continue minop = min(minop, oper) print(minop) solve()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; long long a[n]; for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; long long ans1 = 0, ans2 = 0; long long now = a[0]; for (int(i) = 0; (i) < (n - 1); (i)++) { if (i == 0 && now == 0) { now = -1; ans1 += 1; } if ((now + a[i + 1]) * now < 0) { now += a[i + 1]; continue; } else { if (now < 0) { ans1 += (1 - now) - a[i + 1]; now = 1; } else { ans1 += a[i + 1] + (now + 1); now = -1; } } } now = a[0]; for (int(i) = 0; (i) < (n - 1); (i)++) { if (i == 0 && now == 0) { now = 1; ans2 += 1; } if ((now + a[i + 1]) * now < 0) { now += a[i + 1]; continue; } else { if (now < 0) { ans2 += (1 - now) - a[i + 1]; now = 1; } else { ans2 += a[i + 1] + (now + 1); now = -1; } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) L = list(map(int, input().split())) tmp = L[0] res = 0 def diff(a,b): if a*b < 0: return True else: return False if tmp == 0: tmp = -1 res = 1 for i in range(1, N): n = L[i] + tmp if diff(n, tmp): tmp = n else: if tmp < 0: res = res + abs(n) + 1 tmp = 1 else: res = res + abs(n) + 1 tmp = -1 res1 = res res = 1 tmp = 1 for i in range(1, N): n = L[i] + tmp if diff(n, tmp): tmp = n else: if tmp < 0: res = res + abs(n) + 1 tmp = 1 else: res = res + abs(n) + 1 tmp = -1 print(min(res, res1)) else: tmp = L[0] res = 0 for i in range(1, N): n = L[i] + tmp if diff(n, tmp): tmp = n else: if tmp < 0: res = res + abs(n) + 1 tmp = 1 else: res = res + abs(n) + 1 tmp = -1 print(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; int a[100000]; int getTotal(int n, int dir) { int total{}, sum{}; for (int i{0}; i < n; ++i) { sum += a[i]; if (dir > 0 && sum <= 0) { total += -sum + 1; sum = 1; } else if (dir < 0 && sum >= 0) { total += sum + 1; sum = -1; } dir *= -1; } return total; } int main() { int n; cin >> n; for (int i{0}; i < n; ++i) cin >> a[i]; int try1 = getTotal(n, 1); int try2 = getTotal(n, -1); cout << ((try1) < (try2) ? (try1) : (try2)) << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) ans=10**15 for i in[-1,1]: ansi,sum=0,0 for a in A: sum+=a if sum*i<=0:;ansi+=abs(sum-i);sum=i i*=-1 ans=min(ans,ansi) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long N; cin >> N; vector<long long> v(N); bool check = false; for (int i = 0; i < N; i++) { cin >> v[i]; if (v[0] < 0) check = true; if (check) v[i] = -v[i]; } vector<long long> a(N), b(N); int cntA = 0, cntB = 0; if (v[0] == 0) { cntA++; cntB++; a[0] = 1; b[0] = 1; } else { a[0] = v[0]; b[0] = -1; cntB += v[0] + 1; } for (int i = 1; i < N; i++) { long long tmp_a = a[i - 1] + v[i]; if (tmp_a == 0) { if (i % 2 == 0) { a[i] = 1; } else { a[i] = -1; } cntA++; } else if (i % 2 == 0 && tmp_a < 0) { a[i] = 1; cntA += (-tmp_a) + 1; } else if (i % 2 == 1 && tmp_a > 0) { a[i] = -1; cntA += tmp_a + 1; } else { a[i] = tmp_a; } long long tmp_b = b[i - 1] + v[i]; if (tmp_b == 0) { if (i % 2 == 0) { b[i] = -1; } else { b[i] = 1; } cntB++; } else if (i % 2 == 0 && tmp_b > 0) { b[i] = -1; cntB += tmp_b + 1; } else if (i % 2 == 1 && tmp_b < 0) { b[i] = 1; cntB += (-tmp_b) + 1; } else { b[i] = tmp_b; } } cout << min(cntA, cntB) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int i; vector<long long int> v(n); vector<long long int> prefix(n, 0); for (i = 0; i < n; i++) cin >> v[i]; prefix[0] = v[0]; long long int ans = 0; for (i = 1; i < n; i++) { prefix[i] = prefix[i - 1] + v[i]; long long int check = prefix[i - 1] * prefix[i]; if (check >= 0) { ans = ans + abs(prefix[i]) + 1; if (prefix[i - 1] < 0) prefix[i] = 1; else prefix[i] = -1; } } cout << ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np N = int(input()) S = list(map(int, input().split())) mod_count = 0 mod_count = 0 pre_fugou = 1 ## 前の符号がなんであったか 1ならplus 0ならマイナス for i in range(N): if i==0 and S[i]==0: if S[i+1]>0: S[i] -= 1 mod_count += 1 pre_fugou = 0 else: S[i] += 1 mod_count += 1 pre_fugou = 1 elif i==0 and S[i]>0: pre_fugou = 1 elif i==0 and S[i]<0: pre_fugou = 0 else: if pre_fugou == 1: while True: if sum(S[:i+1]) > -1: S[i] -= 1 mod_count += 1 pre_fugou = 0 else: pre_fugou = 0 break elif pre_fugou == 0: while True: if sum(S[:i+1]) < 1: S[i] += 1 mod_count += 1 pre_fugou = 1 else: pre_fugou = 1 break
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 int a[n], sum[n], sum2[n]; cin >> a[0]; sum[0] = a[0]; long long int ans = 0; for (int i = 1; i < n; i++) cin >> a[i]; if (sum[0] > 0) { for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; } } else { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; } } } } else if (sum[0] < 0) { for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; } } else { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; } } } } else { long long int ans2 = 1, ans3 = 1; sum[0] = 1; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] >= 0) { ans2 += sum[i] + 1; sum[i] = -1; } } else { if (sum[i] <= 0) { ans2 += 1 - sum[i]; sum[i] = 1; } } } sum2[0] = -1; for (int i = 1; i < n; i++) { sum2[i] = sum2[i - 1] + a[i]; if (i & 1) { if (sum2[i] <= 0) { ans3 += 1 - sum2[i]; sum2[i] = 1; } } else { if (sum2[i] >= 0) { ans3 += sum2[i] + 1; sum2[i] = -1; } } } ans = min(ans2, ans3); } 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, c = 0; cin >> n; int sum[n]; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] == 0) { a[0]++; c++; } sum[0] = a[0]; int e = a[0] / abs(a[0]); for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i - 1] * sum[i] >= 0) { c += abs(sum[i] - pow(-1, i) * e); sum[i] = pow(-1, i) * e; } } cout << c << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(ai) for ai in input().split()] count = 0 a_sum = 0 for i, ai in enumerate(a): if i == 0: a_sum = ai else: tmp_sum = a_sum + ai if tmp_sum < 0 and a_sum < 0: c = abs(tmp_sum) + 1 elif tmp_sum > 0 and a_sum > 0: c = -abs(tmp_sum) - 1 elif tmp_sum == 0 and a_sum < 0: c = -1 elif tmp_sum == 0 and a_sum > 0: c = 1 else: c = 0 count += abs(c) a_sum = tmp_sum + c print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; static const int NIL = -1; int n; void printArray(int array[], int n) { for (int i = (0); i < (n); ++i) { if (i) cout << " "; cout << array[i]; } cout << endl; } int sequence(int* a, bool sign) { int sum = 0, cnt = 0; for (int i = (0); i < (n); ++i) { if (sign) { sum += a[i]; if (sum > 0) { int rem = abs(-1 - sum); cnt += rem; sum = -1; } sign = false; } else { sum += a[i]; if (sum < 0) { int rem = abs(1 - sum); cnt += rem; sum = 1; } sign = true; } } if (sum == 0) cnt++; return cnt; } int main(int argc, char const* argv[]) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; int a[n]; for (int i = (0); i < (n); ++i) cin >> a[i]; int pos = sequence(a, true); int neg = sequence(a, false); cout << min(pos, neg) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { static int[][] map; static int[][] label; static ArrayList<String> list; static int M; static int N; static int T; static int P; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); long[] map = new long[n]; for (int i = 0; i < n; i++) { map[i] = scanner.nextLong(); } long sum = map[0]; long ans = 0; boolean sign = true; if(sum < 0){ sign = false; sum = -1; } for (int i = 1; i < n; i++) { sum += map[i]; if (sign) { if (sum >= 0) { ans += sum + 1; sum = -1; } sign = false; } else { if (sum <= 0) { ans -= sum - 1; sum = 1; } sign = true; } } sum = map[0]; long ans2 = 0; sign = false; if(sum < 0){ sign = true; sum = 1; } for (int i = 1; i < n; i++) { sum += map[i]; if (sign) { if (sum >= 0) { ans2 += sum + 1; sum = -1; } sign = false; } else { if (sum <= 0) { ans2 -= sum - 1; sum = 1; } sign = true; } } System.out.println(Math.min(ans, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
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 ans = 0; int sum = a[0]; bool plus = (sum > 0); if (sum == 0) { ans++; plus = true; } for (int i = 1; i < n; i++) { sum += a[i]; if ((plus && sum < 0) || (!plus && sum > 0)) { plus = !plus; continue; } if (plus) { ans += (sum + 1); sum = -1; } else { ans += (-sum + 1); sum = 1; } plus = !plus; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count3 = 0 count4 = 0 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count3 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count3 += abs(currentSum) + 1 currentSum = -1 elif A[i] <= 0 and restSum == 0: count3 += abs(currentSum) + 1 currentSum = 1 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(count1, count2, count3, 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
python3
n = int(input()) a = [int(ai) for ai in input().split()] count = 0 a_sum = a[0] for ai in a[1:]: next_sum = a_sum + ai if next_sum * a_sum < 0: a_sum = next_sum continue count += abs(next_sum) + 1 if a_sum < 0: a_sum = 1 else: a_sum = -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
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e9; const long long MOD = (long long)1e9 + 7; const double EPS = (double)1e-10; struct Accelerate_Cin { Accelerate_Cin() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; }; signed main() { long long n; cin >> n; static long long sum[100010] = {0}; long long cont = 0; for (long long t = 0; t < n; t++) { long long a; cin >> a; if (t == 0) sum[t] = a; if (t != 0) sum[t] = sum[t - 1] + a; if (sum[t] * sum[t - 1] >= 0 && t != 0) { cont += abs(sum[t]) + 1; if (sum[t - 1] < 0) sum[t] = 1; if (sum[t - 1] > 0) sum[t] = -1; } } cout << cont << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[100010]; long long ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (i != 0) { a[i] += a[i - 1]; if (a[i] == 0 && i == n - 1) { if (a[i - 1] > 0) { ans++; a[i]--; } else { ans++; a[i]++; } } else if (a[i - 1] > 0) { if (a[i] > 0) { ans += llabs(-1 - a[i]); a[i] = -1; } } else if (a[i - 1] < 0) { if (a[i] < 0) { ans += 1 - a[i]; a[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; 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++; } } 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
python3
n = int(input()) b = [int(x) for x in input().split()] a = list() temp = 0 count1 = 0 count2 = 0 a = b.copy() if a[0] == 0: a[0] = 1 count1 = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: if sum > 0: temp = -1 * abs(sum) - 1 count1 += abs(temp - a[i]) else: temp = abs(sum) + 1 count1 += abs(temp - a[i]) a[i] = temp sum += a[i] a = b.copy() if a[0] == 0: a[0] = 1 if a[0] > 0: a[0] = -1 else: a[0] = 1 count2 = abs(a[0]) + 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count2 += abs(sum - a[i]) + 1 if sum > 0: temp = -1 * abs(sum) - 1 count2 += abs(temp - a[i]) else: temp = abs(sum) + 1 count2 += abs(temp - a[i]) a[i] = temp sum += a[i] print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); ArrayList<Integer> aArrayList = new ArrayList<>(); for (int i=0; i<n; i++) { aArrayList.add(Integer.parseInt(sc.next())); } int sign = 1; int ans1 = 0; int sum1 = 0; for (Integer a : aArrayList) { if (sign > 0){ int d = Math.max(sign-(sum1+a), 0); ans1 += d; sum1 += d + a; }else { int d = Math.min(sign-(sum1+a), 0); ans1 -= d; sum1 += d + a; } sign *= -1; } sign = -1; int ans2 = 0; int sum2 = 0; for (Integer a : aArrayList) { if (sign > 0){ int d = Math.max(sign-(sum2+a), 0); ans2 += d; sum2 += d + a; }else { int d = Math.min(sign-(sum2+a), 0); ans2 -= d; sum2 += d + a; } sign *= -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() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0, cumsum = a[0]; if (a[0] == 0) { int i = 1; while (a[i] == 0 && i < n) i++; if (i == n || a[i] < 0) cumsum = 1; else cumsum = -1; ans += 1; } for (int i = 1; i < n; i++) { if (cumsum > 0) { if (cumsum + a[i] >= 0) { ans += cumsum + a[i] + 1; cumsum = -1; } else cumsum += a[i]; } else { if (cumsum + a[i] <= 0) { ans += 1 - cumsum - a[i]; cumsum = 1; } else cumsum += a[i]; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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.at(i); } int sum = 0, sign = 1; int ans_a = 0; for (int i = 0; i < n; i++) { sum += a.at(i); if (sum <= 0 && sign == 1) { while (sum <= 0) { sum++; ans_a++; } } if (sum >= 0 && sign == -1) { while (sum >= 0) { sum--; ans_a++; } } sign *= -1; } sum = 0, sign = -1; int ans_b = 0; for (int i = 0; i < n; i++) { sum += a.at(i); if (sum <= 0 && sign == 1) { while (sum <= 0) { sum++; ans_b++; } } if (sum >= 0 && sign == -1) { while (sum >= 0) { sum--; ans_b++; } } sign *= -1; } int ans = min(ans_a, ans_b); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- """ Created on Sat Sep 8 15:51:53 2018 @author: maezawa """ n = int(input()) a = list(map(int, input().split())) sa = 0 cnt = 0 for i in range(0,n-1): sa += a[i] na = -sa//abs(sa)*(abs(sa)+1) if abs(a[i+1]) > abs(na) and a[i+1]*na > 0: continue cnt += abs(na-a[i+1]) a[i+1] = na print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; int sum = a[0]; int c = 1; int ans0 = 0, ans1 = 0; for (int i = 1; i < n; i++) { sum += a[i]; if (sum * c < 1) { ans0 += 1 - sum * c; sum = c; } c *= -1; } c = -1; for (int i = 1; i < n; i++) { sum += a[i]; if (sum * c < 1) { ans1 += 1 - sum * c; sum = c; } c *= -1; } if (ans0 < ans1) cout << ans0 << endl; else cout << ans1 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int N, count = 0; cin >> N; vector<long int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; long long int su = A[0]; bool plus = A[0] > 0; for (int i = 1; i < N; i++) { plus = !plus; su += A[i]; if (plus) { if (su <= 0) { count += -1 * su + 1; su = 1; } } else { if (su >= 0) { count += su + 1; su = -1; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N=int(input()) A=list(map(int,input().split())) cur=A[0] ans=0 isplus=True ind=1 if cur<0: isplus=False if cur==0: # すべて0の場合 allzero=True firstNotZero=0 firstNotZeroInd=0 for i in range(N): if A[i]!=0: firstNotZero=A[i] firstNotZeroInd=i allzero=False break if allzero: print(N) exit(0) # 0以外が出てくる場合 if firstNotZero>0: cur=-1 ind=firstNotZeroInd isplus=False print("ind",ind,"isplus",isplus) else: cur=1 ind=firstNotZeroInd isplus=True print("ind",ind,"isplus",isplus) for i in range(ind,N): print("i",i,"cur",cur,"A[i]",A[i]) if isplus: if cur+A[i]>=0: diff=abs((cur+A[i])-(-1)) print("cur",cur,"cur+A[i]",cur+A[i]," -> -1 diff",diff) ans+=diff print("ans",ans) cur=-1 else: print("no problem") cur+=A[i] isplus=False else: if cur+A[i]<=0: diff=abs((cur+A[i])-1) print("cur",cur,"cur+A[i]",cur+A[i]," -> 1 diff",diff) ans+=diff print("ans",ans) cur=1 else: print("no problem") cur+=A[i] isplus=True 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 constexpr int INF = 1e9; const constexpr long long MOD = 1e9 + 7; vector<pair<int, int> > vp; struct Less { bool operator()(const pair<int, int>& x, const pair<int, int>& y) const { return x.first > y.first; } }; long long GCD(long long a, long long b) { if (b == 0) return a; return GCD(b, a % b); } vector<int> g[200010]; int h[100010]; int n; long long f(long long a[]) { long long ans = 0; if (a[0] < 0) { ans += abs(a[0]) + 1; a[0] = 1; } long long sum = a[0]; for (int i = 1; i < n; ++i) { if (i % 2 != 0) { if (sum + a[i] >= 0) { ans += abs(sum + a[i]) + 1; a[i] = -(sum)-1; } } if (i % 2 == 0) { if (sum + a[i] <= 0) { ans += abs(sum + a[i]) + 1; a[i] = -(sum) + 1; } } sum += a[i]; } return ans; } long long ff(long long a[]) { long long ans = 0; if (a[0] > 0) { ans += abs(a[0]) + 1; a[0] = -1; } long long sum = a[0]; for (int i = 1; i < n; ++i) { if (i % 2 != 0) { if (sum + a[i] <= 0) { ans += abs(sum + a[i]) + 1; a[i] = -(sum) + 1; } } if (i % 2 == 0) { if (sum + a[i] >= 0) { ans += abs(sum + a[i]) + 1; a[i] = -(sum)-1; } } sum += a[i]; } return ans; } int main(void) { cin >> n; long long a[100010] = {0}; long long b[100010] = {0}; for (long long i = (long long)0; i < (long long)n; ++i) { cin >> a[i]; b[i] = a[i]; } long long ans1 = f(a); long long ans2 = ff(b); 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
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; namespace ProgramingStydying { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).ToList(); var ans = 0; if(a[0] == 0) { ans = Math.Min(Solve(n, a, 1), Solve(n, a, -1)) + 1; } else { ans = Solve(n, a, a[0]); } Console.WriteLine(ans); } static int Solve(int n, List<int> a, int sum) { var ans = 0; for (int i = 1; i < n; i++) { if (sum > 0) { sum += a[i]; if (sum < 0) { continue; } else { while (sum >= 0) { sum--; ans++; } } } else if (sum < 0) { sum += a[i]; if (sum > 0) { continue; } else { while (sum <= 0) { sum++; ans++; } } } } return ans; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
<?php error_reporting(0); $stdin = file_get_contents('php://stdin'); $line = explode("\n",$stdin); $fi = 0; $cnt = 0; $list = array(); $key = new stdclass(); foreach($line as $l) { if (strlen($l)==0) continue; if ($fi == 0) { $a = explode(" ",$l); $key->A = $a; $fi++; continue; } if ($fi > 0) { $a = explode(" ",$l); $key->X[] = $a; } } $cnt=0; $prev=null; $new=array(); foreach($key->X[0] as $v) { if ($prev != null) { //2回目以降処理 if ($prev > 0) { //前の数が正なら、この数を負にする必要がある if (($prev + $v) >= 0) { //ダメなので-1まで減らす $wk = $prev + $v; $cnt += $wk+1; $prev = -1; $new[]=$v-$cnt; } else { $prev = $prev + $v; $new[]=$v; } } else { //前はマイナスなのでプラスにする必要がある if (($prev + $v)<= 0) { $wk = $prev + $v; $cnt += 1-$wk; $prev = 1; $new[]=$v+$cnt; } else { $prev = $prev + $v; $new[]=$v; } } } else { $prev = $v; $new[]=$v; } } $chk=0; printf("%d\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
java
import java.util.*; import java.text.DecimalFormat; // warm-up public class Main { static void solve() { Scanner sc = new Scanner(System.in); int n=sc.nextInt(), t=n, i=0; double[] a = new double[n]; double o = 0, s = 0; while (t-->0) a[i++] = sc.nextDouble(); for (i=0; i<n; i++) { double k=a[i]; if (s+a[i]==0) a[i]=(-s<0) ? s+1 : 1-s; else if ((s<0 && s+a[i]<0)||(s>0 && s+a[i]>0)) a[i]=(s+a[i]<0) ? -s+1 : -s-1; o+=Math.abs(k-a[i]); s+=a[i]; } System.out.println(new DecimalFormat("#").format(o)); sc.close(); } public static void main(String args[]) { solve(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python2
#coding:utf-8 if __name__ == "__main__": n = int(raw_input()) seq = map(int, raw_input().split(" ")) is_positive = True if seq[0] < 0: is_positive = False sum = seq[0] operation = 0 for i in range(1, n): sum += seq[i] if sum == 0: operation += 1 if is_positive: sum -= 1 else: sum += 1 elif sum > 0 and is_positive: operation += abs(sum) + 1 sum = -1 elif sum < 0 and not is_positive: operation += abs(sum) + 1 sum = 1 if sum > 0: is_positive = True else: is_positive = False print operation
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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())) cnt1=cnt2=sm1=sm2=0 for x,y in enumerate(a): sm1+=y if x%2 ==1: if sm1<1: cnt1 += 1-sm1 sm1=1 elif sm1>-1: cnt1+= 1+sm1 sm1=-1 for x,y in enumerate(a): sm2+=y if x%2 ==0: if sm2<1: cnt2 += 1-sm2 sm2=1 elif sm1>-1: cnt2 += 1+sm2 sm2=-1 print(min(cnt1,cnt2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
'use strict' let input = require("fs").readFileSync("/dev/stdin", "utf8"); let Nums = input.split('\n'); let amount = Nums[0]*1; let arr = Nums[1].split(" ").map(x => x*1); let isPrus = arr[0] > 0? true: false; let sum = 0; let ans = 0; for(let i = 0; i < amount; i++){ //console.log(arr[i]) //console.log(isPrus) sum += arr[i]; if(sum > 0 != isPrus){ ans += Math.abs(sum); sum = 0; } // 足し合わせが0になった時も合わせて処理 if(sum == 0){ if(isPrus){ sum = 1; ans += 1; } else { sum = -1; ans += 1; } } isPrus = !isPrus; } console.log(ans);
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; int count = 0; if (a[0] == 0) { for (int i = 1; i < N; i++) { if (a[i] > 0) { a[0] = 1; break; } else if (a[i] < 0) { a[0] = -1; break; } } count++; } int sum = a[0]; for (int i = 1; i < N; i++) { if (sum * a[i] < 0 && abs(sum) < abs(a[i])) { sum += a[i]; } else { if (sum > 0) { count += a[i] + sum + 1; sum = -1; } else { count += 1 - sum - a[i]; sum = 1; } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100000]; long long c; long long csum; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } c = 0; csum = 0; for (int i = 0; i < n; i++) { long long bsum = csum; bsum += a[i]; if (csum != 0 && csum * bsum >= 0) { if (csum > 0) { c += (bsum + 1); bsum -= (bsum + 1); } else { c += (-bsum + 1); bsum += (-bsum + 1); } } csum = bsum; } cout << c << 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
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**7) def read_h(typ=int): return list(map(typ, input().split())) def read_v(n, m=1, typ=int): return [read_h() if m > 1 else typ(input()) for _ in range(n)] def calc_num_op(cumul, a): tmp_cumul = cumul + a # print('current cumul:', cumul) # print('current a:', a) # print('bare cumul:', tmp_cumul) # print('goodiness:', tmp_cumul != 0 and cumul // abs(cumul) != tmp_cumul // abs(tmp_cumul)) if (cumul <= 0 and tmp_cumul <= 0): return abs(-cumul - a + 1), 1 if (cumul >= 0 and tmp_cumul >= 0): return abs(-cumul - a - 1), -1 return 0, tmp_cumul def solve(arr, sign): num_op = 0 cumul = arr[0] # print('current cumul:', cumul) if cumul == 0: num_op += 1 cumul += 1 if sign == '+' else -1 # print('modified cumul:', cumul) for a in arr[1:]: delta, cumul = calc_num_op(cumul, a) # print('delta:', delta) # print('modified cumul:', cumul) num_op += delta return num_op def main(): _ = read_h() arr = read_h() op_plus = solve(arr, '+') # print('op plus:', op_plus) op_minus = solve(arr, '-') # print('op minus:', op_minus) print(min(op_plus, op_minus)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sumi = a[0]; long long sump = a[0]; long long cnt = 0; for (int i = 0; i < n - 1; i++) { sump += a[i + 1]; if (sumi < 0) { if (sump <= 0) { cnt += 1 - sump; sump = 1; } } else if (sumi > 0) { if (sump >= 0) { cnt += sump + 1; sump = -1; } } sumi = sump; } 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(void) { int n, a[100010]; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int sum = a[0], cnt = 0; for (int i = 1; i < n; ++i) { if (sum < 0) { if (sum + a[i] > 0) { sum += a[i]; } else { cnt += abs(sum + a[i]) + 1; sum = 1; } } else { if (sum + a[i] < 0) { sum += a[i]; } else { cnt += abs(sum + a[i]) + 1; sum = -1; } } } 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
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]; int ans = 0; for(int i = 0; i<n;i++){ int j = sc.nextInt(); a[i] = j; } if(a[0] == 0){ a[0] = -1; int ans1 = calc(a,ans,n) + 1; a[0] = 1; ans = 0; int ans2 = calc(a,ans,n) + 1; ans = Math.min(ans1, ans2); }else{ ans = calc(a,ans,n); } System.out.println(ans); sc.close(); } private static int calc(int[] a,int ans,int n) { int sum=a[0]; boolean plusFlag; // TODO 自動生成されたメソッド・スタブ if(a[0] > 0){ plusFlag = true; }else{ plusFlag = false; } for(int i = 1;i<n;i++){ sum += a[i]; if(plusFlag){ if(sum >= 0){ ans += Math.abs(sum)+1; sum = -1; } }else{ if(sum <= 0){ ans += Math.abs(sum)+1; sum = 1; } } plusFlag = !plusFlag; } return ans; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) _arr = list(map(int, input().split())) ans = [] for first in (_arr[0], 1, -1): arr = _arr[:] c = 0 prev = 0 for i in range(n): t = prev + arr[i] if i == 0: arr[i] = first elif prev > 0 and t >= 0: diff = t + 1 c += diff arr[i] -= diff elif prev < 0 and t <= 0: diff = -1 * t + 1 c += diff arr[i] += diff prev += arr[i] ans.append(c) print(arr) print(min(ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from itertools import accumulate import copy def sol(acmA, sign=1): add = 0 ans = 0 sumA = acmA[0] for i in range(1,N): acmA[i] += add if sign == 1: if (i%2==0 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==1 and 0<acmA[i-1] and acmA[i]<0) :continue else: if (i%2==1 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==0 and 0<acmA[i-1] and acmA[i]<0) :continue tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1 #print(i, tmp_add, acmA[i]) acmA[i] += tmp_add sumA += acmA[i] add += tmp_add ans +=abs(tmp_add) if acmA == 0: return ans +1 else: return ans N = int(input()) A = list(map(int,input().split())) acmA = list(accumulate(A)) acmA2 = copy.deepcopy(acmA) print(min(sol(acmA, 1),sol(acmA2,-1))) #print(acmA)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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> a(n), dp1(n), dp2(n); for (long long i = (0); i < (long long)(n); i++) cin >> a[i]; long long ans1 = 0, ans2 = 0; if (a[0] > 0) { ans2 = a[0] - (-1); dp1[0] = a[0]; dp2[0] = -1; } else { ans1 = 1 - a[0]; dp1[0] = 1; dp2[0] = a[0]; } for (long long i = (1); i < (long long)(n); i++) { if (dp1[i - 1] < 0) { if (dp1[i - 1] + a[i] > 0) { dp1[i] = dp1[i - 1] + a[i]; } else { dp1[i] = 1; ans1 += 1 - (dp1[i - 1] + a[i]); } } else { if (dp1[i - 1] + a[i] < 0) { dp1[i] = dp1[i - 1] + a[i]; } else { dp1[i] = -1; ans1 += (dp1[i - 1] + a[i]) - (-1); } } if (dp2[i - 1] < 0) { if (dp2[i - 1] + a[i] > 0) { dp2[i] = dp2[i - 1] + a[i]; } else { dp2[i] = 1; ans2 += 1 - (dp2[i - 1] + a[i]); } } else { if (dp2[i - 1] + a[i] < 0) { dp2[i] = dp2[i - 1] + a[i]; } else { dp2[i] = -1; ans2 += (dp2[i - 1] + a[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; const int INF = 1e9; const int mod = 1e9 + 7; int main() { int n; cin >> n; int sum; cin >> sum; long long ans = 0; for (int i = 1; i < n; i++) { int a; cin >> a; if (sum > 0) { if (sum + a >= 0) { ans += abs(sum + a) + 1; sum = -1; } else sum += a; } else { if (sum + a <= 0) { ans += abs(sum + a) + 1; sum = 1; } else sum += a; } } 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> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") constexpr int INF = 2147483647; constexpr long long int INF_LL = 9223372036854775807; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; long long int ans = INF_LL; { long long int tmp = 0; long long int sum = a[0]; if (sum < 0) { tmp += abs(sum) + 1; sum = 1; } for (int i = 1; i < N; i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { tmp += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { tmp += abs(sum) + 1; sum = -1; } } } ans = min(ans, tmp); } { long long int tmp = 0; long long int sum = a[0]; if (sum > 0) { tmp += abs(sum) + 1; sum = -1; } for (int i = 1; i < N; i++) { sum += a[i]; if (i % 2 == 0) { if (sum >= 0) { tmp += abs(sum) + 1; sum = -1; } } else { if (sum <= 0) { tmp += abs(sum) + 1; sum = 1; } } } ans = min(ans, tmp); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(_) for _ in input().split()] new_a_plus = [a[i] for i in range(n)] new_a_minus = [a[i] for i in range(n)] count_plus = 0 if a[0] <= 0: new_a_plus[0] = 1 count_plus += 1 - a[0] for i in range(1, n): if i % 2 == 0: if sum(new_a_plus[:i+1]) <= 0: new_a_plus[i] = -sum(new_a_plus[:i])+1 count_plus += new_a_plus[i] - a[i] else: if sum(new_a_plus[:i+1]) >= 0: new_a_plus[i] = -sum(new_a_plus[:i])-1 count_plus += a[i] - new_a_plus[i] count_minus = 0 if a[0] >= 0: new_a_minus[0] = -1 count_minus += a[0] + 1 for i in range(1, n): if i % 2 == 0: if sum(new_a_minus[:i+1]) >= 0: new_a_minus[i] = -sum(new_a_minus[:i])-1 count_minus += a[i] - new_a_minus[i] else: if sum(new_a_minus[:i+1]) <= 0: new_a_minus[i] = -sum(new_a_minus[:i])+1 count_minus += new_a_minus[i] - a[i] 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
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const long INF64 = 1000000000000000ll; const long long MOD = 1000000007ll; int main() { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < (int)(n); i++) std::cin >> a[i]; int ans1 = 0, ans2 = 0; int sum = 0; int han = -1; for (int i = 0; i < (int)(n); i++) { han *= -1; sum += a[i]; if (han < 0) { ans1 += max(0, sum + 1); sum -= max(0, sum + 1); } else { ans1 -= min(0, sum - 1); sum -= min(0, sum - 1); } } han = 1; sum = 0; for (int i = 0; i < (int)(n); i++) { han *= -1; sum += a[i]; if (han < 0) { ans2 += max(0, sum + 1); sum -= max(0, sum + 1); } else { ans2 -= min(0, sum - 1); sum -= min(0, sum - 1); } } std::cout << min(ans1, ans2) << std::endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#![allow(non_snake_case)] #![allow(dead_code)] #![allow(unused_macros)] #![allow(unused_imports)] use std::str::FromStr; use std::io::*; use std::collections::*; use std::cmp::*; struct Scanner<I: Iterator<Item = char>> { iter: std::iter::Peekable<I>, } macro_rules! exit { () => {{ exit!(0) }}; ($code:expr) => {{ if cfg!(local) { writeln!(std::io::stderr(), "===== Terminated =====") .expect("failed printing to stderr"); } std::process::exit($code); }} } impl<I: Iterator<Item = char>> Scanner<I> { pub fn new(iter: I) -> Scanner<I> { Scanner { iter: iter.peekable(), } } pub fn safe_get_token(&mut self) -> Option<String> { let token = self.iter .by_ref() .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if token.is_empty() { None } else { Some(token) } } pub fn token(&mut self) -> String { self.safe_get_token().unwrap_or_else(|| exit!()) } pub fn get<T: FromStr>(&mut self) -> T { self.token().parse::<T>().unwrap_or_else(|_| exit!()) } pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> { (0..len).map(|_| self.get()).collect() } pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> { (0..row).map(|_| self.vec(col)).collect() } pub fn char(&mut self) -> char { self.iter.next().unwrap_or_else(|| exit!()) } pub fn chars(&mut self) -> Vec<char> { self.get::<String>().chars().collect() } pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> { (0..row).map(|_| self.chars()).collect() } pub fn line(&mut self) -> String { if self.peek().is_some() { self.iter .by_ref() .take_while(|&c| !(c == '\n' || c == '\r')) .collect::<String>() } else { exit!(); } } pub fn peek(&mut self) -> Option<&char> { self.iter.peek() } } fn main() { let cin = stdin(); let cin = cin.lock(); let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char)); let n: usize = sc.get(); let a: Vec<i64> = sc.vec(n); let mut p = 0; let mut ans = 0; for i in 0..n { let mut s = p + a[i]; if s == 0 { s += if p > 0 { 1 } else { -1 }; ans += 1; } else if s * p > 0 { ans += s.abs()+1; s += if s > 0 { -s - 1 } else { s + 1 }; } p = s; } println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const long long INFL = LLONG_MAX; const long double pi = acos(-1); int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; long long xx(vector<long long> &v) { long long s = 0; long long ans = 0; int n = int((v).size()); for (int i = 0; i < n; i++) { if (!i) s = v[0]; else { if (s > 0) { if (s + v[i] < 0) { s += v[i]; continue; } long long x, y; x = max(s + v[i] + 1, (long long)0); ans += x; if (x != 0) s = -1; } else { if (s + v[i] > 0) { s += v[i]; continue; } long long x, y; x = max(1 - (s + v[i]), (long long)0); ans += x; if (x != 0) s = 1; } } } return ans; } int main() { ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0); int n; cin >> n; vector<long long> v(n); for (int(i) = 0; (i) < (n); (i)++) cin >> v[i]; if (n == 1) { cout << 0 << '\n'; return 0; } long long ans = INFL; if (v[0] == 0) { v[0] += 1; ans = min(ans, xx(v) + 1); v[0] = -1; ans = min(ans, xx(v) + 1); } else if (v[0] > 0) { ans = min(ans, xx(v)); v[0] = -1; ans = min(ans, xx(v) + v[0] + 1); } else { ans = min(ans, xx(v)); v[0] = 1; ans = min(ans, xx(v) + 1 - v[0]); } cout << ans << '\n'; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <vector> #include <cmath> int main(void) { // in int n; std::cin >> n; std::vector<long long> a(n); for(int i = 0; i < n; ++i) std::cin >> a[i]; // sum int cost_all = 0; std::vector<long long> s(n, 0); for(int k = 0; k < 2; ++k) { int cost = 0; if(k == 0) { s[0] = (a[0] > 0 ? a[0] : 1); cost += std::abs(a[0] - s[0]); } else { s[0] = (a[0] < 0 ? a[0] : -1); cost += std::abs(a[0] - s[0]); } for(int i = 1; i < n; ++i) { // current sum s[i] = s[i-1] + a[i]; if(s[i] * s[i-1] < 0) continue; else { if(s[i-1] < 0) { cost += std::abs(1 - s[i-1] - a[i]); a[i] = 1 - s[i-1]; } else { cost += std::abs(- 1 - s[i-1] - a[i]); a[i] = - 1 - s[i-1]; } } s[i] = s[i-1] + a[i]; } cost_all = (cost < cost_all ? cost : cost_all); } //for(int i = 0; i < n; ++i) { // std::cout << a[i] << std::endl; //} std::cout << cost << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100010]; int sum[100010] = {0}; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; if (a[0] >= 0) { for (int i = 0; i < n; i++) { int j = i; while (j >= 0) { sum[i] += a[j]; j--; } if (i % 2 == 0) { if (sum[i] < 0) { while (sum[i] <= 0) { sum[i]++; a[i]++; ans++; } } } else { if (sum[i] >= 0) { while (sum[i] >= 0) { sum[i]--; a[i]--; ans++; } } } } if (sum[n - 1] == 0) ans++; } else { for (int i = 0; i < n; i++) { int j = i; while (j >= 0) { sum[i] += a[j]; j--; } if (i % 2 == 0) { if (sum[i] >= 0) { while (sum[i] >= 0) { sum[i]--; a[i]--; ans++; } } } else { if (sum[i] < 0) { while (sum[i] < 0) { sum[i]++; a[i]++; ans++; } } } } if (sum[n - 1] == 0) ans++; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int sub[n]; long long ans = 0; sub[0] = a[0]; int i = 0; if (a[0] == 0) { while (a[i] == 0) { i++; } ans = (i - 1) * 2 + 1; if (a[i] > 0) { sub[i - 1] = -1; } else { sub[i - 1] = 1; } } for (; i < n; i++) { sub[i] = sub[i - 1] + a[i]; if ((sub[i] > 0 && sub[i - 1] < 0) || (sub[i] < 0 && sub[i - 1] > 0)) { continue; } if (sub[i - 1] < 0 && sub[i] <= 0) { ans += (1 - sub[i]); sub[i] = 1; } if (sub[i - 1] > 0 && sub[i] >= 0) { ans += sub[i] + 1; sub[i] = -1; } } printf("%d", 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
UNKNOWN
solver::[Integer]->Integer solver xs = let h = head xs in min (check_tot (abs (h-1)) 1 (tail xs)) (check_tot (abs (h+1)) (-1) (tail xs)) main::IO() main=do _<-getLine datc<-getLine print (solver (map read (words datc))) --おそい。Step_sumを作る事無く、シーケンシャルにいく --今のカウント手数、ここまでの修正されたトータル(これはゼロでない事が保証される)、食べるリスト。 check_tot::Integer -> Integer -> [Integer] -> Integer check_tot st _ [] = st check_tot st tot xs | (tot > 0)&&((tot+(head xs))>=0) = let dec = (tot+(head xs))+1 in check_tot (dec+st) (-1) (tail xs) | (tot > 0)&&((tot+(head xs)) <0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs)) >0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs))<=0) = let inc = 1-(tot+(head xs)) in check_tot (inc+st) 1 (tail xs)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 c1 = 0, c2 = 0; long long sum = 0; long long c = 1; for (int i = 0; i < n; ++i) { sum += a[i]; if (sum * c <= 0) { c1 += abs(sum) + 1; sum = c; } c *= -1; cout << sum << endl; } c = -1; sum = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (sum * c <= 0) { c2 += abs(sum) + 1; sum = c; } c *= -1; } cout << min(c1, c2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { new Main().solve(); } void solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; long A = 0; long ANS = 0; long ans = 0; long q = 1; a[0] = sc.nextLong(); A = a[0]; for (int i = 1; i < n; i++) { a[i] = sc.nextLong(); A += a[i]; q = q * -1; if (A <= 0 && q == 1) { ans += Math.abs(A - 1); A += Math.abs(A - 1); } else if (A >= 0 && q == -1) { ans += Math.abs(A + 1); A -= Math.abs(A + 1); } } ANS = ans; A = a[0]; ans = 0; q = -1; for (int i = 0; i < n; i++) { A += a[i]; q = q * -1; if (A <= 0 && q == 1) { ans += Math.abs(A - 1); A += Math.abs(A - 1); } else if (A >= 0 && q == -1) { ans += Math.abs(A + 1); A -= Math.abs(A + 1); } } ANS = Math.min(ANS, ans); System.out.println(ANS); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) def sol(S): ret = 0 B = [S] for a in A[1:]: b = a if S * (S + b) > 0: b = (abs(S) + 1) * (1 if S < 0 else -1) if S + b == 0: b = b - 1 if b < 0 else b + 1 ret += abs(b - a) S += b B.append(b) return ret ans = min( sol(A[0]), sol(-A[0] // abs(A[0]) if A[0] != 0 else -1) + abs(A[0]) + 1 ) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static uint64_t calc_count(vector<long long> &vec, int64_t sign) { unsigned long long count = 0; long long total = vec[0]; if (total == 0) { total = sign; count++; } for (uint64_t i = 1; i < vec.size(); i++) { sign *= -1; total += vec[i]; if ((total == 0) || (sign * total < 0)) { count += abs(sign - total); total = sign; } } return count; } int32_t main() { uint64_t N; cin >> N; vector<long long> vec; for (uint64_t i = 0; i < N; i++) { long long val; cin >> val; vec.push_back(val); } cout << min(calc_count(vec, 1), calc_count(vec, -1)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import Control.Applicative import Data.List main = do _ <- getLine as <- (map read) . words <$> getLine print $ minimum [sum $ zipWith (\a b -> abs (a-b)) (f 0 as True) as, sum $ zipWith (\a b -> abs (a-b)) (f 0 as False) as] where f :: Integer -> [Integer] -> Bool -> [Integer] f _ [] _ = [] f ps (x:xs) m = if ps * (ps+x) < 0 then x : (f (ps+x) xs m) else if ps > 0 then (-ps-1) : (f (-1) xs m) else if ps < 0 then (-ps+1) : (f 1 xs m) else if x /= 0 then x : (f x xs m) else if m then 1 : (f 1 xs m) else (-1) : (f (-1) xs m)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long ans = 1e9; ; for (int j = 0; j < 2; j++) { long long sum = 0, cnt = 0; if (j == 0) { for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum > 0) continue; cnt += 1 - sum; sum = 1; } else { if (sum < 0) continue; cnt += 1 + sum; sum = -1; } } ans = min(ans, cnt); } else { for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum < 0) continue; cnt += 1 + sum; sum = -1; } else { if (sum > 0) continue; cnt += 1 - sum; 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
python3
# @oj: atcoder # @id: hitwanyang # @email: [email protected] # @date: 2020-08-17 17:23 # @url:https://atcoder.jp/contests/abc059/tasks/arc072_a import sys,os from io import BytesIO, IOBase import collections,itertools,bisect,heapq,math,string from decimal import * # region fastio BUFSIZE = 8192 BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ def main(): n=int(input()) a=list(map(int,input().split())) prefix=[a[0]] for i in range(1,len(a)): prefix.append(prefix[-1]+a[i]) print (prefix) s,t=0,0 ans1,ans2=0,0 for i in range(len(prefix)): if i%2==0: if prefix[i]+s<=0: ans1+=abs(prefix[i]+s)+1 s=s+abs(prefix[i]+s)+1 else: if prefix[i]+s>=0: ans1+=abs(prefix[i]+s+1) s=s-abs(prefix[i]+s+1) for i in range(len(prefix)): if i%2==1: if prefix[i]+t<=0: ans2+=abs(prefix[i]+t)+1 t=t+abs(prefix[i]+t)+1 else: if prefix[i]+t>=0: ans2+=abs(prefix[i]+t+1) t=t-abs(prefix[i]+t+1) print (min(ans1,ans2)) 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
import numpy as np n = int(input()) a = list(map(int, input().split())) c1 = 0 sum = a[0] for i in range(1, n): if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0): if sum > 0: c1 += a[i] + sum + 1 sum = -1 else: c1 += -a[i] - sum + 1 sum = 1 else: sum += a[i] c2 = abs(a[0]) + 1 sum = - a[0] for i in range(1, n): if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0): if sum > 0: c2 += a[i] + sum + 1 sum = -1 else: c2 += -a[i] - sum + 1 sum = 1 else: sum += a[i] print(min(c1, c2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long res1 = 0, res2 = 0; long long som = v[0]; if (som > 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using Pl = pair<ll, ll>; using vi = vector<int>; using vii = vector<vi>; using vl = vector<ll>; using vll = vector<vl>; using vs = vector<string>; using vb = vector<bool>; using vc = vector<char>; using vcc = vector<vc>; const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; const int inf = (1 << 30) - 1; const ll infll = (1LL << 62) - 1; ll ceil(const ll& a, const ll& b) { return ((a) + (b)-1) / b; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w, n; cin >> h >> w >> n; vi a(n); for (int i = 0; i < n; i++) cin >> a[i]; vii ans(h, vi(w)); int cur = 1; for (int i = 0; i < h; i++) { if (i % 2 == 0) { for (int j = 0; j < w; j++) { ans[i][j] = cur; a[cur - 1]--; if (a[cur - 1] == 0) cur++; } } else { for (int j = w - 1; j >= 0; j--) { ans[i][j] = cur; a[cur - 1]--; if (a[cur - 1] == 0) cur++; } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cout << ans[i][j] << ' '; } cout << '\n'; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ull = unsigned long long int; using P = pair<ll, ll>; using P3 = pair<P, int>; using PP = pair<P, P>; constexpr ll INF = 1LL << 60; constexpr ll MOD = ll(1e9) + 7; constexpr int di[] = {0, 1, 0, -1}; constexpr int dj[] = {1, 0, -1, 0}; constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1}; constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr double EPS = 1e-9; int main() { int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) a[i] += a[i - 1]; ll s = 0, ans1 = 0, ans2 = 0; for (int i = 0; i < n; i++) { if (i % 2) { if (a[i] + s <= 0) { ll d = abs(a[i] + s) + 1; ans1 += d; s += d; } } else { if (a[i] + s >= 0) { ll d = abs(a[i] + s) + 1; ans1 += d; s -= d; } } } s = 0; for (int i = 0; i < n; i++) { if (i % 2) { if (a[i] + s >= 0) { ll d = abs(a[i] + s) + 1; ans2 += d; s -= (abs(a[i]) + 1); } } else { if (a[i] + s <= 0) { ll d = abs(a[i] + s) + 1; ans2 += d; s += d; } } } 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> int main() { long long n; std::cin >> n; long long a; long long sum_plus[n], sum_minus[n]; for (long long i = 0; i < n; ++i) { std::cin >> a; if (i == 0) sum_plus[i] = a; else sum_plus[i] = sum_plus[i - 1] + a; sum_minus[i] = sum_plus[i]; } long long count = 0; bool plus = true; for (long long i = 0; i < n; ++i) { if (plus) { if (sum_plus[i] <= 0) { long long add = -sum_plus[i] + 1; count += add; for (long long j = i; j < n; ++j) sum_plus[j] += add; } } else { if (sum_plus[i] >= 0) { long long add = sum_plus[i] + 1; count += add; for (long long j = i; j < n; ++j) sum_plus[j] -= add; } } plus = !plus; } long long ans = count; count = 0; plus = false; for (long long i = 0; i < n; ++i) { if (plus) { if (sum_minus[i] <= 0) { long long add = -sum_minus[i] + 1; count += add; for (long long j = i; j < n; ++j) sum_minus[j] += add; } } else { if (sum_minus[i] >= 0) { long long add = sum_minus[i] + 1; count += add; for (long long j = i; j < n; ++j) sum_minus[j] -= add; } } plus = !plus; } ans = std::min(ans, count); std::cout << ans << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[20000]; int getsign(long long int n) { if (n > 0) { return 1; } if (n < 0) { return -1; } return -1; } long long int count(int sign0, long long a[], int n) { long long int sum = 0; long long int sign = sign0; long long int count = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (getsign(sum) != sign) { count += abs(sign - sum); sum = sign; } sign = (sign * -1); } return count; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } cout << min(count(1, a, n), count(-1, a, n)) << 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,a=int(input()),list(map(int,input().split()));ans=0 if a[0]==0: bol=True for i in range(1,n): if a[i-1]!=a[i]: bol=False if a[i]>0:a[0]=(1if i%2==0else-1) else:a[0]=(1if i%2!=0else-1) ans+=1;break if bol:print(n*2-1);exit() b=[a[0]];m=("+"if a[0]<0else"-") for i in range(1,n): b.append(b[i-1]+a[i]) if b[i-1]*b[i]>=0: r=eval(m+"1")-b[i] ans+=abs(r) b[i]+=r m=("+"if b[i]<0else"-") 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 sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) res = -min(0, ~a[0]) sm = 1 if a[0] >= 1: res = 0 sm = a[0] #print(res, sm) for i in range(1, N): if sm > 0: res += max(-1, a[i] + sm) + 1 sm = min(-1, sm + a[i]) else: res += -min(1, sm + a[i]) + 1 sm = max(1, sm + a[i]) #print(res, sm) res2 = max(0, a[0] + 1) sm = -1 if a[0] <= -1: res = 0 sm = a[0] for i in range(1, N): if sm > 0: res2 += max(-1, a[i] + sm) + 1 sm = min(-1, sm + a[i]) else: res2 += -min(1, sm + a[i]) + 1 sm = max(1, sm + a[i]) #print(res2, sm) print(min(res, res2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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[i]; } long long ans = 0; long long sum1 = 0; long long sum2 = 0; if (a[0] > 0) { sum1 = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 1) { if (sum1 + a[i] < 0) { sum1 += a[i]; } else { ans += sum1 + a[i] + 1; sum1 = -1; } } else if (i % 2 == 0) { if (sum1 + a[i] > 0) { sum1 += a[i]; } else { ans += 1 - sum1 - a[i]; sum1 = 1; } } } } else if (a[0] < 0) { sum1 = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 1) { if (sum1 + a[i] > 0) { sum1 += a[i]; } else { ans += 1 - sum1 - a[i]; sum1 = 1; } } else if (i % 2 == 0) { if (sum1 + a[i] < 0) { sum1 += a[i]; } else { ans += 1 + sum1 + a[i]; sum1 = -1; } } } } else { long long ans1 = 1; long long ans2 = 1; sum1 = 1; sum2 = -1; for (int i = 1; i < n; i++) { if (i % 2 == 1) { if (sum1 + a[i] < 0) { sum1 += a[i]; } else { ans1 += sum1 + a[i] + 1; sum1 = -1; } } else if (i % 2 == 0) { if (sum1 + a[i] > 0) { sum1 += a[i]; } else { ans1 += 1 - sum1 - a[i]; sum1 = 1; } } } for (int i = 1; i < n; i++) { if (i % 2 == 1) { if (sum2 + a[i] > 0) { sum2 += a[i]; } else { ans2 += 1 - sum2 - a[i]; sum2 = 1; } } else if (i % 2 == 0) { if (sum2 + a[i] < 0) { sum2 += a[i]; } else { ans2 += 1 + sum2 + a[i]; sum2 = -1; } } } ans = min(ans1, ans2); } 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; std::vector<int> a; for (int i = 0; i < N; i++) { int temp; cin >> temp; a.push_back(temp); } int Num1 = 0, Num2 = 0; int sum1 = 0, sum2 = 0; for (int i = 0; i < N; i += 2) { if (sum1 + a[i] < 0) sum1 += a[i]; else Num1 += sum1 + a[i] + 1, sum1 = -1; if (sum2 + a[i] > 0) sum2 += a[i]; else Num2 += 1 - sum2 - a[i], sum2 = 1; if (i != N - 1) { if (sum1 + a[i + 1] > 0) sum1 += a[i + 1]; else Num1 += 1 - sum1 - a[i + 1], sum1 = 1; if (sum2 + a[i + 1] < 0) sum2 += a[i + 1]; else Num2 += sum2 + a[i + 1] + 1, sum2 = -1; } } printf("%d\n", Num1 < Num2 ? Num1 : Num2); 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; static const long long INF = 1000000000000000000; int main() { int n; cin >> n; long long A[n]; long long B[n]; long long sum[n]; long long ans = 0; for (int i = 0; i < n; i++) { cin >> A[i]; B[i] = A[i]; sum[i] = 0; } long long minans = INF; long long temp = 0; int p[2] = {1, -1}; if (A[0] == 0) { for (int k = 0; k < 2; k++) { ans = 0; ans++; A[0] = p[k]; sum[0] = A[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); } } ans = 0; sum[0] = A[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); ans = 0; if (B[0] > 0) { temp = B[0]; B[0] = -1; ans = temp + 1; sum[0] = B[0]; } else { temp = B[0]; B[0] = 1; ans = -temp + 1; sum[0] = B[0]; } for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + B[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); cout << minans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[100000]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long int Mp = 0; int Sp = 0; int dif = 0; for (int i = 0; i < n; i++) { Sp += a[i]; if (i % 2 == 0) { if (Sp <= 0) { dif = 1 - Sp; Mp += dif; Sp += dif; } } else { if (Sp >= 0) { dif = Sp + 1; Mp += dif; Sp += -dif; } } } long long int Mn = 0; int Sn = 0; int di = 0; for (int i = 0; i < n; i++) { Sn += a[i]; if (i % 2 == 1) { if (Sn <= 0) { di = 1 - Sp; Mn += di; Sn += di; } } else { if (Sn >= 0) { di = Sn + 1; Mn += di; Sn += -di; } } } if (Mp < Mn) { cout << Mp; } else { cout << Mn; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int count(int sign0, vector<int> a, int n) { int count = 0; long total = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if ((total + a.at(i)) * sign0 < 0) total += a.at(i); else { count += abs(total + a.at(i)) + 1; total = -1 * sign0; } } else if (i % 2 == 0) { if ((total + a.at(i)) * sign0 > 0) total += a.at(i); else { count += abs(total + a.at(i)) + 1; total = sign0; } } } return count; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a.at(i); int plus = count(1, a, n); int minus = count(-1, a, n); cout << min(plus, minus) << 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()) num_list = list(map(int, input().split())) count = 0 sum_ = num_list[0] if sum_ > 0: for i in range(1, n): sum_ += num_list[i] if i%2 == 0: if sum_ <= 0: while sum_ <= 0: sum_ += 1 count += 1 else: if sum_ >= 0: while sum_ >= 0: sum_ -= 1 count += 1 elif sum_ < 0: for i in range(1, n): sum_ += num_list[i] if i%2 == 1: if sum_ <= 0: while sum_ <= 0: sum_ += 1 count += 1 else: if sum_ >= 0: while sum_ >= 0: sum_ -= 1 count += 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
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1001001001; const long long LINF = 1001001001001001001ll; const int MOD = 1000000007; 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; } long long sign(long long A) { return (A > 0) - (A < 0); } int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long ans = 0; long long sum = 0; long long sig; for (int i = 0; i < (n); ++i) { sum += a[i]; if (i == 0) { sig = sign(sum); continue; } if (sig == -sign(sum)) { sig = sign(sum); continue; } long long diff = -sig - sum; sum += diff; ans += abs(diff); sig = sign(sum); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) a_orig = a[:] ans1 = 0 ans2 = 0 tot = [0 for i in range(n)] tot[0] = a[0] if a[0] <= 0: a[0] = 1 tot[0] = 1 for i in range(1, n): tot[i] = tot[i-1] + a[i] if i % 2 == 0: if tot[i] <= 0: tot[i] = 1 a[i] = tot[i] - tot[i-1] else: if tot[i] >= 0: tot[i] = -1 a[i] = tot[i] - tot[i-1] print(tot) print(a) for i in range(n): ans1 += abs(a[i]-a_orig[i]) a = a_orig[:] tot = [0 for i in range(n)] tot[0] = a[0] if a[0] >= 0: a[0] = -1 tot[0] = -1 for i in range(1, n): tot[i] = tot[i-1] + a[i] if i % 2 == 1: if tot[i] <= 0: tot[i] = 1 a[i] = tot[i] - tot[i-1] else: if tot[i] >= 0: tot[i] = -1 a[i] = tot[i] - tot[i-1] for i in range(n): ans2 += abs(a[i]-a_orig[i]) print(tot) print(a) 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
python3
n = int(input()) a = [int(_) for _ in input().split()] even_p = 0 even_n = 0 odd_p = 0 odd_n = 0 for i in range(n): if i % 2 == 0: if a[i] > 0: odd_p += 1 elif a[i] < 0: odd_n += 1 else: if a[i] > 0: even_p += 1 elif a[i] < 0: even_n += 1 cnt = 0 if odd_p - odd_n > even_p - even_n: if a[0] > 0: sum_i = a[0] else: cnt += abs(a[0]-1) sum_i = 1 else: if a[0] < 0: sum_i = a[0] else: cnt += abs(a[0] + 1) sum_i = -1 for i in range(1, n): if sum_i > 0: if sum_i + a[i] < 0: sum_i += a[i] else: cnt += abs(a[i]+sum_i+1) sum_i = -1 elif sum_i < 0: if sum_i + a[i] > 0: sum_i += a[i] else: cnt += abs(a[i]+sum_i-1) 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
cpp
#include <bits/stdc++.h> using namespace std; int n; int a[100010]; int solve(int sign) { int ans = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sign == 1) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } else { if (sum >= 0) { ans += 1 + sum; sum = -1; } } sign *= -1; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long t = 1; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cout << min(solve(1), solve(-1)); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, a[100000], b[100000]{}, ans = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } b[0] += a[0]; for (int i = 1; i < n; i++) { b[i] += a[i] + b[i - 1]; if (b[i] * b[i - 1] >= 0) { if (b[i - 1] > 0) { ans += abs(b[i]) + 1; b[i] = -1; } else { ans += abs(b[i]) + 1; b[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
python3
from sys import stdin import sys import math n = int(input()) a = list(map(int, stdin.readline().rstrip().split())) pair = 0 odd = 1 for i in range(0, len(a), 2): pair += a[i] for i in range(1, len(a), 2): odd += a[i] #print(odd) #print(pair) count = 0 current_sum = 0 for i in range(len(a)): ## odd if i % 2 == 1 and odd > pair: if current_sum + a[i] < 1: diff = 1 - (current_sum + a[i]) a[i] += diff count += diff ## odd elif i % 2 == 1 and odd < pair: if current_sum + a[i] > -1: diff = -1 - (current_sum + a[i]) a[i] += diff count += -1 * diff ## pair elif i % 2 == 0 and odd > pair: if current_sum + a[i] > -1: diff = -1 - (current_sum + a[i]) a[i] += diff count += -1 * diff elif i % 2 == 0 and odd < pair: if current_sum + a[i] < 1: diff = 1 - (current_sum + a[i]) a[i] += diff count += diff else: print("error") current_sum += 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; long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long int cnt = 0; long int total = a[0]; if (total == 0) { total = 1; cnt++; } for (int i = 1; i < n; i++) { if (total > 0) { int temp = total; temp += a[i]; if (temp >= 0) { cnt += temp + 1; total = -1; continue; } else { total = temp; continue; } } if (total < 0) { int temp = total; temp += a[i]; if (temp <= 0) { cnt += (-temp + 1); total = 1; continue; } else { total = temp; continue; } } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int n; int a[100010]; void input() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; } int main() { cin.tie(0); ios::sync_with_stdio(false); input(); int cost1 = 0; int cost2 = 0; int s1 = 0; int s2 = 0; for (int i = 0; i < n; ++i) { s1 += a[i]; if (i % 2 && s1 >= 0) { cost1 += (s1 + 1); s1 = -1; } else if (i % 2 == 0 && s1 <= 0) { cost1 += (1 - s1); s1 = 1; } } for (int i = 0; i < n; ++i) { s2 += a[i]; if (i % 2 && s2 <= 0) { cost2 += (1 - s2); s2 = 1; } else if (i % 2 == 0 && s2 >= 0) { cost2 += (s2 + 1); s2 = -1; } } cout << min(cost1, cost2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); long long prev = 1000000000000000000, sum = 0, cnt = 0; for (int i = 0; i < (int)(N); i++) { cin >> a[i]; sum += a[i]; if (prev != 1000000000000000000) { if (prev <= 0 && sum <= 0) { cnt += (1 - sum); sum = 1; } else if (prev >= 0 && sum >= 0) { cnt += (abs(-1 - sum)); sum = -1; } } prev = sum; } 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 = [int(i) for i in input().split()] s = a[0] count=0 for i in range(1,n): if (s+a[i])*s>=0: if s+a[i]<0: a[i]+=(abs(s+a[i])+1) count+=(abs(s+a[i])+1) elif s+a[i]>0: a[i]-=(abs(s+a[i])+1) count+=(abs(s+a[i])+1) elif s+a[i]==0: if s>0: a[i]-=1 count+=1 elif s<0: a[i]+=1 count+=1 s+=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<int64_t> vec(N); for (int i = 0; i < N; i++) { cin >> vec.at(i); } int64_t ans = 0; int64_t x = vec.at(0); for (int i = 1; i < N; i++) { while ((vec.at(i) + x) * x >= 0) { if (x > 0) { vec.at(i)--; ans++; } if (x < 0) { vec.at(i)++; ans++; } } x = vec.at(i) + x; } 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 N; const int MAX_N = 1.0e5 + 100; int a[MAX_N]; int main() { cin >> N; for (int i = 0; i < N; i++) cin >> a[i]; long long e_sum = 0; long long even = 0; for (int i = 0; i < N; i++) { e_sum += a[i]; if (i % 2 == 0 && e_sum < 0) { even += abs(e_sum) + 1; e_sum = 1; } else if (i % 2 == 1 && e_sum > 0) { even += abs(e_sum) + 1; e_sum = -1; } } long long o_sum = 0; long long odd = 0; for (int i = 0; i < N; i++) { o_sum += a[i]; if (i % 2 == 1 && o_sum <= 0) { odd += abs(o_sum) + 1; o_sum = 1; } else if (i % 2 == 0 && o_sum >= 0) { odd += abs(o_sum) + 1; o_sum = -1; } } cout << min(even, odd) << endl; return 0; }