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; #define int long long //----***型定義***---- using ll = long long; using pll = pair<ll, ll>; using pdd = pair<double, double>; typedef long double lb; typedef long double ld; typedef pair<int,int> P; //----***Like a Pythonista***---- #define REP(ii,jj,nn) for (ll ii=jj;ii<(nn);ii++) #define RREP(ii,nn,jj) for (ll ii = nn; jj<ii;ii--) #define each(i,...) for (auto&& i:__VA_ARGS__) #define ALL(vec) (vec).begin(),(vec).end() #define sum(...) accumulate(ALL(__VA_ARGS__),0LL) #define dsum(...) accumulate(ALL(__VA_ARGS__),0.0L) #define vec(type,name,...) vector<type> name(__VA_ARGS__) template<class T> inline auto max(const T& a){ return *max_element(ALL(a)); } template<class T> inline auto min(const T& a){ return *min_element(ALL(a)); } inline ll gcd(ll a,ll b){if(b == 0) return a;return gcd(b,a%b);} inline ll lcm(ll a,ll b){ll g = gcd(a,b);return a / g * b;} //----***定数***---- #define MOD 1e9+7; #define INF 1e9; #define EPS 1e-9; //----***入出力***--- #define print(out) cout<< out << "\n"; #define debug(var) do{std::cerr << #var << " ↓ "<<"\n";view(var);}while(0); #define dbg cerr<<"🥺🥺🥺🥺🥺🥺"<<endl; template<typename T> void view(T e){std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } } //----***初期時読み込み***---- struct initial{initial(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);};}initial_; signed main(){ int N;cin>>N; vector<int> A(N); REP(i,0,N)cin>>A[i]; int ans,cnt=0; vector<int> tmp_A=A; // case 偶数が正 for(int i=0;i<N;i++){ if(i%2==0&&A[i]<=0){ cnt+=-(tmp_A[i]-1); tmp_A[i]=1; } if(i%2==1&&A[i]>=0){ cnt+=tmp_A[i]+1; tmp_A[i]=-1; } } int S=tmp_A[0]; REP(i,1,N){ if((S+tmp_A[i])*S>=0){ cnt+=abs(S+tmp_A[i])+1; S=abs(S+tmp_A[i]); } else{ S+=tmp_A[i]; } } ans=cnt; // case 偶数が負 cnt=0;tmp_A=A; for(int i=0;i<N;i++){ if(i%2==1&&A[i]<=0){ cnt+=-(tmp_A[i]-1); tmp_A[i]=1; } if(i%2==0&&A[i]>=0){ cnt+=tmp_A[i]+1; tmp_A[i]=-1; } } // debug(tmp_A) // debug(cnt) S=tmp_A[0]; REP(i,1,N){ if((S+tmp_A[i])*S>=0){ // debug(i)debug(S) cnt+=abs(S+tmp_A[i])+1; S=(tmp_A[i]<0?-1:1); // debug(cnt) } else{ S+=tmp_A[i]; } } 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
python3
import sys import math INF = 10**9+7 def k(i): if(i == 1): return 1 else: return(i * k(i-1)) def comb(n, r): if(n == r or r == 1): return 1 else: return k(n) / (k(n-r) * k(r)) stdin = sys.stdin def na(): return map(int, stdin.readline().split()) def ns(): return stdin.readline().strip() def nsl(): return list(stdin.readline().strip()) def ni(): return int(stdin.readline()) def nil(): return list(map(int, stdin.readline().split())) n = ni() a = nil() b = [] for i in range(n): b.append(a[i]) sum = 0 c1 = 0 c2 = 0 if a[0] == 0: a[0] = 1 c1 += 1; for i in range(0, n-1): sum += a[i] sum2 = sum + a[i+1] if(sum * sum2 >= 0): k = abs(sum2) + 1 h = k - (abs(sum) - 1) l = k - h if sum > 0 : a[i] -= l sum -= l a[i + 1] -= h else: a[i] += l sum += l a[i + 1] += h c1 += h+l sum = 0 a = b if a[0] == 0: a[0] = 1 c2 += 1; else: c2 = abs(a[0]) + 1 if a[0] > 0: a[0] = -1 else: a[0] = 1 for i in range(0, n-1): sum += a[i] sum2 = sum + a[i+1] if(sum * sum2 >= 0): k = abs(sum2) + 1 h = k - (abs(sum) - 1) l = k - h if sum > 0 : a[i] -= l sum -= l a[i + 1] -= h else: a[i] += l sum += l a[i + 1] += h c2 += k 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
UNKNOWN
#include <bits/stdc++.h> int comp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main(void) { int n, i; int a[100001]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", a + i); } int n1 = 0, n2 = 0; { int sum = 0; int f = 0; for (i = 0; i < n; i++) { sum += a[i]; if (f) { if (sum <= 0) { n1 = n1 + 1 - sum; sum = 1; } } else { if (sum >= 0) { n1 = n1 + 1 + sum; sum = -1; } } f = !f; } } { int sum = 0; int f = 1; for (i = 0; i < n; i++) { sum += a[i]; if (f) { if (sum <= 0) { n2 = n2 + 1 - sum; sum = 1; } } else { if (sum >= 0) { n2 = n2 + 1 + sum; sum = -1; } } f = !f; } } printf("%ld\n", n1 < n2 ? n1 : n2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const int MAX_N = int(1e5); long long n, a[MAX_N], dp[MAX_N]; void solve() { long long sum_diff = 0, ans = 0; if (dp[0] == 0) { dp[0] = 1; sum_diff++; ans++; } for (long long i = 0; i < (long long)(n - 1); i++) { long long diff = 0; dp[i + 1] += sum_diff; if (dp[i] * dp[i + 1] > 0) { if (dp[i + 1] > 0) { diff = -1 - dp[i + 1]; sum_diff += diff; dp[i + 1] = -1; } else { diff = 1 - dp[i + 1]; sum_diff += diff; dp[i + 1] = 1; } } if (dp[i + 1] == 0) { if (dp[i] > 0) { sum_diff++, diff = 1; dp[i + 1] = 1; } else { sum_diff--, diff = -1; dp[i + 1] = -1; } } ans += abs(diff); } cout << ans << endl; } int main() { cin >> n; for (long long i = 0; i < (long long)(n); i++) { cin >> a[i]; if (i == 0) dp[0] = a[0]; else dp[i] = dp[i - 1] + a[i]; } 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 ch_sign(int n) { if (n == 0) return 0; return (n > 0) - (n < 0); } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int sign1 = 1, sign2 = -1; long long s1 = 0, s2 = 0; int ans1 = 0, ans2 = 0; for (int i = 0; i < n; ++i) { s1 += a[i]; s2 += a[i]; sign1 *= -1; sign2 *= -1; if (ch_sign(s1) != sign1) { ans1 += abs(s1) + 1; s1 = sign1; } if (ch_sign(s2) != sign2) { ans2 += abs(s2) + 1; s2 = sign2; } } 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()) al = list(map(int, input().split())) def is_diff_sign(s, t): return (s > 0 > t) or (s < 0 < t) count = 0 prev_sum = al[0] prev_positive = al[0] > 0 for a in al[1:]: _sum = prev_sum + a if is_diff_sign(prev_sum, _sum): prev_sum = _sum prev_positive = prev_sum > 0 continue if prev_positive: prev_sum = -1 else: prev_sum = 1 prev_positive = not prev_positive count += abs(_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
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long man = 0; if (a[0] == 0) { a[0] = 1; long min = f(a, n); a[0] = -1; min = Math.min(min, f(a, n)); man = min + 1; } else { man = f(a, n); } System.out.println(man); } static long f(long a[], int n) { long total = a[0]; long man = 0; for (int i = 1; i < n; i++) { if (total * (total + a[i]) >= 0) { long x = Math.abs(total + a[i]) + 1; total += a[i] + (total > 0 ? -x : x); man += x; } else { total += a[i]; } } return man; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n), b(n); for (long long(i) = (0); (i) < (long long)(n); ++(i)) cin >> a[i], b[i] = a[i]; long long sum = a[0]; long long ans = 0; for (int i = 1; i < n; ++i) { if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) { sum += a[i]; } else { if (sum > 0) { sum += a[i]; for (; sum >= 0; --sum) { ++ans; } } else { sum += a[i]; for (; sum <= 0; ++sum) { ++ans; } } } } long long ans2 = 0; sum = a[0]; if (sum > 0) { for (; sum >= 0; --sum) { ++ans2; } } else { for (; sum <= 0; ++sum) { ++ans2; } } for (int i = 1; i < n; ++i) { if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) { sum += a[i]; } else { if (sum > 0) { sum += a[i]; for (; sum >= 0; --sum) { ++ans2; } } else { sum += a[i]; for (; sum <= 0; ++sum) { ++ans2; } } } } cout << min(ans, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Text; using System.Collections.Generic; using System.Linq; class Program { static List<long> rep; static void Main(string[] args){ //入力を受け取る var N = long.Parse(Console.ReadLine()); var A = Console.ReadLine().Split().Select(a => long.Parse(a)).ToArray(); long ans = 0; long sum = A[0]; for(int i =1 ;i <N; i++){ if(sum > 0){ if(sum+A[i] >= 0){ var aim = sum*(-1)-1; ans += (long) Math.Abs(A[i]-aim); A[i] = aim; } }else{ if(sum+A[i] <= 0){ var aim = sum*(-1)+1; ans += (long) Math.Abs(A[i]-aim); A[i] = aim; } } sum += A[i]; }  Console.WriteLine(ans); } static int LowerBound(long num){ var l = 0; var r = rep.Count()-1; while(l <= r){ var mid = l+(r-l)/2; if(rep[mid] < num){ l = mid+1; }else{ r = mid-1; } } return l; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, c = 0; cin >> n; int a[n], s[2]; for (i = 0; i < n; i++) { cin >> a[i]; } s[0] = a[0], s[1] = 0; for (i = 1; i < n; i++) { s[1] = s[0] + a[i]; if (s[0] * s[1] >= 0) { for (; s[0] * s[1] >= 0;) { if (s[0] > 0) s[1] -= 1; else s[1] += 1; c += 1; } } s[0] = s[1]; cout << "Sum=" << s[0] << endl; } cout << "result" << c << 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(auto& x:a){ cin >> x; } int sum = 0; int count1 = 0; int count2 = 0; for(int i = 0; i<n; i++){ sum += a.at(i); if(i_sum>=0 && i%2==1){ sum -= abs(sum)+1; count1 += abs(sum) +1; }else if(i_sum<=0 && i%2==0){ sum += abs(sum)+1; count1 += abs(sum)+1; } } sum =0; for(int i = 0; i<n; i++){ sum += a.at(i); if(i_sum>=0 && i%2==0){ sum -= abs(sum)+1; count2 += abs(sum) +1; }else if(i_sum<=0 && i%2==1){ sum += abs(sum)+1; count2 += abs(sum)+1); } } int count; count = min(count1, count2); 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())) wa=a[0] ans1,ans2=0,0 for i in range(1,n): # print(wa) if wa>0: if wa+a[i]<0: wa+=a[i] else: ans1+=abs(wa+a[i])+1 wa=-1 else: if wa+a[i]>0: wa+=a[i] else: ans1+=abs(wa+a[i])+1 wa=1 if a[0]>0: ans2+=a[0]+1 wa=-1 else: ans2+=-a[0]+1 wa=1 for i in range(1,n): if wa>0: if wa+a[i]<0: wa+=a[i] else: ans2+=abs(wa+a[i])+1 wa=-1 else: if wa+a[i]>0: wa+=a[i] else: ans2+=abs(wa+a[i])+1 wa=1 print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; bool code(long long int n) { if (n < 0) return 1; else if (n > 0) return 0; } int main() { int n; long long int sum = 0; long long int ans = 0; long long int ans2 = 0; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } if (a.at(0) != 0) { sum = a.at(0); for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } cout << ans << endl; return 0; } else if (a.at(0) == 0) { sum = -1; ans = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } sum = 1; ans2 = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans2++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans2 += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } if (ans > ans2) cout << ans2 << endl; else { cout << ans << endl; } cout << " \a" << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!/usr/bin/env python3 from itertools import accumulate def main(): n = int(input()) a = list(map(int, input().split())) a = list(accumulate(a)) ans = 10**18 diff = [0, 0]# a[0]<0, a[0]>0それぞれの初期コスト for i in range(2): if a[0] * [-1,1][i] >= 0: diff[i] = [-1,1][i] * (abs(a[0])+1) for j in range(2): ans2 = abs(diff[j]) for i in range(1,n): p = a[i] + diff[j] q = a[i-1] + diff[j] if p * q >= 0: ans2 += abs(p)+1 diff[j] -= p+1 ans = min(ans, ans2) print(ans) 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; const long long INFF = 0x3f3f3f3f3f3f3f3f; long long a[1000010]; int n; long long solve() { long long sum = 0; long long oo = a[0]; for (int i = 1; i < n; i++) { if (oo < 0) { oo += a[i]; if (oo <= 0) { sum += 1 - oo; oo = 1; } continue; } else { oo += a[i]; if (oo >= 0) { sum += oo + 1; oo = -1; } } } return sum; } int main() { scanf("%d", &n); long long sum = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } if (a[0] == 0) { a[0] = 1; long long sum1 = solve(); a[0] = -1; long long sum2 = solve(); sum = min(sum1, sum2) + 1; } else { long long sum0 = solve(); a[0] = 1; long long sum1 = solve() + abs(1 - a[0]); a[0] = -1; long long sum2 = solve() + abs(-1 - a[0]); sum = min(sum0, min(sum1, sum2)); } printf("%lld\n", sum); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long #define mp make_pair #define pb push_back #define ff first #define ss second #define set0(a) memset ((a), 0 , sizeof(a)) #define set1(a) memset((a),-1,sizeof (a)) #define pi pair<int, int> #define ps pair<string, string> #define pl pair<long, long> #define pll pair<long long, long long> #define vll vector<long long> #define vl vector<long> #define vi vector<int> #define vs vector<string> #define vps vector< ps > #define vpi vector< pi > #define vpl vector< pl > #define vpll vector< pll > #define flash ios_base::sync_with_stdio(false); cin.tie(NULL); #define tc(t) for(long long l=0;l<t;l++) #define rep(i,s,n,d) for(long long i=s;i<n;i=i+d) bool sortbysec(const pll &a, const pll &b) { return (a.second < b.second); } void func(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n,1){ cin>>a[i]; } ll count1=0; if(a[0]==0){ if(a[1]>0){ a[0]=-1; } else a[0]=1; count1++; } ll sum[n]={}; sum[0]=a[0]; rep(i,1,n,1){ sum[i]=sum[i-1]+a[i]; } ll sum1=a[0]; rep(i,1,n,1){ if(sum1*(sum1+a[i])>=0){ ll d=1; if(sum1<0){ d=1; }else{ d=-1; } int dif=abs(sum1+a[i]-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } cout<<count1<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1000000007; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int t = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { t += 1 - sum; sum += t; } } else { if (sum >= 0) { t += sum + 1; sum -= t; } } } int u = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum <= 0) { u += 1 - sum; sum += u; } } else { if (sum >= 0) { u += sum + 1; sum -= u; } } } cout << min(t, u) << 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 + 7; const long long longinf = 1LL << 60; const int mx = 100010; const long long mod = 1e9 + 7; int main() { int n; cin >> n; vector<int> a(n); for (int i = (int)(0); i < (int)(n); ++i) { cin >> a[i]; } int ansa = 0, sum = a[0]; bool pm = (a[0] > 0 ? true : false); for (int i = 1; i < n; i++) { sum += a[i]; if (pm) { if (sum >= 0) { ansa += sum + 1; sum = -1; } pm = false; } else { if (sum <= 0) { ansa += abs(sum) + 1; sum = 1; } pm = true; } } int ansb = abs(a[0]) + 1; sum = (a[0] > 0 ? -1 : 1); pm = (a[0] > 0 ? false : true); for (int i = 1; i < n; i++) { sum += a[i]; if (pm) { if (sum >= 0) { ansb += sum + 1; sum = -1; } pm = false; } else { if (sum <= 0) { ansb += abs(sum) + 1; sum = 1; } pm = true; } } cout << min(ansa, ansb) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> constexpr int kMod = 1000000007; constexpr int kNmax = 1e5 + 1; int sum[kNmax]; int main() { int n; std::cin >> n; std::cin >> sum[0]; for (int i = 1; i < n; ++i) { int a; std::cin >> a; sum[i] = sum[i - 1] + a; } int cnt1 = 0, cnt2 = 0; int offset = 0; for (int i = 0; i < n; ++i) { int v = sum[i] + offset; if (i % 2 == 0) { if (v <= 0) { offset += std::abs(v) + 1; cnt1 += std::abs(v) + 1; } } else { if (v >= 0) { offset -= v + 1; cnt1 += v + 1; } } } offset = 0; for (int i = 0; i < n; ++i) { int v = sum[i] + offset; if (i % 2 == 0) { if (v >= 0) { offset -= v + 1; cnt2 += v + 1; } } else { if (v <= 0) { offset += std::abs(v) + 1; cnt2 += std::abs(v) + 1; } } } std::cout << std::min(cnt1, cnt2) << 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
cpp
#include <bits/stdc++.h> using namespace std; long num_operate(long n, long sum, long* a) { long j; for (long i = 1; i < n; i++) { if (sum * (sum + a[i]) < 0) sum += a[i]; else { j += abs(sum + a[i]) + 1; if (sum < 0) sum = 1; else if (sum > 0) sum = -1; } } return j; } int main() { cin.tie(0); ios::sync_with_stdio(false); long n; cin >> n; vector<long> a(n); for (long i = 0; i < n; i++) cin >> a[i]; long sum = a[0]; if (sum == 0) { long cnt1 = num_operate(n, 1, &a.front()); long cnt2 = num_operate(n, 1, &a.front()); cout << min(cnt1, cnt2) << endl; } else { long cnt = num_operate(n, sum, &a.front()); cout << cnt << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; long long buf; cin >> N; vector<long long> aaa = vector<long long>(N + 1, 0); for (int i = 1; i < N + 1; i++) { cin >> buf; aaa.at(i) = buf + aaa.at(i - 1); } bool be = true; bool af; vector<long long> ans = vector<long long>(2); long long change = 0; for (int l = 0; l < 2; l++) { vector<long long> aa = aaa; if (l == 0) { be = true; } else { be = false; } for (int i = 1; i < N + 1; i++) { if (aa.at(i) == 0) { if (af) { change = 1 - aa.at(i); ans.at(l) += 1 - aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = false; } else { change = -(aa.at(i) + 1); ans.at(l) += 1 + aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = true; } } else { if (aa.at(i) < 0) { af = true; } else { af = false; } if (af == be) { if (af) { change = 1 - aa.at(i); ans.at(l) += 1 - aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = false; } else { change = -(aa.at(i) + 1); ans.at(l) += 1 + aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = true; } } else { be = af; } } } } long long tt = min(ans.at(0), ans.at(1)); cout << tt << 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 long long mod = 1e9 + 7; const long long INF = 1e18; const double pi = acos(-1.0); int main(void) { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long ans, sum = 0, res1 = 0, res2 = 0; for (int sign = 0; sign < (2); ++sign) { for (int i = 0; i < (n); ++i) { sum += a[i]; if ((i % 2 ^ sign) && sum >= 0) { res1 += sum + 1; sum = -1; } else if ((i % 2 ^ sign) && sum <= 0) { res2 += abs(sum - 1); sum = 1; } } } ans = min(res1, res2); 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
def main(): n = int(input()) A = list(map(int, input().split())) res = 0 sums = [] for i in range(n): sums.append(sum(A[:i]) + A[i]) if i == 0: if A[i] == 0: index = -1 for num in A: if num != 0: index = A.index(num) break if index == -1: res += 3 A[i] = 1 A[i+1] = -2 sums[i] = 1 elif (index % 2 and A[index] > 0) or (index % 2 == 0 and A[index] < 0): A[i] = -1 sums[i] = -1 res += 1 else: A[i] = 1 sums[i] = 1 res += 1 else: if sums[i] == 0: if sums[i-1] > 0: A[i] -= 1 sums[i] = sums[i-1] + A[i] res += 1 else: A[i] += 1 sums[i] = sums[i-1] + A[i] res += 1 elif (sums[i-1] > 0) and (sums[i] > 0): res += A[i] - (-sums[i-1] - 1) A[i] = -sums[i-1] - 1 sums[i] = sums[i-1] + A[i] elif (sums[i-1] < 0) and (sums[i] < 0): res += 1 - (sums[i-1] + A[i]) A[i] = abs(sums[i-1]) + 1 sums[i] = sums[i-1] + A[i] print(res) 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; struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; template <typename T> inline size_t maxElement(T beginIt, T endIt) { return max_element(beginIt, endIt); } template <typename T> inline size_t minElement(T beginIt, T endIt) { return min_element(beginIt, endIt); } template <typename T> inline size_t maxIndex(T beginIt, T endIt) { return distance(beginIt, *max_element(beginIt, endIt)); } template <typename T> inline size_t minIndex(T beginIt, T endIt) { return distance(beginIt, *min_element(beginIt, endIt)); } template <typename T> inline int sum(T beginIt, T endIt) { return accumulate(beginIt, endIt, 0); } template <typename T> inline int mean(T beginIt, T endIt) { return sum(beginIt, endIt) / distance(beginIt, endIt); } template <typename T> inline void debug(T x) { cerr << x << " " << "(L:" << 17 << ")" << endl; } signed main(void) { int num = 0; int N; array<int, 100000> A; string S; cin >> N; for (int i = 0; i < N; ++i) { cin >> A[i]; } int tmp = A[0]; for (int i = 1; i < N; ++i) { if (tmp > 0) { if (A[i] >= -tmp) { num += abs(-tmp - 1 - A[i]); A[i] = -tmp - 1; } } else { if (A[i] <= -tmp) { num += abs(-tmp + 1 - A[i]); A[i] = -tmp + 1; } } tmp += A[i]; } cout << num << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { int n; cin >> n; long long a[120000]; for (int i = 0; i < (n); i++) cin >> a[i]; long long total = 0; long long total2 = 0; long long count1 = 0; long long count2 = 0; for (int i = 0; i <= (n - 2) / 2; i++) { total += a[2 * i]; total2 += a[2 * i]; if (total >= 0) { count1 += (abs(total) + 1); total = -1; } if (total2 <= 0) { count2 += (abs(total2) + 1); total2 = 1; } total += a[2 * i + 1]; total2 += a[2 * i + 1]; if (total <= 0) { count1 += (abs(total) + 1); total = 1; } if (total2 >= 0) { count2 += (abs(total2) + 1); total2 = -1; } } cout << min(count1, count2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long cnt = 0; if (sum == 0) { int ind = 1; for (int i = 0; i < n; i++) { if (a[i] != 0) ind = i; break; } if (a[ind] > 0) sum = ((ind + 1) % 2 == 0 ? 1 : -1); else sum = ((ind + 1) % 2 == 0 ? -1 : 1); cnt++; } for (int i = 1; i < n; i++) { long long nsum = sum + a[i]; if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) { sum = nsum; continue; } sum = (sum > 0 ? -1 : 1); cnt += (nsum == 0 ? 1 : abs(nsum) + 1); } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long ns[] = new long[n]; for (int i = 0; i < n; i++) { ns[i] = sc.nextLong(); } long sum = ns[0]; long ans = 0; boolean isNegative = ns[0] < 0; for (int i = 1; i < n; i++) { sum += ns[i]; if (isNegative && sum < 0) { ans -= sum - 1; sum = 1; } else if (!isNegative && sum > 0) { ans += sum + 1; sum = -1; } else if (sum == 0) { ans++; if (isNegative) sum = 1; else sum = -1; } isNegative = !isNegative; } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n, a[200000], sum[200000]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int cnt1 = 0; for (int i = 0; i < n; i++) { if (i == 0) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; if (i % 2 == 0 & sum[i] <= 0) { cnt1 += abs(sum[i]) + 1; sum[i] = 1; } else if (i % 2 == 1 & sum[i] >= 0) { cnt1 += abs(sum[i]) + 1; sum[i] = -1; } } int cnt2 = 0; for (int i = 0; i < n; i++) { if (i == 0) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; if (i % 2 == 0 & sum[i] >= 0) { cnt2 += abs(sum[i]) + 1; sum[i] = -1; } else if (i % 2 == 1 & sum[i] <= 0) { cnt2 += abs(sum[i]) + 1; sum[i] = 1; } } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- """ Created on Sat Sep 8 15:51:53 2018 @author: maezawa """ def f(n, a0, cnt, sa, sign): a = a0[:] if a[0] == 0: a[0] = 1 cnt += 1 if sign == -1: a[0] = -a[0] cnt += 2*abs(a[0]) for i in range(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 else: cnt += abs(na-a[i+1]) a[i+1] = na return cnt n = int(input()) a = list(map(int, input().split())) sa = 0 cnt = 0 cnt0 = f(n, a, cnt, sa, -1) cnt1 = f(n, a, cnt, sa, 1) cnt = min([cnt0,cnt1]) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np from copy import deepcopy n = int(input()) a = list(map(int, input().split())) c = [0] * n for i in range(n): c[i] = c[i - 1] + a[i] c = np.array(c) ans1, ans2 = 0, 0 tmp = deepcopy(c) for i in range(n): t = tmp[i] if i % 2 == 0 and tmp[i] >= 0: tmp[i:] -= t + 1 ans1 += t + 1 elif i % 2 == 1 and tmp[i] <= 0: tmp[i:] += -t + 1 ans1 += -t + 1 tmp = deepcopy(c) for i in range(n): t = tmp[i] if i % 2 == 1 and tmp[i] >= 0: tmp[i:] -= t + 1 ans2 += t + 1 elif i % 2 == 0 and tmp[i] <= 0: tmp[i:] += -t + 1 ans2 += -t + 1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) def wh(cst,ttl,flg): for i in range(1,n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+1 ttl -= memo cst += memo elif flg < 0: memo = abs(ttl)+1 ttl += memo cst += memo flg *= -1 return cst ttl = a[0] cst = 0 if a[0]>=0: flg = 1 elif a[0]<0: flg = -1 cst = wh(cst,ttl,flg) ttl = a[0] cst2 = 0 if a[0]>0: flg = -1 cst2 += abs(ttl)+1 ttl += 0-ttl-1 elif a[0]<0: flg = 1 cst2 += abs(ttl)+1 ttl += 0-ttl+1 cst2 = wh(cst2,ttl,flg) print(min(cst,cst2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 T1, class T2> using dict = std::unordered_map<T1, T2>; int main() { int n; cin >> n; int64_t a[n]; for (int i = 0; i < (int)(n); i++) cin >> a[i]; int64_t s = 0; int64_t count1 = 0; int64_t count2 = 0; for (int i = 0; i < (int)(n); i++) { s += a[i]; if (i % 2 == 1 && s <= 0) { count1 += -s + 1; s = 1; } else if (i % 2 == 0 && s >= 0) { count1 += s + 1; s = -1; } } for (int i = 0; i < (int)(n); i++) { s += a[i]; if (i % 2 == 1 && s >= 0) { count2 += s + 1; s = 1; } else if (i % 2 == 0 && s <= 0) { count2 += -s + 1; s = -1; } } cout << min(count1, count2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using PII = pair<int, int>; using VI = vector<int>; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int main() { ios::sync_with_stdio(false), cin.tie(0); int N; cin >> N; vector<ll> V(N); for (int _n = N, i = 0; i < _n; ++i) cin >> V[i]; if (V[0]) { ll sum = V[0]; ll ans = 0; for (int i = (1), _b = (N - 1); i <= _b; ++i) { ll nsum = sum + V[i]; ll target = (ll)-1 * (sum / abs(sum)); if (nsum == 0) { ans += abs(target - nsum); sum = target; } else { ll nsign = nsum / abs(nsum); cerr << "[" << "nsign" << "]:", debug_out(nsign); if (nsign == target) { sum = nsum; continue; } else { ans += abs(target - nsum); sum = target; } } } cout << ans << endl; } else { ll ans1 = 1; ll sum = 1; for (int i = (1), _b = (N - 1); i <= _b; ++i) { ll nsum = sum + V[i]; ll target = (ll)-1 * (sum / abs(sum)); if (nsum == 0) { ans1 += abs(target - nsum); sum = target; } else { ll nsign = nsum / abs(nsum); if (nsign == target) { sum = nsum; continue; } else { ans1 += abs(target - nsum); sum = target; } } } ll ans2 = 1; sum = -1; for (int i = (1), _b = (N - 1); i <= _b; ++i) { ll nsum = sum + V[i]; ll target = (ll)-1 * (sum / abs(sum)); if (nsum == 0) { ans2 += abs(target - nsum); sum = target; } else { ll nsign = nsum / abs(nsum); if (nsign == target) { sum = nsum; continue; } else { ans2 += abs(target - nsum); sum = target; } } } cout << min(ans1, ans2) << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import itertools from collections import Counter from collections import defaultdict import bisect from heapq import heappush, heappop def main(): n = int(input()) a = list(map(int, input().split())) ans = 0 cumulative = 0 count = 0 for i in range(len(a)): cumulative += a[i] if i % 2 == 0: # positive if cumulative <= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) else: # negative if cumulative >= 0: count += abs(cumulative) + 1 cumulative += (abs(cumulative) + 1) ans = max(ans, count) cumulative = 0 count = 0 for i in range(len(a)): cumulative += a[i] if i % 2 == 0: # negative if cumulative >= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) else: # positive if cumulative <= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) ans = max(ans, count) print(ans) 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; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long res1 = 0, sum1 = a[0], res2 = 0, sum2 = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 0) { if (sum1 > 0) { sum1 += a[i]; continue; } res1 += 1 - sum1; sum1 = 1; } else { if (sum1 < 0) { sum1 += a[i]; continue; } res1 += sum1 + 1; sum1 = -1; } } for (int i = 1; i < n; i++) { if (i % 2 == 0) { if (sum2 < 0) { sum2 += a[i]; continue; } res2 += 1 - sum2; sum2 = 1; } else { if (sum2 > 0) { sum2 += a[i]; continue; } res2 += sum2 + 1; sum2 = -1; } } cout << min(res1, res2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<signed long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } signed long long ans = 0; if (a[0] >= 0) { signed long long sum = a[0]; for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum + a[i] < 0) { sum += a[i]; } else { ans += sum + a[i] + 1; sum = -1; } } else { if (sum + a[i] > 0) { sum += a[i]; } else { ans += abs(sum + a[i] - 1); sum = 1; } } } } else { signed long long sum = a[0]; for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum + a[i] > 0) { sum += a[i]; } else { ans += abs(sum + a[i] - 1); sum = 1; } } else { if (sum + a[i] < 0) { sum += a[i]; } else { ans += sum + a[i] + 1; sum = -1; } } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; enum { POS, NEG }; int minNum(vector<int> &arr, int n) { if (n == 1 && arr[0] == 0) return 1; if (n == 1) return 0; int sol = 0; int acum = arr[0], aux, y, aux2; int sign = acum > 0 ? POS : NEG; for (int i = 1; i < n; i++) { aux = acum + arr[i]; if (sign == POS) { if (aux >= 0) { sol += abs(arr[i] + (acum + 1)); arr[i] = -(acum + 1); } sign = NEG; } else { if (aux <= 0) { sol += abs(arr[i] + (acum - 1)); arr[i] = -(acum - 1); } sign = POS; } acum += arr[i]; } return sol; } int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; cout << minNum(arr, 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, sumA = 0, sumB = 0, mA = 0, mB = 0; for (int i = 0; i < n; i++) { cin >> a; sumA += a; sumB += a; if (i % 2) { if (sumA <= 0) { mA += 1 - sumA; sumA = 1; } if (sumB >= 0) { mB += 1 + sumB; sumB = -1; } } else { if (sumA >= 0) { mA += 1 + sumA; sumA = -1; } if (sumB <= 0) { mB += 1 - sumB; sumB = 1; } } } cout << min(mA, mB) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main() { int n, a[100010]; long sgn = 1, cont = 0, ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { cont += a[i]; if (cont * sgn < 0) { sgn = -1 * sgn; } else if (cont * sgn >= 0) { ans = ans + 1 + cont * sgn; sgn = -1 * sgn; cont = sgn; } } printf("%ld", 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
java
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; int[] result = new int[n]; int even = 0; int odd = 0; boolean sign = true; //正=true, 負=false for(int i = 0; i < n; i++) { input[i] = sc.nextInt(); if(i % 2 == 0) { even += input[i]; } else { odd += input[i]; } } if(even > 0 && odd < 0) { //正負 sign = true; } else if(even < 0 && odd > 0) { //負正 sign = false; } else if(even > 0 && odd > 0) { //正正 if(even > odd) { sign = true; } else { sign = false; } } else if(even < 0 && odd < 0) { //負負 if(even > odd) { sign = false; } else { sign = true; } } else if(even == 0) { if(odd < 0) { sign = true; } else { sign = false; } } else if(odd == 0){ if(even > 0) { sign = true; } else { sign = false; } } //System.out.println(Arrays.toString(input)); //System.out.println(sign + ""); //System.out.println(counting(input, result, 0, 0, sign)); counting(input, result, 0, 0, sign); } public static void counting(int[] input, int[] result, int count, int index, boolean sign) { if(index > 0) { result[index] = result[index - 1] + input[index]; } else { result[index] = input[index]; } if(sign) { while(result[index] <= 0) { result[index]++; count++; } sign = false; } else { while(result[index] >= 0) { result[index]--; count++; } sign = true; } if(index < result.length - 1) { counting(input, result, count, index+1, sign); } else { System.out.println(count); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) pre_total = a[0] cnt = 0 for i in range(1, len(a)): new_total = pre_total + a[i] if pre_total <0 and new_total <= 0: a[i] += abs(new_total) + 1 cnt += abs(new_total) + 1 new_total = pre_total + a[i] pre_total = new_total elif pre_total >0 and new_total >= 0: a[i] -= abs(new_total) + 1 cnt += abs(new_total) + 1 new_total = pre_total + a[i] pre_total = new_total else: pre_total = new_total 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> const int N = 1e5 + 10; using namespace std; int mod = 1e9 + 7; int num[N], num2[N]; long long sum[N], sum2[N]; int main() { int n; while (~scanf("%d", &n)) { long long ans = 0, ans2 = 0; for (int i = 1; i <= n; i++) { scanf("%d", num + i); num2[i] = num[i]; sum[i] = sum[i - 1] + num[i]; sum2[i] = sum2[i - 1] + num[2]; } if (num[1] == 0) { ans += 1; num[1] = 1; sum[1]++; } if (num2[1] == 0) { ans2 += 1; num2[1] = -1; sum2[1]--; } for (int i = 2; i <= n; i++) { sum[i] = sum[i - 1] + num[i]; int a = sum[i - 1] < 0; int b = sum[i] < 0; if (sum[i] == 0) { if (sum[i - 1] < 0) num[i]++; else num[i]--; sum[i] = sum[i - 1] + num[i]; ans++; } else if (!(a ^ b)) { if (sum[i] < 0) { num[i] -= sum[i]; num[i]++; ans -= sum[i]; sum[i] = sum[i - 1] + num[i]; ans++; } else if (sum[i] > 0) { num[i] -= sum[i]; num[i]--; ans += sum[i]; ans++; sum[i] = num[i] + sum[i - 1]; } } } for (int i = 2; i <= n; i++) { sum2[i] = sum2[i - 1] + num2[i]; int a = sum2[i - 1] < 0; int b = sum2[i] < 0; if (sum2[i] == 0) { if (sum2[i - 1] < 0) num2[i]++; else num2[i]--; sum2[i] = sum2[i - 1] + num2[i]; ans2++; } else if (!(a ^ b)) { if (sum2[i] < 0) { num2[i] -= sum2[i]; num2[i]++; ans2 -= sum2[i]; sum2[i] = sum2[i - 1] + num2[i]; ans2++; } else if (sum2[i] > 0) { num2[i] -= sum2[i]; num2[i]--; ans2 += sum2[i]; ans2++; sum2[i] = num2[i] + sum2[i - 1]; } } } printf("%lld\n", min(ans, ans2)); } 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.*; // warm-up public class Main { static void solve() { Scanner sc = new Scanner(System.in); int n=sc.nextInt(), t=n, i=0; long[] a = new long[n]; long o = 0, s = 0; while (t-->0) a[i++] = sc.nextLong(); for (i=0; i<n; i++) { long 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(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
python3
n = int(input()) i = input() i = i.split() for item in range(len(i)): i[item] = int(i[item]) totn = 0 totp = 0 countp = 0 countn = 0 for x in range(len(i)): totp += i[x] totn += i[x] if x %2 == 1: if totn == 0: countn += 1 totn = 1 elif totn < 0: countn += abs(totn) + 1 totn = 1 if totp == 0: countp += 1 totp = -1 elif totp > 0: countp += abs(totp) + 1 totp = -1 if x %2 == 0: if totn == 0: countn += 1 totn = -1 elif totn > 0: countn += abs(totn) + 1 totn = -1 if totp == 0: countp += 1 totp = 1 elif totp < 0: countp += abs(totp) + 1 count = min(countn, countp) print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int counter = 0; if (a[0] >= 0) { for (int i = 1; i < n; i++) { int total = 0; for (int j = 0; j <= i; j++) { total += a[j]; } if (i % 2 == 0 && total < 0) { counter += abs(total - 0); a[i] += abs(total - 0); } else if (i % 2 != 0 && total >= 0) { counter += abs(total - (-1)); a[i] -= abs(total - (-1)); } } } else { for (int i = 1; i < n; i++) { int total = 0; for (int j = 0; j <= i; j++) { total += a[j]; } if (i % 2 == 0 && total >= 0) { counter += abs(total - (-1)); a[i] -= abs(total - (-1)); } else if (i % 2 != 0 && total < 0) { counter += abs(total - 0); a[i] += abs(total - 0); } } } cout << counter << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void print(const T& x) { cout << x << endl; } template <class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); } struct PreMain { PreMain() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } premain; int main() { int N; cin >> N; vector<int> A(N); for (int(i) = 0; (i) < (int)(N); ++(i)) cin >> A[i]; int tmp = 0; int cand1 = 0; for (int(i) = 0; (i) < (int)(N); ++(i)) { tmp += A[i]; if (tmp == 0 || (i % 2 == 0) != (tmp > 0)) { int next_tmp = (i % 2 == 0) ? 1 : -1; cand1 += abs(tmp - next_tmp); tmp = next_tmp; } } int cand2 = 0; tmp = 0; for (int(i) = 0; (i) < (int)(N); ++(i)) { tmp += A[i]; if (tmp == 0 || (i % 2 == 0) != (tmp < 0)) { int next_tmp = (i % 2 == 0) ? -1 : 1; cand2 += abs(tmp - next_tmp); tmp = next_tmp; } } int ans = min(cand1, cand2); print(ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int sum = 0; int ans = 0; for (int i = 0; i < n;) { if (sum < 0 && sum + a[i] <= 0) { int change = 0; change += 1 - sum - a[i]; a[i] += change; ans += change; } if (sum > 0 && sum + a[i] >= 0) { int change = 0; change += -1 - sum - a[i]; a[i] += change; ans -= change; } while ((i < n && sum <= 0 && sum + a[i] > 0) || (i < n && sum >= 0 && sum + a[i] < 0)) { sum += a[i]; i++; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> vector; long long temp; for (int i = 0; i < n; i++) { cin >> temp; vector.push_back(temp); } long long answer1 = 0; long long answer2 = 0; long long sum1 = 0; long long sum2 = 0; for (int i = 0; i < n; i++) { if (i == 0) { if (vector[0] > 0) sum1 = vector[0]; else { sum1 = 1; answer1 += abs(1 - vector[0]); } } else if (sum1 < 0) { if (sum1 + vector[i] > 0) { sum1 += vector[i]; } else { answer1 += abs((-1) * sum1 + 1 - vector[i]); sum1 = 1; } } else { if (sum1 + vector[i] < 0) { sum1 += vector[i]; } else { answer1 += abs((-1) * sum1 - 1 - vector[i]); sum1 = -1; } } } for (int i = 0; i < n; i++) { if (i == 0) { if (vector[0] > 0) { sum2 = -1; answer2 += abs(-1 - vector[0]); } else { sum2 = vector[0]; } } else if (sum2 < 0) { if (sum2 + vector[i] > 0) { sum2 += vector[i]; } else { answer2 += abs((-1) * sum2 + 1 - vector[i]); sum2 = 1; } } else { if (sum2 + vector[i] < 0) { sum2 += vector[i]; } else { answer2 += abs((-1) * sum2 - 1 - vector[i]); sum2 = -1; } } } cout << min(answer1, answer2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Linq; namespace ABC059_C{ class Program{ static void Main(string[] args){ var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); var ans1 = 0; var ans2 = 0; var flag = true; var sum = 0; for(var i = 0;i < n;i++){ sum += a[i]; if(flag){ if(sum <= 0){ ans1 += Math.Abs(sum) + 1; sum += Math.Abs(sum) + 1; } }else{ if(sum >= 0){ ans1 += Math.Abs(sum) + 1; sum -= Math.Abs(sum) + 1; } } flag = (flag) ? false : true; } sum = 0; flag = false; for(var i = 0;i < n;i++){ sum += a[i]; if(flag){ if(sum <= 0){ ans2 += Math.Abs(sum) + 1; sum += Math.Abs(sum) + 1; } }else{ if(sum >= 0){ ans2 += Math.Abs(sum) + 1; sum -= Math.Abs(sum) + 1; } } flag = (flag) ? false : true; } Console.WriteLine(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
python3
n = int(input()) a = list(map(int, input().split())) cnt = 0 flag = 0 for i in range(1,n): if a[0] != 0: flag = 1 break elif a[0] == 0 and a[i] != 0: sign = int(a[i]/a[i]) a[0] = ((-1)**i)*sign flag = 2 break else: continue if flag == 0: a[0] = 1 if flag != 1: cnt = 1 sum = a[0] s = [a[0]] for k in range(1,n): sum = sum + a[k] if s[k-1]*sum < 0: s.append(sum) flag = 3 else: if s[k-1] > 0: cnt = cnt + (sum + 1) sum = -1 s.append(sum) flag = 4 elif s[k-1] < 0: cnt = cnt + (-1)*(sum - 1) sum = 1 s.append(sum) flag = 5 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() { long long n, sum = 0, tmp = 0, c = 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; } if (b[0] > 0) { 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; } } else if (b[0] < 0) { 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; } } cout << c; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool DifSign(int a, int b) { if (a == 0 || b == 0) return false; return ((a > 0 && b < 0) || (a < 0 && b > 0)); } int main() { int N; int ans = 0; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; int sum = A[0]; for (int i = 1; i < N; i++) { if (!DifSign(sum, sum + A[i])) { int tmp = abs(sum + A[i]) + 1; ans += tmp; if (sum + A[i] > 0) A[i] -= tmp; else A[i] += tmp; } sum += A[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int n; long long a[MAXN]; long long s[MAXN]; void solve() { s[0] = a[0]; for (int i = 1; i < n; i++) { s[i] = s[i - 1] + a[i]; } long long cnt = 0; long long carry = 0; for (int i = 1; i < n; i++) { if ((s[i] + carry) * s[i - 1] >= 0) { cnt += abs(s[i] + carry) + 1; if (s[i - 1] < 0) { carry += abs(s[i] + carry) + 1; s[i] = 1; } else { carry -= abs(s[i] + carry) + 1; s[i] = -1; } } else { s[i] += carry; } } cout << cnt << endl; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } 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() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int cnt1 = 0; int sum1 = 0; for (int i = 0; i < n; ++i) { sum1 += a[i]; if (i % 2 == 0 && sum1 <= 0) { cnt1 += (1 - sum1); sum1 = 1; } else if (i % 2 == 1 && sum1 >= 0) { cnt1 += (1 + sum1); sum1 = -1; } } int cnt2 = 0; int sum2 = 0; for (int i = 0; i < n; ++i) { sum2 += a[i]; if (i % 2 == 1 && sum2 <= 0) { cnt2 += (1 - sum2); sum2 = 1; } else if (i % 2 == 0 && sum2 >= 0) { cnt2 += (1 + sum2); sum2 = -1; } } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } long long sum = a.at(0); long long op_1 = 0; bool flag = sum > 0 ? 1 : 0; for (int j = 1; j < n; j++) { if (flag) { sum += a.at(j); if (sum >= 0) { op_1 += sum + 1; sum = -1; } flag = 0; } else { sum += a.at(j); if (sum <= 0) { op_1 += -1 * sum + 1; sum = 1; } flag = 1; } } sum = a.at(0); long long op_2 = 0; if (sum > 0) { sum = -1; op_2 += sum + 1; } else { sum = 1; op_2 += sum * -1 + 1; } for (int j = 1; j < n; j++) { if (flag) { sum += a.at(j); if (sum >= 0) { op_2 += sum + 1; sum = -1; } flag = 0; } else { sum += a.at(j); if (sum <= 0) { op_2 += -1 * sum + 1; sum = 1; } flag = 1; } } cout << (op_1 > op_2 ? op_2 : op_1) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } using namespace std; using pii = pair<int, int>; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int main() { long long N, A[100007], msum = 0, psum = 0, mct, pct; cin >> N; for (int i = 0; i < (N); i++) { cin >> A[i]; } for (int i = 0; i < (N); i++) { if (i % 2 == 0) { if (A[i] + psum > 0) { psum += A[i]; } else { pct += -(A[i] + psum) + 1; psum = 1; } if (A[i] + msum >= 0) { mct += A[i] + msum + 1; msum = -1; } else { msum += A[i]; } } else { if (A[i] + msum > 0) { msum += A[i]; } else { mct += -(A[i] + msum) + 1; msum = 1; } if (A[i] + psum >= 0) { pct += A[i] + psum + 1; psum = -1; } else { psum += A[i]; } } } cout << min(pct, mct) << 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 <typename T> void showvector(vector<T> v) { for (T x : v) cout << x << " "; cout << "\n"; } template <typename T> void showvector1(vector<T> v) { long long int n = v.size(); for (long long int i = 1; i <= n - 1; i++) cout << v[i] << "\n"; } template <typename T> void showset(set<T> s) { for (T x : s) cout << x << " "; cout << "\n"; } template <class T> void showvectorpair(vector<T> v) { for (auto it = v.begin(); it != v.end(); it++) cout << it->first << " " << it->second << "\n"; cout << "\n"; } template <typename T, typename P> void showmap(map<T, P> m) { for (auto it = m.begin(); it != m.end(); it++) cout << it->first << " " << it->second << "\n"; cout << "\n"; } template <typename T> bool comp(T a, T b) { return (a > b); } template <class T> bool comppair(T a, T b) { if (a.first == b.first) return (a.second > b.second); return (a.first > b.first); } bool sameparity(long long int a, long long int b) { return (a % 2 == b % 2); } bool difparity(long long int a, long long int b) { return !(a % 2 == b % 2); } bool isprime(long long int x) { if (x <= 1) return false; for (long long int i = 2; i <= sqrt(x); i++) { if (x % i == 0) return false; } return true; } bool iseven(long long int x) { return !(x % 2); } bool isodd(long long int x) { return (x % 2); } void vfun() { long long int n, k; cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int test = 1; while (test--) { long long int n; cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; long long int sum = v[0], psum = v[0], cnt = 0; if (v[0] == 0) { cnt = 1; if (v[1] > 0) sum = psum = -1; else sum = psum = 1; } for (long long int i = 1; i <= n - 1; i++) { sum += v[i]; if (psum > 0) { if (sum >= 0) { cnt += (sum + 1); sum = -1; } } else { if (sum <= 0) { cnt += (abs(sum) + 1); sum = 1; } } psum = sum; } long long int dcnt = abs(v[0]) + 1; if (v[0] > 0) sum = psum = -1; else if (v[0] < 0) sum = psum = 1; else { if (v[1] > 0) sum = psum = -1; else sum = psum = 1; } for (long long int i = 1; i <= n - 1; i++) { sum += v[i]; if (psum > 0) { if (sum >= 0) { dcnt += (sum + 1); sum = -1; } } else { if (sum <= 0) { dcnt += (abs(sum) + 1); sum = 1; } } psum = sum; } cout << min(dcnt, cnt) << "\n"; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys n = int(input()) a = [int(n) for n in input().split()] b = [n for n in a] sum = [0]*n sum[0] = a[0] ans = 0 for i in range(1,n): sum[i] = sum[i-1] if((sum[i]+a[i])*sum[i-1] >= 0): if(sum[i-1] > 0): ans+=sum[i-1] + a[i]+1 a[i]-=sum[i-1] + a[i]+1 else: ans+=1 - sum[i-1] - a[i] a[i]+=1 - sum[i-1] - a[i] sum[i] += a[i] sum[0] = 1 if b[0] < 0 else -1 ansa = abs(b[0]) + 1 for i in range(1,n): sum[i] = sum[i-1] if((sum[i]+b[i])*sum[i-1] >= 0): if(sum[i-1] > 0): ansa+=sum[i-1] + b[i]+1 b[i]-=sum[i-1] + b[i]+1 else: ansa+=1 - sum[i-1] - b[i] b[i]+=1 - sum[i-1] - b[i] sum[i] += b[i] print(min(ans,ansa)) # print(a) # print(sum)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { long long i, j, k, ans = 0, be, n, a; scanf("%lld%lld", &n, &be); for (i = 1; i < n; ++i) { scanf("%lld", &a); if (be > 0 && -1 < be + a) ans += be + a + 1, be = -1; else if (be < 0 && 1 > be + a) ans += 1 + -be - a, be = -1; else be += a; } printf("%lld", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int main() { ll n; cin >> n; vector<ll> a(n); (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n) cin >> a[i]; vector<ll> sum(n); sum[0] = a[0]; (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n - 1) sum[i + 1] = sum[i] + a[i + 1]; ll c = 0; ll d = 0; if (sum[0] == 0) { (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 1, n) { if (sum[i] == 0 and i != n - 1) continue; c = 1; d = (sum[i] < 0 ? 1 : -1); break; } } (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n - 1) { if ((d + sum[i]) * (d + sum[i + 1]) < 0) continue; c += abs(d + sum[i + 1]) + 1; if (d + sum[i] >= 0) d -= d + sum[i + 1] + 1; else d += abs(d + sum[i + 1]) + 1; } cout << c << 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 std.stdio, std.algorithm, std.conv, std.array, std.string, std.math; 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] - 1).abs, 1, as[1..$]); auto op3 = check((as[0] + 1).abs, -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
cpp
#include <bits/stdc++.h> using namespace std; struct point { int x; int y; }; int gcd(int m, int n) { if ((0 == m) || (0 == n)) return 0; while (m != n) { if (m > n) m = m - n; else n = n - m; } return m; } int lcm(int m, int n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } int input() { int x; cin >> x; return x; } int moji(char in) { int ans = (int)in - (int)'a'; if ((ans < 0) || (ans > 25)) { ans = 26; } return ans; } const int VV = 10; int cost[VV][VV]; int d[VV]; bool used[VV]; void dijkstra(int s) { fill(d, d + VV, 100000); fill(used, used + VV, false); d[s] = 0; while (true) { int v = -1; for (int u = 0; u < VV; u++) { if (!used[u] && (v == -1 || d[u] < d[v])) v = u; } if (v == -1) break; used[v] = true; for (int u = 0; u < VV; u++) { d[u] = min(d[u], d[v] + cost[v][u]); } } } int compare_int(const void* a, const void* b) { return *(int*)a - *(int*)b; } int binary_searchh(long long x, long long k[], int n) { int l = 0; int r = n; while (r - l >= 1) { int i = (l + r) / 2; if (k[i] == x) return i; else if (k[i] < x) l = i + 1; else r = i; } return -1; } struct File { int aa; int bb; File(const int& aa, const int& bb) : aa(aa), bb(bb) {} }; bool operator<(const File& a, const File& b) { return std::tie(a.aa, a.bb) < std::tie(b.aa, b.bb); } long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } long long lcm(long long a, long long b) { long long g = gcd(a, b); return a / g * b; } long long kaijo(long long x) { long long l = 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 + 7; long long sum = 1; for (int i = x; i > 0; i--) { sum *= i; if (sum > l) { sum %= l; } } return sum; } int main() { long long n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long sum = 0; long long tmp = a[0]; for (int i = 1; i < n; i++) { if (tmp >= 0) { tmp += a[i]; if (tmp > 0) { sum += tmp + 1; tmp = -1; } } else { tmp += a[i]; if (tmp <= 0) { sum += abs(tmp) + 1; tmp = 1; } } } long long summ = 0; long long tmpp = 0; if (a[0] > 0) { tmpp = -1; summ = a[0] + 1; } else { tmpp = 1; summ = a[0] + 1; } for (int i = 1; i < n; i++) { if (tmpp >= 0) { tmpp += a[i]; if (tmpp > 0) { summ += tmpp + 1; tmpp = -1; } } else { tmpp += a[i]; if (tmpp <= 0) { summ += abs(tmpp) + 1; tmpp = 1; } } } cout << min(sum, summ) << 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()) b=list(map(int,input().split())) a=b condition='' cnt=0 wa=0 for i in range(n): wa+=a[i] if i == 0: if a[i]>0: condition='minus' else: condition='plus' elif condition == 'plus': condition='minus' if wa<=0: cnt+=abs(wa)+1 a[i]+=abs(wa)+1 wa+=abs(wa)+1 elif condition == 'minus': condition='plus' if wa>=0: cnt+=abs(wa)+1 a[i]-=abs(wa)+1 wa-=abs(wa)+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> template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } using namespace std; std::mt19937 mt((long long)time(0)); long long dx[4] = {0, 1, 0, -1}; long long dy[4] = {1, 0, -1, 0}; using Weight = long long; using Flow = long long; struct Edge { long long src, dst; Weight weight; Flow cap; Edge() : src(0), dst(0), weight(0) {} Edge(long long s, long long d, Weight w) : src(s), dst(d), weight(w) {} }; using Edges = std::vector<Edge>; using Graph = std::vector<Edges>; using Array = std::vector<Weight>; using Matrix = std::vector<Array>; void add_edge(Graph& g, long long a, long long b, Weight w = 1) { g[a].emplace_back(a, b, w); g[b].emplace_back(b, a, w); } void add_arc(Graph& g, long long a, long long b, Weight w = 1) { g[a].emplace_back(a, b, w); } struct uf_tree { std::vector<long long> parent; long long __size; uf_tree(long long size_) : parent(size_, -1), __size(size_) {} void unite(long long x, long long y) { if ((x = find(x)) != (y = find(y))) { if (parent[y] < parent[x]) std::swap(x, y); parent[x] += parent[y]; parent[y] = x; __size--; } } bool is_same(long long x, long long y) { return find(x) == find(y); } long long find(long long x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); } long long size(long long x) { return -parent[find(x)]; } long long size() { return __size; } }; template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(signed y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} mod_int& operator+=(const mod_int& rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int& operator-=(const mod_int& rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int& operator*=(const mod_int& rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int& operator/=(const mod_int& rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int& rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int& rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int& rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int& rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int& rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (long long i = 2; i <= (long long)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * signed(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (long long i = (long long)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <signed M, unsigned F> std::ostream& operator<<(std::ostream& os, const mod_int<M, F>& rhs) { return os << rhs.x; } template <signed M, unsigned F> std::istream& operator>>(std::istream& is, mod_int<M, F>& rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <signed M, unsigned F> bool operator==(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) { return lhs.x == rhs.x; } template <long long M, unsigned F> bool operator!=(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) { return !(lhs == rhs); } const signed MF = 1000010; const signed MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(long long n, long long r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(long long n) { return mint(n).fact(); } mint inv_fact(long long n) { return mint(n).inv_fact(); } template <typename T, typename E> struct SegmentTree { typedef function<T(T, T)> F; typedef function<T(T, E)> G; typedef function<E(E, E)> H; typedef function<E(E, long long)> P; long long n; F f; G g; H h; P p; T d1; E d0; vector<T> dat; vector<E> laz; SegmentTree( long long n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(), P p = [](E a, long long b) { return a; }) : f(f), g(g), h(h), d1(d1), d0(d0), p(p) { init(n_); if (n_ == (long long)v.size()) build(n_, v); } void init(long long n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, d1); laz.clear(); laz.resize(2 * n - 1, d0); } void build(long long n_, vector<T> v) { for (long long i = 0; i < n_; i++) dat[i + n - 1] = v[i]; for (long long i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } inline void eval(long long len, long long k) { if (laz[k] == d0) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = d0; } T update(long long a, long long b, E x, long long k, long long l, long long r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(long long a, long long b, E x) { return update(a, b, x, 0, 0, n); } T query(long long a, long long b, long long k, long long l, long long r) { eval(r - l, k); if (r <= a || b <= l) return d1; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(long long a, long long b) { return query(a, b, 0, 0, n); } }; class compress { public: static const long long MAP = 10000000; map<long long, long long> zip; long long unzip[MAP]; compress(vector<long long>& x) { sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); for (long long i = 0; i < x.size(); i++) { zip[x[i]] = i; unzip[i] = x[i]; } } }; unsigned euclidean_gcd(unsigned a, unsigned b) { while (1) { if (a < b) swap(a, b); if (!b) break; a %= b; } return a; } template <class T> struct CumulativeSum2D { vector<vector<T>> data; CumulativeSum2D(long long W, long long H) : data(W + 1, vector<long long>(H + 1, 0)) {} void add(long long x, long long y, T z) { ++x, ++y; if (x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build() { for (long long i = 1; i < data.size(); i++) { for (long long j = 1; j < data[i].size(); j++) { data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(long long sx, long long sy, long long gx, long long gy) { return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; long long nC2(long long n) { return n * (n - 1) / 2; } class node { public: long long depth; long long num; node(long long d, long long n) { depth = d; num = n; } }; CumulativeSum2D<long long> sumB(4001, 4001); template <class T> struct CumulativeSum { vector<T> data; CumulativeSum(long long sz) : data(sz, 0){}; void add(long long k, T x) { data[k] += x; } void build() { for (long long i = 1; i < data.size(); i++) { data[i] += data[i - 1]; } } T query(long long k) { if (k < 0) return (0); return (data[min(k, (long long)data.size() - 1)]); } T query(long long left, long long right) { return query(right) - query(left - 1); } }; std::vector<bool> IsPrime; void sieve(size_t max) { if (max + 1 > IsPrime.size()) { IsPrime.resize(max + 1, true); } IsPrime[0] = false; IsPrime[1] = false; for (size_t i = 2; i * i <= max; ++i) if (IsPrime[i]) for (size_t j = 2; i * j <= max; ++j) IsPrime[i * j] = false; } vector<int64_t> divisor(int64_t n) { vector<int64_t> ret; for (int64_t i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } long long binary_search(function<bool(long long)> isOk, long long ng, long long ok) { while (abs(ok - ng) > 1) { long long mid = (ok + ng) / 2; if (isOk(mid)) ok = mid; else ng = mid; } return ok; } std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s) { long long n = g.size(); const Weight inf = std::numeric_limits<Weight>::max() / 8; Edges es; for (long long i = 0; i < n; i++) for (auto& e : g[i]) es.emplace_back(e); std::vector<Weight> dist(n, inf); dist[s] = 0; bool negCycle = false; for (long long i = 0;; i++) { bool update = false; for (auto& e : es) { if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; update = true; } } if (!update) break; if (i > n) { negCycle = true; break; } } return std::make_pair(dist, !negCycle); } std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s, long long d) { long long n = g.size(); const Weight inf = std::numeric_limits<Weight>::max() / 8; Edges es; for (long long i = 0; i < n; i++) for (auto& e : g[i]) es.emplace_back(e); std::vector<Weight> dist(n, inf); dist[s] = 0; bool negCycle = false; for (long long i = 0; i < n * 2; i++) { bool update = false; for (auto& e : es) { if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; update = true; if (e.dst == d && i == n * 2 - 1) negCycle = true; } } if (!update) break; } return std::make_pair(dist, !negCycle); } vector<long long> Manachar(string S) { long long len = S.length(); vector<long long> R(len); long long i = 0, j = 0; while (i < S.size()) { while (i - j >= 0 && i + j < S.size() && S[i - j] == S[i + j]) ++j; R[i] = j; long long k = 1; while (i - k >= 0 && i + k < S.size() && k + R[i - k] < j) R[i + k] = R[i - k], ++k; i += k; j -= k; } return R; } std::vector<long long> tsort(const Graph& g) { long long n = g.size(), k = 0; std::vector<long long> ord(n), in(n); for (auto& es : g) for (auto& e : es) in[e.dst]++; std::queue<long long> q; for (long long i = 0; i < n; ++i) if (in[i] == 0) q.push(i); while (q.size()) { long long v = q.front(); q.pop(); ord[k++] = v; for (auto& e : g[v]) { if (--in[e.dst] == 0) { q.push(e.dst); } } } return *std::max_element(in.begin(), in.end()) == 0 ? ord : std::vector<long long>(); } std::vector<Weight> dijkstra(const Graph& g, long long s) { const Weight INF = std::numeric_limits<Weight>::max() / 8; using state = std::tuple<Weight, long long>; std::priority_queue<state> q; std::vector<Weight> dist(g.size(), INF); dist[s] = 0; q.emplace(0, s); while (q.size()) { Weight d; long long v; std::tie(d, v) = q.top(); q.pop(); d *= -1; if (dist[v] < d) continue; for (auto& e : g[v]) { if (dist[e.dst] > dist[v] + e.weight) { dist[e.dst] = dist[v] + e.weight; q.emplace(-dist[e.dst], e.dst); } } } return dist; } Matrix WarshallFloyd(const Graph& g) { auto const INF = std::numeric_limits<Weight>::max() / 8; long long n = g.size(); Matrix d(n, Array(n, INF)); for (long long i = (0); i < (long long)(n); i++) d[i][i] = 0; for (long long i = (0); i < (long long)(n); i++) for (auto& e : g[i]) d[e.src][e.dst] = std::min(d[e.src][e.dst], e.weight); for (long long k = (0); k < (long long)(n); k++) for (long long i = (0); i < (long long)(n); i++) for (long long j = (0); j < (long long)(n); j++) { if (d[i][k] != INF && d[k][j] != INF) { d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]); } } return d; } const long long BLACK = 1, WHITE = 0; bool isValid(vector<vector<long long>>& mapData, long long gyo, long long retu) { bool f = true; for (long long i = (0); i < (long long)(gyo); i++) { for (long long j = (0); j < (long long)(retu); j++) { long long colorCnt = 0; if (j > 0 && mapData[i][j] == mapData[i][j - 1]) { colorCnt++; } if (i > 0 && mapData[i][j] == mapData[i - 1][j]) { colorCnt++; } if (i < gyo - 1 && mapData[i][j] == mapData[i + 1][j]) { colorCnt++; } if (j < retu - 1 && mapData[i][j] == mapData[i][j + 1]) { colorCnt++; } if (colorCnt > 1) { f = false; } } } return f; } void getNext(long long nowX, long long nowY, long long* pOutX, long long* pOutY, long long gyo, long long retu) { if (nowX == retu - 1) { *pOutY = nowY + 1; *pOutX = 0; return; } *pOutX = nowX + 1; *pOutY = nowY; } void dfs(vector<vector<long long>> mapData, long long nowX, long long nowY, long long gyo, long long retu, long long* outCnt) { if (nowX == retu - 1 && nowY == gyo - 1) { mapData[nowY][nowX] = BLACK; if (isValid(mapData, gyo, retu)) { *outCnt = *outCnt + 1; } mapData[nowY][nowX] = WHITE; if (isValid(mapData, gyo, retu)) { *outCnt = *outCnt + 1; } return; } mapData[nowY][nowX] = BLACK; long long nextX, nextY; getNext(nowX, nowY, &nextX, &nextY, gyo, retu); dfs(mapData, nextX, nextY, gyo, retu, outCnt); mapData[nowY][nowX] = WHITE; getNext(nowX, nowY, &nextX, &nextY, gyo, retu); dfs(mapData, nextX, nextY, gyo, retu, outCnt); } void dec(map<long long, long long>& ma, long long a) { ma[a]--; if (ma[a] == 0) { ma.erase(a); } } long long N; long long solve(long long ans, vector<long long> A, vector<long long> cu) { for (long long i = (0); i < (long long)(N); i++) { if (cu[i] == 0) { ans++; if (i == 0) { if (cu[i + 1] < 0) { cu[i] = 1; } else { cu[i] = -1; } } else { if (cu[i - 1] < 0) { cu[i] = 1; } else { cu[i] = -1; } } } if (i == N - 1) { break; } if (cu[i] < 0 == cu[i + 1] < 0) { if (cu[i + 1] > 0) { ans += cu[i + 1] + 1; cu[i + 1] -= cu[i + 1] + 1; } else { ans += -cu[i + 1] + 1; cu[i + 1] += -cu[i + 1] + 1; } } cu[i + 2] = cu[i + 1] + A[i + 2]; } return ans; } signed main() { cin >> N; vector<long long> A(N + 2), A2; vector<long long> cu(N + 2); long long su = 0; for (long long i = (0); i < (long long)(N); i++) { cin >> A[i]; su += A[i]; cu[i] = su; } long long ans1 = 0, ans2 = 0; ans1 = solve(ans1, A, cu); if (A[0] < 0) { ans2 = -A[0] + 1; A[0] = 1; } else { ans2 = A[0] + 1; A[0] = -1; } su = 0; for (long long i = (0); i < (long long)(N); i++) { su += A2[i]; cu[i] = su; } ans2 = solve(ans2, A2, cu); 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; vector<int> arr; stack<int> st; queue<int> qu; queue<pair<int, int> > qu2; priority_queue<int> pq; static const int NIL = -1; static const int INF = 1000000007; int a[100005]; int b[100005]; int main() { int n; cin >> n; for (int i = 0; i < (int)(n); i++) { cin >> a[i]; b[i] = a[i]; } int temp1 = 0; for (int i = 0; i < (int)(n); i++) { b[i] = b[i - 1] + b[i]; if (i % 2) { if (b[i] <= 0) { temp1 += 1 - b[i]; b[i] = 1; } } else { if (b[i] >= 0) { temp1 += b[i] + 1; b[i] = -1; } } } for (int i = 0; i < (int)(n); i++) { cerr << b[i]; } cerr << endl; int temp2 = 0; for (int i = 0; i < (int)(n); i++) { a[i] = a[i - 1] + a[i]; if (i % 2 == 0) { if (a[i] <= 0) { temp2 += 1 - a[i]; a[i] = 1; } } else { if (a[i] >= 0) { temp2 += a[i] + 1; a[i] = -1; } } } for (int i = 0; i < (int)(n); i++) { cerr << a[i]; } cerr << endl; int ans = min(temp1, temp2); 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())) #値のコピーを作るときはスライスする B=A[:] #a1の符号を正にする場合 count_1=0 sum_1=0 if A[0]==0: A[0]+=1 count_1+=1 sum_1+=A[0] elif A[0]>0: sum_1+=A[0] elif A[0]<0: A[0]=1 count_1+=(abs(A[0])+1) sum_1+=A[0] for i in range(1,n): #奇数番目の項までの和は正に if i%2==0:#奇数番目の項 if sum_1+A[i]>0: sum_1+=A[i] elif sum_1+A[i]<0: count_1 += (abs(sum_1+A[i])+1) sum_1=1 elif sum_1+A[0]==0: count_1+=1 sum_1=1 else: pass #偶数番目の項までの和は負に elif i%2==1:#偶数番目の項 if sum_1+A[i]>0: count_1 += (abs(sum_1+A[i])+1) sum_1=-1 elif sum_1+A[i]<0: sum_1+=A[i] elif sum_1+A[i]==0: sum_1 = -1 count_1+=1 else: pass #a1の符号を負にする場合 count_2=0 sum_2=0 if B[0]==0: B[0]-=1 count_2+=1 sum_2+=B[0] elif B[0]>0: B[0]= -1 count_2+=(abs(B[0])+1) sum_2+=B[0] elif B[0]<0: sum_2+=B[0] for i in range(1,n): if i%2==0:#奇数番目の項 #奇数番目の項までの和は負に if sum_2+B[i]>0: count_2 += (abs(sum_2+B[i])+1) sum_2= -1 elif sum_2+B[i]<0: sum_2+=B[i] elif sum_2+B[i]==0: sum_2 = -1 count_2+=1 else: pass #偶数番目の項までの和は負に elif i%2==1:#偶数番目の項 if sum_2+B[i]>0: sum_2+=B[i] elif sum_2+B[i]<0: count_2 += (abs(sum_2+B[i])+1) sum_2=1 elif sum_2+B[i]==0: count_2+=1 sum_2=1 else: pass print(min(count_1,count_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
// -*- coding:utf-8-unix -*- use proconio::{input, fastout}; // ABC086C - Traveling // https://atcoder.jp/contests/abs/fasks/arc089_a #[fastout] fn main() { input! { n: usize, a_vec: [isize; n], } let mut count = 0; let mut prevsum = a_vec[0]; let mut nowsum; for i in 1..n { nowsum = prevsum + a_vec[i]; if nowsum*prevsum < 0 { prevsum = nowsum; continue; } if prevsum > 0 { // Make nowsum negative count += nowsum.abs() + 1; prevsum = -1; } else {// prevsum < 0 // Make nowsum positive count += nowsum.abs() + 1; prevsum = 1; } } println!("{}", count); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long change_num(long long p[], int N) { long long res = 0; long long sum = p[0]; for (int i = 1; i < N; i++) { if ((sum < 0 && (sum + p[i]) > 0) || (sum > 0 && (sum + p[i]) < 0)) { sum += p[i]; continue; } if (sum > 0 && sum + p[i] >= 0) { sum += p[i]; while (sum >= 0) { res++; sum--; } continue; } if (sum < 0 && sum + p[i] <= 0) { sum += p[i]; while (sum <= 0) { res++; sum++; } continue; } } return res; } int main() { int N; cin >> N; long long a[N]; for (int i = 0; i < N; i++) cin >> a[i]; long long ans = 0; long long sum = a[0]; if (a[0] == 0) { long long plus_ans = 0; a[0] = 1; plus_ans = change_num(a, N) + 1; long long minus_ans = 0; a[0] = -1; minus_ans = change_num(a, N) + 1; if (plus_ans < minus_ans) { ans = plus_ans; } else { ans = minus_ans; } } else { ans = change_num(a, N); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = [int(x) for x in input().split()] flag = True if A[0] > 0 else False count = 0 for i in range(n): if i == 0: continue sum_for_i = sum(A[:i]) sum_for_next = sum(A[:i+1]) if (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))): continue else: #print("needs to be changed: A[{}] ({})".format(i, A[i])) while not (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))): if (flag and not i%2) or (not flag and i%2): A[i] += 1 count += 1 else: A[i] -= 1 count += 1 sum_for_i = sum(A[:i]) sum_for_next = sum(A[:i+1]) print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) sum=A[0] ans=0 for i in range(n-1):#1_n-1 if sum*(sum+A[i+1])<0: sum+=A[i+1] else: ans+=abs(sum+A[i+1])+1 if sum>0: sum=-1 else: sum=1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{BufWriter, Write}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes .by_ref() .map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, [graph1; $len:expr]) => {{ let mut g = vec![vec![]; $len]; let ab = read_value!($next, [(usize1, usize1)]); for (a, b) in ab { g[a].push(b); g[b].push(a); } g }}; ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } #[allow(unused)] macro_rules! debug { ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap()); } #[allow(unused)] macro_rules! debugln { ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap()); } /* mod mod_int { use std::ops::*; pub trait Mod: Copy { fn m() -> i64; } #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> } impl<M: Mod> ModInt<M> { // x >= 0 pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) } fn new_internal(x: i64) -> Self { ModInt { x: x, phantom: ::std::marker::PhantomData } } pub fn pow(self, mut e: i64) -> Self { debug_assert!(e >= 0); let mut sum = ModInt::new_internal(1); let mut cur = self; while e > 0 { if e % 2 != 0 { sum *= cur; } cur *= cur; e /= 2; } sum } #[allow(dead_code)] pub fn inv(self) -> Self { self.pow(M::m() - 2) } } impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> { type Output = Self; fn add(self, other: T) -> Self { let other = other.into(); let mut sum = self.x + other.x; if sum >= M::m() { sum -= M::m(); } ModInt::new_internal(sum) } } impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> { type Output = Self; fn sub(self, other: T) -> Self { let other = other.into(); let mut sum = self.x - other.x; if sum < 0 { sum += M::m(); } ModInt::new_internal(sum) } } impl<M: Mod, T: Into<ModInt<M>>> Mul<T> for ModInt<M> { type Output = Self; fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) } } impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> { fn add_assign(&mut self, other: T) { *self = *self + other; } } impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> { fn sub_assign(&mut self, other: T) { *self = *self - other; } } impl<M: Mod, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> { fn mul_assign(&mut self, other: T) { *self = *self * other; } } impl<M: Mod> Neg for ModInt<M> { type Output = Self; fn neg(self) -> Self { ModInt::new(0) - self } } impl<M> ::std::fmt::Display for ModInt<M> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { self.x.fmt(f) } } impl<M: Mod> ::std::fmt::Debug for ModInt<M> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { let (mut a, mut b, _) = red(self.x, M::m()); if b < 0 { a = -a; b = -b; } write!(f, "{}/{}", a, b) } } impl<M: Mod> From<i64> for ModInt<M> { fn from(x: i64) -> Self { Self::new(x) } } // Finds the simplest fraction x/y congruent to r mod p. // The return value (x, y, z) satisfies x = y * r + z * p. fn red(r: i64, p: i64) -> (i64, i64, i64) { if r.abs() <= 10000 { return (r, 1, 0); } let mut nxt_r = p % r; let mut q = p / r; if 2 * nxt_r >= r { nxt_r -= r; q += 1; } if 2 * nxt_r <= -r { nxt_r += r; q -= 1; } let (x, z, y) = red(nxt_r, r); (x, y - q * z, z) } } // mod mod_int macro_rules! define_mod { ($struct_name: ident, $modulo: expr) => { #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] struct $struct_name {} impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } } } } const MOD: i64 = 1_000_000_007; define_mod!(P, MOD); type ModInt = mod_int::ModInt<P>; //n^p mod m fn repeat_square(n: i64, p: i64, m: i64) -> i64 { if p == 0 { 1 } else if p == 1 { n % m } else if p % 2 == 0 { repeat_square(n, p / 2, m).pow(2) % m } else { (n * repeat_square(n, p - 1, m)) % m } } fn ncr_mod(n: i64, r: i64, m: i64) -> i64 { let mut denominator = n; let mut numerator = 1; for i in 1..r { denominator = (denominator * (n - i)) % m; numerator = (numerator * (i + 1)) % m; } (denominator * repeat_square(numerator, m - 2, m)) % m } */ fn solve() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($($format:tt)*) => (let _ = write!(out,$($format)*);); } input! { n: usize, a: [i32; n], } let mut cnt_odd = 0; let mut cnt_even = 0; let mut cum_1 = vec![0; n]; let mut cum_2 = vec![0; n]; //cum_1[even] < 0,cum_2[odd] < 0 if a[0] >= 0 { cnt_even += a[0].abs() + 1; cum_1[0] = a[0]; cum_2[0] = -a[0]; } else { cnt_odd += a[0].abs() + 1; cum_1[0] = -a[0]; cum_2[0] = a[0]; } //+ - + - for i in 1..n { cum_1[i] = cum_1[i-1] + a[i]; if i % 2 == 1 { if cum_1[i] >= 0 { cnt_odd += cum_1[i].abs() + 1; cum_1[i] = -1; } } else { if cum_1[i] <= 0 { cnt_odd += cum_1[i].abs() + 1; cum_1[i] = 1; } } } //- + - + for i in 1..n { cum_2[i] = cum_2[i-1] + a[i]; if i % 2 != 1 { if cum_2[i] >= 0 { cnt_even += cum_2[i].abs() + 1; cum_2[i] = -1; } } else { if cum_2[i] <= 0 { cnt_even += cum_2[i].abs() + 1; cum_2[i] = 1; } } } puts!("{}\n",min(cnt_odd, cnt_even)); } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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) { long long n, b, c = 0; cin >> n >> b; for (int i = 0; i < n - 1; i++) { long long a; cin >> a; a += b; if (a * b >= 0) { if (b > 0) { c += a + 1; a = -1; } else { c += 1 - a; a = 1; } } b = a; } 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
cpp
#include <bits/stdc++.h> using namespace std; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } signed main() { long long n; cin >> n; vector<long long> a(n); for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i]; long long sum = 0; long long prev = 0; sum += a[0]; long long ans = 0; for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum < 0) { ans += abs(sum) + 1; sum = 1; } else { ans++; sum = (prev < 0 ? 1 : -1); } } } sum = 0; prev = 0; long long ans2 = 0; sum += a[0]; if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum < 0) { ans2 += abs(sum) + 1; sum = 1; } else { ans2++; sum = 1; } for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum < 0) { ans2 += abs(sum) + 1; sum = 1; } else { ans2++; sum = (prev < 0 ? 1 : -1); } } } sum = 0; prev = 0; long long ans3 = 0; sum += a[0]; if (sum > 0) { ans3 += sum + 1; sum = -1; } else if (sum < 0) { ans3 += abs(sum) + 1; sum = 1; } else { ans3++; sum = -1; } for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans3 += sum + 1; sum = -1; } else if (sum < 0) { ans3 += abs(sum) + 1; sum = 1; } else { ans3++; sum = (prev < 0 ? 1 : -1); } } } cout << min(ans, min(ans, ans2)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, a[100001], b[100001]{}, ans = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] != 0) { b[0] += a[0]; } else { ans++; int j = 1; while (j < n && a[j] == 0) j++; if (j != n) { b[0] = (a[j] / abs(a[j])); if (j % 2 == 1) b[0] *= -1; } else b[0] = 1; } for (int i = 1; i < n; i++) { b[i] += a[i] + b[i - 1]; if (b[i] * (b[i - 1] / abs(b[i - 1])) >= 0) { if (b[i - 1] > 0) { 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
cpp
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long #define mp make_pair #define pb push_back #define ff first #define ss second #define set0(a) memset ((a), 0 , sizeof(a)) #define set1(a) memset((a),-1,sizeof (a)) #define pi pair<int, int> #define ps pair<string, string> #define pl pair<long, long> #define pll pair<long long, long long> #define vll vector<long long> #define vl vector<long> #define vi vector<int> #define vs vector<string> #define vps vector< ps > #define vpi vector< pi > #define vpl vector< pl > #define vpll vector< pll > #define flash ios_base::sync_with_stdio(false); cin.tie(NULL); #define tc(t) for(long long l=0;l<t;l++) #define rep(i,s,n,d) for(long long i=s;i<n;i=i+d) bool sortbysec(const pll &a, const pll &b) { return (a.second < b.second); } void func(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n,1){ cin>>a[i]; } ll count1=0; if(a[0]==0){ if(a[1]>0){ a[0]=1; } else a[0]=-1; count1++; } ll sum[n]={}; sum[0]=a[0]; rep(i,1,n,1){ sum[i]=sum[i-1]+a[i]; } ll sum1=a[0]; rep(i,1,n,1){ ll d=0; ll dif=0; if(sum1>0){ if(a[i]+sum1>=0){ d=-1; dif=abs(a[i]+sum1-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } else{ if(a[i]+sum1<=0){ d=1; dif=abs(a[i]+sum1-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } } cout<<count1<<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> std::vector<int> seq, sum; int main() { int n; std::cin >> n; seq.resize(n); sum.resize(n); std::cin >> seq[0]; sum[0] = seq[0]; for (int i = 1; i < n; i++) { std::cin >> seq[i]; sum[i] = sum[i - 1] + seq[i]; } bool is_plus = true; long ans = 0; int dif = 0; if (sum[0] <= 0) { dif = 1 - sum[0]; ans = dif; } for (int i = 1; i < sum.size(); i++) { if (is_plus && sum[i] + dif >= 0) { int tmp = -(sum[i] + dif) - 1; dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } else if (!is_plus && sum[i] + dif <= 0) { int tmp = 1 - (sum[i] + dif); dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } is_plus = !is_plus; } long ya = ans; ans = 0; is_plus = false; if (sum[0] > 0) { dif = -1 - sum[0]; ans = -dif; } else { dif = 0; } for (int i = 1; i < sum.size(); i++) { if (is_plus && sum[i] + dif >= 0) { int tmp = -(sum[i] + dif) - 1; dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } else if (!is_plus && sum[i] + dif <= 0) { int tmp = 1 - (sum[i] + dif); dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } is_plus = !is_plus; } std::cout << (ya < ans ? ya : ans) << 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
cpp
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); int main() { int N; scanf("%d", &N); vector<int> arr(N); for (int i = (0); i < (N); ++i) { scanf("%d", &arr[i]); } int count = 0; int sum = 0; bool positive = true; if (arr[0] < 0) { positive = false; } sum += arr[0]; for (int i = 1; i < N; i++) { if (positive) { if (sum + arr[i] < 0) { sum = sum + arr[i]; positive = false; } else { int must = -sum - 1; int diff = abs(arr[i] - must); count += diff; sum += must; positive = false; } } else { if (sum + arr[i] > 0) { sum = sum + arr[i]; positive = true; } else { int must = -sum + 1; int diff = abs(must - arr[i]); count += diff; sum += must; positive = true; } } } printf("%d", count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; scanf("%d", &n); vector<long> a(n); for (long i = 0; i < n; i++) scanf(" %d", &a[i]); long sum = a[0]; long j = 0; for (long i = 1; i < n; i++) { if (sum * (sum + a[i]) < 0) sum += a[i]; else { j += abs(sum + a[i]) + 1; if (sum < 0) sum = 1; else if (sum > 0) sum = -1; } } printf("%ld\n", j); 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 sum = 0; long long res = 0; bool f = true; for (int i = 0; i < n; i++) { long long ai; cin >> ai; if (sum == 0) { sum += ai; if (sum > 0) { f = false; } else { f = true; } } else { sum += ai; if (f) { if (sum > 0) { f = false; } else { res += abs(sum - 1); sum = 1; f = false; } } else { if (sum < 0) { f = true; } else { res += sum + 1; sum = -1; f = true; } } } } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy n=int(input()) a=[int(i) for i in input().split()] ans=0 sum=0 if a[0]==0: a[0]=1 ans+=1 for j in a: if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0: ans+=abs(sum+j)+1 sum=-numpy.sign(sum) else: sum+=j pans=ans a[0]=-1 ans+=1 for j in a: if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0: ans+=abs(sum+j)+1 sum=-numpy.sign(sum) else: sum+=j mans=ans ans=min(pans,mans) else: for j in a: if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0: ans+=abs(sum+j)+1 sum=-numpy.sign(sum) else: sum+=j 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; int *a; int ans = 0; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0; int opr1 = 0, opr2 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum > 0) continue; opr1 += abs(sum) + 1; sum = 1; } else { if (sum < 0) continue; opr1 += abs(sum) + 1; sum = -1; } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum < 0) continue; opr2 += abs(sum) + 1; sum = -1; } else { if (sum > 0) continue; opr2 += abs(sum) + 1; sum = 1; } } ans = min(opr1, opr2); cout << ans << endl; delete (a); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n), es(n), os(n); for (int i = 0; i < n; i++) { cin >> a[i]; } es[0] = os[0] = a[0]; int ecnt = 0, ocnt = 0; for (int i = 0; i < n; i++) { if (i != 0) es[i] = es[i - 1] + a[i]; if (i % 2 == 0 && es[i] <= 0) { ecnt += abs(1 - es[i]); es[i] = 1; } else if (i % 2 != 0 && es[i] >= 0) { ecnt += abs(-1 - es[i]); es[i] = -1; } } for (int i = 0; i < n; i++) { if (i != 0) os[i] = os[i - 1] + a[i]; if (i % 2 == 0 && os[i] >= 0) { ocnt += abs(-1 - os[i]); os[i] = -1; } else if (i % 2 != 0 && os[i] <= 0) { ocnt += abs(1 - os[i]); os[i] = 1; } } cout << (ecnt < ocnt ? ecnt : ocnt) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long dptemp[100010]; long long s1[100010], dp[100010]; long long mi = 9223372036854775807, n, a, sum, pri1, pri2, all; void cir() { for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } void copyy() { for (int a = 1; a <= n; a++) dptemp[a] = dp[a]; } int main() { scanf("%lld", &n); dp[0] = 0; for (a = 1; a <= n; a++) { scanf("%lld", &s1[a]); dp[a] = s1[a] + dp[a - 1]; dptemp[a] = dp[a]; } if (dp[1] > 0) { copyy(); all = 0; cir(); copyy(); all = dp[1] + 1; dp[1] = -1; cir(); } else if (dp[1] < 0) { copyy(); all = 0; cir(); copyy(); all = -dp[1] + 1; dp[1] = 1; cir(); } else { copyy(); all = 1; dp[1] = 1; cir(); copyy(); all = -1; dp[1] = -1; cir(); } printf("%d\n", mi); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline bool feq(const double& a, const double& b) { return fabs(a - b) < 1e-10; } inline int gcd(int a, int b) { if (b == 0) return a; return a < b ? gcd(b, a) : gcd(b, a % b); } long long mo = 1000000007; const long long INF = 1e18; bool f(pair<long long, long long> p1, pair<long long, long long> p2) { return p1.first < p2.first; } int main() { int n; cin >> n; long long cnt = 0; long long sum = 0; cin >> sum; { long long a; cin >> a; a += sum; if ((sum < 0 && a > 0) || (sum > 0 && a < 0)) { sum = a; } else { if (abs(sum) < abs(a)) { cnt += (abs(sum) + 1); sum = a; } else { cnt += (abs(a) + 1); sum = (-1) * (sum > 0 ? 1 : -1); } } } for (int i = 0; i < n - 2; ++i) { long long a; cin >> a; a += sum; if ((sum < 0 && a > 0) || (sum > 0 && a < 0)) { sum = a; } else { cnt += (abs(a) + 1); sum = (-1) * (sum > 0 ? 1 : -1); } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import qualified Data.ByteString.Char8 as BC import Data.Maybe (fromJust) main = do n <- readLn :: IO Int (a:as) <- getIntListBC let ans1 = solve a as x = (+1) $ abs a ans2 = if a >= 0 then x + solve (a-x) as else x + solve (a+x) as print $ min ans1 ans2 bsToInt :: BC.ByteString -> Int bsToInt = fst . fromJust . BC.readInt getIntListBC :: IO [Int] getIntListBC = map bsToInt . BC.words <$> BC.getLine solve :: Int -> [Int] -> Int solve _ [] = 0 solve s (a:as) | s > 0 = let n = negate $ s + 1 in if n > a then solve (s + a) as else (abs $ a - n) + solve (s + n) as | s < 0 = let n = negate $ s - 1 in if n < a then solve (s + a) as else (abs $ n - a) + solve (s + n) as | otherwise = 1 + solve 1 as
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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> long body(std::vector<long>& a, long base) { long ans = 0; std::vector<long> s(a.size()); s.at(0) = a.at(0); for (unsigned long i = 1; i < a.size(); i++) { s.at(i) = s.at(i - 1) + a.at(i); } long diff = 0; long prev = base; for (unsigned long i = 0; i < s.size(); i++) { s.at(i) += diff; long n = 0; if (prev > 0 && s.at(i) >= 0) { n = s.at(i) + 1; ans += n; diff -= n; s.at(i) += diff; } else if (prev < 0 && s.at(i) <= 0) { n = -s.at(i) + 1; ans += n; diff += n; s.at(i) += diff; } prev = s.at(i); } return ans; } int main(int argc, char** argv) { long n; std::cin >> n; std::vector<long> a(n); for (long i = 0; i < n; i++) { std::cin >> a.at(i); } long ans_a, ans_b; ans_a = body(a, -1); ans_b = body(a, 1); long ans = std::min(ans_a, ans_b); std::cout << ans << 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
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; long long sum = a[0]; long long ans1 = 0; if (sum == 0) { ans1++; sum += 1; } for (int i = 0; i < n - 1; ++i) { if (sum < 0) { if (a[i + 1] + sum > 0) sum += a[i + 1]; else { ans1 += abs(1 - (a[i + 1] + sum)); sum = 1; } } else { if (a[i + 1] + sum < 0) sum += a[i + 1]; else { ans1 += abs(-1 - (a[i + 1] + sum)); sum = -1; } } } sum = a[0]; long long ans2 = 0; if (sum == 0) { ans2++; sum = -1; } for (int i = 0; i < n - 1; ++i) { if (sum < 0) { if (a[i + 1] + sum > 0) sum += a[i + 1]; else { ans2 += abs(1 - (a[i + 1] + sum)); sum = 1; } } else { if (a[i + 1] + sum < 0) sum += a[i + 1]; else { ans2 += 1 + (a[i + 1] + sum); sum = -1; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i as = gets.split.map(&:to_i) ans = 0 sums = Array.new(n) sums[0] = as[0] (1..n-1).each do |i| if sums[i-1] < 0 if as[i] + sums[i-1] <= 0 t = sums[i-1].abs - as[i] + 1 as[i] += t ans += t end else if as[i] + sums[i-1] >= 0 t = sums[i-1].abs + as[i] + 1 as[i] -= t ans += t end end sums[i] = sums[i-1] + as[i] end puts 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(void) { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int tmp1 = 0, tmp2 = 0; int ans1 = 0, ans2 = 0; for (int i = 0; i < n; ++i) { tmp1 += a[i]; if (i % 2 == 0) { if (tmp1 >= 0) { ans1 += 1 + tmp1; tmp1 = -1; } else { } } else { if (tmp1 <= 0) { ans1 += 1 - tmp1; tmp1 = -1; } else { } } } for (int i = 0; i < n; ++i) { tmp2 += a[i]; if (i % 2 == 0) { if (tmp2 <= 0) { ans2 += 1 - tmp2; tmp2 = 1; } else { } } else { if (tmp2 >= 0) { ans2 += 1 + tmp2; tmp2 = 1; } else { } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int s = a[0]; int cnt = 0; for (int i = 1; i < n; i++) { if (s > 0) { if (s + a[i] < 0) { s += a[i]; continue; } else if (s + a[i] == 0) { a[i]--; cnt++; s += a[i]; continue; } else if (s + a[i] > 0) { cnt += s + a[i] + 1; s = -1; continue; } } else if (s < 0) { if (s + a[i] > 0) { s += a[i]; continue; } else if (s + a[i] == 0) { a[i]++; cnt++; s += a[i]; continue; } else if (s + a[i] < 0) { cnt += abs(s + a[i]) + 1; s = 1; continue; } } else { cnt++; } } cout << cnt; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); } const long long int MOD = 1e9 + 7, INF = 1e18; long long int N, arr[100000], sums[100000]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { cin >> arr[i]; } bool flag; long long int sum = 0; long long int ans = 0; sums[0] = arr[0]; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { sums[i + 1] = arr[i + 1] + sums[i]; } flag = false; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { sums[i] += sum; if (flag ^ ((i % 2) == 1)) { if (sums[i] >= 0) { sum -= (sums[i] + 1); ans += abs(sums[i] + 1); sums[i] -= (sums[i] + 1); } } else { if (sums[i] <= 0) { sum -= (sums[i] - 1); ans += abs(sums[i] - 1); sums[i] -= (sums[i] - 1); } } } long long int tmp = ans; sum = 0; ans = 0; sums[0] = arr[0]; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { sums[i + 1] = arr[i + 1] + sums[i]; } flag = true; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { sums[i] += sum; if (flag ^ ((i % 2) == 1)) { if (sums[i] >= 0) { sum -= (sums[i] + 1); ans += abs(sums[i] + 1); sums[i] -= (sums[i] + 1); } } else { if (sums[i + 1] <= 0) { sum -= (sums[i] - 1); ans += abs(sums[i] - 1); sums[i] -= (sums[i] - 1); } } } cout << min(tmp, ans) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long mod = 998244353; int main() { long long n; cin >> n; vector<long long> a(n + 1); for (int i = (0); i < (n); i++) { long long aa; cin >> aa; a[i + 1] = a[i] + aa; } long long cnt1 = 0; long long cnt2 = 0; long long cnt = 0; for (int i = (1); i < (n + 1); i++) { if (i % 2) { if (a[i] + cnt <= 0) { cnt1 += 1 - a[i]; cnt += 1 - a[i]; } } else { if (a[i] + cnt >= 0) { cnt1 += 1 + a[i]; cnt += -a[i] - 1; } } } cnt = 0; for (int i = (1); i < (n + 1); i++) { if (i % 2) { if (a[i] + cnt >= 0) { cnt2 += 1 + a[i]; cnt += -a[i] - 1; } } else { if (a[i] + cnt <= 0) { cnt2 += 1 - a[i]; cnt += 1 - a[i]; } } } cout << min(cnt1, cnt2) << 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, b, a[100005]; int abs(int p) { return p > 0 ? p : -p; } int f(int p) { int i, s = 0; for (i = 1; i < n; i++) { if (p > 0) { p += a[i]; if (p >= 0) s += p + 1, p = -1; } else { p += a[i]; if (p <= 0) s += -p + 1, p = 1; } } return s; } int main() { int i; cin >> n; for (i = 0; i < n; i++) scanf("%d", &a[i]); cout << min(f(a[0]), f(-a[0])) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int A[100001]; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } int sum = 0; int counter = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 0) { while (sum <= 0) { sum++; counter++; } } else { while (sum >= 0) { sum--; counter++; } } } int counterNeg = 0; sum = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 0) { while (sum >= 0) { sum--; counterNeg++; } } else { while (sum <= 0) { sum++; counterNeg++; } } } int ans = counter > counterNeg ? counterNeg : counter; cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy import sys write = sys.stdout.write n = int(input()) A = list(map(int,input().split())) # +, -, +, ... B = copy.deepcopy(A) #-, +, -, ... sumA = [] sumB = [] cntA = 0 cntB = 0 if A[0] == 0: A[0] += 1 B[0] -= 1 elif A[0] > 0: cntB += B[0]+1 B[0] = -1 else: cntA += abs(A[0])+1 A[0] = 1 sumA.append(A[0]) sumB.append(B[0]) for i in range(1, n): tempA = sumA[i-1] + A[i] tempB = sumB[i-1] + B[i] if i%2 == 1: #Aは-, Bは+ if tempA == 0: #A[i] -= 1 cntA += 1 sumA.append(-1) elif tempA > 0: #A[i] -= abs(tempA) + 1 cntA += abs(tempA) + 1 sumA.append(-1) else: sumA.append(tempA) if tempB == 0: #B[i] += 1 cntB += 1 sumB.append(1) elif tempB < 0: #B[i] += abs(tempA) + 1 cntB += abs(tempA) + 1 sumB.append(1) else: sumB.append(tempB) else: #Aは+, Bは- if tempA == 0: cntA += 1 sumA.append(1) elif tempA < 0: cntA += abs(tempA) + 1 sumA.append(1) else: sumA.append(tempA) if tempB == 0: #B[i] -= 1 cntB += 1 sumB.append(-1) elif tempB > 0: #B[i] -= abs(tempB) + 1 cntB += abs(tempB) + 1 sumB.append(-1) else: sumB.append(tempB) print(str(min(cntA, cntB)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int tmp1 = 0, tmp2 = 0; int ans1 = 0, ans2 = 0; for (int i = 0; i < n; ++i) { tmp1 += a[i]; if ((i + 1) % 2 == 0) { if (tmp1 <= 0) { ans1 += 1 - tmp1; tmp1 = 1; } else { } } else { if (tmp1 >= 0) { ans1 += 1 + tmp1; tmp1 = -1; } else { } } } for (int i = 0; i < n; ++i) { tmp2 += a[i]; if ((i + 1) % 2 == 0) { if (tmp2 >= 0) { ans2 += 1 + tmp2; tmp2 = -1; } else { } } else { if (tmp2 <= 0) { ans2 += 1 - tmp2; tmp2 = 1; } else { } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, c = 0, b = 0; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (b > 0) { if (a[i] + b >= 0) { c += a[i] + b + 1; b = -1; } else { b += a[i]; } } else if (b < 0) { if (a[i] + b <= 0) { c += 1 - (a[i] + b); b = 1; } else { b += a[i]; } } else { if (a[i] == 0) { if (i == 0) { c++; } else { c += 2; } } else if (a[i] == 1 || a[i] == -1) { if (i == 0) { b = a[i]; } else { b = a[i]; c++; } } else if (a[i] > 1) { if (i == 0) { b = a[i]; } else { b = a[i] - 1; } } else { if (i == 0) { b = a[i]; } else { b = a[i] + 1; } } } } cout << c; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int cntEven = 0, cntOdd = 0, n; cin >> n; long long a[n], b[n], sumEven[n], sum[n]; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; if (i == 0) sumEven[i] = b[i]; else if (i > 0) sumEven[i] = b[i] + sumEven[i - 1]; if (i == 0) sum[i] = b[i]; else if (i > 0) sum[i] = b[i] + sum[i - 1]; if ((i % 2 == 0) && (sumEven[i] <= 0)) { while (sumEven[i] < 1) { b[i]++; cntEven++; if (i == 0) sumEven[i] = b[i]; else if (i > 0) sumEven[i] = b[i] + sumEven[i - 1]; } } if ((i % 2 == 1) && (sumEven[i] >= 0)) { while (sumEven[i] > -1) { b[i]--; cntEven++; sumEven[i] = b[i] + sumEven[i - 1]; } } if ((i % 2 == 1) && (sum[i] <= 0)) { while (sum[i] < 1) { a[i]++; cntOdd++; sum[i] = a[i] + sum[i - 1]; } } if ((i % 2 == 0) && (sum[i] >= 0)) { while (sum[i] > -1) { a[i]--; cntOdd++; if (i == 0) sum[i] = a[i]; else if (i > 0) sum[i] = a[i] + sum[i - 1]; } } } cout << min(cntEven, cntOdd) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AtCoder { class Program { static void Main(string[] args) { var n = long.Parse(Console.ReadLine()); var aList = Console.ReadLine().Split(' ').Select(value => long.Parse(value)).ToList(); if(aList[0] == 0) { var min = long.MaxValue; aList[0] = 1; min = Math.Min(Sequence(aList), min); aList[0] = -1; min = Math.Min(Sequence(aList), min); Console.WriteLine(min + 1); } else { Console.WriteLine(Sequence(aList)); } } private static long Sequence(List<long> list) { long sum = list[0]; var isPreviousSumPositive = list[0] > 0; long count = 0; for(var i = 1; i < list.Count; i++) { sum += list[i]; if(isPreviousSumPositive && sum >= 0) { count += sum + 1; sum = -1; } else if(!isPreviousSumPositive && sum <= 0) { count += -sum + 1; sum = 1; } if(sum > 0) { isPreviousSumPositive = true; } else { isPreviousSumPositive = false; } } return count; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n, i, check = 0; long long int a, count = 0, sum = 0; scanf("%d", &n); scanf("%lld", &a); for (i = 0; i < n; i++) { scanf("%lld", &a); if (i == 0 && a == 0) check = -1; sum += a; if (check == 1 && sum >= 0) { count += (1 + sum); sum = -1; } else if (check == -1 && sum <= 0) { count += (1 - sum); sum = 1; } if (sum >= 0) { check = 1; } else { check = -1; } } printf("%lld", count); return 0; }