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 | python3 | n=int(input())
l=list(map(int,input().split()))
c,f=0,l[0]/abs(l[0])
s=l[0]
for i in range(1,n):
t=s+l[i]
if s*t>=0:
s=-1*f
c+=abs(t)+1
else:
f=-1*f
s=s+l[i]
print(int(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 | python3 | import numpy as np
from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
c = [0] * n
for i in range(n):
c[i] = c[i - 1] + a[i]
c = np.array(c)
ans1, ans2 = 0, 0
tmp = deepcopy(c)
for i in range(n):
t = tmp[i]
if i % 2 == 0 and tmp[i] >= 0:
tmp -= t + 1
ans1 += t + 1
elif i % 2 == 1 and tmp[i] <= 0:
tmp += -t + 1
ans1 += -t + 1
tmp = deepcopy(c)
for i in range(n):
t = tmp[i]
if i % 2 == 1 and tmp[i] >= 0:
tmp -= t + 1
ans2 += t + 1
elif i % 2 == 0 and tmp[i] <= 0:
tmp += -t + 1
ans2 += -t + 1
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 1000000000 + 7;
int main() {
int n;
cin >> n;
int cnt = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
int s = a + sum;
if (sum < 0 && s <= 0) {
cnt += 1 - s;
sum = 1;
} else if (sum > 0 && s >= 0) {
cnt += 1 + s;
sum = -1;
} else {
sum = s;
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | l = int(input())
n = [int(i) for i in input().split()]
ans = []
ans.append(n[0])
count = 0
for i in range(1, l):
nsum = sum(n[j] for j in range(0, i))
while(True):
#print("i:" + str(i))
#print("n:" + str(n[i]))
#print(("nsum:" + str(nsum)))
if (n[i-1] < 0 and n[i] <= 0) or (n[i-1] > 0 and n[i] >= 0) or (nsum * (nsum + n[i]) >= 0):
if n[i-1] < 0:
count += 1
n[i] += 1
else:
count += 1
n[i] -= 1
else:
ans.append(n[i])
break
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
ls = list(map(int, input().split()))
def calc(ls):
count = 0
acc = ls[0]
sign = acc // abs(acc)
for x in ls[1:]:
y = acc + x
if y == 0 or y // abs(y) == sign:
z = -sign - y
count += abs(z)
acc = -sign
else:
acc = y
sign *= -1
return count
if ls[0] != 0:
count = calc(ls)
else:
ls0 = ls.copy()
ls0[0] = 1
ls1 = ls.copy()
ls1[0] = -1
count = min(calc(ls0), calc(ls1)) + 1
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void chmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void chmin(T &a, T b) {
if (a > b) a = b;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long sum = a[0];
long long change = 0LL;
if (sum > 0) {
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && sum + a[i] >= 0) {
sum += a[i];
change += abs(-1 - sum);
sum = -1;
} else if (i % 2 == 0 && sum + a[i] <= 0) {
sum += a[i];
change += abs(1 - sum);
sum = 1;
} else {
sum += a[i];
}
}
} else {
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && sum + a[i] >= 0) {
sum += a[i];
change += abs(-1 - sum);
sum = -1;
} else if (i % 2 == 1 && sum + a[i] <= 0) {
sum += a[i];
change += abs(1 - sum);
sum = 1;
} else {
sum += a[i];
}
}
}
cout << change;
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())
L = np.array([int(i) for i in input().split()])
count = 0
s = L[0]
if L[0] == 0:
if L[1] > 0:
L[0] = -1
else:
L[0] = 1
count += 1
# print(L)
loopnum = n//2
if n%2 == 0:
loopnum -= 1
#+-+-...
for i in range(loopnum):
s = s + L[2*i+1]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
s = s + L[2*i+2]
if s <= 0:
subt = s - 1
count -= subt
s = s - subt
if n%2 == 0:
s = s + L[-1]
if s >= 0:
subt = s + 1
count += subt
cand1 = count
count = 0
s = L[0]
#-+-+...
for i in range(loopnum):
s = s + L[2*i+1]
if s <= 0:
subt = s - 1
count -= subt
s = s - subt
s = s + L[2*i+2]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
if n%2 == 0:
s = s + L[-1]
if s <= 0:
subt = s - 1
count -= subt
cand2 = count
#print(cand1)
#print(cand2)
print(min(cand1,cand2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 N[100000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> N[i];
}
int ans = 0;
int sum = N[0];
if (N[0] == 0) {
if (N[1] >= 0) {
N[0] = -1;
}
if (N[1] < 0) {
N[0] = 1;
}
}
if (N[0] > 0) {
for (int i = 1; i < n; i++) {
sum = sum + N[i];
if (i % 2 == 0 && sum <= 0) {
N[i] = N[i] - sum + 1;
ans = ans - sum + 1;
sum = 1;
}
if (i % 2 == 1 && sum >= 0) {
N[i] = N[i] - sum - 1;
ans = ans + sum + 1;
sum = -1;
}
}
}
if (N[0] < 0) {
for (int i = 1; i < n; i++) {
sum = sum + N[i];
if (i % 2 == 0 && sum >= 0) {
N[i] = N[i] - sum - 1;
ans = ans + sum + 1;
sum = -1;
}
if (i % 2 == 1 && sum >= 0) {
N[i] = N[i] - sum + 1;
ans = ans - sum + 1;
sum = 1;
}
}
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ABC059C
{
class Program
{
// -1 4 3 2 -5 4
// -1 4 -4 2 -5 5 -> 8
static long sequentialize(int[] a)
{
var n = a.Length;
var sum = a[0];
var changeCount = 0L;
int v = a[0] > 0 ? 1 : -1;
for (int i = 1; i < n; i++)
{
var temp = sum + a[i];
if (sum > 0 && temp > 0)
{
// tempを-1にしたい
var prev = a[i];
a[i] = -sum - 1;
changeCount += Math.Abs(prev - a[i]);
}
else if (sum < 0 && temp < 0)
{
// tempを+1にしたい
var prev = a[i];
a[i] = -sum + 1;
changeCount += Math.Abs(prev - a[i]);
}
else if (temp == 0)
{
changeCount += 1;
a[i] = v;
}
sum += a[i];
v = -v;
}
return changeCount;
}
static void Solve()
{
var n = Input.NextInt();
var a = Input.NextInt(n).ToArray();
var b = new int[n];
a.CopyTo(b, 0);
var changeCount1 = sequentialize(a);
// 先頭の符号反転
b[0] = b[0] / -b[0];
var changeCount2 = sequentialize(b) + Math.Abs(b[0]) + 1;
Console.WriteLine(Math.Min(changeCount1, changeCount2));
}
#region Competitive Template
public static void Main(string[] args)
{
var needsFlushOutput = true;
if (needsFlushOutput)
{
// 細かく出力しないようにする
var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
Console.SetOut(sw);
}
// 仮想的に標準入力をセットする
// NextLine系を使っていると使えない
//Input.SetText("");
Solve();
Console.Out.Flush();
}
static class Input
{
static char[] separator = { ' ' };
public static bool IsEof { get; set; }
static Queue<string> q { get; set; }
static Input()
{
IsEof = false;
q = new Queue<string>();
}
/// <summary>
/// 入力予約
/// </summary>
/// <param name="items"></param>
public static void SetText(IEnumerable<string> items)
{
foreach (var item in items)
{
SetText(item);
}
}
/// <summary>
/// 入力予約
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool SetText(string s)
{
if (s == null) return false;
foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
{
q.Enqueue(elem);
}
return true;
}
/// <summary>
/// 内部queueに入力からの値をsplitして格納する
/// </summary>
/// <returns></returns>
static bool read()
{
var s = Console.ReadLine();
if (s == null) return false;
foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
{
q.Enqueue(elem);
}
if (!q.Any()) return read();
return true;
}
/// <summary>
/// 次のstringを一つ読み込む
/// </summary>
/// <returns></returns>
public static string Next()
{
if (!q.Any())
{
if (!read())
{
IsEof = true;
return "";
}
}
return q.Dequeue();
}
public static int NextInt() => int.Parse(Next());
public static long NextLong() => long.Parse(Next());
public static double NextDouble() => double.Parse(Next());
public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList();
public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList();
public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList();
public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList();
public static List<string> NextLine() => Console.ReadLine().Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList();
public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList();
public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList();
}
#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 | a = list(map(int,input().split()))
def judge(count):
idx = 0
for i in range(2,n+1):
if count == -1:
if sum(a[0:i]) < 0:
pass
else:
check = sum(a[0:i]) + 1
j = a[i-1]
a[i-1] = j - check
idx += check
elif count == 1:
if sum(a[0:i]) > 0:
pass
else:
check = 1 - sum(a[0:i])
j = a[i-1]
a[i-1] = check+ j
idx += check
count *= -1
return idx
if a[0] > 0:
b = judge(-1)
print(b)
elif a[0] < 0:
c = judge(1)
print(c)
else:
a[0] = 1
b = judge(-1)
a[0] = -1
c = judge(1)
print(min(b,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;
ll ts = 1000000007;
ll sum, sum2, ans, i;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
bool can = false;
ll ans = 0, sum = a[0], nextSum = a[0];
for (int i = 1; i < n; i++) {
nextSum += a[i];
if (sum < 0 && nextSum < 0 || sum > 0 && nextSum > 0 || nextSum == 0) {
ll N;
if (nextSum >= 0) N = nextSum + 1;
if (nextSum < 0) N = nextSum - 1;
ans += abs(N);
nextSum -= a[i];
for (int j = 0; j < abs(N); j++) {
if (a[0] >= 0 && i % 2 == 1 || a[0] <= 0 && i % 2 == 0)
a[i]--;
else
a[i]++;
}
nextSum += a[i];
sum = nextSum;
} else {
sum = nextSum;
}
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int dptemp[100010];
int s1[100010], dp[100010];
int mi = 0x3f3f3f3f, n, a, sum, pri1, pri2, all;
scanf("%d", &n);
dp[0] = 0;
for (a = 1; a <= n; a++) {
scanf("%d", &s1[a]);
dp[a] = s1[a] + dp[a - 1];
dptemp[a] = dp[a];
}
sum = 0;
all = 0;
if (dp[1] == 0) {
dp[1]++;
sum++;
all = 1;
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] >= 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
for (a = 1; a <= n; a++) dp[a] = dptemp[a];
dp[1]--;
sum--;
all = 1;
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] > 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
} else if (dp[1] > 0) {
sum = 0;
all = 0;
for (a = 1; a <= n; a++) dp[a] = dptemp[a];
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] >= 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
} else {
sum = 0;
all = 0;
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] > 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
}
printf("%d\n", mi);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> an(n);
for (int i = 0; i < n; ++i) {
cin >> an[i];
}
int cnt_min = INT_MAX;
for (int j = 0; j < 2; ++j) {
int sign = j == 0 ? -1 : 1;
int accum = an[0];
int cnt = 0;
if (accum * sign <= 0) {
auto x = sign - accum;
accum += x;
cnt += abs(x);
}
for (int i = 1; i < n; ++i) {
auto new_accum = accum + an[i];
if (new_accum * accum >= 0) {
int x = -sign - new_accum;
new_accum += x;
cnt += abs(x);
}
int new_sign = new_accum > 0 ? 1 : -1;
accum = new_accum;
sign = new_sign;
}
if (cnt < cnt_min) {
cnt_min = cnt;
}
}
cout << cnt_min << 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())
nList = list(map(int,input().split()))
gokei = nList[0]
count1 = 0
for i in range(1,n):
gokei_tmp =gokei+ nList[i]
if (gokei < 0 and gokei_tmp > 0) or (gokei > 0 and gokei_tmp < 0):
gokei = gokei_tmp
else:
count1 += abs(gokei_tmp)
gokei_tmp -= gokei_tmp
if gokei > 0:
gokei_tmp -= 1
else:
gokei_tmp += 1
count1 += 1
gokei = gokei_tmp
gokei = nList[0]
count2 = abs(gokei)
if nList[0] > 0:
gokei -= gokei -1
else:
gokei -= gokei +1
count2 += 1
for i in range(1,n):
gokei_tmp =gokei+ nList[i]
if (gokei < 0 and gokei_tmp > 0) or (gokei > 0 and gokei_tmp < 0):
gokei = gokei_tmp
else:
count2 += abs(gokei_tmp)
gokei_tmp -= gokei_tmp
if gokei > 0:
gokei_tmp -= 1
else:
gokei_tmp += 1
count2 += 1
gokei = gokei_tmp
if count1 >= count2:
print(count2)
else:
print(count1)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 < (int)(n); i++) {
cin >> a[i];
}
int cnt1 = 0;
int cnt2 = 0;
int now1 = a[0];
int now2 = a[0];
for (int i = 1; i < (int)(n); i++) {
now1 += a[i];
if (i % 2 != 0) {
if (now1 >= 0) {
cnt1 += now1 + 1;
now1 = -1;
}
} else {
if (now1 <= 0) {
cnt1 += 1 - now1;
now1 = 1;
}
}
}
for (int i = 1; i < (int)(n); i++) {
now2 += a[i];
if (i % 2 != 0) {
if (now2 <= 0) {
cnt2 += 1 - now2;
now2 = 1;
}
} else {
if (now2 >= 0) {
cnt2 += 1 + now2;
now2 = -1;
}
}
}
int ans = min(cnt1, cnt2);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
count=0
tmp=A[1]+A[0]
for i in range(2,n):
if tmp<0:
if tmp+A[i]<=0:
count+= abs(1-(A[i]+tmp))
tmp=1
else:
tmp+=A[i]
continue
else:
if tmp+A[i]>=0:
count+=abs(-1-(A[i]+tmp))
tmp=-1
else:
tmp+=A[i]
continue
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long l1[n + 1];
long long x = 0, s = 0;
for (int i = 1; i <= n; i++) {
cin >> l1[i];
x += l1[i];
if (i >= 2) {
if (x - l1[i] <= 0 && x <= 0) {
s += abs((-x + l1[i] + 1) - l1[i]);
l1[i] = l1[i] - x + 1;
x = 1;
} else if (x - l1[i] >= 0 && x >= 0) {
s += abs(-(x - l1[i] + 1) - l1[i]);
l1[i] = -(x - l1[i] + 1);
x = -1;
}
}
}
cout << s << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int cnt = 0;
int total = a[0];
if (total == 0) {
total = 1;
cnt++;
}
for (int i = 1; i < n; i++) {
if (total > 0) {
int temp = total;
temp += a[i];
if (temp >= 0) {
cnt += temp + 1;
total = -1;
continue;
} else {
total = temp;
continue;
}
}
if (total < 0) {
int temp = total;
temp += a[i];
if (temp <= 0) {
cnt += (-temp + 1);
total = 1;
continue;
} else {
total = temp;
continue;
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from itertools import accumulate
def sol(sign=1):
add = 0
ans = 0
for i in range(1,N):
acmA[i] += add
if sign == 1:
if (i%2==0 and acmA[i-1]*acmA[i]<0) or (i%2==1 and acmA[i-1]*acmA[i]>0) :continue
else:
if (i%2==1 and acmA[i-1]*acmA[i]<0) or (i%2==0 and acmA[i-1]*acmA[i]>0) :continue
tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
#print(i, tmp_add, acmA[i])
acmA[i] += tmp_add
add += tmp_add
ans +=abs(tmp_add)
return ans
N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
print(min(sol(1),sol(-1)))
#print(acmA) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int n, a[111111], hoge, huga, nyaa = 0, nyan = 0;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
if (!a[0]) {
hoge = 1;
huga = -1;
nyaa = nyan = 1;
} else {
hoge = ((a[0]) > (-a[0]) ? (a[0]) : (-a[0]));
huga = ((a[0]) > (-a[0]) ? (-a[0]) : (a[0]));
nyaa += abs(a[0] - hoge);
nyan += abs(a[0] - huga);
}
int p = 1;
for (int i = 1; i < n; i++) {
hoge += a[i];
if (p) {
if (hoge >= 0) {
nyaa += hoge + 1;
hoge = -1;
}
} else {
if (hoge <= 0) {
nyaa += 1 - hoge;
hoge = 1;
}
}
huga += a[i];
if (p) {
if (huga <= 0) {
nyan += 1 - huga;
huga = 1;
}
} else {
if (huga >= 0) {
nyan += huga + 1;
huga = -1;
}
}
p ^= 1;
}
printf("%d\n", ((nyaa) > (nyan) ? (nyan) : (nyaa)));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void scan(vector<T>& a, long long n, istream& cin) {
T c;
for (long long(i) = 0; (i) < (n); ++(i)) {
cin >> c;
a.push_back(c);
}
}
using vs = vector<string>;
using vi = vector<long long>;
using pii = pair<long long, long long>;
using psi = pair<string, long long>;
using vvi = vector<vi>;
using pss = pair<string, string>;
using vpii = vector<pii>;
template <class T>
bool valid(T x, T w) {
return 0 <= x && x < w;
}
long long dx[4] = {1, -1, 0, 0};
long long dy[4] = {0, 0, 1, -1};
long long dp[1000000];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
;
;
;
long long n;
cin >> n;
vi a;
for (long long(i) = 0; (i) < (n); ++(i)) {
long long c;
cin >> c;
a.push_back(c);
}
long long s = a[0];
for (long long(i) = 0; (i) < (n - 1); ++(i)) {
if (s * (s + a[i + 1]) < 0) {
s += a[i + 1];
dp[i + 1] = dp[i];
} else {
dp[i + 1] = dp[i] + abs(s + a[i + 1]) + 1;
s = s < 0 ? 1 : -1;
}
}
cout << dp[n - 1] << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long inf = LLONG_MAX;
long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
template <typename T>
struct segment_tree {
long long n;
long long m = 1;
vector<T> v;
T id_e;
T seg_func(T a, T b) { return a + b; }
void init(vector<T> a, T b) {
id_e = b;
while (m < a.size()) m *= 2;
n = 2 * m - 1;
vector<T> w(n);
v = w;
for (long long i = 0; i < a.size(); i++) v[m - 1 + i] = a[i];
for (long long i = a.size(); i < m; i++) v[m - 1 + i] = id_e;
for (long long i = m - 2; i >= 0; i--) {
v[i] = seg_func(v[2 * i + 1], v[2 * i + 2]);
}
}
void change(long long a, T b) {
a += m - 1;
v[a] = b;
while ((a - 1) / 2 != a) {
a = (a - 1) / 2;
v[a] = seg_func(v[2 * a + 1], v[2 * a + 2]);
}
}
T subsub(long long a, long long b, long long cur, long long l, long long r) {
if (a <= l && r <= b) return v[cur];
if (r < a || b < l) return id_e;
if (l < r) {
return seg_func(subsub(a, b, 2 * cur + 1, l, (l + r) / 2),
subsub(a, b, 2 * cur + 2, (l + r) / 2 + 1, r));
}
}
T subsum(long long a, long long b) {
if (a == b) return v[m - 1 + a];
T ans = id_e;
long long cur = 0, l = 0, r = m - 1;
ans = seg_func(ans, subsub(a, b, cur, l, r));
return ans;
}
};
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
segment_tree<long long> seg1, seg2;
seg1.init(a, 0);
seg2.init(a, 0);
long long ans1 = 0, ans2 = 0;
for (long long i = 0; i < n; i++) {
long long x = seg1.subsum(0, i), y = seg2.subsum(0, i);
if (i == 0) {
if (x <= 0) {
seg1.change(i, 1);
ans1 += 1 - x;
}
if (y >= 0) {
seg2.change(i, -1);
ans2 += x + 1;
}
} else {
long long xx = seg1.subsum(0, i - 1), yy = seg2.subsum(0, i - 1);
if (xx > 0 && x >= 0) {
seg1.change(i, a[i] - (x + 1));
ans1 += x + 1;
} else if (xx < 0 && x <= 0) {
seg1.change(i, a[i] + (1 - x));
ans1 += 1 - x;
}
if (yy > 0 && y >= 0) {
seg2.change(i, a[i] - (y + 1));
ans2 += y + 1;
} else if (yy < 0 && y <= 0) {
seg2.change(i, a[i] + (1 - y));
ans2 += 1 - y;
}
}
cout << seg2.subsum(0, i) << endl;
}
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 | // AtCoder-Template.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
#define FOR(i,N) for(auto i=0; i<N; ++i)
#define FORREV(i,N,_cnt) for(auto i=N-1,cnt=_cnt; cnt > 0; --i, --cnt)
#define ALL(x) x.begin(), x.end()
/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T& a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T& a, const T& b) { if (b > a) a = b; }
/* clang-format on */
int main() {
#ifdef DEBUG
ifstream ifs("in.txt");
cin.rdbuf(ifs.rdbuf());
#endif
ll N; cin >> N; vector<ll> A(N); FOR(i, N) cin >> A[i];
// RUISEKI
vector<ll> Rui(N); Rui[0] = A[0];
ll ans = 0;
if (Rui[0] == 0) {
ans += 1;
Rui[0] = 1;
}
FOR(i, N - 1) {
Rui[i + 1] += Rui[i] + A[i + 1];
if (i + 1 >= 1 and Rui[i+1] * Rui[i] >= 0) {
if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) {
ans += 1; Rui[i + 1] = -1;
}
else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) {
ans += 1; Rui[i + 1] = 1;
}
else { // 両方とも同じ符号になった場合
if (Rui[i + 1] > 0) {
ans += Rui[i + 1] + 1;
Rui[i + 1] = -1;
}
else {
ans += - Rui[i + 1] + 1;
Rui[i + 1] = 1;
}
}
}
}
///////////////////////////////////////////////
///////// ///////////////
///////////////////////////////////////////////
Rui.clear(); Rui.resize(N); Rui[0] = A[0];
ll tmp = 0;
if (Rui[0] == 0) {
tmp += Rui[0] ;
Rui[0] = -1;
}
FOR(i, N - 1) {
Rui[i + 1] += Rui[i] + A[i + 1];
if (i + 1 >= 1 and Rui[i + 1] * Rui[i] >= 0) {
if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) {
tmp += 1; Rui[i + 1] = -1;
}
else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) {
tmp += 1; Rui[i + 1] = 1;
}
else { // 両方とも同じ符号になった場合
if (Rui[i + 1] > 0) {
tmp += Rui[i + 1] + 1;
Rui[i + 1] = -1;
}
else {
tmp += -Rui[i + 1] + 1;
Rui[i + 1] = 1;
}
}
}
}
chmin(ans, tmp);
cout << ans << endl;
//cout << 0 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
b=list(map(int,input().split()))
a=b
condition=''
cnt=0
wa=0
for i in range(n):
wa+=a[i]
if i == 0:
if a[i]>0:
condition='minus'
else:
condition='plus'
elif condition == 'plus':
condition='minus'
if wa<=0:
cnt+=abs(wa)+1
a[i]+=abs(wa)+1
wa+=abs(wa)+1
elif condition == 'minus':
condition='plus'
if wa>=0:
cnt+=abs(wa)+1
a[i]-=abs(wa)+1
wa-=abs(wa)+1
cnt1=cnt
a=b
condition=''
cnt=0
wa=0
for i in range(n):
a[i]=a[i]/abs(a[i])*(-1)
cnt+=abs(a[i])+1
wa+=a[i]
if i == 0:
if a[i]>0:
condition='minus'
else:
condition='plus'
elif condition == 'plus':
condition='minus'
if wa<=0:
cnt+=abs(wa)+1
a[i]+=abs(wa)+1
wa+=abs(wa)+1
elif condition == 'minus':
condition='plus'
if wa>=0:
cnt+=abs(wa)+1
a[i]-=abs(wa)+1
wa-=abs(wa)+1
cnt2=cnt
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0], count = 0;
for (int i = 1; i < n; i++) {
if (sum > 0) {
if (sum + a[i] >= 0) {
count += abs(a[i] - (-1 - sum));
a[i] = -1 - sum;
}
} else {
if (sum + a[i] <= 0) {
count += abs(a[i] - (1 - sum));
a[i] = 1 - sum;
}
}
sum += a[i];
}
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 | python3 | n = int(input())
n_list = list(map(int, input().split()))
count = 0
prev_sum = n_list[0]
for i in n_list[1:]:
if prev_sum < 0:
if -prev_sum >= i:
count += abs((-prev_sum + 1) - i)
i = -prev_sum + 1
else:
if -prev_sum < i:
count += abs((-prev_sum - 1) - i)
i = -prev_sum - 1
prev_sum += i
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
sum = a[i];
continue;
}
if (sum > 0) {
if (sum + a[i] >= 0) {
ans += sum + a[i] + 1;
sum += a[i] - (sum + a[i] + 1);
continue;
}
} else {
if (sum + a[i] <= 0) {
ans -= sum + a[i] - 1;
sum += a[i] - (sum + a[i] - 1);
continue;
}
}
sum += a[i];
}
sum = 0;
long long tmp = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
sum = a[i] > 0 ? -1 : 1;
tmp += a[i] > 0 ? a[i] + 1 : -(a[i] - 1);
continue;
}
if (sum > 0) {
if (sum + a[i] >= 0) {
tmp += sum + a[i] + 1;
sum += a[i] - (sum + a[i] + 1);
continue;
}
} else {
if (sum + a[i] <= 0) {
tmp -= sum + a[i] - 1;
sum += a[i] - (sum + a[i] - 1);
continue;
}
}
sum += a[i];
}
cout << (ans < tmp ? ans : tmp) << 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 r(int a) {
if (a % 2 == 0)
return -1;
else
return 1;
}
int main() {
int n, i;
long long a[1000], ans = 0, ans2 = 0, tmp = 0, tmp2 = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lld", &a[i]);
for (i = 0; i < n; i++) {
tmp += a[i];
tmp2 += a[i];
if (tmp * r(i) <= 0) {
ans += tmp * r(i + 1) + 1;
tmp = r(i);
}
if (tmp2 * r(1 + i) <= 0) {
ans2 += tmp2 * r(i) + 1;
tmp2 = r(i + 1);
}
}
printf("%lld\n", ans2 < ans ? ans2 : ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int body(std::vector<int>& a) {
int ans = 0;
std::vector<int> s(a.size());
s.at(0) = a.at(0);
for (unsigned int i = 1; i < a.size(); i++) {
s.at(i) = s.at(i - 1) + a.at(i);
}
int diff = 0;
for (unsigned int i = 1; i < s.size(); i++) {
s.at(i) += diff;
int n = 0;
if (s.at(i - 1) > 0 && s.at(i) >= 0) {
n = s.at(i) + 1;
ans += n;
diff -= n;
s.at(i) += diff;
} else if (s.at(i - 1) < 0 && s.at(i) <= 0) {
n = -s.at(i) + 1;
ans += n;
diff += n;
s.at(i) += diff;
}
}
return ans;
}
int main(int argc, char** argv) {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a.at(i);
}
int ans;
if (a.at(0) != 0) {
ans = body(a);
} else {
a.at(0) = -1;
int ans_a = body(a);
a.at(0) = 1;
int ans_b = body(a);
ans = std::min(ans_a, ans_b);
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = a[0]
if f == 0:
f = 1
ans1 += 1
for i in range(1, N):
if f * (f + a[i]) < 0:
f += a[i]
continue
ans1 += abs(f + a[i]) + 1
if f > 0:
f = -1
else:
f = 1
f = -a[0]
if f == 0:
f = -1
ans1 += 1
for i in range(1, N):
if f * (f + a[i]) < 0:
f += a[i]
continue
ans2 += abs(f + a[i]) + 1
if f > 0:
f = -1
else:
f = 1
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long cost1 = 0;
long long sum1 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 > 0)
continue;
else if (i % 2 == 0 && sum1 < 0) {
cost1 += abs(sum1) + 1;
sum1 = 1;
} else if (i % 2 == 1 && sum1 > 0) {
cost1 += abs(sum1) + 1;
sum1 = -1;
} else {
continue;
}
}
long long cost2 = 0;
long long sum2 = 0;
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum2 > 0) {
cost2 += abs(sum2) + 1;
sum2 = -1;
} else if (i % 2 == 0 && sum2 < 0) {
continue;
} else if (i % 2 == 1 && sum2 > 0) {
continue;
} else {
cost2 += abs(sum2) + 1;
sum2 = 1;
}
}
long long ans = min(cost1, cost2);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
nums = gets.split.map(&:to_i)
cumulative = nums.shift
ans = 0
nums.each do |num|
if cumulative > 0
cumulative += num
if cumulative >= 0
decrement = cumulative.abs + 1
ans += decrement
cumulative -= decrement
end
else
cumulative += num
if cumulative <= 0
increment = cumulative.abs + 1
ans += increment
cumulative += increment
end
end
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (auto &k : a) cin >> k;
ll sum = a[0];
ll sign = (sum > 0) ? 1 : -1;
ll ans = 0;
for (int i = 1; i < n; ++i) {
sign *= -1;
ll tempsum = sum + a[i];
ll sumsign = (tempsum > 0) ? 1 : -1;
if (sumsign != sign) {
ans += abs(abs(sum - sign) * sign - a[i]);
sum = sign;
} else {
sum += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sumi = a[0];
long long sump = a[0];
long long cnt = 0;
for (int i = 0; i < n - 1; i++) {
sump += a[i + 1];
if (sumi < 0) {
if (sump <= 0) {
cnt += 1 - sump;
sump = 1;
}
} else if (sumi > 0) {
if (sump >= 0) {
cnt += sump + 1;
sump = -1;
}
}
sumi = sump;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 0:
if sum_ <= 0:
count += abs(sum_) + 1
sum_ = 1
else:
if sum_ >= 0:
count += abs(sum_) + 1
sum_ = -1
print(count)
elif sum_ < 0:
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 1:
if sum_ <= 0:
count += abs(sum_) + 1
sum_ = 1
else:
if sum_ >= 0:
count += abs(sum_) + 1
sum_ = -1
print(count)
else:
sum_ = 1
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 0:
if sum_ <= 0:
count += abs(sum_) + 1
sum_ = 1
else:
if sum_ >= 0:
count += abs(sum_) + 1
sum_ = -1
count1 = count
sum_ = -1
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 1:
if sum_ <= 0:
count += abs(sum_) + 1
sum_ = 1
else:
if sum_ >= 0:
count += abs(sum_) + 1
sum_ = -1
count2 = count
print(min(count1, count2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
count=0
if abs(A[1]-A[0])!=0:
tmp=A[1]+A[0]
if tmp>0 and A[0]>0:
count+=abs(-1-(A[1]+A[0]))
tmp=-1
elif tmp<0 and A[0]<0:
count+=abs(1-(A[1]+A[0]))
tmp=1
else:
count+=abs(-1-(A[1]+A[0]))
tmp=-1
for i in range(2,n):
if tmp<0:
if tmp+A[i]<=0:
count+= abs(1-(A[i]+tmp))
tmp=1
else:
tmp+=A[i]
continue
else:
if tmp+A[i]>=0:
count+=abs(-1-(A[i]+tmp))
tmp=-1
else:
tmp+=A[i]
continue
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
ll ans = INF;
ll cnt = 0;
ll k;
vector<ll> sum(n, 0);
sum[0] = a[0];
if (a[0] == 0) {
cnt++;
sum[0] = 1;
}
for (ll i = 1; i < n; i++) {
sum[i] = a[i] + sum[i - 1];
if (sum[i] * sum[i - 1] >= 0) {
k = sum[i];
sum[i] = -sum[i - 1] / abs(sum[i - 1]);
cnt += abs(k - sum[i]);
}
}
ans = min(ans, cnt);
cnt = 0;
if (a[0] == 0) {
sum[0] = -1;
cnt++;
} else {
k = a[0];
sum[0] = -abs(a[0]) / a[0];
cnt += abs(sum[0] - k);
}
for (ll i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i - 1];
if (sum[i] * sum[i - 1] >= 0) {
k = sum[i];
sum[i] = -abs(sum[i - 1]) / sum[i - 1];
cnt += abs(k - sum[i]);
}
}
ans = min(ans, cnt);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans = 1e9
for sign in (1, -1):
s = sign
res, acc = 0, 0
for a in A:
acc += a
if acc * s <= 0:
res += abs(acc-s)
acc = s
s *= -1
print(acc, a, res)
ans = min(ans, res)
print()
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.NoSuchElementException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author HBonsai
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CPushpush solver = new CPushpush();
solver.solve(1, in, out);
out.close();
}
static class CPushpush {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.nextIntArray(n);
ArrayDeque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
if (i % 2 == (n - 1) % 2) {
q.addFirst(a[i]);
} else {
q.addLast(a[i]);
}
}
StringBuilder ans = new StringBuilder();
while (!q.isEmpty()) {
ans.append(q.poll()).append(' ');
}
out.println(ans);
}
}
static class FastScanner implements Closeable {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public FastScanner(InputStream in) {
this.in = in;
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public int[] nextIntArray(final int n) {
final int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public void close() {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int A[100005], N;
long long ch(int init) {
int t = A[0];
long long res = init;
for (int i = 1; i < N; i++) {
if (t + A[i] == 0 || t * (t + A[i]) > 0) {
if (t > 0) {
res += abs(t + 1 + A[i]);
t = -1;
} else {
res += abs(t) + 1 - A[i];
t = 1;
}
} else {
t += A[i];
}
}
return res;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", &A[i]);
long long t = ch(0), a, p;
a = -A[1];
a > 0 ? a++ : a--;
p = abs(a - A[0]);
t = min(t, ch(p));
printf("%lld\n", t);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
long long int A, all;
unsigned long long score = 0;
int buff;
bool flag = true;
cin >> N;
cin >> A;
all = A;
for (int i = 0; i < N - 1; i++) {
cin >> A;
if (flag == true) {
if (all + A == 0) {
flag = false;
score += 1;
A = 0 - all;
} else if (all * (all + A) > 0) {
long long int buff;
if (all > 0) {
buff = 0 - 1 - all;
} else {
buff = 1 - all;
}
score += abs(A - buff);
A = buff;
}
} else {
flag = true;
if (A > 0) {
all = 0 - 1;
} else {
all = 1;
}
if (all + A == 0) {
flag = false;
score += 1;
A = 0 - all;
}
}
all += A;
}
cout << score << 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 MAX = 1e5 + 111;
int n;
int a[MAX];
long long calc(int id, long long sum) {
long long ans = 0;
for (int i = id; i < n; ++i) {
long long cur = sum + a[i];
if (sum < 0 && cur > 0) {
sum = cur;
continue;
}
if (sum > 0 && cur < 0) {
sum = cur;
continue;
}
if (cur == 0) {
if (sum < 0)
cur = 1;
else
cur = -1;
sum = cur;
ans++;
continue;
}
if (sum < 0) {
ans += 1 - cur;
cur = 1;
} else {
ans += cur + 1;
cur = -1;
}
sum = cur;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int id = -1;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] != 0 && id == -1) id = i;
}
if (id == -1) {
cout << 1 + (n - 1) * 2;
return 0;
}
if (id != 0) {
long long ans1 = calc(id, -1);
long long ans2 = calc(id, 1);
cout << min(ans1, ans2) + 1 + 2 * (id - 1);
} else {
long long ans1 = calc(1, 1) + abs(a[0] - 1);
long long ans2 = calc(1, -1) + abs(a[0] + 1);
cout << min(ans1, ans2);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
long long int a[n];
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int oddcount = 0, evencount = 0;
long long int oddsum = 0, evensum = 0;
bool oddplus = true, evenplus = false;
for (int i = 0; i < (n); i++) {
oddsum += a[i];
evensum += a[i];
if (oddplus && oddsum <= 0) {
oddcount += 1 - oddsum;
oddsum = 1;
} else if (!oddplus && oddsum >= 0) {
oddcount += 1 + oddsum;
oddsum = -1;
}
if (evenplus && evensum <= 0) {
evencount += 1 - evensum;
evensum = 1;
} else if (!evenplus && evensum >= 0) {
evencount += 1 + evensum;
evensum = -1;
}
oddplus = !oddplus;
evenplus = !evenplus;
cout << a[i] << " :"
<< "a[i]" << endl;
cout << evensum << " :"
<< "evensum" << endl;
cout << evencount << " :"
<< "evencount" << endl;
}
cout << fmin(oddcount, evencount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, a[100001], ans = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; a[i] == 0; ++i) {
ans = 2 * (i + 1) - 1;
x = i + 1;
}
int sum1 = a[x], sum2 = a[x];
for (int i = x + 1; i < n; i++) {
sum2 += a[i];
if (sum2 >= 0 && sum1 > 0) {
ans += abs(sum2) + 1;
a[i] = a[i] - abs(sum2) - 1;
sum2 = sum2 - abs(sum2) - 1;
}
if (sum2 <= 0 && sum1 < 0) {
ans += abs(sum2) + 1;
a[i] = a[i] + abs(sum2) + 1;
sum2 = sum2 + abs(sum2) + 1;
}
sum1 = sum2;
}
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<long long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<long long int> B(N);
B[0] = A[0];
for (int i = 1; i < N; i++) B[i] = B[i - 1] + A[i];
long long int ans = 0;
long long int base = 0;
for (int i = 1; i < N; i++) {
if ((B[i] + base) * (B[i - 1] + base) > 0) {
if (B[i] + base > 0) {
ans += abs(B[i] + base) + 1;
base -= abs(B[i] + base) + 1;
continue;
} else if (B[i] + base < 0) {
ans += abs(B[i] + base) + 1;
base += abs(B[i] + base) + 1;
continue;
}
}
if (i < N - 1) {
if (B[i] + base == 0) {
if (B[i + 1] + base > 0) {
ans += 1;
base -= 1;
continue;
} else if (B[i + 1] + base < 0) {
ans += 1;
base += 1;
continue;
}
}
} else {
if (B[i] + base == 0) ans++;
}
if (i == 1 && B[i - 1] + base == 0) {
if (B[i] + base > 0) {
ans++;
base -= 1;
} else {
ans++;
base += 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 a[100010];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int cnt;
cin >> cnt;
for (int i = 0; i < cnt; ++i) {
cin >> a[i];
}
int sum = 0;
int ans1 = 0;
for (int i = 0; i < cnt; ++i) {
if (i % 2 == 0 && sum + a[i] > 0) {
sum += a[i];
} else if (i % 2 == 0 && sum + a[i] <= 0) {
ans1 += (1 - sum - a[i]);
sum += (1 - sum);
} else if (i % 2 == 1 && sum + a[i] >= 0) {
ans1 += (a[i] - (-1 - sum));
sum += (-1 - sum);
} else {
sum += a[i];
}
}
int ans2 = 0;
sum = 0;
for (int i = 0; i < cnt; ++i) {
if (i % 2 == 1 && sum + a[i] > 0) {
sum += a[i];
} else if (i % 2 == 1 && sum + a[i] <= 0) {
ans2 += (1 - sum - a[i]);
sum += (1 - sum);
} else if (i % 2 == 0 && sum + a[i] >= 0) {
ans2 += (a[i] - (-1 - sum));
sum += (-1 - sum);
} else {
sum += a[i];
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)1e18;
int main() {
int n;
cin >> n;
long long int ans1 = 0, ans2 = 0;
;
vector<int> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
long long int t = a[0];
for (int i = (1); i < (n); ++i) {
if ((t > 0 && t + a[i] >= 0) || (t < 0 && t + a[i] <= 0)) {
ans1 += abs(t + a[i]) + 1;
if (t > 0) {
t = -1;
} else {
t = 1;
}
} else {
t += a[i];
}
}
t = a[0] > 0 ? -1 : 1;
ans2 += abs(a[0]) + 1;
for (int i = (1); i < (n); ++i) {
if ((t > 0 && t + a[i] >= 0) || (t < 0 && t + a[i] <= 0)) {
ans2 += abs(t + a[i]) + 1;
if (t > 0) {
t = -1;
} else {
t = 1;
}
} else {
t += a[i];
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n), pref(n, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
pref[i] = pref[i - (i > 0)] + v[i];
}
int x;
int ans = 0;
for (int i = 1; i < n; i++) {
if (pref[i] > 0 && pref[i - 1] > 0) {
x = abs(v[i]) + 1;
ans += x;
for (int j = i; j < n; j++) {
pref[j] -= x;
}
} else if (pref[i] < 0 && pref[i - 1] < 0) {
x = abs(v[i]) + 1;
ans += x;
for (int j = i; j < n; j++) {
pref[j] += x;
}
} else if (pref[i] == 0) {
if (pref[i - 1] > 0) {
ans++;
for (int j = i; j < n; j++) {
pref[j]--;
}
} else {
ans++;
for (int j = i; j < n; j++) {
pref[j]++;
}
}
}
}
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef long l;
typedef pair<int,int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
const int INF=1000000001;
const double PI=3.141592653589;
const ll LMAX=1000000000000001;
ll gcd(ll a,ll b){if(a<b)swap(a,b);while((a%b)!=0){a=b;b=a%b;}return b;}
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int main(){
int n; cin>>n;
vector<ll> a(n);
rep(i,n) cin>>a[i];
bool f[]={1,0};
ll ans=LMAX;
rep(j,2){
ll sum=0;
ll c=0;
bool F=f[j];
rep(i,n){
if(F){
if(sum+a[i]<0) sum+=a[i];
else{
c+=abs(sum+a[i])+1;
sum=-1;
}
}else{
if(sum+a[i]>0) sum+=a[i];
else{
c+=abs(sum+a[i])+1;
sum=1;
}
}
}
ans=min(ans,c);
}
cout<<c<<endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t n;
cin >> n;
vector<int64_t> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int64_t sum1 = 0LL, cost1 = 0LL;
for (int i = 0; i < n; i++) {
sum1 += a[i];
int64_t diff = abs(sum1) + 1LL;
if (i % 2 == 0 && sum1 < 0LL) {
sum1 += diff;
cost1 += diff;
}
if (i % 2 == 1 && sum1 > 0LL) {
sum1 -= diff;
cost1 += diff;
}
if (sum1 == 0LL) {
if (i % 2 == 0) {
sum1--;
cost1++;
}
if (i % 2 == 1) {
sum1++;
cost1++;
}
}
}
int64_t sum2 = 0LL, cost2 = 0LL;
for (int i = 0; i < n; i++) {
sum2 += a[i];
int64_t diff = abs(sum2) + 1LL;
if (i % 2 == 0 && sum2 > 0LL) {
sum2 -= diff;
cost2 += diff;
}
if (i % 2 == 1 && sum2 < 0LL) {
sum2 += diff;
cost2 += diff;
}
if (sum2 == 0LL) {
if (i % 2 == 0) {
sum2++;
cost2++;
}
if (i % 2 == 1) {
sum2--;
cost2++;
}
}
}
cout << min(cost1, cost2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int,input().split()))
ans = 0
s = A[0]
if s > 0:
flag = 1
else:
flag = -1
for i in range(1,N):
s += A[i]
if flag == 1 and s >= 0:
ans += s + 1
s = -1
elif flag == -1 and s <= 0:
ans += 1 - s
s = 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 | python3 | #!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
cnt = 0
current_sum = a[0]#現在の操作後の値
# 正負どちらを起点にするかで2通り考える必要がある。
# a[0]に従うパターン
for i in range(n-1):
if current_sum * (current_sum + a[i+1]) < 0:#符号が違う
current_sum += a[i+1]
cnt += 0#そのまま
elif current_sum < 0:
cnt += 1 - (current_sum + a[i+1])
current_sum = 1
else:
cnt += (current_sum + a[i+1]) +1
current_sum = -1
case1 = cnt
cnt = abs(a[0])+1
current_sum = -1 * a[0] // abs(a[0])#現在の操作後の値
for i in range(n-1):
if current_sum * (current_sum + a[i+1]) < 0:#符号が違う
current_sum += a[i+1]
cnt += 0#そのまま
elif current_sum < 0:
cnt += 1 - (current_sum + a[i+1])
current_sum = 1
else:
cnt += (current_sum + a[i+1]) +1
current_sum = -1
#print(cnt,case1)
print(min(cnt,case1)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n, *A = map(int, open(0).read().split())
B = [A[0]]
C = [A[0]]
D = [0]
for i in range(1, n):
if (C[i-1] + A[i]) * C[i-1] < 0:
B.append(A[i])
C.append(C[i-1] + A[i])
D.append(D[i-1])
else:
if C[i-1] > 0:
B.append(-(C[i-1]+1))
C.append(-1)
D.append(D[i-1] + C[i-1]+1 + A[i])
else:
B.append(-C[i-1]+1)
C.append(1)
D.append(D[i-1] + -C[i-1]+1 - A[i])
print(D[-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;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
long long a[N];
long long sum = 0, cnt = 0;
for (long long i = 0; i < N; i++) cin >> a[i];
for (long long i = 0; i < N; i++) {
if (sum > 0 && sum + a[i] > 0) {
if (a[i] < 0)
cnt += sum + a[i] + 1;
else
cnt += abs(sum + a[i]) + 1;
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
if (a[i] > 0)
cnt += abs(sum) - a[i] + 1;
else
cnt += abs(sum + a[i]) + 1;
sum = 1;
} else if (sum + a[i] == 0) {
if (a[i] >= 0) {
sum++;
cnt++;
} else {
sum--;
cnt++;
}
} else
sum += a[i];
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n,a=int(input()),list(map(int,input().split()));ans=0
if a==[0]*n:print(n*2-1);exit()
ma=[0,0]
for i in range(1,n):
if ma[0]<abs(a[i]):ma[0]=abs(a[i]);ma[1]=i
if a[ma[1]]>0:m=(1if ma[1]%2==0else-1)
else:m=(1if ma[1]%2!=0else-1)
if a[0]*m<=0:ans+=abs(m-a[0])
m*=-1
b=[a[0]];m=(1if a[0]<0else-1)
for i in range(1,n):
b.append(b[i-1]+a[i])
if b[i-1]*b[i]>=0:
ans+=abs(m-b[i])
b[i]=m
m*=-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;
constexpr long long MOD = 1e9 + 7;
long long dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
long long ans = 0;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec) is >> x;
return is;
}
signed main() {
cin >> N;
vector<long long> a(N);
cin >> a;
for (long long i = 0; i < (long long)(N - 1); i++) {
a[i + 1] = a[i] + a[i + 1];
}
if (a[0] < 0) {
for (long long i = 0; i < (long long)(N); i++) a[i] *= -1;
}
long long base = 0;
for (long long i = 0; i < (long long)(N); i++) {
if (i == 0) continue;
if (i & 1) {
long long tmp = (a[i] + base) - (-1);
if (tmp > 0) {
ans += tmp;
base -= tmp;
}
} else {
long long tmp = 1 - (a[i] + base);
if (tmp > 0) {
ans += tmp;
base += tmp;
}
}
}
long long tmp = ans;
ans = 0;
for (long long i = 0; i < (long long)(N); i++) a[i] *= -1;
base = 1 - a[0];
for (long long i = 0; i < (long long)(N); i++) {
if (i == 0) continue;
if (i & 1) {
long long tmp = -1 - (a[i] + base);
if (tmp > 0) {
ans += tmp;
base -= tmp;
}
} else {
long long tmp = 1 - (a[i] + base);
if (tmp > 0) {
ans += tmp;
base += tmp;
}
}
}
ans = max((long long)tmp, ans);
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import java.util.*
fun main(args: Array<String>) {
val sc = Scanner(System.`in`)
val n = sc.nextInt()
val a = (0 until n).map { sc.next().toLong() }
println(problem059c(n, a))
}
fun problem059c(n: Int, a: List<Long>): Long {
val count1 = compute(n, a)
val a = a.toMutableList()
val a0 = a[0]
var count = 0L
if (a0 > 0) {
val tmp = a0 + 1
a[0] = a0 - tmp
count += tmp
} else {
val tmp = a0 - 1
a[0] = a0 - tmp
count -= tmp
}
val count2 = compute(n, a) + count
return Math.min(count1, count2)
}
fun compute(n: Int, a: List<Long>): Long {
val a = a.toMutableList()
var count = 0L
var sum = 0L
for (i in 0 until n) {
if (sum + a[i] == 0L) {
if (a[i] < 0) {
a[i] = a[i] - 1
} else {
a[i] = a[i] + 1
}
}
sum += a[i]
if (i >= n - 1) {
continue
}
val sum2 = sum + a[i + 1]
if (sum * sum2 < 0) {
continue
} else {
if (sum > 0) {
val tmp = sum2 + 1
a[i + 1] = sum2 - tmp
count += tmp
} else {
val tmp = sum2 - 1
a[i + 1] = sum2 - tmp
count -= tmp
}
}
}
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 n, no, ans;
int main() {
cin >> n;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
if (i == 0) {
no = a;
} else {
if (no > 0) {
no += a;
if (no >= 0) {
ans += no + 1;
no = -1;
}
} else {
no += a;
if (no <= 0) {
ans += no * -1 + 1;
no = 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;
const long long MOD = 1e9 + 7;
template <typename T>
void putv(vector<T>& V) {
for (auto x : V) cout << x << " ";
cout << endl;
}
template <class T>
vector<T> getv(long long n) {
vector<T> Vector_temp;
for (int(i) = 0; (i) < (n); (i)++) {
T input;
cin >> input;
Vector_temp.emplace_back(input);
}
return Vector_temp;
}
long long gcd(long long c, long long b) {
while (1) {
if (c % b != 0) {
long long tmp = b;
b = c % b;
c = tmp;
} else {
return b;
}
}
}
int main() {
long long n;
cin >> n;
vector<long long> a((n), 0);
;
a = getv<long long>(n);
long long sum = 0;
sum = a[0];
int b = -1;
if (sum > 0) {
b = 1;
}
long long ans = 0;
for (int(i) = 1; (i) < n; (i)++) {
sum += a[i];
if (b > 0) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
b = -1;
} else {
if (sum <= 0) {
ans += ((-sum) + 1);
sum = 1;
}
b = 1;
}
}
cout << (ans) << endl;
;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def f(a)
g = lambda{|car, cdr, total=0|
sum = car
r = [car]
cdr.each{|curr|
case sum <=> 0
when 1;
r << new_curr = [curr, -sum-1].min
sum += new_curr
total += curr - new_curr
when -1;
r << new_curr = [curr, -sum+1].max
sum += new_curr
total += new_curr - curr
end
}
# p r
total += 1 if sum == 0
total
}
x = g.(a[0], a[1..-1])
y = g.(a[0] > 0 ? -1 : 1, a[1..-1], a[0].abs+1)
[x, y].min
end
N = gets.to_i
A = gets.split.take(N).map(&:to_i)
p f(A)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
def sol(S):
ret = 0
for a in A[1:]:
b = a
if S * (S + b) > 0:
b = (abs(S) + 1) * (1 if S < 0 else -1)
if S + b == 0:
b = b - 1 if b < 0 else b + 1
ret += abs(b - a)
S += b
return ret
ans = min(
sol(A[0]),
sol(-1 if A[0] >= 0 else 0) + abs(A[0]) + (1 if A[0] >= 0 else 0)
)
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;
vector<long long> a(n + 1, 0);
for (long long &x : a) cin >> x;
long long ans = 0;
if (a[0] == 0) {
int cnt = 0, memo;
for (int i = 0; i < n && a[i] == 0; i++) {
cnt++;
int m = 1 + i;
memo = m;
}
if (memo == n) {
a[0] = 1;
ans++;
}
if (a[memo] > 0 && cnt % 2 != 0) {
a[0] = -1;
ans++;
}
if (a[memo] > 0 && cnt % 2 == 0) {
a[0] = 1;
ans++;
}
if (a[memo] < 0 && cnt % 2 != 0) {
a[0] = 1;
ans++;
}
if (a[memo] < 0 && cnt % 2 == 0) {
a[0] = -1;
ans++;
}
}
long long sum = a[0];
if (a[0] > 0) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
ans++;
}
}
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
ans++;
}
}
}
}
if (a[0] < 0) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum <= 0) {
sum++;
ans++;
}
}
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
ans++;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Comparator;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
public class Main {
// 標準入力
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 標準入力数値配列用 int
static int[] inputval() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
int[] intarray = new int[strarray.length];
for (int i = 0; i < intarray.length; i++) {
intarray[i] = Integer.parseInt(strarray[i]);
}
return intarray;
}
/* 標準入力数値配列用 long */
static long[] inputLongArr() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
long[] longarray = new long[strarray.length];
for (int i = 0; i < longarray.length; i++) {
longarray[i] = Long.parseLong(strarray[i]);
}
return longarray;
}
// 標準入力数値リスト用 int
static List<Integer> inputIntList() throws Exception {
List<String> strList = Arrays.asList(br.readLine().trim().split(" "));
List<Integer> intList = new ArrayList<Integer>();
for (String elem : strList){
intList.add(Integer.parseInt(elem));
}
return intList;
}
// 標準入力数値配列用 integer 降順ソート用
static Integer[] inputvalInteger() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
Integer[] intarray = new Integer[strarray.length];
for (int i = 0; i < intarray.length; i++) {
intarray[i] = Integer.parseInt(strarray[i]);
}
return intarray;
}
/*標準入力long*/
static long inputLong() throws Exception {
return Long.parseLong(br.readLine());
}
/*標準入力long*/
static int inputInt() throws Exception {
return Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws Exception {
// write your code here
int n = inputInt();
long [] al = inputLongArr();
long sum = al[0];
long sum2 = al[0];
long ans = 0;
long ans2 = 0;
boolean nextPlusF = al[0] < 0;
for(int i=1;i<n;i++){
sum += al[i];
if(nextPlusF && sum <=0){
ans += 1-sum;
sum += ans;
}else if ((! nextPlusF) && sum >= 0){
ans += sum +1;
sum -= ans;
}
nextPlusF = !nextPlusF;
}
nextPlusF = !(al[0] < 0);
for(int i=1;i<n;i++){
sum2 += al[i];
if(nextPlusF && sum2 <=0){
ans2 += 1-sum2;
sum2 += ans2;
}else if ((! nextPlusF) && sum2 >= 0){
ans2 += sum2 +1;
sum2 -= ans2;
}
nextPlusF = !nextPlusF;
}
System.out.println(Math.min(ans,ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long int> list(n);
for (int i = 0; i < n; i++) {
cin >> list.at(i);
}
long long int count = 0, sum = 0;
bool flag = true;
for (int i = 0; i < n; i++) {
if (flag) {
sum = list.at(i);
if (sum == 0) {
if (i == 0) {
count += 1;
} else {
count += 2;
}
} else {
flag = false;
sum = sum / abs(sum) * (abs(sum) - 1);
}
} else {
long long int temp_sum = sum;
sum += list.at(i);
if (sum * temp_sum >= 0) {
count += abs(sum) + 1;
if (temp_sum > 0) {
sum = -1;
} else {
sum = 1;
}
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100010], sum[100010], ans, s;
long long delta;
int main() {
scanf("%d", &n);
sum[0] = 0LL;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
ans = 0LL;
delta = 0LL;
for (int i = 2; i <= n; i++) {
sum[i] += delta;
if (sum[i - 1] < 0) {
if (sum[i] <= 0) {
ans += 1 - sum[i];
delta += 1 - sum[i];
sum[i] = 1;
}
} else if (sum[i] >= 0) {
ans += sum[i] + 1;
delta -= sum[i] + 1;
sum[i] = -1;
}
}
s = 0LL;
delta = 0LL;
if (sum[1] < 0) {
s += 1 - sum[1];
delta += 1 - sum[1];
sum[1] = 1;
} else {
s += sum[1] + 1;
delta -= sum[1] + 1;
sum[1] = -1;
}
for (int i = 2; i <= n; i++) {
sum[i] += delta;
if (sum[i - 1] < 0) {
if (sum[i] <= 0) {
s += 1 - sum[i];
delta += 1 - sum[i];
sum[i] = 1;
}
} else if (sum[i] >= 0) {
s += sum[i] + 1;
delta -= sum[i] + 1;
sum[i] = -1;
}
}
printf("%lld\n", min(s, ans));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void dump_func() {}
template <class Head, class... Tail>
void dump_func(Head&& head, Tail&&... tail) {
cerr << head;
if (sizeof...(Tail) == 0) {
cerr << " ";
} else {
cerr << ", ";
}
dump_func(std::move(tail)...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[n];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
int res = -1;
int s = 0;
int flag = 1;
int c = 0;
for (int i = 0; i < (int)(n); i++) {
s += a[i];
if (flag * s > 0) {
} else {
c += abs(s) + 1;
s = flag;
}
flag *= -1;
}
res = c;
s = 0;
flag = -1;
c = 0;
for (int i = 0; i < (int)(n); i++) {
s += a[i];
if (flag * s > 0) {
} else {
c += abs(s) + 1;
s = flag;
}
flag *= -1;
}
res = min(c, res);
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
long ans = 0;
long now = sc.nextInt();
for(int i = 1; i < N; i++){
int A = sc.nextInt();
if(now > 0){
if(now + A >= 0){
ans += now+A+1;
now = -1;
}else{
now = now+A;
}
}else if(now < 0){
if(now + A <= 0){
ans += Math.abs(now+A)+1;
now = 1;
}else{
now = now+A;
}
}else{
if(A < 0){
ans++;
now++;
}else if(A > 0){
ans++;
now--;
}else{
ans++;
}
}
}
System.out.println(ans);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long res1 = 0, res2 = 0;
long long sum1[n], sum2[n];
if (a[0] < 0) {
sum1[0] = -a[0] + 1;
sum2[0] = a[0];
res1 = res1 + abs(-a[0] + 1);
} else if (a[0] > 0) {
sum1[0] = a[0];
sum2[0] = -a[0] - 1;
res2 = res2 + abs(-a[0] - 1);
} else if (a[0] == 0) {
sum1[0] = 1;
res1 = res1 + 1;
sum2[0] = -1;
res2 = res2 + 1;
}
for (int i = 1; i < n; i++) {
sum1[i] = sum1[i - 1] + a[i];
long long sum = sum1[i];
if (sum1[i] < 0 && sum1[i - 1] < 0) {
sum1[i] += -sum + 1;
res1 += abs(-sum + 1);
} else if (sum1[i] > 0 && sum1[i - 1] > 0) {
sum1[i] += -sum - 1;
res1 += abs(-sum - 1);
}
if (sum1[i] == 0) {
sum1[i] = (i % 2 == 0) ? sum1[i] + 1 : sum1[i] - 1;
res1 += 1;
}
sum2[i] = sum2[i - 1] + a[i];
sum = sum2[i];
if (sum2[i] <= 0 && sum2[i - 1] < 0) {
sum2[i] += -sum + 1;
res2 += abs(-sum + 1);
} else if (sum2[i] >= 0 && sum2[i - 1] > 0) {
sum2[i] += -sum - 1;
res2 += abs(-sum - 1);
}
if (sum2[i] == 0) {
sum2[i] = (i % 2 == 0) ? sum2[i] - 1 : sum2[i] + 1;
res2 += 1;
}
}
cout << min(res1, res2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
ll min(ll a, ll b) {
if (a >= b)
return b;
else
return a;
}
ll max(ll a, ll b) {
if (a >= b)
return a;
else
return b;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool flag = true;
bool fl = false;
bool f = false;
bool used[210];
bool graph[100][100];
bool visited[8];
int abc[26] = {0};
int main() {
ll n, a[100000], b[100000], ansA = 0, ansB = 0;
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i <= n - 1;) {
if (a[i] <= 0) ansA += abs(1 - a[i]), a[i] = 1;
i++;
if (i == n) break;
a[i] += a[i - 1];
if (a[i] >= 0) ansA += abs(a[i] + 1), a[i] = -1;
i++;
a[i] += a[i - 1];
}
for (int i = 0; i <= n - 1;) {
if (b[i] >= 0) ansB += abs(b[i] + 1), b[i] = -1;
i++;
if (i == n) break;
b[i] += b[i - 1];
if (b[i] <= 0) ansB += abs(1 - b[i]), b[i] = 1;
i++;
b[i] += b[i - 1];
}
std::cout << min(ansA, ansB) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int N;
cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long long sum = 0;
long long cnt = 0;
for (int i = 0; i < N; i++) {
if (sum + a[i] == 0) {
if (sum < 0) {
cnt++;
a[i]++;
} else if (sum > 0) {
cnt++;
a[i]--;
} else {
if (a[i + 1] > 0)
a[i]++;
else if (a[i + 1] < 0)
a[i]--;
else
a[i]++;
cnt++;
}
}
if (sum < 0 && sum + a[i] < 0) {
int diff = abs(sum + a[i]) + 1;
cnt += diff;
a[i] += diff;
} else if (sum > 0 && sum + a[i] > 0) {
int diff = abs(sum + a[i]) + 1;
cnt += diff;
a[i] -= diff;
}
sum += a[i];
}
for (int i = 0; i < N; i++) {
cerr << a[i] << " ";
}
cerr << endl;
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); ++i) {
cin >> a.at(i);
}
int c = 0;
int ans = 0;
bool ch = true;
int co = 0;
if (a.at(0) != 0) {
for (int i = 0; i < (int)(n); ++i) {
if (ch) {
if (co + a.at(i) >= 0) {
c += co + a.at(i) + 1;
co = -1;
} else {
co += a.at(i);
}
ch = false;
} else {
if (co + a.at(i) <= 0) {
c += 1 - co - a.at(i);
co = 1;
} else {
co += a.at(i);
}
ch = true;
}
}
ch = false;
co = 0;
for (int i = 0; i < (int)(n); ++i) {
if (ch) {
if (co + a.at(i) >= 0) {
ans += co + a.at(i) + 1;
co = -1;
} else {
co += a.at(i);
}
ch = false;
} else {
if (co + a.at(i) <= 0) {
ans += 1 - co - a.at(i);
co = 1;
} else {
co += a.at(i);
}
ch = true;
}
}
ans = min(ans, c);
cout << ans << endl;
} else {
for (int i = 0; i < (int)(n); ++i) {
if (ch) {
if (co + a.at(i) >= 0) {
c += co + a.at(i) + 1;
co = -1;
} else {
co += a.at(i);
}
ch = false;
} else {
if (co + a.at(i) <= 0) {
c += 1 - co - a.at(i);
co = 1;
} else {
co += a.at(i);
}
ch = true;
}
}
ch = false;
co = 0;
for (int i = 0; i < (int)(n); ++i) {
if (ch) {
if (co + a.at(i) >= 0) {
ans += co + a.at(i) + 1;
co = -1;
} else {
co += a.at(i);
}
ch = false;
} else {
if (co + a.at(i) <= 0) {
ans += 1 - co - a.at(i);
co = 1;
} else {
co += a.at(i);
}
ch = true;
}
}
ans = min(ans, c);
cout << ans << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | program main
implicit none
integer(8) :: i, j, k, nc
integer(8) :: n, sum, sign
integer(8) , allocatable :: a(:)
read(*,*) n
allocate( a(n) )
read(*,*) a
if( a(1) .le. 0 ) then
sign = -1
else
sign = 1
end if
sum = a(1)
nc = 0
do i = 2, n
sign = sign *(-1)
if( (sum+a(i))*sign < 0 ) then
nc = nc+abs(sum+a(i))+1
a(i) = sign*(abs(sum+a(i))+1) +a(i)
end if
sum = sum + a(i)
end do
write(*,'(i0)') nc
end program main
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def solve():
n = int(input())
a = list(map(int, input().split()))
i = 0
sum = 0
ans = 0
for i in range(n-1):
sum += a[i]
if sum > 0 and sum+a[i+1] > 0:
tmp = -1 - sum
ans += abs(tmp - a[i+1])
a[i+1] = tmp
elif sum < 0 and sum+a[i+1] < 0:
tmp = 1 - sum
ans += abs(tmp - a[i+1])
a[i+1] = tmp
if sum > 0 and sum+a[n-1] > 0:
tmp = -1 -sum
ans += abs(tmp - a[n-1])
a[n-1] = tmp
elif sum < 0 and sum+a[n-1] < 0:
tmp = 1 - sum
ans += abs(tmp - a[i-1])
a[n-1] = tmp
print(ans)
# print(a)
if __name__ == "__main__":
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
int n;
cin >> n;
int a[114514];
int sum[114514];
for (int i = 0; i < n; i++) cin >> a[i];
sum[0] = a[0];
for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + a[i];
int p = 0, ans1 = 0;
for (int i = 0; i < n; i++) {
int k = sum[i] + p;
if (i % 2 == 0) {
if (k < 0) {
ans1 += 1 - k;
p += 1 - k;
} else if (k == 0) {
p++;
ans1 += 1;
}
} else {
if (k > 0) {
ans1 += k + 1;
p -= k + 1;
} else if (k == 0) {
p--;
ans1 += 1;
}
}
}
p = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
int k = sum[i] + p;
if (i % 2 == 0) {
if (k > 0) {
ans2 += k + 1;
p -= k + 1;
} else if (k == 0) {
p--;
ans2 += 1;
}
} else {
if (k < 0) {
ans2 += 1 - k;
p += 1 - k;
} else if (k == 0) {
p++;
ans2 += 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 | import Control.Monad
import Data.List
main=do
_<-getLine
(a:as)<-map read.words<$>getLine::IO[Integer]
print.sum.snd$ mapAccumL f a as
f a b
| a*(a+b) < 0 = (a+b,0)
| a < 0 = (1, 1-(a+b))
| otherwise = ((-1), 1+(a+b))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans1 = 0
ans2 = 0
cur = a[0]
for i in range(1, n):
cun = a[i]
# iが偶数のときプラス
if i % 2 == 0:
# プラスじゃないとき
if a[i] + cur <= 0:
ans1 += -(a[i] + cur) + 1
cun = -cur + 1
# iが奇数のときマイナス
else:
# マイナスじゃないとき
if a[i] + cur >= 0:
ans1 += a[i] + cur + 1
cun = -cur - 1
cur += cun
for i in range(1, n):
cun = a[i]
# iが偶数のときマイナス
if i % 2 == 0:
# マイナスじゃないとき
if a[i] + cur >= 0:
ans2 += a[i] + cur + 1
cun = -cur - 1
# iが奇数のときプラス
else:
# プラスじゃないとき
if a[i] + cur <= 0:
ans2 += -(a[i] + cur) + 1
cun = -cur + 1
cur += cun
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long array[n];
for (int i = 0; i < n; i++) {
cin >> array[i];
}
long long answer = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (sum == 0)
sum += array[i];
else if (sum < 0) {
if (sum + array[i] > 0) {
sum += array[i];
} else {
answer += abs((-1) * sum + 1 - array[i]);
sum = 1;
}
} else {
if (sum + array[i] < 0) {
sum += array[i];
} 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>
using namespace std;
int n, a[100000], sum[2][100000], c[2];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] == 0) {
sum[0][0] = 1;
sum[1][0] = -1;
c[0] = c[1] = 1;
} else {
sum[0][0] = sum[1][0] = a[0];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < 2; j++) {
if (sum[j][i - 1] > 0) {
if (sum[j][i - 1] + a[i] >= 0) {
c[j] += abs(sum[j][i - 1] + a[i]) + 1;
sum[j][i] = -1;
} else
sum[j][i] = sum[j][i - 1] + a[i];
} else {
if (sum[j][i - 1] + a[i] > 0)
sum[j][i] = sum[j][i - 1] + a[i];
else {
c[j] += abs(sum[j][i - 1] + a[i]) + 1;
sum[j][i] = 1;
}
}
}
}
cout << min(c[0], c[1]) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
int b[100001];
for (int i = 0; i < (n); i++) {
int l;
cin >> l;
b[i] = l;
}
for (int i = 0; i < (n); i++) b[i + 1] += b[i];
long long int sm = 0;
for (int i = 1; i < n; i++) {
if (b[i - 1] * b[i] < 0) continue;
int target = (b[i - 1] > 0) ? -1 : 1;
sm += abs(b[i] - target);
int dif = -b[i] + target;
for (int j = i; j < n; j++) {
b[j] += dif;
}
}
cout << (sm) << "\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 check(long long int a) {
if (a > 0) return 1;
if (a < 0) return 2;
return 0;
}
int main() {
long long int sum, n, i, cnt = 0;
cin >> n;
long long int x, a[n], h[n];
for (i = 0; i < n; i++) cin >> a[i];
sum = a[0];
if (sum == 0) cnt++, sum++;
h[0] = sum;
for (i = 1; i < n; i++) {
if (sum == 0) {
cnt++;
if (h[i - 1] > 0)
h[i] = sum = 1;
else
h[i] = sum = -1;
sum = 1;
continue;
}
if (check(sum + a[i]) == check(sum)) {
if (sum < 0) {
x = 1 - sum;
cnt += (x - a[i]);
sum = 1;
} else {
x = -1 - sum;
cnt += (a[i] - x);
sum = -1;
}
} else
sum += a[i];
h[i] = sum;
}
cout << cnt;
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 int> A(N);
vector<long long int> VL(N), VR(N);
long long int t = 0LL;
for (int i = 0; i < N; i++) {
cin >> A[i];
t += A[i];
VL[i] = VR[i] = t;
}
long long int l = 0LL, r = 0LL;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (VL[i] > 0) {
continue;
} else {
l += 1 - VL[i];
VL[i] = 1;
if (i < N - 1) {
VL[i + 1] = VL[i] + A[i + 1];
}
}
} else {
if (VL[i] < 0) {
continue;
} else {
l += VL[i] + 1;
VL[i] = -1;
if (i < N - 1) {
VL[i + 1] = VL[i] + A[i + 1];
}
}
}
}
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (VR[i] < 0) {
continue;
} else {
r += VR[i] + 1;
VR[i] = -1;
if (i < N - 1) {
VR[i + 1] = VR[i] + A[i + 1];
}
}
} else {
if (VR[i] > 0) {
continue;
} else {
r += 1 - VR[i];
VR[i] = 1;
if (i < N - 1) {
VR[i + 1] = VR[i] + A[i + 1];
}
}
}
}
cout << min(l, r) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ABC059C
{
class Program
{
// -1 4 3 2 -5 4
// -1 4 -4 2 -5 5 -> 8
static int sequentialize(int[] a)
{
var n = a.Length;
var sum = a[0];
var changeCount = 0;
int v = a[0] > 0 ? 1 : -1;
for (int i = 1; i < n; i++)
{
var temp = sum + a[i];
if (sum > 0 && temp > 0)
{
// tempを-1にしたい
var prev = a[i];
a[i] = -sum - 1;
changeCount += Math.Abs(prev - a[i]);
}
else if (sum < 0 && temp < 0)
{
// tempを+1にしたい
var prev = a[i];
a[i] = -sum + 1;
changeCount += Math.Abs(prev - a[i]);
}
else if (temp == 0)
{
changeCount += 1;
a[i] = v;
}
sum += a[i];
v = -v;
}
return changeCount;
}
static void Solve()
{
var n = Input.NextInt();
var a = Input.NextInt(n).ToArray();
var b = new int[n];
a.CopyTo(b, 0);
var changeCount1 = sequentialize(a);
// 先頭の符号反転
b[0] = b[0] / -b[0];
var changeCount2 = sequentialize(b) + Math.Abs(b[0]) + 1;
Console.WriteLine(Math.Min(changeCount1, changeCount2));
}
#region Competitive Template
public static void Main(string[] args)
{
var needsFlushOutput = true;
if (needsFlushOutput)
{
// 細かく出力しないようにする
var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
Console.SetOut(sw);
}
// 仮想的に標準入力をセットする
// NextLine系を使っていると使えない
//Input.SetText("");
Solve();
Console.Out.Flush();
}
static class Input
{
static char[] separator = { ' ' };
public static bool IsEof { get; set; }
static Queue<string> q { get; set; }
static Input()
{
IsEof = false;
q = new Queue<string>();
}
/// <summary>
/// 入力予約
/// </summary>
/// <param name="items"></param>
public static void SetText(IEnumerable<string> items)
{
foreach (var item in items)
{
SetText(item);
}
}
/// <summary>
/// 入力予約
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static bool SetText(string s)
{
if (s == null) return false;
foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
{
q.Enqueue(elem);
}
return true;
}
/// <summary>
/// 内部queueに入力からの値をsplitして格納する
/// </summary>
/// <returns></returns>
static bool read()
{
var s = Console.ReadLine();
if (s == null) return false;
foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
{
q.Enqueue(elem);
}
if (!q.Any()) return read();
return true;
}
/// <summary>
/// 次のstringを一つ読み込む
/// </summary>
/// <returns></returns>
public static string Next()
{
if (!q.Any())
{
if (!read())
{
IsEof = true;
return "";
}
}
return q.Dequeue();
}
public static int NextInt() => int.Parse(Next());
public static long NextLong() => long.Parse(Next());
public static double NextDouble() => double.Parse(Next());
public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList();
public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList();
public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList();
public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList();
public static List<string> NextLine() => Console.ReadLine().Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList();
public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList();
public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList();
}
#endregion
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int64_t> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
int64_t t = 0LL, res1 = 0LL, res2 = 0LL;
for (int i = 0; i < N; i++) {
t += a.at(i);
if (i % 2 == 0) {
if (t <= 0) {
res1 += (1 - t);
t = 1LL;
}
} else {
if (t >= 0) {
res1 += abs(-1 - t);
t = -1LL;
}
}
}
t = 0LL;
for (int i = 0; i < N; i++) {
t += a.at(i);
if (i % 2 == 1) {
if (t <= 0) {
res2 += (1 - t);
t = 1LL;
}
} else {
if (t >= 0) {
res2 += abs(-1 - t);
t = -1LL;
}
}
}
int res = min(res1, res2);
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;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (i == 0 && a[0] == 0) {
for (long long j = 0; j < n - 1; j++) {
if (a[j + 1]) {
a[0] = a[j + 1] / abs(a[j + 1]);
if ((j + 1) & 1) a[0] *= (-1);
break;
}
}
if (!a[0]) a[0] = 1;
res++;
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
} else if (i == 0) {
l = a[0];
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100100];
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0, sum = a[0];
int sign = (a[0] > 0 ? 1 : -1);
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum == 0) {
sum += -sign;
ans++;
}
if (sign > 0 && sum > 0) {
long long x = sum + 1;
sum -= x;
ans += x;
} else if (sign < 0 && sum < 0) {
long long y = abs(sum) + 1;
sum += y;
ans += y;
}
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 | python2 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def seiyasum(num,alist):
sum1 = 0
for a in range(num):
sum1 = sum1+a
return sum1
n = int(raw_input())
a = map(int,raw_input().split(" "))
k = a[0]
answer = 0
for m in range(n-1):
i = a[m+1]
if(k > 0):
l = -1
else:
l = 1
while((k+i)*k>=0):
i = i + l
answer = answer + 1
k = k+i
print answer |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long counter = 0;
if (a[0] >= 0) {
for (long long i = 1; i < n; i++) {
long long total = 0;
for (int j = 0; j <= i; j++) {
total += a[j];
}
if (i % 2 == 0 && total <= 0) {
counter += abs(total - 1);
a[i] += abs(total - 1);
} else if (i % 2 != 0 && total >= 0) {
counter += abs(total - (-1));
a[i] -= abs(total - (-1));
}
}
} else {
for (long long i = 1; i < n; i++) {
long long total = 0;
for (long long j = 0; j <= i; j++) {
total += a[j];
}
if (i % 2 == 0 && total >= 0) {
counter += abs(total - (-1));
a[i] -= abs(total - (-1));
} else if (i % 2 != 0 && total <= 0) {
counter += abs(total - 1);
a[i] += abs(total - 1);
}
}
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | use std::io::prelude::*;
fn input<T>() -> T
where
T: std::str::FromStr,
{
let stdin = std::io::stdin();
let token: String = stdin
.lock()
.bytes()
.map(|c| c.unwrap() as char)
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect();
token.parse().ok().unwrap()
}
fn main() {
let n: usize = input();
let a: Vec<i64> = (0..n).map(|_| input()).collect();
let sum: Vec<i64> = a
.iter()
.scan(0, |acc, &a| {
*acc += a;
Some(*acc)
})
.collect();
let mut x = 0;
let mut ans = 0;
for i in 1..n {
if (sum[i - 1] + x > 0 && sum[i] + x < 0) || (sum[i - 1] + x < 0 && sum[i] + x > 0) {
continue;
}
if sum[i - 1] + x > 0 {
ans += sum[i] + x + 1;
x -= sum[i] + x + 1;
} else {
ans += -sum[i] - x + 1;
x -= sum[i] + x - 1;
}
}
println!("{}", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Sequence solver = new Sequence();
solver.readInput();
solver.solve();
solver.writeOutput();
}
static class Sequence {
private int n;
private long a[];
private int output;
private Scanner scanner;
public Sequence() {
this.scanner = new Scanner(System.in);
}
public void readInput() {
n = Integer.parseInt(scanner.next());
a = new long[n];
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(scanner.next());
}
}
private int count(boolean sign) {
int count=0;
long sum=0;
for(int i=0; i<n; i++) {
sum += a[i];
if((i%2==0) == sign) {
// a[i]までの合計を正にするとき
if(sum<=0) {
count += Math.abs(sum)+1;
sum = 1;
}
} else if((i%2==0) != sign){
// a[i]までの合計を負にするとき
if(0<=sum) {
count += Math.abs(sum)+1;
sum = -1;
}
}
}
return count;
}
public void solve() {
output = Math.min(count(true), count(false));
}
public void writeOutput() {
System.out.println(output);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int calc(int n, int* a, bool isPositive) {
int nCount = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (isPositive) {
if ((a[i] + sum) < 0) {
sum += a[i];
} else {
nCount += abs(a[i] + sum) + 1;
sum = -1;
}
isPositive = !isPositive;
} else {
if ((a[i] + sum) > 0) {
sum += a[i];
} else {
nCount += abs(a[i] + sum) + 1;
sum = 1;
}
isPositive = !isPositive;
}
}
return nCount;
}
int main() {
int n;
int a[100005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
bool isPositive;
int nCount[2];
isPositive = true;
nCount[0] = calc(n, a, isPositive);
isPositive = false;
nCount[1] = calc(n, a, isPositive);
cout << min(nCount[0], nCount[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Inf = 1e9;
const double EPS = 1e-9;
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int bitCount(long bits) {
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int ans = Inf;
int cnt = 0;
vector<int> a(n), b(n);
for (int i = 0; i < (int)n; ++i) {
cin >> a[i];
b[i] = a[i];
}
int sum = a[0];
for (int i = 1; i < (int)n; ++i) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
int diff = abs(sum) + 1;
cnt += diff;
sum = -1;
} else if (i % 2 == 0 && sum <= 0) {
int diff = abs(sum) + 1;
cnt += diff;
sum = 1;
}
}
ans = min(ans, cnt);
cnt = 0;
sum = b[0];
for (int i = 1; i < (int)n; ++i) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
int diff = abs(sum) + 1;
cnt += diff;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
int diff = abs(sum) + 1;
cnt += diff;
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(lambda x: int(x), input().split()))
sum_last=a[0]
ans=0
for i in a[1:]:
sum_cur=sum_last+i
if sum_cur*sum_last>=0:
ans+=abs(sum_cur)+1
if sum_last>0:
sum_last=-1
else:
sum_last=1
else:
sum_last=sum_cur
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n;
long long solve(vector<long long> a, bool flag) {
long long sum = a[0], ans;
if (flag)
ans = 0;
else {
ans = abs(a[0]) + 1;
if (a[0] < 0) {
sum = 1;
} else {
sum = -1;
}
}
for (int i = 1; i < n; i++) {
long long tmp = sum;
sum += a[i];
if (sum >= 0 && tmp > 0) {
ans += abs(sum) + 1;
sum = -1;
} else if (sum <= 0 && tmp < 0) {
ans += abs(sum) + 1;
sum = 1;
}
}
return ans;
}
int main() {
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
cout << min(solve(a, true), solve(a, false)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long Num[1111111];
long long Sum[1111111];
long long Out;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &Num[i]);
if (Num[1] > 0) {
for (int i = 1; i <= n; i++) {
Sum[i] = Sum[i - 1] + Num[i];
if (i % 2 == 0) {
if (Sum[i] >= 0) {
Out += (Sum[i] + 1);
Sum[i] = -1;
}
} else {
if (Sum[i] <= 0) {
Out += (1 - Sum[i]);
Sum[i] = 1;
}
}
}
} else {
for (int i = 1; i <= n; i++) {
Sum[i] = Sum[i - 1] + Num[i];
if (i % 2 == 1) {
if (Sum[i] >= 0) {
Out += (Sum[i] + 1);
Sum[i] = -1;
}
} else {
if (Sum[i] <= 0) {
Out += (1 - Sum[i]);
Sum[i] = 1;
}
}
}
}
printf("%lld\n", Out);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5 + 5;
int box[MaxN];
int n;
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%d", &box[i]);
long long sum = box[1];
long long ans = 0;
for (int i = 2; i <= n; i++) {
long long temp = sum + box[i];
if (temp * sum >= 0) {
ans += abs(temp) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else
sum = temp;
}
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()))
cnt = 0
sum = A[0]
for i in range(1,n):
if abs(sum)>=abs(A[i]):
cnt += abs(sum+A[i]) + 1
if sum>0:
A[i] -=abs(sum+A[i]) + 1
else:
A[i] +=abs(sum+A[i]) + 1
sum += A[i]
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
def check1(a):
sum = 0
for i in range(len(a)):
if(i % 2 == 0):
sum += a[i]
if(a[i] >= 0 or sum >= 0):
return (i, -1)
else:
sum += a[i]
if(a[i] <= 0 or sum <= 0):
return (i, 1)
return True
def check2(a):
sum = 0
for i in range(len(a)):
if(i % 2 == 0):
sum += a[i]
if(a[i] <= 0 or sum <= 0):
return (i, 1)
else:
sum += a[i]
if(a[i] >= 0 or sum >= 0):
return (i, -1)
return True
n = input()
b = input().split()
a = [int(b[i]) for i in range(len(b))]
a2 = list(a)
ans1 = 0
ans2 = 0
while(True):
c = check1(a)
if(c == True):
break
a[c[0]] += c[1]
ans1 += 1
while(True):
c = check2(a2)
if(check2(a2) == True):
break
a2[c[0]] += c[1]
ans2 += 1
print(ans1) if(ans1<ans2) else print(ans2) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
using namespace std;
int main(){
int n, ans=0, d=0;
cin >> n;
int a[n];
cin >> a[0];
int sum[0]=a[0];
for(int i=1; i<n; i++){
cin >> a[i];
sum[i] = sum[i-1] + a[i];
}
for(int i=1; i<n; i++){
if((sum[i-1]+d)*(sum[i]+d)>=0){
d -= sum[i]+1;
ans += abs(sum[i]+1);
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | gets
seq = gets.split.map(&:to_i)
def foo(seq)
cnt = 0
sum = seq.shift
seq.each{|a|
if sum < 0
if sum + a > 0
sum += a
else
cnt += 1 - (sum + a)
sum = 1
end
else
if sum + a < 0
sum += a
else
cnt += 1 + (sum + a)
sum = -1
end
end
## p [a, sum, cnt]
}
return cnt
end
if seq[0] != 0
p foo(seq)
else
seq.shift
p [foo([1] + seq), foo([-1] + seq)].min
end
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > vii;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
long long sum = a[0], op_cnt = 0;
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] < 0) {
op_cnt += (-1) * (sum + a[i]) + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] > 0) {
op_cnt += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
cout << op_cnt << endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.