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
java
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { // Your code here! BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] str_a = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(str_a[i]); } int sum = 0; int count = 0; sum += a[0]; if (sum == 0) { a[0]++; sum++; count++; } for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == (a[0]>0?1:0)) { while (sum >= 0) { a[i]--; sum--; count++; } } else { while (sum <= 0) { a[i]++; sum++; count++; } } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using std::abs; using std::cin; using std::cout; using std::endl; using std::max; using std::min; using std::priority_queue; using std::queue; using std::set; using std::sort; using std::string; using std::to_string; using std::vector; int main(void) { int n; cin >> n; vector<long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long sum; long cnt; sum = a[0] == 0 ? 1 : a[0]; cnt = a[0] == 0 ? 1 : 0; for (int i = 1; i < n; i++) { if (sum > 0) { sum += a[i]; if (sum >= 0) { cnt += sum + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { cnt += -sum + 1; sum = 1; } } } long ans = cnt; sum = a[0] == 0 ? -1 : a[0]; cnt = a[0] == 0 ? 1 : 0; for (int i = 1; i < n; i++) { if (sum > 0) { sum += a[i]; if (sum >= 0) { cnt += sum + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { cnt += -sum + 1; sum = 1; } } } ans = min(ans, cnt); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = list(map(int,input().split(" "))) def is_positive(sumval,modify): if sumval <= 0: modify += abs(sumval - 1) sumval = 1 return sumval,modify def is_negative(sumval,modify): if sumval >= 0: modify += abs(sumval + 1) sumval = (-1) return sumval,modify pos_sum,pos_modi = is_negative(a[0],0) neg_sum,neg_modi = is_positive(a[0],0) for i in range(1,N): tmp_sum,tmp_modi = is_positive(pos_sum + a[i],pos_modi) pos_sum,pos_modi = is_negative(neg_sum + a[i],neg_modi) neg_sum = neg_sum neg_modi = tmp_modi print(min(pos_modi,neg_modi))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long sum = 0; long long res1 = 0; long long res2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (sum + a[i] > 0) { sum += a[i]; } else { res1 = res1 + 1 - (sum + a[i]); sum = 1; } } else { if (sum + a[i] < 0) { sum += a[i]; } else { res1 = res1 + (sum + a[i]) + 1; sum = -1; } } } for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (sum + a[i] < 0) { } else { res2 = res2 + 1 + sum; a[i]; sum = -1; } } else { if (sum + a[i] > 0) { sum += a[i]; } else { res2 = res2 + 1 - sum - a[i]; sum = 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 getS(int val) { if (val < 0) return -1; if (val > 0) return 1; return 0; } int minNum(vector<int> &arr, int n, int sign) { int acu, sol = 0; for (int i = 0; i < n; i++) { acu += arr[i]; if (getS(acu) != sign) { sol += abs(sign - acu); acu = sign; } sign = sign * -1; } return sol; } int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; int a1 = minNum(arr, n, -1); int a2 = minNum(arr, n, 1); cout << (a1 < a2 ? a1 : a2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long long int MOD = 1e9 + 7; const long long int INF = 1e18; const long long int maxn = 1e6 + 4; void solve() { int n; cin >> n; vector<vector<pair<int, int> > > dp(n + 1, vector<pair<int, int> >(2)); dp[0][0] = {0, 0}; dp[0][0] = {0, 0}; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n; i++) { dp[i + 1][1].first = dp[i][0].first; if (-dp[i][0].second < v[i]) { dp[i + 1][1].second = v[i] + dp[i][0].second; } else { dp[i + 1][1].first += abs(-dp[i][0].second - v[i]) + 1; dp[i + 1][1].second = -dp[i][0].second + 1 + dp[i][0].second; } dp[i + 1][0].first = dp[i][1].first; if (-dp[i][0].second > v[i]) { dp[i + 1][0].second = v[i] + dp[i][1].second; } else { dp[i + 1][0].first += abs(-dp[i][1].second - v[i]) + 1; dp[i + 1][0].second = -dp[i][1].second - 1 + dp[i][1].second; } } cout << min(dp[n][0].first, dp[n][1].first); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 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> const long long INF = INT_MAX / 2; const long long MOD = 1000000007; const long double PI = 3.1415926; using namespace std; long long int n, ans = 0, sum = 0, cnt = 0; string s; vector<long long int> v; vector<long long int> v1; vector<pair<long long int, long long int> > vp; vector<pair<long long int, long long int> > vp1; vector<vector<long long int> > vv(50, vector<long long int>(50, INF)); vector<string> vs; vector<char> vc; set<long long int> st; map<char, long long int> mp; long long int cal(long long int a, long long int n) { for (long long int i = (long long int)(0); i < (long long int)(n); i++) { sum += v[i]; if (i % 2 == a && sum <= 0) { ans += (1 - sum); sum = 1; } else if (i % 2 != a && sum >= 0) { ans += (sum + 1); sum = -1; } } return ans; } int main() { cin >> n; v.resize(n); for (long long int i = (long long int)(0); i < (long long int)(n); i++) cin >> v[i]; cout << min(cal(0, n), cal(1, n)) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int cnt1 = 0; int sum1 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && sum1 + a[i] <= 0) { cnt1 += 1 - sum1 - a[i]; sum1 = 1; } else if (i % 2 == 1 && sum1 + a[i] >= 0) { cnt1 += sum1 + a[i] + 1; sum1 = -1; } else { sum1 += a[i]; } } int cnt2 = 0; int sum2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && sum2 + a[i] >= 0) { cnt2 += sum2 + a[i] + 1; sum2 = -1; } else if (i % 2 == 1 && sum2 + a[i] <= 0) { cnt2 += 1 - sum2 - a[i]; sum2 = 1; } else { sum2 += a[i]; } } if (cnt1 < cnt2) { cout << cnt1 << endl; } else { cout << 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
python3
n=int(input()) a=list(map(int,input().split())) ans=0 res=0 #0が正、1が負 sum=a[0] if a[0]<0: res=1 for i in range(n-1): sum = sum+a[i+1] if sum<0: if res==1: adj = 1-sum a[i+1]=a[i+1]+adj ans=ans+abs(adj) sum=sum+adj res = 0 else: res=1 elif sum>0: if res==0: adj = -1-sum a[i+1]=a[i+1]+adj ans=ans+abs(adj) sum=sum+adj res = 1 else: res=0 if sum==0: if res == 1: a[i+1]=a[i+1]+1 sum=sum+1 ans+=1 res=0 else: a[i+1]=a[i+1]-1 sum=sum-1 ans+=1 res=1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long ans = 0; if (sum == 0) { int i = 1; while (a[i] == 0) i++; sum = a[i] / abs(a[i]); if (i % 2 != 0) sum *= -1; ans++; } for (int i = 1; i < n; i++) { long long tmp = sum + a[i]; if (sum > 0 && tmp > 0) { ans += tmp + 1; sum = -1; } else if (sum < 0 && tmp < 0) { ans += -tmp + 1; sum = 1; } else if (tmp == 0) { ans++; if (sum < 0) sum = 1; else sum = -1; } else sum = tmp; } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5; bool flag, ok; long long sum, ans, anv; int n; int a[MaxN + 5], b[MaxN + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sum = a[1]; if (a[1] < 0) flag = 1, ok = 1; if (a[1] != 0) { for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + a[i] <= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = 1 - sum; ans += (a[i] - t); sum += a[i]; } else sum += a[i]; flag = 0; } else { if (sum + a[i] >= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = -1 - sum; ans += (t - a[i]); sum += a[i]; } else sum += a[i]; flag = 1; } } } if (a[1] == 0) ans = 1LL << 60; int tr = b[1]; if (ok) b[1] = 1, flag = 0; else b[1] = -1, flag = 1; anv += (abs(b[1] - tr)); sum = b[1]; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + b[i] <= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = 1 - sum; anv += (b[i] - t); sum += b[i]; } else sum += b[i]; flag = 0; } else { if (sum + b[i] >= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = -1 - sum; anv += (t - b[i]); sum += b[i]; } else sum += b[i]; flag = 1; } } printf("%lld\n", min(ans, anv)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; namespace ProgramingStudying2 { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).ToArray(); int Solve(int first) { var sum = first; var ans = 0; for(int i = 1; i < n; i++) { if (sum > 0) { sum += a[i]; if (sum >= 0) { ans += sum + 1; sum = -1; } } else if(sum < 0) { sum += a[i]; if (sum <= 0) { ans += -sum + 1; sum = 1; } } } return ans; } if(a[0] > 0) { Console.WriteLine(Math.Min(Solve(a[0]), Solve(-1) + a[0] + 1)); } else if(a[0] < 0) { Console.WriteLine(Math.Min(Solve(a[0]), Solve(1) - a[0] + 1)); } else { Console.WriteLine(Math.Min(Solve(1) + 1, Solve(-1) + 1)); } } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N=int(input()) s=list(map(int,input().split())) def f(N,s,t): ss=s[0] w=0 for i in range(N-1): if t==1: if ss+s[i+1]>=t: ss=ss+s[i+1] else: w+=t-ss-s[i+1] ss=1 t=-1 elif t==-1: if ss+s[i+1]<=t: ss=ss+s[i+1] else: w+=ss+s[i+1]-t ss=-1 t=1 return w if s[0]<0: t=1 print(f(N,s,t)) elif s[0]>0: t=-1 print(f(N,s,t)) else: t=1 a=f(N,s,t) t=-1 b=f(N,s,t) print(min(a,b)+1)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt(); long sum = 0, ans = 0, ans2 = 0; // + - + - + ... for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum <= 0) { ans += 1 - sum; sum = 1; } else if(i % 2 == 1 && sum >= 0) { ans += sum + 1; sum = -1; } } // - + - + - ... for(int i = 0 ; i < n ; i++) { sum += a[i]; if(i % 2 == 0 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if(i % 2 == 1 && sum <= 0) { ans2 += 1 - sum; sum = 1; } } System.out.println(Math.min(ans, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from itertools import accumulate def sgn(n): if n>0: return 1 elif n==0: return 0 else: return -1 n=int(input()) a=[int(i) for i in input().split()] b=list(accumulate(a)) #print(list(accumulate(a))) if b[0]>0: f=1 else: f=-1 ans=0 for i in range(n): #print(i,b) if ((i%2==0) and(sgn(b[i])!=f)) or ((i%2==1) and(sgn(b[i])!=-f)): tmp = abs(b[i])+1 ans += tmp for j in range(i,n): b[j] += -tmp*f*(-1)**(i-1) #print(b) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=[int(x) for x in input().split()] for num,i in enumerate(a): if i!=0: non0=num break if non0==0: ans=0 elif non0==1: ans=2 else: ans=(non0-2)*2+2 Sum=a[non0] if a[non0]>0: flag=+1 else: flag=-1 for i in range(non0+1,n): Sum+=a[i] if flag==1: if Sum>=0: ans+=Sum+1 Sum=-1 flag=-1 elif flag==-1: if Sum<=0: ans+=1-Sum Sum=1 flag=1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sum = 0; long long num = 0; for (int i = 0; i < n; i++) { long long a; cin >> a; if (i == 0) { sum = a; continue; } if (sum > 0) { sum += a; if (sum >= 0) { num += (sum + 1); sum = -1; } } else { sum += a; if (sum <= 0) { num += (-sum + 1); sum = 1; } } } 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; void ctsm(const long long &tmp, long long int &sm, long long int &ct) { if ((0 <= sm + tmp) && (0 < sm)) { ct += 1 + sm + tmp; sm = -1; } else if ((sm + tmp <= 0) && (sm < 0)) { ct += 1 - sm - tmp; sm = 1; } else sm = sm + tmp; } int main() { int n; if (scanf("%d", &n) < 1) return 0; long long int tmp; long long int sm = 0; long long int ct = 0; if (scanf("%lld", &tmp) < 1) return 0; sm = sm + tmp; long long int opsm = 0; long long int opct = 0; tmp = (-1) * tmp; opsm = opsm + tmp; opct += abs(tmp) + 1; for (int i = 1; i < n; i++) { if (scanf("%lld", &tmp) < 1) return 0; ctsm(tmp, sm, ct); ctsm(tmp, opsm, opct); } printf("%lld\n", ct < opct ? ct : opct); 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(1001); for (int i = 0; i < n; i++) { cin >> a[i]; } int c1 = 0, s1 = 0; int c2 = 0, s2 = 0; for (int i = 0; i < n; i++) { s1 += a[i]; s2 += a[i]; if (i % 2 == 0) { if (s1 <= 0) { c1 += 1 - s1; s1 = 1; } if (s2 >= 0) { c2 += s2 + 1; s2 = -1; } } else { if (s2 <= 0) { c2 += 1 - s2; s2 = 1; } if (s1 >= 0) { c1 += s1 + 1; s1 = -1; } } } cout << min(c1, c2) << 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
fun main(args: Array<String>) { val n = readLine()?.toInt() ?: return val aList = readLine()?.split(" ")?.map { it.toLong() } ?: return val v1 = calc(aList, true) val v2 = calc(aList, false) println(Math.min(v1, v2)) } private fun calc(list: List<Long>, type: Boolean): Long { var count = 0L var sum = if (type) Math.abs(list[0]) else -Math.abs(list[0]) if (sum == 0L) { sum += if (type) 1 else -1 count++ } for (i in 1 until list.size) { val sign = sum < 0 if (sign == (sum + list[i] > 0)) { sum += list[i] continue } val expected = if (sign) Math.abs(sum) + 1 else -Math.abs(sum) - 1 val diff = Math.abs(expected - list[i]) sum += expected count += diff } return count }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int A[n]; for (int i = 0; i < n; i++) scanf("%d", &A[i]); long long cnt1 = 0, cnt2 = 0, sum = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 0) { if (sum <= 0) { cnt1 += abs(sum) + 1; sum += abs(sum) + 1; } } else { if (sum >= 0) { cnt1 += abs(sum) + 1; sum -= abs(sum) + 1; } } } sum = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 1) { if (sum <= 0) { cnt2 += abs(sum) + 1; sum += abs(sum) + 1; } } else { if (sum >= 0) { cnt2 += abs(sum) + 1; sum -= abs(sum) + 1; } } } cout << cnt1 << " " << cnt2 << endl; cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, count, sum, count2, x, bsum, s; vector<int> a; cin >> n; a.resize(n); cin >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; } count = 0; sum = 0; for (int i = 0; i < n - 1; i++) { bsum = sum; sum += a[i]; if (sum * (sum + a[i + 1]) > 0) { x = abs(sum + a[i + 1]) + 1; s = abs(sum); if (a[i] * a[i + 1] < 0) { if (s - 1 > x) { a[i] = a[i] > 0 ? a[i] - x : a[i] + x; sum = bsum + a[i]; } else { a[i] = a[i] > 0 ? a[i] - (s - 1) : a[i] + (s - 1); sum = bsum + a[i]; s = x - (s - 1); a[i + 1] = a[i + 1] > 0 ? a[i + 1] + s : a[i + 1] - s; } } else { a[i + 1] = a[i + 1] > 0 ? -s - 1 : s + 1; } count += x; } if (sum + a[i + 1] == 0) { count++; a[i + 1] > 0 ? a[i + 1]++ : a[i + 1]--; } } cout << count; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int n; int a[100000]; int solve(int t) { int res = 0, sum = 0; for (int i = 0; i < n; i++, t = -t) { sum += a[i]; if (sum * t > 0) continue; res += abs(sum - t); sum += t * abs(sum - t); } return res; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int res = solve(1); res = min(res, solve(-1)); cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { long long int n, x; long long int cnt; vector<long long int> v, sum; cin >> n; for (long long int i = 0; i < n; i++) { cin >> x; v.push_back(x); sum.push_back(0); } sum[0] = v[0]; cnt = 0; if (sum[0] == 0 && sum[1] >= 0) { cnt++; v[0]--; sum[0] = -1; } else if (sum[0] == 0 && sum[1] < 0) { cnt++; v[0]++; sum[0] = 1; } for (long long int i = 1; i < n; i++) { sum[i] = sum[i - 1] + v[i]; if (sum[i - 1] >= 0 && sum[i] >= 0) { cnt += sum[i - 1] + 1 + v[i]; v[i] = sum[i - 1] * (-1) - 1; sum[i] = -1; } else if (sum[i - 1] < 0 && sum[i] < 0) { cnt += sum[i - 1] * (-1) + 1 - v[i]; v[i] = sum[i - 1] * (-1) + 1; sum[i] = 1; } else if (sum[i] == 0 && sum[i - 1] > 0) { cnt++; v[i]--; sum[i] = -1; } else if (sum[i] == 0 && sum[i - 1] < 0) { cnt++; v[i]++; sum[i] = 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using i_i = pair<int, int>; using ll_ll = pair<ll, ll>; using d_ll = pair<double, ll>; using ll_d = pair<ll, double>; using d_d = pair<double, double>; template <class T> using vec = vector<T>; static constexpr ll LL_INF = 1LL << 60; static constexpr int I_INF = 1 << 28; static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288); static constexpr double EPS = numeric_limits<double>::epsilon(); static map<type_index, const char* const> scanType = {{typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}}; template <class T> static void scan(vector<T>& v); [[maybe_unused]] static void scan(vector<string>& v, bool isWord = true); template <class T> static inline bool chmax(T& a, T b); template <class T> static inline bool chmin(T& a, T b); template <class T> static inline T gcd(T a, T b); template <class T> static inline T lcm(T a, T b); template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val); template <class T> T mod(T a, T m); template <class Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid& x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid& x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int& k) const { return seg[k + sz]; } template <class C> int find_subtree(int a, const C& check, Monoid& M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <class C> int find_first(int a, const C& check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <class C> int find_last(int b, const C& check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int main(int argc, char* argv[]) { ll n; cin >> n; vec<ll> a(n); scan(a); SegmentTree<ll> seg( n, [](ll x, ll y) { return x + y; }, 0LL); for (int i = (0); i < (n); i++) { seg.set(i, a[i]); } seg.build(); ll ans = 0; bool next_sign = (a[0] < 0) ? true : false; if (a[0] == 0) seg.update(0, 1LL); for (int i = (2); i < (n + 1); i++) { ll sum = seg.query(0, i); if ((next_sign && sum > 0) || (!next_sign && sum < 0)) { next_sign = !next_sign; continue; } ll to = (next_sign) ? 1 : -1; ll diff = abs(sum - to); ans += diff; seg.update(i - 1, seg[i - 1] + (to - sum)); next_sign = !next_sign; } ((cout) << (ans) << (endl)); return 0; } template <class T> static void scan(vector<T>& v) { auto tFormat = scanType[typeid(T)]; for (T& n : v) { scanf(tFormat, &n); } } static void scan(vector<string>& v, bool isWord) { if (isWord) { for (auto& n : v) { cin >> n; } return; } int i = 0, size = v.size(); string s; getline(cin, s); if (s.size() != 0) { i++; v[0] = s; } for (; i < size; ++i) { getline(cin, v[i]); } } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T gcd(T a, T b) { return __gcd(a, b); } template <class T> inline T lcm(T a, T b) { T c = min(a, b), d = max(a, b); return c * (d / gcd(c, d)); } template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) { std::fill((T*)arr, (T*)(arr + N), val); } template <class T> T mod(T a, T m) { return (a % m + m) % m; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e12; const int inf = 1e9; const int mod = 1e9 + 7; int main() { cout << fixed << setprecision(10); int n; cin >> n; vector<long long> v(n, 0); for (int i = 0; i < (n); i++) cin >> v[i]; long long ans = inf; for (int i = 0; i < (2); i++) { long long now = 0; long long sum = 0; for (int j = 0; j < (n); j++) { if (i == 0) { sum += v[j]; if (j % 2 == 0) { if (sum <= 0) { now += 1 - sum; sum = 1; } } else { if (sum >= 0) { now += sum + 1; sum = -1; } } } else { sum += v[j]; if (j % 2 == 0) { if (sum >= 0) { now += sum + 1; sum = -1; } } else { if (sum <= 0) { now += 1 - sum; sum = 1; } } } } ans = min(ans, now); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n=int(input()) a=list(map(int,input().split())) r=[0] for i in range(n): r.append(r[i]+a[i]) r.pop(0) print(r) pm=[1-2*(i%2) for i in range(n)] mp=[1-2*((i+1)%2) for i in range(n)] print(pm) print(mp) sum1,sum2=0,0 sousa1,sousa2=0,0 for i in range(n): if np.sign(r[i]+sousa1) != pm[i]: sum1+=abs(pm[i]-r[i]-sousa1) sousa1+=1-2*(i%2)-r[i] for i in range(n): if np.sign(r[i]+sousa2) != mp[i]: sum2+=abs(mp[i]-r[i]-sousa2) sousa2+=1-2*((i+1)%2)-r[i] print(min(sum1,sum2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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> a; int N; signed main() { cin >> N; for (int i = 0; i < (int)(N); i++) { int t; cin >> t; a.push_back((t)); } int res = 0; int cnt = 0; int sum = 0; for (int i = 0; i < (int)(N); i++) { sum += a[i]; if (sum < 0) { cnt += -sum + 1; sum = 1; } i++; if (i == N) break; sum += a[i]; if (sum > 0) { cnt += sum + 1; sum = -1; } } res = cnt; cnt = 0; for (int i = 0; i < (int)(N); i++) { sum += a[i]; if (sum > 0) { cnt += sum + 1; sum = -1; } i++; if (i == N) break; sum += a[i]; if (sum < 0) { cnt += -sum + 1; sum = 1; } } res = min(res, cnt); cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Setup { Setup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } SETUP; template <class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const long long INF = 1LL << 60; signed main() { long long n; cin >> n; vector<long long> v(n); for (auto& a : v) { cin >> a; } vector<long long> acc(n + 1); acc[0] = 0; long long ans = 0; for (long long i = 0; i < n; i++) { acc[i + 1] = acc[i] + v[i]; if (acc[i] < 0 && acc[i + 1] < 0) { ans += abs(1 - acc[i + 1]); acc[i + 1] = 1; } else if (acc[i] > 0 && acc[i + 1] > 0) { ans += abs(-1 - acc[i + 1]); acc[i + 1] = -1; } else if (acc[i + 1] == 0) { if (acc[i] < 0) { ans++; acc[i + 1]++; } else if (acc[i] > 0) { ans++; acc[i + 1]--; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); i++) cin >> a[i]; int sum = 0; int ans1 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum >= 0) { ans1 += sum + 1; sum = -1; } } else { if (sum <= 0) { ans1 += -sum + 1; sum = 1; } } } int ans2 = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum >= 0) { ans2 += sum + 1; sum = -1; } } else { if (sum <= 0) { ans2 += -sum + 1; sum = 1; } } } int ans = min(ans1, ans2); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100004]; int main() { scanf("%d", &n); for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]); long long ans = 0; if (!a[1]) ++ans; if (a[2] > 0) a[1] = -1; else a[1] = 1; for (int i = (2); i <= (int)(n); ++i) { if (a[i - 1] > 0) { if (a[i] + a[i - 1] < 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] + 1 + a[i - 1]); a[i] = -1; } else { if (a[i] + a[i - 1] > 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] - 1 + a[i - 1]); a[i] = 1; } } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) prv_total =0 cnt = 0 if a[0] == 0: if a[1]>= 0: a[0] = -1 else: a[0] = -1 cnt += 1 for i in range(n-1): total = prv_total + a[i] nxt_total = total+a[i+1] if total == 0 and i != 0: if prv_total > 0: cnt += 1 total -= 1 else: cnt += 1 total += 1 if total > 0 and nxt_total >= 0: a[i+1] -= nxt_total+1 cnt += nxt_total+1 nxt_total -= nxt_total+1 elif total < 0 and nxt_total <=0: a[i+1] += abs(nxt_total)+1 cnt += abs(nxt_total)+1 nxt_total += abs(nxt_total)+1 prv_total = total total = prv_total + a[-1] if total == 0: cnt += 1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 1e9 + 7; int n; vector<ll> a; ll cost(ll a0) { ll cst = 0, rs = a0; bool sign = rs > 0; for (int i = 1; i < n; i++) { sign = !sign; rs += a[i]; if (rs == 0) { cst += sign ? 1 : -1; } else { if (rs > 0 == true && !sign) { cst += rs + 1; rs = -1; } else if (rs > 0 == false && sign) { cst += abs(rs) + 1; rs = 1; } } } return cst; } int main() { cin >> n; a.resize(n); for (ll &i : a) cin >> i; ll ans; if (a[0] == 0) { ans = cost(-1); ans = min(ans, cost(1)); cout << ans << endl; } else { ans = cost(a[0]); 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 MOD = 1000000007; const int INF = 1e9; //long long using ll = long long; //出力系 #define print(x) cout << x << endl #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl // begin() end() #define all(x) (x).begin(),(x).end() //for #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define REPR(i,n) for(int i=n, i##_len=(n); i>=0; i--) #define FOR(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i) //最大公約数 unsigned gcd(unsigned a, unsigned b) { if(a < b) return gcd(b, a); unsigned r; while ((r=a%b)) { a = b; b = r; } return b; } int main(){ int n; cin >> n vector<int> a(n); REP(i, n) cin >> a[i]; int ans1; // +-+-の場合 if(n.at(0) <= 0) a[i] = a[i] + abs(1 - a[i]); for(int i = 0; i < N; i++){ if(i % 2 == 0){ if(a[i] + a[i + 1] >= 0 ){ ans1 += -1 - a[i]; a[i] + a[i + 1] = a[i] + (-1 - a[i]); } }else if(i % 2 == 1){ if(a[i] + a[i + 1] >= 0){ ans1 += abs(1 - a[i]); a[i] + a[i + 1] = a[i] + abs(1 - a[i]); } } } int ans2; if(n.at(0) <= 0) a[i] = a[i] + abs(1 - a[i]); for(int i = 0; i < N; i++){ if(a[i] + a[i + 1] >= 0){ ans2 += abs(1 - a[i]); a[i] + a[i + 1] = a[i] + abs(1 - a[i]); }else if(i % 2 == 1){ if(i % 2 == 0){ if(a[i] + a[i + 1] >= 0 ){ ans2 += -1 - a[i]; a[i] + a[i + 1] = a[i] + (-1 - a[i]); } } } } // +-+-の場合 }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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
a = [1,-3, 1, 0] X = 0 ans = 0 for i in a: X += i if X > 0: b = -1 - X ans += b - a[i+1] else: b = 1 - X ans += b - a[i+1] print (ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int 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 sign = (a[0] > 0) - (a[0] < 0); int s = a[0]; int ans = 0; for (int i = 1; i < n; ++i) { s += a[i]; sign *= -1; if (ch_sign(s) != sign) { ans += abs(s - sign); s = sign; } } 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> const double PI = acos(-1); const double EPS = 1e-15; long long INF = (long long)1E17; long mod(long a) { long long c = a % (long long)(1E9 + 7); if (c >= 0) return c; return c + (long long)(1E9 + 7); } using namespace std; bool prime_(int n) { if (n == 1) { return false; } else if (n == 2) { return true; } else { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } } long long gcd_(long long a, long long b) { if (a < b) { swap(a, b); } if (a % b == 0) { return b; } else { return gcd_(b, a % b); } } long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; } class UnionFind { public: vector<int> Parent; UnionFind(int N) { Parent = vector<int>(N, -1); } int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } int size(int A) { return -Parent[root(A)]; } bool connect(int A, int B) { A = root(A); B = root(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; int main() { int n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long s = 0; long long ans = 0; for (int i = 0; i < n; i++) { if (i == 0) { s += a[i]; if (s > 0) continue; ans += 1 - s; s = 1; } else { s += a[i]; if (s < 0) continue; ans += s + 1; s = -1; } } s = 0; long long temp = 0; for (int i = 0; i < n; i++) { if (i == 0) { s += a[i]; if (s < 0) continue; temp += s + 1; s = -1; } else { s += a[i]; if (s > 0) continue; temp += 1 - s; s = 1; } } ans = min(ans, temp); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int sum; long long int ans1 = 0, ans2 = 0; vector<long long int> a; for (int i = 1; i <= n; i++) { long long int tmp; cin >> tmp; a.push_back(tmp); } sum = 0; for (int i = 1; i <= n; i++) { sum += a[i - 1]; if (i % 2 == 0 && sum < 0) { ans1 += abs(sum) + 1; sum = 1; } else if (i % 2 == 1 && sum > 0) { ans1 += abs(sum) + 1; sum = -1; } } sum = 0; for (int i = 1; i <= n; i++) { sum += a[i - 1]; if (i % 2 == 0 && sum > 0) { ans2 += abs(sum) + 1; sum = -1; } else if (i % 2 == 1 && sum < 0) { ans2 += abs(sum) + 1; sum = 1; } } std::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 int n; cin >> n; long long int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long int count = 0; if (a[0] == 0) { for (int i = 0; i > n; ++i) { if (a[i] > 0) { a[0] = -1; ++count; break; } else if (a[i] < 0) { a[0] = 1; ++count; break; } } } long long int cal = a[0]; if (a[0] == 0) { cout << n * (n + 1) / 2; } else { for (int i = 1; i < n; ++i) { if (cal + a[i] == 0) { if (cal < 0) { ++count; ++a[i]; cal = 1; } else { ++count; --a[i]; cal = -1; } } else if (cal < 0 && cal + a[i] < 0) { count += -(cal + a[i] - 1); a[i] += -(cal + a[i] - 1); } else if (cal > 0 && cal + a[i] > 0) { count += (cal + a[i] + 1); a[i] -= (cal + a[i] + 1); } cal += a[i]; } } cout << count << "\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; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1000000007; int main() { ll N, sum = 0, ans = 0; cin >> N; vector<ll> A(N); for (long long i = 0; i < (N); ++i) cin >> A.at(i); if (A.at(0) == 0) { A.at(0) = 1; ans++; for (long long i = 0; i < (N); ++i) { if (i == 0) { sum += A.at(i); continue; } if (sum > 0) { if (sum + A.at(i) < 0) { sum += A.at(i); } else { ans += sum + A.at(i) + 1; sum = -1; } } else { if (sum + A.at(i) > 0) { sum += A.at(i); } else { ans += 1 - (sum + A.at(i)); sum = 1; } } } A.at(0) = -1; ll ans1 = 1; for (long long i = 0; i < (N); ++i) { if (i == 0) { sum += A.at(i); continue; } if (sum > 0) { if (sum + A.at(i) < 0) { sum += A.at(i); } else { ans1 += sum + A.at(i) + 1; sum = -1; } } else { if (sum + A.at(i) > 0) { sum += A.at(i); } else { ans1 += 1 - (sum + A.at(i)); sum = 1; } } } cout << min(ans, ans1) << endl; return 0; } for (long long i = 0; i < (N); ++i) { if (i == 0) { sum += A.at(i); continue; } if (sum > 0) { if (sum + A.at(i) < 0) { sum += A.at(i); } else { ans += sum + A.at(i) + 1; sum = -1; } } else { if (sum + A.at(i) > 0) { sum += A.at(i); } else { ans += 1 - (sum + A.at(i)); 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
python3
a = list(map(int, input().split())) t1, s1 = 0, 0 for i, x in enumerate(a): s1 += x if i % 2 == 0: if s1 < 1: t1 += (1-s1) s1 = 1 else: if s1 > -1: t1 += (s1+1) s1 = -1 t2, s2 = 0, 0 for i, x in enumerate(a): s2 += x if i % 2 == 0: if s2 > -1: t2 += (s2+1) s2 = -1 else: if s2 < 1: t2 += (1-s2) s2 = 1 print(min(t1, t2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; int a[1000000] = {}; for (int i = 0; i < n; i++) { cin >> a[i]; } ll s1, s2; int ans1 = 0, ans2 = 0; if (a[0] != 0) { s1 = a[0]; } else { s1 = (a[1] > 0) ? -1 : 1; ans1++; } for (int i = 1; i < n; i++) { if (s1 > 0 && s1 + a[i] > 0) { ans1 += s1 + a[i] + 1; s1 = -1; } else if (s1 < 0 && s1 + a[i] < 0) { ans1 += -(s1 + a[i]) + 1; s1 = 1; } else if (s1 + a[i] == 0) { if (s1 > 0) { s1 = -1; } else { s1 = 1; } ans1++; } else { s1 += a[i]; } } cout << ans1 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A.at(i); bool flag; bool flag2 = false; for (int i = 0; i < N; i++) { if (A.at(i) > 0) { flag = true; flag2 = true; break; } else if (A.at(i) < 0) { flag = false; flag2 = true; break; } } if (!flag2) { cout << 0 << endl; return 0; } int ans = 0; int total = A.at(0); for (int i = 0; i < N - 1; i++) { int count = 0; if (flag) { if (total + A.at(i + 1) >= 0) { count = -1 - total - A.at(i + 1); ans += abs(count); A.at(i + 1) = A.at(i + 1) + count; } total += A.at(i + 1); flag = false; } else if (!flag) { if (total + A.at(i + 1) <= 0) { count = 1 - total - A.at(i + 1); ans += abs(count); A.at(i + 1) = A.at(i + 1) + count; } total += A.at(i + 1); flag = true; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; 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 (sum >= 0 && i % 2 == 1) { sum -= abs(sum) + 1; count1 += abs(sum) + 1; } else if (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 (sum >= 0 && i % 2 == 0) { sum -= abs(sum) + 1; count2 += abs(sum) + 1; } else if (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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long> a(n); long long wa = 0; int now_sign = 0; int pre_sign = 0; long long count = 0; long long min_count = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] != 0) { pre_sign = a[0] / abs(a[0]); wa = a[0]; for (int i = 1; i < n; i++) { wa += a[i]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign == pre_sign || now_sign == 0) { count += abs(wa) + 1; wa = -1 * pre_sign; now_sign = -1 * pre_sign; } pre_sign = now_sign; } min_count = count; } else { pre_sign = 1; wa = 1; count = 1; for (int i = 1; i < n; i++) { wa += a[i]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign == pre_sign || now_sign == 0) { count += abs(wa) + 1; wa = -1 * pre_sign; now_sign = -1 * pre_sign; } pre_sign = now_sign; } min_count = count; pre_sign = -1; wa = -1; count = 1; for (int i = 1; i < n; i++) { wa += a[i]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign == pre_sign || now_sign == 0) { count += abs(wa) + 1; wa = -1 * pre_sign; now_sign = -1 * pre_sign; } pre_sign = now_sign; } min_count = min(min_count, count); } cout << min_count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename T> using V = vector<T>; template <typename T, typename U> using P = pair<T, U>; int n; V<int> a; int solve(bool first_plus) { bool will_plus = first_plus; int manip_count = 0; int sum = 0; for (int i = 0; i < (n); ++i) { int not_enough = 0; if (will_plus) { if (!(sum + a[i] > 0)) { not_enough = 1 - (sum + a[i]); manip_count += not_enough; } } else { if (!(sum + a[i] < 0)) { not_enough = -1 - (sum + a[i]); manip_count += abs(not_enough); } } sum += a[i] + not_enough; will_plus = !will_plus; } return manip_count; } int main() { cin >> n; a.resize(n); for (int i = 0; i < (n); ++i) { cin >> a[i]; } int first_plus = solve(true); int first_minus = solve(false); cout << min(first_plus, first_minus) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, Inf = 0x3f3f3f3f; long long a[N], n; long long sum[N]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; long long cost = 0, ans = Inf; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] >= 0) cost += sum[i] + 1, sum[i] = -1; } else { if (sum[i] <= 0) cost += -sum[i] + 1, sum[i] = 1; } } ans = min(ans, cost); cost = 0; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i]; if (!(i & 1)) { if (sum[i] >= 0) cost += sum[i] + 1, sum[i] = -1; } else { if (sum[i] <= 0) cost += -sum[i] + 1, sum[i] = 1; } } ans = min(ans, cost); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long N; cin >> N; vector<long> A(N), SP(N), SN(N); long ansp = 0, ansn = 0; for (int i = 0; i < N; i++) cin >> A[i]; for (int i = 0; i < N; i++) { SP[i] = (i == 0 ? 0 : SP[i - 1]) + A[i]; while ((i % 2 == 0 && SP[i] <= 0) || (i % 2 == 1 && SP[i] >= 0)) { SP[i] += 1 - 2 * (i % 2); ansp++; } } for (int i = 0; i < N; i++) { SN[i] = (i == 0 ? 0 : SN[i - 1]) + A[i]; while ((i % 2 == 0 && SN[i] >= 0) || (i % 2 == 1 && SN[i] <= 0)) { SN[i] += -1 + 2 * (i % 2); ansn++; } } cout << min(ansp, ansn); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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) { vector<int> v; int res = 0; int sign = 0; int n, t; int sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> t; v.push_back(t); } if (v[0] > 0) { sign = 0; } else { sign = 1; } sum += v[0]; for (int i = 1; i < v.size(); i++) { sum += v[i]; if (sign == 0) { if (sum >= 0) { res += (sum + 1); sum -= (sum + 1); } } else { if (sum <= 0) { res += ((-1 * sum) + 1); sum += ((-1 * sum) + 1); } } sign = 1 - sign; } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n]; for (long long i = (0); i < (n); i++) { cin >> a[i]; } for (long long i = (0); i < (n - 1); i++) { a[i + 1] += a[i]; } int ans1 = 0, cnt = 0; for (long long i = (0); i < (n); i++) { if (i % 2 == 0) { if (a[i] + cnt <= 0) { ans1 += -(a[i] + cnt) + 1; cnt += -(a[i] + cnt) + 1; } } else { if (a[i] + cnt >= 0) { ans1 += a[i] + cnt + 1; cnt -= a[i] + cnt + 1; } } } int ans2 = 0; cnt = 0; for (long long i = (0); i < (n); i++) { if (i % 2 == 1) { if (a[i] + cnt <= 0) { ans2 += -(a[i] + cnt) + 1; cnt += -(a[i] + cnt) + 1; } } else { if (a[i] + cnt >= 0) { ans2 += a[i] + cnt + 1; cnt -= a[i] + cnt + 1; } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main() { long long int n, i, a[100000], sum, count = 0, flag = 0; scanf("%lld", &n); for (i = 0; i < n; i++) { scanf("%lld", &a[i]); if (a[0] == 0 && a[i] != 0 && flag == 0) flag = i; } if (flag != 0) { if ((a[flag] > 0 && flag % 2 == 0) || (a[flag] < 0 && flag % 2 == 1)) a[0] = 1; else a[0] = -1; count++; } else if (flag == 0 && a[0] == 0 && a[n - 1] == 0) { a[0]++; count++; } for (i = 0; i < n; i++) { if (i == 0) sum = a[0]; else { if (sum > 0 && sum + a[i] >= 0) { count += 1 + sum + a[i]; a[i] = -1 * sum - 1; sum = -1; } else if (sum < 0 && sum + a[i] <= 0) { count += 1 - sum - a[i]; a[i] = -1 * sum + 1; sum = 1; } else if (sum + a[i] == 0) { if (sum > 0) a[i]--; else if (sum < 0) a[i]++; count++; sum += a[i]; } else sum += a[i]; } } printf("%lld\n", count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = [int(x) for x in input().split()] ans_plusstart = 0 sum_plusstart = 0 ans_minusstart = 0 sum_minusstart = 0 if A[0] < 0: ans_plusstart += 1 - A[0] sum_minusstart = A[0] sum_plusstart = 1 else: ans_minusstart += 1 + A[0] sum_minusstart = -1 sum_plusstart = max(1, A[0]) if A[0] == 0: ans_plusstart = 1 for i in range(1, N): #print(sum_plusstart, ans_plusstart, sum_minusstart, ans_minusstart) if i % 2 == 0: # スタートと同じ記号にしたい if A[i] + sum_plusstart > 0: sum_plusstart = A[i] + sum_plusstart if A[i] + sum_plusstart <= 0: ans_plusstart += 1-(A[i] + sum_plusstart) sum_plusstart = 1 if A[i] + sum_minusstart < 0: sum_minusstart = A[i] + sum_minusstart if A[i] + sum_minusstart >= 0: ans_minusstart += 1 + A[i] + sum_minusstart sum_minusstart = -1 else: # スタートと違う記号にしたい if A[i] + sum_plusstart < 0: sum_plusstart = A[i] + sum_plusstart if A[i] + sum_plusstart >= 0: ans_plusstart += 1+A[i] + sum_plusstart sum_plusstart = -1 if A[i] + sum_minusstart > 0: sum_minusstart = A[i] + sum_minusstart if A[i] + sum_minusstart <= 0: ans_minusstart += 1 - (A[i] + sum_minusstart) sum_minusstart = -1 print(min(ans_minusstart, ans_plusstart))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 sub_mod(long long a, long long b, long long x) { long long tmp = (a - b) % x; if (tmp < 0) tmp += x; return tmp; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> nums(n, 0); for (int i = 0; i < n; ++i) { cin >> nums[i]; } int out = (1e9); for (int idx = 0; idx < 2; ++idx) { int sum = 0, base = idx, ans = 0; for (int i = 0; i < n; ++i) { if (base == 0 && sum + nums[i] < 0) { ans += abs(sum + nums[i] - 1); sum = 1; } else if (base == 1 && sum + nums[i] > 0) { ans += abs(sum + nums[i] + 1); sum = -1; } else if (sum + nums[i] == 0) { sum = (base == 0 ? 1 : -1); ans += 1; } else { sum += nums[i]; } base = 1 - base; } out = min(out, ans); } cout << out << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys 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 sum2 == 0: if sum >0: a[i+1] -= 1 sum2 -= 1 c1 -=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 sum2 == 0: if sum >0: a[i+1] -= 1 sum2 -= 1 c2 -=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
cpp
#include <bits/stdc++.h> int main() { int s[10000]; int i, n; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &s[i]); int sum = s[0], op = 0; i = 0; while (i < n - 1) { if (sum > 0) { while (s[i + 1] >= -sum) { s[i + 1]--; op++; } } else { while (s[i + 1] <= -sum) { s[i + 1]++; op++; } } i++; } printf("%d", op); 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; cin >> a; long long sum = a; long long cnt = 0; for (int i = 1; i < n; ++i) { cin >> a; int next = sum + a; int c, diff; c = diff = 0; if (sum > 0) { if (next > 0) { diff = (-1 - sum); a = diff - a; c = diff; } } else { if (next < 0) { diff = (1 - sum) - a; a += diff; c = diff; } } sum += a; cnt += abs(c); } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input().strip()) A = list(map(int, input().strip().split(" "))) def solver(_sign): s = A[0] count = 0 if s == 0: count += 1 if n == 1: return count else: if _sign: # 先頭が正 s = 1 else: s = -1 for a in A[1:]: prev = s sign = prev > 0 s += a if s == 0: count += 1 if sign: # previous is positive s = -1 else: # prev is negative s = 1 elif sign == (s > 0): # previous and current have the same sign count += abs(s)+1 if s > 0: s = -1 else: s = 1 else: pass return count print(min(solver(True), solver(False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long func(long long a[], int n) { long long cnt = 0; long long s = 0; for (int i = 1; i < n; i++) { s += a[i - 1]; long long t = 0, u; if (s > 0) { u = (-1) * s - 1; if (u < a[i]) { t = a[i] - u; a[i] = u; } } else { u = (-1) * s + 1; if (u > a[i]) { t = u - a[i]; a[i] = u; } } cnt += t; } return cnt; } int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < (n); i++) cin >> a[i]; long long cnt1 = func(a, n); int d; if (a[0] > 0) { d = a[0] + 1; a[0] = -1; } else { d = (-1) * a[0] + 1; a[0] = 1; } long long cnt2 = d + func(a, n); cout << min(cnt1, cnt1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=[int(i) for i in input().split()] check=a[0] ans=0 if check==0: for i in range(1,n): if check+a[i]==0: continue elif check+a[i]>0: check=-1 ans=1 break else: check=1 ans=1 break for i in range(1,n): check2=check+a[i] if check<0: if check2>0: check=check2 else: ans+=abs(check2+1) check=1 else: if check2<0: check=check2 else: ans+=abs(check2+1) check=-1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INFTY = 1001001001; int main() { int N; cin >> N; vector<long long> a(N + 1), b(N + 1); for (int i = 0; i < N; ++i) { cin >> a[i]; b[i] = a[i]; if (i != 0) { a[i] += a[i - 1]; b[i] += b[i - 1]; } } long long ans = INFTY; bool sign; for (int i = 0; i < 2; ++i) { sign = i; if (sign) { for (int i = 0; i < N + 1; ++i) { a[i] = b[i]; } } long long v = 0, count = 0; for (int i = 0; i < N; ++i) { if (sign) { if (a[i] >= 0) { count += abs(-a[i] - 1); v -= abs(-a[i] - 1); } } else { if (a[i] <= 0) { count += abs(-a[i] + 1); v += abs(-a[i] + 1); } } a[i + 1] += v; sign = !(sign); } ans = min(ans, count); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 cnt = 0; int tmp_sum = 0; if (a[0] == 0) { int pos = -1; for (int i = 1; i < n && pos == -1; i++) if (a[i] != 0) pos = i; if (pos != -1) { if (a[pos] > 0) { for (int j = 1; pos - j >= 0; j++) { if (j % 2 == 1) a[pos - j] = -1; else a[pos - j] = 1; } } else { for (int j = 1; pos - j >= 0; j++) { if (j % 2 == 1) a[pos - j] = 1; else a[pos - j] = -1; } } cnt += pos; } else { cnt = n * 2 - 1; } } if (a[0] > 0) { tmp_sum = a[0]; for (int i = 1; i < n; i++) { tmp_sum += a[i]; if (i % 2 == 1 && tmp_sum >= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum -= (abs(tmp_sum) + 1); } if (i % 2 == 0 && tmp_sum <= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum += (abs(tmp_sum) + 1); } } } else if (a[0] < 0) { tmp_sum = a[0]; for (int i = 1; i < n; i++) { tmp_sum += a[i]; if (i % 2 == 1 && tmp_sum <= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum += (abs(tmp_sum) + 1); } if (i % 2 == 0 && tmp_sum >= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum -= (abs(tmp_sum) + 1); } } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template <class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>; using ld = long double; using vld = vector<ld>; using vi = vector<int>; using vvi = vector<vi>; vll conv(vi &v) { vll r(v.size()); for (long long i = 0; i < (long long)(v.size()); i++) r[i] = v[i]; return r; } inline void input(int &v) { v = 0; char c = 0; int p = 1; while (c < '0' || c > '9') { if (c == '-') p = -1; c = getchar(); } while (c >= '0' && c <= '9') { v = (v << 3) + (v << 1) + c - '0'; c = getchar(); } v *= p; } template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << "(" << v.first << ", " << v.second << ")"; return o; } template <size_t...> struct seq {}; template <size_t N, size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, size_t... Is> void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using s = int[]; (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...}; } template <class Ch, class Tr, class... Args> auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t) -> basic_ostream<Ch, Tr> & { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; } ostream &operator<<(ostream &o, const vvll &v) { for (long long i = 0; i < (long long)(v.size()); i++) { for (long long j = 0; j < (long long)(v[i].size()); j++) o << v[i][j] << " "; o << endl; } return o; } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; for (long long i = 0; i < (long long)(v.size()); i++) o << v[i] << (i != v.size() - 1 ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const deque<T> &v) { o << '['; for (long long i = 0; i < (long long)(v.size()); i++) o << v[i] << (i != v.size() - 1 ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const unordered_set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U, typename V> ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]"; return o; } vector<int> range(const int x, const int y) { vector<int> v(y - x + 1); iota(v.begin(), v.end(), x); return v; } template <typename T> istream &operator>>(istream &i, vector<T> &o) { for (long long j = 0; j < (long long)(o.size()); j++) i >> o[j]; return i; } template <typename T, typename S, typename U> ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " "; } return o; } template <typename T> ostream &operator<<(ostream &o, const queue<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.front(); tmp.pop(); o << x << " "; } return o; } template <typename T> ostream &operator<<(ostream &o, const stack<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " "; } return o; } template <typename T> unordered_map<T, ll> counter(vector<T> vec) { unordered_map<T, ll> ret; for (auto &&x : vec) ret[x]++; return ret; }; string substr(string s, P x) { return s.substr(x.first, x.second - x.first); } void vizGraph(vvll &g, int mode = 0, string filename = "out.png") { ofstream ofs("./out.dot"); ofs << "digraph graph_name {" << endl; set<P> memo; for (long long i = 0; i < (long long)(g.size()); i++) for (long long j = 0; j < (long long)(g[i].size()); j++) { if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i)))) continue; memo.insert(P(i, g[i][j])); ofs << " " << i << " -> " << g[i][j] << (mode ? " [arrowhead = none]" : "") << endl; } ofs << "}" << endl; ofs.close(); system(((string) "dot -T png out.dot >" + filename).c_str()); } size_t random_seed; namespace std { using argument_type = P; template <> struct hash<argument_type> { size_t operator()(argument_type const &x) const { size_t seed = random_seed; seed ^= hash<ll>{}(x.first); seed ^= (hash<ll>{}(x.second) << 1); return seed; } }; }; // namespace std int main() { int n, a[100100]; cin >> n; int total = 0; int count = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; cerr << "i " << i << ", a[i] " << a[i] << " total: " << total << " count " << count << endl; total += a[i]; if (i == 0) { continue; } if (a[0] >= 0) { cerr << "a[i] >= 0, so... " << endl; if (i % 2 == 1) { if (total >= 0) { count += total + 1; total = -1; cerr << "aaaa should be minus!" << endl; } } else if (total < 0) { count += abs(total) + 1; total = 1; cerr << "bbbb should be plus!" << endl; } } else { if (i % 2 == 0) { if (total >= 0) { count += total + 1; total = -1; cerr << "cccc should be minus!" << endl; } } else if (total < 0) { count += abs(total) + 1; total = 1; cerr << "dddd should be plus!" << endl; } } cerr << "total " << total << " is after operation" << endl; } if (total == 0) { cerr << "total is zero, increment total" << endl; ++count; } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N, sum = 0, ans = 0; cin >> N; int m; for (int i = 0; i < N; i++) { int a; cin >> a; if (i == 0) m = a; else { a += m; if (m > 0) { if (a <= 0) { if (m + a == 0) { ans++; a--; } } else { for (;;) { if (a < 0) break; ans++; a--; } } } else { if (a >= 0) { if (m + a == 0) { ans++; a++; } } else { for (;;) { if (a > 0) break; ans++; a++; } } } m = a; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int array[n]; for (int i = 0; i < n; i++) { cin >> array[i]; } int answer = 0; int sum = 0; for (int i = 0; i < n; i++) { if (sum == 0) sum += array[0]; else if (sum < 0) { if (sum + array[i] > 0) { sum += array[i]; } else { if (sum == -1) { answer += abs(2 - array[i]); sum += 2; } else { answer += abs((-1) * sum + 1 - array[i]); sum = 1; } } } else { if (sum + array[i] < 0) { sum += array[i]; } else { if (sum == 1) { answer += abs(-2 - array[i]); sum += -2; } else { answer += abs((-1) * sum - 1 - array[i]); sum = -1; } } } } cout << answer << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> template <typename T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { for (typename std::vector<T>::size_type i = 0; i < v.size(); ++i) { os << v[i]; if (i + 1 < v.size()) { os << ' '; } } return os; } unsigned solve(unsigned N, const std::vector<long long> &a, int flag) { unsigned c = 0; long long s = 0; for (unsigned n = 0; n < N; ++n) { long long b = s + a[n]; if (n % 2 == flag) { if (b <= 0) { c += std::abs((+1) - b); s = +1; } else { s = b; } } else { if (b >= 0) { c += std::abs((-1) - b); s = -1; } else { s = b; } } } return c; } int main() { unsigned N; std::cin >> N; std::vector<long long> a(N); for (unsigned n = 0; n < N; ++n) { std::cin >> a[n]; } std::cout << std::min(solve(N, a, 0), solve(N, a, 1)) << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#pragma warning disable using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Text.RegularExpressions; using System.Diagnostics; using System.Numerics; using System.Collections; static class MainClass { public static void Main(string[] args) { var n = Console.ReadLine().ToInt32(); var an = Console.ReadLine().SplittedStringToInt32List(); var sum = 0; var ruisekiwa = new int[n]; ruisekiwa[0] = an[0]; var count = 0; for (int i = 1; i < n; i++) { var before = ruisekiwa[i - 1] + an[i]; if ((before < 0 && ruisekiwa[i - 1] < 0)) { count += Math.Abs(before) + 1; before = 1; } else if (before > 0 && ruisekiwa[i - 1] > 0) { count += before + 1; before = -1; } ruisekiwa[i] = before; } if (ruisekiwa[n - 1] == 0) count++; Console.WriteLine(count); Console.ReadLine(); //var nm = Console.ReadLine().SplittedStringToInt32List(); //var n = nm[0]; //var m = nm[1]; //InitializeUnionFind(n); //var abs = new List<KeyValuePair<int,int>>(); //for (int i = 0; i < m; i++) //{ // var ab = Console.ReadLine().SplittedStringToInt32List(); // abs.Add(new KeyValuePair<int, int>(ab[0] - 1, ab[1] - 1)); //} //abs.Reverse(); //var ls = new List<int>(); //foreach (var item in abs) //{ // Unite(item.Key, item.Value); // ls.Add(ParentCount); //} //ls.Reverse(); //var before = -1; //var befores = 0; //for (int i = 0; i < m; i++) //{ // if (before != -1) // { // var num = ls[i] - befores; // Console.WriteLine((num * (num - 1))/2 + before); // before = (num * (num - 1)) / 2 + before; // } // else // { // var num = ls[i] - befores; // Console.WriteLine((num * ( num - 1)) / 2); // before = (num * (num - 1)) / 2; // } // befores = ls[i]; //} //Console.ReadLine(); } public static BigInteger[] factors; public static BigInteger[] revFactors; public static void SetFactor(int N) { factors = new BigInteger[N]; factors[0] = 1; revFactors = new BigInteger[N]; for (int i = 1; i < N; i++) { factors[i] = factors[i - 1] * i; factors[i] %= Mod1e9; } revFactors[N - 1] = BigInteger.ModPow(factors[N - 1], Mod1e9 - 2, Mod1e9); for (int i = N - 2; i >= 0; i--) { revFactors[i] = revFactors[i + 1] * (i + 1); revFactors[i] %= Mod1e9; } } public static BigInteger GetCombination(int a, int b) { return a >= b ? (((factors[a] * revFactors[b]) % Mod1e9) * revFactors[a - b]) % Mod1e9 : 0; } public static int ParentCount; public static int[] Parents; public static int[] IfParentsChildrenCount; public static int[] Ranks; public static void InitializeUnionFind(int N) { Parents = new int[N]; Ranks = new int[N]; IfParentsChildrenCount = new int[N]; for (int i = 0; i < N; i++) { Parents[i] = i; Ranks[i] = 0; IfParentsChildrenCount[i] = 1; } ParentCount = N + 1; } public static int Root(int a) { if (Parents[a] == a) { return a; } else { Parents[a] = Root(Parents[a]); return Parents[a]; } } public static bool Same(int a, int b) { return Root(a) == Root(b); } public static void Unite(int a, int b) { var parentA = Root(a); var parentB = Root(b); if (parentA == parentB) return; if (Ranks[parentA] < Ranks[parentB]) { Parents[parentA] = parentB; IfParentsChildrenCount[parentB] += IfParentsChildrenCount[parentA]; ParentCount -= IfParentsChildrenCount[parentA]; IfParentsChildrenCount[parentA] = 0; } else { Parents[parentB] = parentA; if (Ranks[parentA] == Ranks[parentB]) { Ranks[parentA]++; } IfParentsChildrenCount[parentA] += IfParentsChildrenCount[parentB]; ParentCount -= IfParentsChildrenCount[parentB]; IfParentsChildrenCount[parentB] = 0; } } #region ライブラリ public static long ToInt64(this string str) => long.Parse(str); public static int ToInt32(this string str) => int.Parse(str); public static BigInteger ToBigInteger(this string str) => BigInteger.Parse(str); public static List<string> SplittedStringToList(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); public static List<int> SplittedStringToInt32List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList(); public static List<long> SplittedStringToInt64List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => long.Parse(x)).ToList(); public static List<BigInteger> SplittedStringToBigInteger(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => BigInteger.Parse(x)).ToList(); public const int INF = int.MaxValue / 4; public const long LONGINF = long.MaxValue / 2; public const int Mod1e9 = 1000000007; public static void PrintArray(bool[,] array) { for (int i = 0; i < array.GetLength(0); i++) { var sb = new StringBuilder(); for (int j = 0; j < array.GetLength(1); j++) { sb.Append(array[i, j]).Append(" "); } Console.WriteLine(sb.ToString()); } } public static int Manhattan(int x1, int x2, int y1, int y2) { return Math.Abs(x1 - x2) + Math.Abs(y1 - y2); } public static Dictionary<T, int> ToCountedDictionary<T>(this IEnumerable<T> items) { var dic = new Dictionary<T, int>(); foreach (var item in items) { if (dic.ContainsKey(item)) dic[item]++; else dic.Add(item, 1); } return dic; } public static long Sums(IEnumerable<int> list) { var sum = 0l; foreach (var item in list) { sum += item; } return sum; #endregion } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) n = 6 A = [-1, 4, 3, 2, -5, 4] import numpy as np b = A.copy() c = A.copy() #+-+-+- def pm(a): for i in range(len(a)): if i==0: if a[i] <= 0: a[i] = 1 elif i%2==0: if sum(a[:i+1]) <= 0: a[i] = 1 - sum(a[:i]) elif i%2==1: if sum(a[:i+1]) >= 0: a[i] = -1 - sum(a[:i]) return sum(abs(np.array(a) - np.array(b))) #-+-+-+ def mp(a): for i in range(len(a)): if i==0: if a[i] >= 0: a[i] = -1 elif i%2==0: if sum(a[:i+1]) >= 0: a[i] = -1 - sum(a[:i]) elif i%2==1: if sum(a[:i+1]) <= 0: a[i] = 1 - sum(a[:i]) return sum(abs(np.array(a) - np.array(b))) print(min(pm(A), mp(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 debug = false; int main() { int n; int a[10005]; long long cnt = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; bool plus; if (sum >= 0) plus = true; else plus = false; for (int i = 1; i < n; i++) { sum += a[i]; if (debug) cout << "sum:" << sum << endl; if (plus) { if (sum >= 0) { cnt += sum + 1; sum = -1; } plus = false; } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } plus = true; } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0, sum = a[0]; for (int i = 1; i < n; i++) { long long sum_c = sum; sum += a[i]; if (sum_c < 0 && sum <= 0) { ans += 1 - sum; sum = 1; } else if (sum_c > 0 && sum >= 0) { ans += sum + 1; sum = -1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ansa = 0, ansb = 0, suma = 0, sumb = 0; cin >> n; for (int i = 0; i < (n); i++) { int c; cin >> c; suma += c; sumb += c; if (i % 2 == 0) { if (suma + c <= 0) { ansa += 1 - c - suma; suma = 1; } if (sumb + c >= 0) { ansb += sumb + c + 1; sumb = -1; } } else { if (suma + c >= 0) { ansa += suma + c + 1; suma = -1; } if (sumb + c <= 0) { ansb += 1 - c - sumb; sumb = 1; } } } cout << min(ansa, ansb) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int n; long long a[MAXN]; long long s[MAXN]; long long countNum(bool direction) { long long cnt = 0; long long carry = 0; if (a[0] == 0) { a[0] = 1; cnt = 1; carry = 1; } cnt = direction ? cnt : abs(a[0]); carry = direction ? carry : -a[0]; s[0] = direction ? a[0] : -a[0]; for (int i = 1; i < n; i++) { s[i] = s[i - 1] + a[i]; } 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; } } return cnt; } void solve() { long long maxCnt = min(countNum(true), countNum(false)); cout << maxCnt << 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
java
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { private static final int mod =(int)1e9+7; public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=sc.nextLong(); } long sum=a[0]; long operations=0; if(a.length==1) { if(a[0]!=0) { System.out.println(0); }else { System.out.println(1); } }else { // if(sum==0) { // if(a.length>=2&&sum+a[1]>0) // sum--; // else // sum++; // // operations++; // } for(int i=0;i<n;i++) { if(sum>0) { if(sum+a[i]<0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum+=a[i]-1; operations++; }else { long req=(long)-1-1l*sum; sum=-1; operations+=(-1l*req+a[i]); } } }else if(sum<0) { if(sum+a[i]>0) { sum+=a[i]; }else { if(sum+a[i]==0) { sum+=a[i]+1; operations++; }else { long req=(long)1+-1l*sum; sum=1; operations+=(req-a[i]); } } }else { if(sum+a[i]>0) { sum--; operations++; }else { sum++; operations++; } } } System.out.println(operations); } } static boolean vis[]=new boolean[10001]; static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Function to find gcd of array of // numbers static int f(int arr[], int n) { int result = n; int max=-1; int ans=0; for (int element: arr){ if(vis[element]==false) result = gcd(n, element); if(result>max) { max=result; ans=element; } } return ans; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> data(N); for (int i = 0; i < N; i++) cin >> data[i]; int count = 0; int ans = data[0]; int saisyo; for (int i = 1; i < N; i++) { ans += data[i]; if (i % 2 == 0) { while (ans <= 0) { ans++; count++; } } else { while (ans >= 0) { ans--; count++; } } } saisyo = count; count = 0; ans = data[0]; for (int i = 1; i < N; i++) { ans += data[i]; if (i % 2 != 0) { while (ans <= 0) { ans++; count++; } } else { while (ans >= 0) { ans--; count++; } } } saisyo = min(saisyo, count); cout << saisyo << 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
var read = require('readline').createInterface({ input: process.stdin, output: process.stdout }); var obj; var inLine = []; read.on('line', function(input){inLine.push(input);}); read.on('close', function(){ obj = init(inLine); myerr("-----start-----"); var start = new Date(); Main(); var end = new Date() - start; myerr("----- end -----"); myerr("time : " + (end) + "ms"); }); function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);} function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);} function next(){return obj.next();} function hasNext(){return obj.hasNext();} function init(input){ var returnObj = { list : input, index : 0, max : input.length, hasNext : function(){return (this.index < this.max);}, next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}} }; return returnObj; } function myout(s){console.log(s);} function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));} //[no]要素の扱い。数値型 //不明値、異常時:引数そのまま返す 1:数値へ変換 //2:半角SPで分割 4:半角SPで分割し、数値配列へ //6:1文字で分割 7:1文字で分割し、数値配列へ //8:半角SPで結合 9:改行で結合 0:文字なしで結合 function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}} function Main(){ var N = nextInt(); var list = nextIntArray(); var output = 0; var sum = new Array(N); sum[0] = list[0]; for(var i = 1; i < N; i++){ sum[i] = sum[i - 1] + list[i]; if((sum[i - 1] < 0 && sum[i] > 0) || (sum[i - 1] > 0 && sum[i] < 0)){ myerr("?"); }else{ if((sum[i - 1] > 0)){ output += sum[i] + 1; sum[i] = -1; }else{ output += Math.abs(sum[i]) + 1; sum[i] = 1; } } } myout(output) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) arr = list(map(int, input().split())) c = 0 prev = 0 for i in range(n): t = prev + arr[i] if i > 0: if prev > 0 and t >= 0: diff = t + 1 c += diff arr[i] -= diff elif prev < 0 and t <= 0: diff = -1 * t + 1 c += diff arr[i] += diff prev += arr[i] print(c)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pl = pair<ll, ll>; ll LINF = 1000000000000000000; 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 (a > b) { a = b; return 1; } return 0; } struct UnionFind { vector<int> d; UnionFind(int n = 0) : d(n, -1) {} int root(int x) { if (d[x] < 0) return x; return d[x] = root(d[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -d[root(x)]; } int factor_size() { int ans = 0; for (ll i = 0; i < (d.size()); ++i) { if (d[i] < 0) { ans++; } } return ans; } }; ll d[201][201]; void warshall_floyd(ll n) { for (ll k = 0; k < (n); ++k) { for (ll i = 0; i < (n); ++i) { for (ll j = 0; j < (n); ++j) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } struct edge { ll to, cost; }; struct dijkstra { ll V; vector<vector<edge>> G; vector<ll> d; dijkstra(ll n) { init(n); } void init(ll n) { V = n; G.resize(V); d.resize(V); for (ll i = 0; i < (V); ++i) { d[i] = LINF; } } void add_edge(ll s, ll t, ll cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void run(ll s) { for (ll i = 0; i < (V); ++i) { d[i] = LINF; } d[s] = 0; priority_queue<pl, vector<pl>, greater<pl>> que; que.push(pl(0, s)); while (!que.empty()) { pl p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(pl(d[e.to], e.to)); } } } } }; const ll mod = 1000000007; struct mint { ll x; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream& is, const mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } } comb(200005); struct Sieve { int n; vector<int> f, primes; Sieve(int n = 1) : n(n), f(n + 1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i * i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x; } vector<int> factorList(int x) { vector<int> res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector<pl> factor(int x) { vector<int> fl = factorList(x); if (fl.size() == 0) return {}; vector<pl> res(1, pl(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } vector<pair<ll, int>> factor(ll x) { vector<pair<ll, int>> res; for (int p : primes) { int y = 0; while (x % p == 0) x /= p, ++y; if (y != 0) res.emplace_back(p, y); } if (x != 1) res.emplace_back(x, 1); return res; } }; using data = pair<ll, vector<ll>>; int h, w; ll xxx[1010][1010]; ll a[1010][1010]; ll dfs(int x, int y) { if (xxx[x][y] > 0) return xxx[x][y]; int tate[4] = {1, -1, 0, 0}; int yoko[4] = {0, 0, 1, -1}; ll ret = 1LL; for (ll i = 0; i < (4); ++i) { if (x + yoko[i] < 0 || y + tate[i] < 0 || x + yoko[i] >= w || y + tate[i] >= h) continue; if (a[x + yoko[i]][y + tate[i]] <= a[x][y]) continue; ret += dfs(x + yoko[i], y + tate[i]); ret %= mod; } return xxx[x][y] = ret; } int main() { ll n; cin >> n; vector<ll> a(n); for (ll i = 0; i < (n); ++i) cin >> a[i]; ll s1, s2; ll ans1 = 0; ll ans2 = 0; if (a[0] > 0) { bool pos = true; s1 = a[0]; s2 = -1; ans2 += a[0] + 1; } else { bool pos = false; s1 = 1; s2 = a[0]; ans1 += 1 - a[0]; } for (ll i = 1; i <= (n - 1); ++i) { if (0 < s1) { if (s1 + a[i] < 0) { s1 += a[i]; } else { ans1 += (s1 + 1) + a[i]; s1 = -1; } if (s2 + a[i] > 0) { s2 += a[i]; } else { ans2 += -(s2 - 1) - a[i]; s2 = 1; } } else { if (s1 + a[i] > 0) { s1 += a[i]; } else { ans1 += -(s1 - 1) - a[i]; s1 = 1; } if (s2 + a[i] < 0) { s2 += a[i]; } else { ans2 += (s2 + 1) + a[i]; s2 = -1; } } } ll ans = min(ans1, ans2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INF = 1e9; const int MOD = 1e9 + 7; int main() { int n; cin >> n; vector<int> a(n); for (auto &e : a) cin >> e; ll cnt = 0; ll sum = 0; for (int i = 0; i < n; ++i) { ll tmp = sum; sum += a[i]; if (i == 0) continue; if (tmp * sum >= 0) { cnt += abs(sum) + 1; if (sum >= 0) { a[i] -= sum; a[i]--; sum = -1; } else { a[i] -= sum; a[i]++; sum = 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
cpp
#include <bits/stdc++.h> using namespace std; const int INTMAX = 2147483647; const int64_t LLMAX = 9223372036854775807; const int MOD = 1000000007; template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } inline void swap(int64_t& a, int64_t& b) { a ^= b; b ^= a; a ^= b; } inline void swap(int& a, int& b) { a ^= b; b ^= a; a ^= b; } vector<int> a; int main() { int n; int64_t ans = 0, tmp, s; cin >> n; a.resize(n); for (int i{0}; i < (int)(n); i++) cin >> a[i]; s = a[0]; tmp = 0; if (s < 0) { tmp += (1 - s); s = 1; } for (int i{1}; i < (int)(n); i++) { if (!(s + a[i]) || (s ^ (s + a[i])) >= 0) { if (s > 0) { tmp += -((-1LL) - (s + a[i])); s = -1; } else { tmp += (1LL - (s + a[i])); s = 1; } } else s += a[i]; } ans = tmp; s = a[0]; tmp = 0; if (s > 0) { tmp += -(-1LL - s); s = -1; } for (int i{1}; i < (int)(n); i++) { if (!(s + a[i]) || (s ^ (s + a[i])) >= 0) { if (s > 0) { tmp += -((-1LL) - (s + a[i])); s = -1; } else { tmp += (1LL - (s + a[i])); s = 1; } } else s += a[i]; } chmin(ans, tmp); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int main() { long long int n; cin >> n; long long int sump = 1; long long int sumn = -1; long long int ansp = 0; long long int ansn = 0; for (int i = 0; i < n; i++) { long long int x; cin >> x; if (sump >= 0) { sump += x; if (sump >= 0) { ansp += sump + 1; sump = -1; } } else { sump += x; if (sump <= 0) { ansp += abs(sump) + 1; sump = 1; } } if (sumn >= 0) { sumn += x; if (sumn >= 0) { ansn += sumn + 1; sumn = -1; } } else { sumn += x; if (sumn <= 0) { ansn += abs(sumn) + 1; sumn = 1; } } 42; } 42; cout << min(ansn, ansp) - 1; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n, b, c = 0; cin >> n >> b; for (int i = 0; i < n - 1; i++) { int 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; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum[n]; sum[0] = a[0]; long long ans = 0; if (sum[0] == 0) { ans = 1; for (int i = 0; i < n; i++) { if (a[i] != 0) { sum[0] = abs(a[i]) / a[i] * pow(-1, (i % 2)); } } } for (int i = 1; i <= (int)(n - 1); i++) { int t = sum[i - 1] + a[i]; if (t == 0) { ans += 1; sum[i] = -abs(sum[i - 1]) / sum[i - 1]; } else if (abs(t) / t != abs(sum[i - 1]) / sum[i - 1]) { sum[i] = t; continue; } else { ans += abs(t) + 1; sum[i] = -abs(t) / t; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long int; const ll INF = (1LL << 32); const ll MOD = (ll)1e9 + 7; const double EPS = 1e-9; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; signed main() { ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> a; for (ll i = 0; i < n; i++) { ll x; cin >> x; a.push_back(x); } ll sum = a[0]; ll ans = 0; for (ll i = (1); i < (n); i++) { if (sum >= 0 and (sum + a[i]) >= 0) { while (sum + a[i] != -1) { a[i]--; ans++; } } else if (sum < 0 and (sum + a[i]) < 0) { while (sum + a[i] != 1) { a[i]++; ans++; } } sum += a[i]; } 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 sys stdin = sys.stdin def li(): return [int(x) for x in stdin.readline().split()] def li_(): return [int(x)-1 for x in stdin.readline().split()] def lf(): return [float(x) for x in stdin.readline().split()] def ls(): return stdin.readline().split() def ns(): return stdin.readline().rstrip() def lc(): return list(ns()) def ni(): return int(ns()) def nf(): return float(ns()) n = ni() a = li() # 正→負→正→... cur = a[0] ans_pn = 0 if cur <= 0: ans_pn = abs(a[0]-1) for i in range(1,n): cur += a[i] if i%2 == 0 and cur <= 0: ans_pn += abs(cur)+1 cur = 1 elif i%2 == 1 and cur >= 0: ans_pn += abs(cur)+1 cur = -1 # 負→正→負... cur = a[0] ans_np = 0 if cur <= 0: ans_np = abs(a[0]+1) for i in range(1,n): cur += a[i] if i%2 == 0 and cur >= 0: ans_np += abs(cur)+1 cur = -1 elif i%2 == 1 and cur <= 0: ans_np += abs(cur)+1 cur = 1 print(min(ans_np, ans_pn))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MOD = 1e9 + 7; const long long LINF = 1e18; long long n; vector<long long> a; long long count(long long sum) { long long cnt = 0; for (long long i = 1; i < n; ++i) { if (sum > 0) { sum += a[i]; if (sum >= 0) { cnt += abs(sum) + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } } return cnt; } int main() { cin >> n; long long ans = LINF; a.resize(n); for (long long i = 0; i < n; ++i) { cin >> a[i]; } if (a[0] == 0) { ans = min(ans, count(1) + 1ll); ans = min(ans, count(-1) + 1ll); } else { long long sum = a[0]; ans = min(ans, count(sum)); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; vector<int> V; list<int> L; stack<int> S; queue<int> Q; deque<int> dq; static const int MAX = 1e5; static const int NMAX = 50; static const int MMAX = 50; int N; long long a, cunt, sum; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; cin >> a; sum += a; for (int i = 1; i < N; i++) { cin >> a; if (sum * a >= 0) { cunt += sum + a + 1; sum = (sum > 0 ? -1 : 1); } else if (sum * a < 0 && abs(sum) >= abs(a)) { cunt += abs(sum) - abs(a) + 1; sum = (-1) * (sum + 1); } else sum += a; } cout << cunt << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); long long sum = 0; long long tmp = 0; long long ans = 0; int sign = 1; for (int i = 0; i < n; i++) { cin >> a.at(i); } sum = a.at(0); sign = sum / abs(sum); for (int i = 1; i < n; i++) { sum += a.at(i); if (sum == 0) { ans += 1; sum = -1 * sign; } else if (sign == sum / abs(sum)) { ans += abs(-1 * sign - sum); sum = -1 * sign; } sign *= -1; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long calc(long long a[], int n) { long long sum = a[0]; long long res = 0; for (int i = 1; i < n; i++) { if (sum < 0 && sum + a[i] <= 0) { res += 1 - sum - a[i]; a[i] += 1 - sum - a[i]; } else if (sum > 0 && sum + a[i] >= 0) { res += abs(a[i] - (-1 - sum)); a[i] = -1 - sum; } sum += a[i]; } return res; } int main() { int n; long long a[100010]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); long long res = 0; if (a[0] == 0) { res++; a[0] = 1; res += calc(a, n); a[0] = -1; res = min(calc(a, n) + 1, res); } else { res = calc(a, n); if (a[0] > 0) { long long tp = abs(-1 - a[0]); a[0] = -1; res = min(res, tp + calc(a, n)); } else { long long tp = abs(1 - a[0]); a[0] = 1; res = min(res, tp + calc(a, n)); } } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; long long a[1000010]; int n; unsigned long long solve() { unsigned long long sum = 0; long long oo = 0, flag; if (a[0] > 0) flag = -1; else if (a[0] < 0) flag = 1; for (int i = 0; i < n; i++) { oo += a[i]; if (flag == 1) { if (oo >= 0) { sum += oo + 1; oo = -1; } } if (flag == -1) { if (oo <= 0) { sum += 0 - oo + 1; oo = 1; } } flag = -flag; } return sum; } int main() { while (scanf("%d", &n) != EOF) { unsigned long long sum; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } if (a[0] == 0) { a[0] = 1; unsigned long long sum1 = solve(); a[0] = -1; unsigned long long sum2 = solve(); sum = min(sum1, sum2) + 1; } else { a[0] = 1; unsigned long long sum1 = solve() + abs(a[0] - 1); a[0] = -1; unsigned long long sum2 = solve() + abs(a[0] + 1); sum = min(sum1, sum2); } printf("%lld\n", sum); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007LL; const long long INF = 1LL << 60; using vll = vector<long long>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pair<long long, long long> = pair<long long, long long>; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int nx[4] = {0, 1, -1, 0}; int ny[4] = {1, 0, 0, -1}; int main() { long long n; cin >> n; vll a(n); for (long long i = 0; i < (n); i++) cin >> a[i]; long long cu = 0; long long k = 0; long long ans = INF; for (long long i = 0; i < (n); i++) { if (i % 2 == 0) { if (cu + a[i] < 0) { k += abs(cu + a[i]) + 1; cu = 1; } else if (cu + a[i] == 0) { cu = 1; k++; } } else { if (cu + a[i] > 0) { k += cu + a[i] + 1; cu = 0; cu--; } else if (cu + a[i] == 0) { cu = 0; cu--; k++; } } } ans = min(ans, k); k = 0; for (long long i = 0; i < (n); i++) { if (i % 2 == 1) { if (cu + a[i] < 0) { k += abs(cu + a[i]) + 1; cu = 1; } else if (cu + a[i] == 0) { cu = 1; k++; } } else { if (cu + a[i] > 0) { k += cu + a[i] + 1; cu = 0; cu--; } else if (cu + a[i] == 0) { cu = 0; cu--; k++; } } } ans = min(ans, k); cout << (ans) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int c[10][10]; int min_ind[10]; int main() { int n; cin >> n; ll a; cin >> a; ll sum = a, op_sum = 0; for (int i = 1; i < n; i++) { ll a; cin >> a; if (sum < 0) { if (sum + a > 0) { sum += a; } else { op_sum += abs(1 - a - sum); sum = 1; } } else { if (sum + a < 0) { sum += a; } else { op_sum += abs(-1 - a - sum); sum = -1; } } } cout << op_sum << 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[100000]; 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) { if (sum < 0) { int diff = 1 - sum; sum += diff; counter += diff; } } else { if (sum > 0) { int diff = sum + 1; sum -= diff; counter += diff; } } } int counterNeg = 0; sum = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 0) { if (sum > 0) { int diff = sum + 1; sum -= diff; counterNeg += diff; } } else { if (sum < 0) { int diff = 1 - sum; sum += diff; counterNeg += diff; } } } 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
n = int(input()) numbers = list(map(int, input().split())) counter = 0 sum_in = 0 sum_in1 = numbers[0] if numbers[0] >= 0: for i in range(len(numbers)): sum_in += numbers[i] if i % 2 == 0: if sum_in <= 0: sub = abs(sum_in) + 1 sum_in += sub numbers[i] += sub counter += sub else: if sum_in >= 0: sub = abs(sum_in) + 1 sum_in -= sub numbers[i] -= sub counter += sub else: for i in range(len(numbers)): sum_in += numbers[i] if i % 2 != 0: if sum_in <= 0: sub = abs(sum_in) + 1 sum_in += sub numbers[i] += sub counter += sub else: if sum_in >= 0: sub = abs(sum_in) + 1 sum_in -= sub numbers[i] -= sub counter += sub print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int a[100005]; void solver() { int cnt = 0; int sum = a[0]; for (int i = 1; i < n; ++i) { if (sum > 0) { if (sum + a[i] >= 0) { cnt += abs(sum + a[i]) + 1; sum = -1; } else { sum = sum + a[i]; } } else if (sum < 0) { if (sum + a[i] <= 0) { cnt += abs(sum + a[i]) + 1; sum = 1; } else { sum = sum + a[i]; } } } cout << cnt << endl; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } solver(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using ll = long long; using namespace std; template <typename T> using vec = std::vector<T>; template <typename T> struct BIT { int n; vector<T> bit; BIT(int n_) : n(n_ + 1), bit(n, 0) {} void add(int i, T x) { if (i == 0) { cout << "BIT add ERROR" << endl; exit(1); } for (int idx = i; idx < n; idx += (idx & -idx)) { bit[idx] += x; } } T sum(int i) { T s(0); for (int idx = i; idx > 0; idx -= (idx & -idx)) { s += bit[idx]; } return s; } }; int main() { ll n; cin >> n; BIT<ll> bt(n); vec<ll> a(n); vec<ll> sm(2); for (ll i = 0; i < (n); i++) { cin >> a[i]; if (i % 2 == 0) sm[0] += a[i]; else sm[1] = a[i]; } ll ans = 0; int plus = (sm[0] >= sm[1]) ? 0 : 1; int minus = 1 - plus; for (ll i = 0; i < (n); i++) { bt.add(i + 1, a[i]); } for (ll i = 0; i < (n); i++) { ll rsum = bt.sum(i + 1); if (i % 2 == plus && rsum <= 0) { bt.add(i + 1, abs(rsum) + 1); ans += abs(rsum) + 1; } else if (i % 2 == minus && rsum >= 0) { bt.add(i + 1, -1 * abs(rsum) - 1); ans += abs(rsum) + 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; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int odd = 0, even = 0, sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum <= 0) { odd += abs(sum) + 1; sum = +1; } else if (i % 2 == 1 && sum >= 0) { odd += abs(sum) + 1; sum = -1; } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { even += abs(sum) + 1; sum = +1; } else if (i % 2 == 0 && sum >= 0) { even += abs(sum) + 1; sum = -1; } } cout << min(odd, even) << 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 first; cin >> first; int oddSum, evenSum; long oddCount, evenCount; if (first == 0) { oddSum = -1; oddCount = 1; evenSum = 1; evenCount = 1; } else if (first > 0) { oddSum = -1; oddCount = first * -1 + 1; evenSum = first; evenCount = 0; } else if (first < 0) { oddSum = first; oddCount = 0; evenSum = 1; evenCount = first * -1 + 1; } for (int i = 1; i < n; i++) { int a; cin >> a; int nextOddSum = oddSum + a; int nextEvenSum = evenSum + a; if ((oddSum > 0 && nextOddSum < 0) || (oddSum < 0 && nextOddSum > 0)) { oddSum = nextOddSum; } else if (nextOddSum == 0) { oddSum = oddSum > 0 ? -1 : 1; oddCount += 1; } else if (nextOddSum > 0) { oddSum = -1; oddCount += nextOddSum + 1; } else if (nextOddSum < 0) { oddSum = 1; oddCount += nextOddSum * -1 + 1; } if ((evenSum > 0 && nextEvenSum < 0) || (evenSum < 0 && nextEvenSum > 0)) { evenSum = nextEvenSum; } else if (nextEvenSum == 0) { evenSum = evenSum > 0 ? -1 : 1; evenCount += 1; } else if (nextEvenSum > 0) { evenSum = -1; evenCount += nextEvenSum + 1; } else if (nextEvenSum < 0) { evenSum = 1; evenCount += nextEvenSum * -1 + 1; } } long answer = min(oddCount, evenCount); cout << answer << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int,input().split())) ans1 = A[0] ans = ans1 A = A[1:] cnt = 0 m = 0 for a in A: m += 1 if ans1 > 0: ans += a if m % 2 == 1 and ans >= 0: cnt += (abs(ans)+1) ans -= (abs(ans)+1) elif m % 2 == 0 and ans <= 0: cnt += (abs(ans)+1) ans += (abs(ans)+1) else: ans += a if m % 2 == 1 and ans <= 0: cnt += (abs(ans)+1) ans += (abs(ans)+1) elif m % 2 == 0 and ans >= 0: cnt += (abs(ans)+1) ans -= (abs(ans)+1) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Linq; using System.Collections.Generic; using System.Numerics; using static System.Console; class Program { static Scanner sc = new Scanner(); internal static void Main(string[] args) { var N = sc.nextInt(); var a = sc.ArrayInt(N); var psum = 0; var msum = 0; var pcnt = 0; var mcnt = 0; for (int i = 0; i < N; i++) { psum += a[i]; msum += a[i]; if (i % 2 == 0) { if (psum <= 0) { pcnt += 1 - psum; psum = 1; } if (msum >= 0) { mcnt += Math.Abs(-1 - msum); msum = -1; } } else { if (psum >= 0) { pcnt += Math.Abs(-1 - psum); psum = -1; } if (msum <= 0) { mcnt += 1 - msum; msum = 1; } } } WriteLine(Math.Min(pcnt, mcnt)); } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i as = gets.split(' ').map { |e| e.to_i } x = 0 bs = [] as.each { |e| x += e bs << x } # p bs ans = 0 for i in (1..(n - 1)) a, b = bs[i - 1], bs[i] if a >= 0 && a <= b d = b + 1 for j in (i..(n-1)) bs[j] = bs[j] - d end # p bs ans += d elsif a <= 0 && a >= b d = -1 * b + 1 for j in (i..(n-1)) bs[j] = bs[j] + d end # p bs ans += d end end ans += 1 if b[n-1] == 0 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
python3
n = int(input()) a = list(map(int, input().split())) s, ans, i = 0, 0, 0 while a[i] == 0 and i < n: ans += 2 i += 1 ans = max(0, ans - 1) if i == n: print(ans) exit() if ans > 0: ans -= 1 if abs(a[i]) == 1: ans += 1 s = a[i] // abs(a[i]) else: s = a[0] i += 1 for j in range(i, n): if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)): s += a[j] else: pre_s = s s = -1 * s // abs(s) ans += abs(a[j] - s + pre_s) print(ans)