s_id stringlengths 10 10 | p_id stringlengths 6 6 | u_id stringlengths 10 10 | date stringlengths 10 10 | language stringclasses 1
value | original_language stringclasses 11
values | filename_ext stringclasses 1
value | status stringclasses 1
value | cpu_time stringlengths 1 5 | memory stringlengths 1 7 | code_size stringlengths 1 6 | code stringlengths 1 539k |
|---|---|---|---|---|---|---|---|---|---|---|---|
s118662985 | p03739 | u191423660 | 1597540355 | Python | Python (3.8.2) | py | Runtime Error | 113 | 20084 | 1236 | def solve():
n = int(input())
a = list(map(int, input().split()))
b = [a[i] for i in range(n)]
# print(a)
# print(b)
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
# print(ans)
# print(a)
tmp_ans_1 = ans
ans = 0
sum = 0
i = 0
if b[0] > 0:
ans += abs(-1 - b[0])
b[0] = -1
elif b[0] < 0:
ans += abs(1 - b[0])
b[0] = 1
for i in range(n-1):
sum += b[i]
if sum > 0 and sum+b[i+1] > 0:
tmp = -1 - sum
ans += abs(tmp - b[i+1])
b[i+1] = tmp
elif sum < 0 and sum+b[i+1] < 0:
tmp = 1 - sum
ans += abs(tmp - b[i+1])
b[i+1] = tmp
# print(ans)
# print(b)
tmp_ans_2 = ans
if tmp_ans_1 <= tmp_ans_2:
ans = tmp_ans_1
elif tmp_ans_2 < tnp_ans_1:
ans = tmp_ans_2
print(ans)
if __name__ == "__main__":
solve()
|
s802299545 | p03739 | u191423660 | 1597540286 | Python | Python (3.8.2) | py | Runtime Error | 117 | 20168 | 1236 | def solve():
n = int(input())
a = list(map(int, input().split()))
b = [a[i] for i in range(n)]
# print(a)
# print(b)
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
# print(ans)
# print(a)
tmp_ans_1 = ans
ans = 0
sum = 0
i = 0
if b[0] > 0:
ans += abs(-1 - b[0])
b[0] = -1
elif b[0] < 0:
ans += abs(1 - b[0])
b[0] = 1
for i in range(n-1):
sum += b[i]
if sum > 0 and sum+b[i+1] > 0:
tmp = -1 - sum
ans += abs(tmp - b[i+1])
b[i+1] = tmp
elif sum < 0 and sum+b[i+1] < 0:
tmp = 1 - sum
ans += abs(tmp - b[i+1])
b[i+1] = tmp
# print(ans)
# print(b)
tmp_ans_2 = ans
if tmp_ans_1 <= tmp_ans_2:
ans = tmp_ans_1
elif tmp_ans_2 < tnp_ans_1:
ans = tmp_ans_2
print(ans)
if __name__ == "__main__":
solve()
|
s388681912 | p03739 | u281152316 | 1595195744 | Python | Python (3.8.2) | py | Runtime Error | 97 | 19928 | 450 | n = int(input())
A = list(map(int,input().split()))
ans = 0
x = 0
S = []
S.append(A[0])
for i in range(1,n):
x = A[i] + S[i - 1]
if S[i - 1] > 0:
if x >= 0:
x = - x - 1
ans += abs(x)
S.append(-1)
else:
S.append(x)
elif S[i - 1] < 0:
if x <= 0:
x = -x + 1
ans += abs(x)
S.append(1)
else:
S.append(x)
print(ans)
|
s548451778 | p03739 | u281152316 | 1595195673 | Python | Python (3.8.2) | py | Runtime Error | 104 | 19936 | 450 | n = int(input())
A = list(map(int,input().split()))
ans = 0
x = 0
S = []
S.append(A[0])
for i in range(1,n):
x = A[i] + S[i - 1]
if S[i - 1] > 0:
if x >= 0:
x = - x - 1
ans += abs(x)
S.append(-1)
else:
S.append(x)
elif S[i - 1] < 0:
if x <= 0:
x = -x + 1
ans += abs(x)
S.append(1)
else:
S.append(x)
print(ans)
|
s541648051 | p03739 | u961288441 | 1593086568 | Python | Python (3.8.2) | py | Runtime Error | 93 | 19932 | 359 | n = int(input())
a = list(map(int, input().split()))
point = a[0]
ans = 0
for i in a[1:]:
if point*i < 0:
if abs(point) < abs(i):
point += i
else:
ans += (abs(point)+1) - abs(i)
point = -int(point/abs(point))
else:
ans += (abs(point)+1) + i
point = -int(point/abs(point))
print(ans) |
s314061533 | p03739 | u984276646 | 1592893654 | Python | Python (3.8.2) | py | Runtime Error | 52 | 19936 | 99 | n = int(input())
a = list(map(int, input().split()))
if a[0] != 0:
print(0)
else:
print(a[n+4]) |
s527569907 | p03739 | u781262926 | 1592420427 | Python | Python (3.4.3) | py | Runtime Error | 182 | 21652 | 435 | 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(C[i-1]+1 + A[i])
elif C[i-1] < 0:
B.append(-C[i-1]+1)
C.append(1)
D.append(-C[i-1]+1 + A[i])
print(A)
print(B)
print(C)
print(D) |
s593524965 | p03739 | u679520304 | 1591678194 | Python | PyPy3 (2.4.0) | py | Runtime Error | 170 | 38256 | 1106 | n = int(input())
a = list(map(int,input().split()))
num = a[0]
ans = 0
if a[0]>0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ans += num+1
num = -1
else:
if num<=0:
ans += 1-num
num = 1
elif a[0]<0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ans += 1-num
num = 1
else:
if num >= 0:
ans += num+1
num = -1
elif a[0]= 0:
ansp = 1
ansm = 1
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ansp += num+1
num = -1
else:
if num<=0:
ansp += 1-num
num = 1
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ansm += 1-num
num = 1
else:
if num >= 0:
ansm += num+1
num = -1
ans = min(ansp,ansm)
print(ans) |
s223925107 | p03739 | u679520304 | 1591144306 | Python | PyPy3 (2.4.0) | py | Runtime Error | 236 | 63216 | 1110 | n = int(input())
a = list(map(int,input().split()))
num = a[0]
ans = 0
if a[0]>0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ans += num+1
num = -1
else:
if num<=0:
ans += 1-num
num = 1
elif a[0]<0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ans += 1-num
num = 1
else:
if num >= 0:
ans += num+1
num = -1
else:
num = 0
ansp = 1
ansn = 1
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ansp += num+1
num = -1
else:
if num<=0:
ansp += 1-num
num = 1
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ansm += 1-num
num = 1
else:
if num >= 0:
ansm += num+1
num = -1
ans = min(ansp,ansm)
print(ans) |
s819339000 | p03739 | u472883337 | 1591062277 | Python | Python (3.4.3) | py | Runtime Error | 161 | 14332 | 927 | n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = [int(-a[0]/ abs(a[0]))]
for i in range(1, l):
b.append(a[i])
ans = 0
summary = a[0]
if(summary == 0):
if(a[1] > 0):
summary = -1
ans+= 1
else:
summary = 1
ans+= 1
for i in range(1, l):
if(summary* (summary+ a[i])>= 0):
if(summary > 0):
ans+= a[i]+ summary+ 1
a[i] = -summary- 1
summary= -1
else:
ans+= -summary+ 1- a[i]
a[i] = -summary+ 1
summary= 1
else:
summary+= a[i]
Ans = 0
Summary = b[0]
if(Summary == 0):
if(b[1] > 0):
Summary = -1
Ans+= 1
else:
Summary = 1
Ans+= 1
for i in range(1, l):
if(Summary* (Summary+ b[i])>= 0):
if(Summary > 0):
Ans+= b[i]+ Summary+ 1
b[i] = -Summary- 1
Summary= -1
else:
Ans+= -Summary+ 1- b[i]
b[i] = -Summary+ 1
Summary= 1
else:
Summary+= b[i]
print(min(ans, Ans)) |
s970990080 | p03739 | u086503932 | 1590724584 | Python | Python (3.4.3) | py | Runtime Error | 90 | 14212 | 846 | #!/usr/bin/env python3
from itertools import accumulate
def main():
n = int(input())
a = list(map(int, input().split()))
a = list(accumulate(a))
if a[0] != 0:
ans = 0
diff = 0
for i in range(1,n):
p = a[i] + diff
q = a[i-1] + diff
if p * q >= 0:
tmp = -q//abs(q) - p
ans += abs(tmp)
diff += tmp
print(ans)
else:
ans = 10**18
for d in [-1,1]:
ans2 = 1
for i in range(1,n):
p = a[i] + d
q = a[i-1] + d
if p * q >= 0:
tmp = -q//abs(q) - p
ans2 += abs(tmp)
diff += tmp
ans = min(ans, ans2)
print(ans)
if __name__ == "__main__":
main()
|
s334857872 | p03739 | u392319141 | 1589248968 | Python | Python (3.4.3) | py | Runtime Error | 135 | 14464 | 429 | N = int(input())
A = list(map(int, input().split()))
def sol(S):
ret = 0
B = [S]
for a in A[1:]:
b = a
if S * (S + b) > 0:
b = (abs(S) + 1) * (1 if S < 0 else -1)
if S + b == 0:
b = b - 1 if b < 0 else b + 1
ret += abs(b - a)
S += b
B.append(b)
return ret
ans = min(
sol(A[0]),
sol(-A[0] // abs(A[0])) + abs(A[0]) + 1
)
print(ans)
|
s044712159 | p03739 | u977193988 | 1589130393 | Python | Python (3.4.3) | py | Runtime Error | 101 | 14460 | 2564 | import sys
def input():
return sys.stdin.readline().strip()
sys.setrecursionlimit(20000000)
def main():
N = int(input())
A = list(map(int, input().split()))
if A[0] == 0:
answer = []
for a in (1, -1):
cnt = 1
S = a
for i in range(1, N):
s = S + A[i]
if A[i] == 0:
if S > 0:
cnt += S + 1
S = -1
else:
cnt += abs(S) + 1
S = 1
else:
if s == 0:
if S < 0:
cnt += 1
S = 1
else:
cnt += 1
S = -1
else:
if S * s > 0:
if S < 0:
cnt += abs(s) + 1
S = 1
else:
cnt += s + 1
S = -1
else:
S = s
answer.append(cnt)
print(min(answer))
else:
if A[0] >0:
cnt1 = 0
S1 = A[0]
cnt2 = A[0]+1
S1 = -1
else:
cnt1 = 0
S1= A[0]
cnt2 = abs(A[0])+1
S2 = 1
answer = []
for cnt,S in ((cnt1,S1),(cnt2,S2)):
for i in range(1, N):
s = S + A[i]
if A[i] == 0:
if S > 0:
cnt += S + 1
S = -1
else:
cnt += abs(S) + 1
S = 1
else:
if s == 0:
if S < 0:
cnt += 1
S = 1
else:
cnt += 1
S = -1
else:
if S * s > 0:
if S < 0:
cnt += abs(s) + 1
S = 1
else:
cnt += s + 1
S = -1
else:
S = s
answer.append(cnt)
print(min(answer))
if __name__ == "__main__":
main()
|
s987640330 | p03739 | u855378574 | 1589055453 | Python | PyPy3 (2.4.0) | py | Runtime Error | 164 | 38480 | 677 | N = int(input())
A = list(map(int, input().split()))
counter = 0 ####操作回数
A.reverse()
S = 0
a = A.pop()
if a==0:
counter += 1
while A:
b = A.pop()
if b == 0:
counter += 2
elif b>0:
A.append(b)
S = -1
break
elif b<0:
A.append(b)
S = 1
break
else:
S += a
while A:
c = A.pop()
if c>=0 and S>0:
counter += abs(c+S)+1
S = -1
elif c<=0 and S<0:
counter += abs(c+S)+1
S = 1
elif (c>=0 and S<0) and S+c<=0:
counter += abs(S+c)+1
S = 1
elif (c=<0 and S>0) and S+c>=0:
counter += abs(S+c)+1
S = -1
else:
S += c
print(counter)
|
s253978364 | p03739 | u097317219 | 1588966571 | Python | PyPy3 (2.4.0) | py | Runtime Error | 174 | 38384 | 382 | def sign(x):
if x<0:
return -1
elif x>0:
return 1
else:
retrun 0
n = int(input())
a = list(map(int,input().split()))
cumulative_sum = a[0]
flag = sign(cumulative_sum)
ans = 0
for i in range(1,n):
cumulative_sum += a[i]
if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0:
ans += abs(-flag - cumulative_sum)
flag = sign(cumulative_sum)
print(ans) |
s645544847 | p03739 | u679520304 | 1588827954 | Python | PyPy3 (2.4.0) | py | Runtime Error | 231 | 63216 | 449 | n = int(input())
a = list(map(int,input().split()))
A = a[0]
for i in range(1,n):
num = abs(A)+1
if A*(A+a[i])<0:
A += a[i]
elif A+a[i]==0:
if A>0:
A += a[i]-num
ans += num
else:
A += a[i]+num
ans += num
elif A*(A+a[i])>0:
if A>0:
A += a[i]-num
ans += num
else:
A += a[i]+num
ans += num
print(ans) |
s829175591 | p03739 | u103520789 | 1588824106 | Python | PyPy3 (2.4.0) | py | Runtime Error | 191 | 55920 | 253 | from functools import reduce
N =int(input())
A = map(int, input().split())
def check(A):
arr = [0]*(N+1)
for i in range(len(A)):
arr[i+1] += arr[i-1] + ai
if arr[i+1]*arr[i] <=0:
return 0
val = abs(min(arr))
return val
print(val) |
s378455420 | p03739 | u607074939 | 1588813335 | Python | Python (3.4.3) | py | Runtime Error | 126 | 14332 | 286 | n = int(input())
a = list(map(int,input().split()))
sum_a = a[0]
cnt = 0
for i in range(1,n):
if sum_a*(sum_a+a[i]) < 0:
sum_a += a[i]
else:
tmp = a[i]
a[i] = int(-sum_a -a[i-1]/abs(a[i-1]))
sum_a += a[i]
cnt += abs(a[i]-tmp)
print(cnt) |
s899084624 | p03739 | u934868410 | 1588699764 | Python | Python (3.4.3) | py | Runtime Error | 133 | 14468 | 519 | n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
ans -= 1
if abs(a[i]) == 1:
ans += 1
s = a[i] // abs(a[i])
else:
s = a[0]
i += 1
for j in range(i, n):
if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
s += a[j]
else:
pre_s = s
s = -1 * s // abs(s)
ans += abs(a[j] - s + pre_s)
print(ans) |
s772741089 | p03739 | u934868410 | 1588699487 | Python | Python (3.4.3) | py | Runtime Error | 128 | 14468 | 506 | n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
if abs(a[i]) == 1:
ans += 1
s = a[i] // abs(a[i])
else:
s = a[0]
i += 1
for j in range(i, n):
if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
s += a[j]
else:
pre_s = s
s = -1 * s // abs(s)
ans += abs(a[j] - s + pre_s)
print(ans) |
s326706965 | p03739 | u934868410 | 1588699245 | Python | Python (3.4.3) | py | Runtime Error | 136 | 14468 | 502 | n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
if abs(a[i]) == 1:
ans += 1
s = a[i] // abs(a[i])
else:
s = a[0]
i += 1
for j in range(i, n):
if abs(a[j]) > abs(s) and a[j] // abs(a[j]) != s // abs(s):
s += a[j]
else:
pre_s = s
s = -1 * s // abs(s)
ans += abs(a[j] - s + pre_s)
print(ans) |
s813745966 | p03739 | u934868410 | 1588698891 | Python | Python (3.4.3) | py | Runtime Error | 130 | 14468 | 446 | n = int(input())
a = list(map(int,input().split()))
s, ans, i = 0,0,0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
if abs(a[i]) == 1:
ans += 1
s = a[i] // abs(a[i])
else:
s = a[0]
i += 1
for j in range(i,n):
if abs(a[j]) > abs(s) and a[j] // abs(a[j]) != s // abs(s):
s += a[j]
else:
s = -1 * s // abs(s)
ans += abs(abs(a[j]) - 2)
print(ans) |
s701083851 | p03739 | u190079347 | 1588476415 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3188 | 616 | 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))
|
s330953217 | p03739 | u949442192 | 1588186605 | Python | Python (3.4.3) | py | Runtime Error | 24 | 6124 | 924 | def resolve(SL):
# L[0]!=0を起点とする
cnt = 0
for i in range(len(SL)-1):
s0 = SL[i]
s1 = SL[i+1]
if(s0>0 and s1>=0):
SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]]
cnt += (s1+1)
elif(s0<0 and s1<=0):
SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]]
cnt += (-s1+1)
# print(SL)
return cnt
def ans(L):
SL = [sum(L[:(i+1)]) for i in range(len(L))]
c0,c1=0,0
if (L[0]>0):
c0 = resolve(SL)
c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL)))
elif (L[0]<0):
c0 = resolve(L)
c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL)))
else:
c0 = 1 + resolve(list(map(lambda x:x+1, SL)))
c1 = 1 + resolve(list(map(lambda x:x-1, SL)))
return(min(c0,c1))
N = int(input())
L = [int(input(x)) for x in range(N)]
print(ans(L)) |
s752354178 | p03739 | u576432509 | 1587839155 | Python | Python (3.4.3) | py | Runtime Error | 44 | 14464 | 767 | n=int(input())
a=list(map(int,input().split()))
# 0 1 2 3 evn odd
# kevn + - + - + -
# kodd - + - + - +
def kf(a,flag):
a0=a[0]*flag
if a0<=0:
kevn=1-a0
sevn=1
elif a0>0:
kevn=0
sevn=a0
for i in range(1,n):
ai=a[i]*flag
if i%2==0:
if sevn+ai<=0:
kevn=kevn+1-(ai+sevn)
sevn=1
elif sevn+ai>0:
kevn=kevn
sevn=sevn+ai
elif i%2==1:
if sevn+ai<0:
kevn=kevn
sevn=sevn+ai
elif sevn+ai>=0:
kevn=kevn+1+(ai+sevn)
sevn=-1
return kevn
kevn1=kf(a,1,n)
kevn=kf(a,-1,n)
kevn=min(kevn,kevn1)
print(kevn) |
s857888878 | p03739 | u626217906 | 1587512611 | Python | PyPy3 (2.4.0) | py | Runtime Error | 237 | 63772 | 1902 | import copy
n = int(input())
a = list(map(int,input().split()))
a_1 = a.copy()
ans = 0
ans_2 = 0
o = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
o = 0
a = a_1
#print(a)
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "-"
ans_2 += abs(-1 - a[0])
a[i] = -1
elif a[0] < 0:
ans_2 += abs(1 - a[0])
a[i] = 1
f = "+"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans_2 += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans_2 += 1
f = "+"
#print(a)
print(min(ans,ans_2))
|
s233679099 | p03739 | u626217906 | 1587512553 | Python | PyPy3 (2.4.0) | py | Runtime Error | 247 | 63772 | 1902 | import copy
n = int(input())
a = list(map(int,input().split()))
a_1 = a.copy()
ans = 0
ans_2 = 0
o = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
o = 0
a = a_1
#print(a)
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "-"
ans_2 += abs(-1 - a[0])
a[i] = -1
elif a[0] < 0:
ans_2 += abs(1 - a[0])
a[i] = 1
f = "+"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans_2 += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans_2 += 1
f = "+"
#print(a)
print(min(ans,ans_2))
|
s361240274 | p03739 | u626217906 | 1587509763 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2110 | 137912 | 861 | n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
if i == 0:
if a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
if f == "+":
if a[i] + sum(a[:i]) > 0:
c = -1 - sum(a[:i])
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + sum(a[:i]) == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + sum(a[:i]) < 0:
c = 1 - sum(a[:i])
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + sum(a[:i]) == 0:
a[i] += 1
ans += 1
f = "+"
#print(a)
print(ans)
|
s993552392 | p03739 | u496821919 | 1587418200 | Python | Python (3.4.3) | py | Runtime Error | 43 | 14468 | 518 | n = int(input())
a = list(map(int,input().split()))
S1 = 0
S2 = 0
#S1が奇数番目が正の場合、S2が偶数番目が負の場合
cnt = 0
for i,num in enumerate(a):
S1 += num
if i % 2 == 0 and S1 <= 0:
cnt1 += 1 - S1
S1 = 1
if i % 2 != 0 and S1 >= 0:
cnt1 += 1 + S1
S1 = -1
S2 += num
if i % 2 == 0 and S2 >= 0:
cnt2 += 1 + S2
S2 = -1
if i % 2 != 0 and S2 <= 0:
cnt2 += 1 - S2
S2 = 1
print(cnt1 if cnt1 <= cnt2 else cnt2)
|
s987259186 | p03739 | u931173291 | 1587005990 | Python | Python (3.4.3) | py | Runtime Error | 81 | 14468 | 522 | n=int(input())
a=list(map(int,input().split()))
sum=0
cnt=0
# 奇数+
for i in range(n):
sum+=a[i]
if i%2==0:
if sum>=0:
cnt+=(sum+1)
sum=-1
else:
if sum<=0:
cnt+=(1-sum)
sum=1
cnt_sbst=cnt
# 奇数-
cnt=0
sum=0
for i in range(n):
z+=a[i]
if i%2==1:
if sum>=0:
cnt+=(sum+1)
sum=-1
else:
if sum<=0:
cnt+=(1-sum)
sum=1
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)
|
s383450010 | p03739 | u931173291 | 1587005395 | Python | Python (3.4.3) | py | Runtime Error | 43 | 14468 | 542 | n=int(input())
a=list(map(int,input().split()))
import sys
sum=0
cnt=0
# 奇数+
for i in range(n):
z+=a[i]
if i%2==0:
if z<0:
sum=z
else:
cnt+=(z+1)
sum=-1
else:
if sum<=0:
cnt+=(1-sum)
sum=1
cnt_sbst=cnt
# 奇数-
for i in range(n):
z+=a[i]
if i%2==1:
if sum>=0:
cnt+=(z+1)
sum=-1
else:
if sum<=0:
cnt+=(1-z)
sum=1
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)
|
s255582063 | p03739 | u609061751 | 1584158247 | Python | PyPy3 (2.4.0) | py | Runtime Error | 238 | 59248 | 1066 | import sys
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
# スタート+
if a[0] <= 0:
A1 = 1
ans1 += abs(a[0]) + 1
else:
A1 = a[0]
ans1 = 0
for i in range(1, n):
nextA = A1 + a[i]
if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
A1 = nextA
elif nextA == 0 and A1 > 0:
ans1 += 1
A1 = -1
elif nextA == 0 and A1 < 0:
ans1 += 1
A1 = 1
elif A1 > 0:
ans1 += abs(nextA) + 1
A1 = -1
else:
ans1 += abs(nextA) + 1
A1 = 1
# スタート-
if a[0] >= 0:
ans2 = abs(a[0]) + 1
A2 = -1
else:
A2 = a[0]
ans2 = 0
for i in range(1, n):
nextA = A2 + a[i]
if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
A2 = nextA
elif nextA == 0 and A2 > 0:
ans2 += 1
A2 = -1
elif nextA == 0 and A2 < 0:
ans2 += 1
A2 = 1
elif A2 > 0:
ans2 += abs(nextA) + 1
A2 = -1
else:
ans2 += abs(nextA) + 1
A2 = 1
print(min(ans1, ans2))
|
s603559040 | p03739 | u609061751 | 1584158164 | Python | PyPy3 (2.4.0) | py | Runtime Error | 237 | 59120 | 1041 | import sys
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
# スタート+
if a[0] <= 0:
A1 = 1
ans1 += abs(a[0]) + 1
else:
A1 = a[0]
for i in range(1, n):
nextA = A1 + a[i]
if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
A1 = nextA
elif nextA == 0 and A1 > 0:
ans1 += 1
A1 = -1
elif nextA == 0 and A1 < 0:
ans1 += 1
A1 = 1
elif A1 > 0:
ans1 += abs(nextA) + 1
A1 = -1
else:
ans1 += abs(nextA) + 1
A1 = 1
# スタート-
if a[0] >= 0:
ans2 = abs(a[0]) + 1
A2 = -1
else:
A2 = a[0]
for i in range(1, n):
nextA = A2 + a[i]
if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
A2 = nextA
elif nextA == 0 and A2 > 0:
ans2 += 1
A2 = -1
elif nextA == 0 and A2 < 0:
ans2 += 1
A2 = 1
elif A2 > 0:
ans2 += abs(nextA) + 1
A2 = -1
else:
ans2 += abs(nextA) + 1
A2 = 1
print(min(ans1, ans2))
|
s288012996 | p03739 | u854612823 | 1583445763 | Python | Python (3.4.3) | py | Runtime Error | 46 | 14332 | 403 | n = input()
a = [int(i) for i in input().split()]
cou = len(a)
counter = 0
X = a[0]
ans = 0
for i in a:
if X > 0:
b = -1 - X
ans += abs(b - a[i])
X = -1
counter += 1
if counter == cou - 1:
break
else:
b = 1 - X
ans += abs(b - a[i])
X = 1
counter += 1
if counter == cou - 1:
break
print (ans) |
s051200063 | p03739 | u854612823 | 1583441592 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 209 | n = input()
a = [int(i) for i in input.split()]
X = 0
ans = 0
for i in a:
X += i
if X > 0:
b = -1 - X
ans += b - a[i+1]
else:
b = 1 - X
ans += b - a[i+1]
print (ans) |
s934153849 | p03739 | u854612823 | 1583440881 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 219 | n = input()
a = [int(i) for i in input.split()]
X = 0
ans = 0
for i in a:
X += i
if X > 0:
b = -1 - X
ans += b - a(i+1)
else X < 0:
b = 1 - X
ans += b - a(i+1)
return ans
print (ans) |
s601693471 | p03739 | u854612823 | 1583440816 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 219 | n = input()
a = [int(i) for i in input.solit()]
X = 0
ans = 0
for i in a:
X += i
if X > 0:
b = -1 - X
ans += b - a(i+1)
else X < 0:
b = 1 - X
ans += b - a(i+1)
return ans
print (ans) |
s965425617 | p03739 | u127499732 | 1580675333 | Python | Python (3.4.3) | py | Runtime Error | 106 | 14332 | 193 | 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)) |
s749782194 | p03739 | u127499732 | 1580674892 | Python | Python (3.4.3) | py | Runtime Error | 106 | 14332 | 196 | 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,1):
t=s+l[i]
if s*t>0:
s=-1*f
c+=abs(t-s+1)
else:
f=-1*f
s=s+l[i]
print(int(c)) |
s356297779 | p03739 | u462329577 | 1580652216 | Python | Python (3.4.3) | py | Runtime Error | 141 | 14644 | 995 | #!/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)) |
s812565026 | p03739 | u779455925 | 1580180294 | Python | PyPy3 (2.4.0) | py | Runtime Error | 248 | 64112 | 523 | # input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
n=int(input())
A=list(map(int,input().split()))
temp=A[0]
if temp<0:
flag=0
if temp>0:
flag=1
count=0
for i in range(1,n):
temp+=A[i]
if flag:
if temp>=0:
count+=temp-(-1)
temp=-1
flag=0
else:
if temp<=0:
count+=1-temp
temp=1
flag=1
#print(temp,flag)
print(count)
|
s165903948 | p03739 | u379959788 | 1579803085 | Python | PyPy3 (2.4.0) | py | Runtime Error | 164 | 38384 | 1135 | import numpy as np
N = int(input())
A = list(map(int, input().split()))
res_plus = np.cumsum(A)
res_minus = np.cumsum(A)
ans_plus = 0
ans_minus = 0
tmp_tmp = 0
# 奇数項を正とする
for i in range(N):
res_plus[i] += tmp_tmp
if i % 2 == 0: # 奇数項
if res_plus[i] > 0:
pass
else:
tmp = 1 - res_plus[i]
ans_plus += tmp
tmp_tmp += tmp
else: # 偶数項
if res_plus[i] < 0:
pass
else:
tmp = res_plus[i] + 1
ans_plus += tmp
tmp_tmp -= tmp
tmp_tmp = 0
# 奇数項を負とする
for i in range(N):
res_minus[i] += tmp_tmp
if i % 2 == 0: # 奇数項
if res_minus[i] < 0:
pass
else:
tmp = res_minus[i] + 1
ans_minus += tmp
tmp_tmp -= tmp
else: # 偶数項
if res_minus[i] > 0:
pass
else:
tmp = 1 - res_minus[i]
ans_minus += tmp
tmp_tmp += tmp
##print(ans_minus, ans_plus)
print(min(ans_plus, ans_minus)) |
s435146592 | p03739 | u875600867 | 1579750951 | Python | Python (3.4.3) | py | Runtime Error | 1329 | 142756 | 1242 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
for _ in range(3):
print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
print(cnt) |
s748792913 | p03739 | u875600867 | 1579750920 | Python | Python (3.4.3) | py | Runtime Error | 1316 | 143064 | 1240 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
for _ in range(3):
print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
print(cnt) |
s225883441 | p03739 | u875600867 | 1579750670 | Python | Python (3.4.3) | py | Runtime Error | 1328 | 142736 | 1240 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
for _ in range(3):
print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
print(cnt) |
s909431484 | p03739 | u875600867 | 1579750522 | Python | Python (3.4.3) | py | Runtime Error | 2104 | 142792 | 1231 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
while True:
print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
print(cnt) |
s303864810 | p03739 | u993435350 | 1579736297 | Python | Python (3.4.3) | py | Runtime Error | 1796 | 139524 | 619 | N = int(input())
A = list(map(int,input().split()))
totals = [0] * N
totals[0] = A[0]
con = 0
if totals[0] == 0:
for i in range(1,N):
if A[i] != 0:
f = A[i]
if f > 0:
totals[0] = -1
else:
totals[0] = 1
break
else:
totals[0] = 1
con += 1
for i in range(1,N):
totals[i] = totals[i - 1] + A[i]
if totals[i - 1] * totals[i] >= 0:
if totals[i - 1] < 0:
con += abs(1 - totals[i])
totals[i] += abs(1 - totals[i])
elif totals[i - 1] > 0:
con += abs(-1 - totals[i])
totals[i] -= abs(-1 - totals[i])
print(totals[:i + 1])
print(con)
|
s716866408 | p03739 | u912115033 | 1579579629 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 369 | a = list(map(int,input().split()))
if a[0] > 0:
f = 1
else:
f = -1
m = a[0]
cnt = 0
for i in range(1, n):
f *= -1
m += a[i]
if f == 1:
if m > 0:
continue
else:
cnt += f-m
m += f-m
else:
if m < 0:
continue
else:
cnt += m-f
m += f-m
print(cnt) |
s257047207 | p03739 | u062189367 | 1579325474 | Python | PyPy3 (2.4.0) | py | Runtime Error | 235 | 63444 | 667 | n = int(input())
a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
b.append(a[i])
def solve(cnt,A,N):
for i in xrange(1, N):
if sum(A[0:i])>0:
if sum(A[0:i+1])>=0:
r = A[i]
A[i]=-sum(A[0:i])-1
cnt+=abs(r-A[i])
else:
if sum(A[0:i+1])<=0:
r = A[i]
A[i]=-sum(A[0:i])+1
cnt+=abs(r-A[i])
return cnt
cnt1=0
if b[0]<=0:
ini=b[0]
b[0]=1
cnt1=abs(1-ini)
ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
ini=a[0]
a[0]=-1
cnt2=abs(-1-ini)
ans2=solve(cnt2,a,n)
print(min(ans1,ans2))
|
s959975122 | p03739 | u121921603 | 1578957617 | Python | PyPy3 (2.4.0) | py | Runtime Error | 183 | 38384 | 349 | import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
v=[]
for s in (1,-1):
x=a[::]
hugou=[s*(-1)**i sor i in range(n)]
total=0
for i in range(n):
if (total+x[i])*hugou[i]<=0:
x[i]=hugou[i]-total
total+=xs[i]
v.append(sum(absa[i]-x[i])for i in range(n))
print(min(v))
|
s063200374 | p03739 | u933341648 | 1578682073 | Python | Python (3.4.3) | py | Runtime Error | 164 | 14332 | 295 | n = int(input())
a = [int(x) for x in input().split()]
res = 0
x = 0
for i in range(n-1):
x += a[i]
sign = (x // abs(x)) * (-1)
tmp = sign - (x + a[i+1])
if sign < 0:
tmp = min(tmp, 0)
else:
tmp = max(tmp, 0)
res += abs(tmp)
a[i+1] += tmp
print(res) |
s880765112 | p03739 | u968404618 | 1578649896 | Python | Python (3.4.3) | py | Runtime Error | 42 | 14332 | 531 | def main():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
def Chk(a, pos):
cnt = 0
tmp = 0
for a in A:
tmp += a
if pos and tmp < 1:
cnt += 1 - tmp
tmp = 1
elif not pos and tmp > -1:
cnt += 1 + tmp
tmp = -1
pos = not pos
return cnt
print(min(Chk(A, True), Chk(A, False)))
if __name__ == '__main__':
main() |
s452218933 | p03739 | u968404618 | 1578649689 | Python | PyPy3 (2.4.0) | py | Runtime Error | 225 | 63088 | 381 | n = input()
A = list(map(int, input().split()))
def Chk(a, pos):
cnt = 0
tmp = 0
for a in A:
tmp += a
if pos and tmp < 1:
cnt += 1 - tmp
tmp = 1
elif not pos and tmp > -1:
cnt += 1 + tmp
tmp = -1
pos = not pos
return cnt
print(min(Chk(A, True), Chk(a, False))) |
s898776002 | p03739 | u968404618 | 1578645364 | Python | Python (3.4.3) | py | Runtime Error | 43 | 15100 | 559 | def main():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
A = ()
A_append = A.append
cnt = 0
for i in range(n-1):
A_append(a[i])
x = sum(A) + a[i+1]
if sum(A) > 0 and x > 0:
y = abs(x)+1
cnt += y
a[i+1] -= y
elif sum(A) < 0 and x < 0:
y = abs(sum(A) - a[i+1])-1
cnt += y
a[i+1] += y
if sum(a) == 0:
cnt += 1
print(cnt)
if __name__ == '__main__':
main() |
s652107092 | p03739 | u223646582 | 1577746211 | Python | Python (3.4.3) | py | Runtime Error | 46 | 14468 | 514 | n = int(input())
L = [[int(i) for i in input().split()], [-int(i) for i in input().split()]]
c = 10**15
for i in range(2):
A = L[i]
if A[0] != 0:
ans = 0
S = A[0]
f = A[0]//abs(A[0])
else:
ans = 1
S = 1
f = 1
for a in A[1:]:
S += a
if S == 0:
ans += 1
S = -f
else:
if S/abs(S) != f*(-1):
ans += abs(S)+1
S = -f
f *= -1
c = min(ans, c)
print(c)
|
s225611515 | p03739 | u223646582 | 1577744846 | Python | Python (3.4.3) | py | Runtime Error | 106 | 14468 | 618 | n = int(input())
A = [int(i) for i in input().split()]
ff = -1
for i in range(n):
if A[i] != 0:
ff = i
break
if A[0] != 0:
ans = 0
S = A[0]
f = A[0]//abs(A[0])
else:
if ff == -1:
ans = 1
S[0] = 1
f = 1
else:
if ff % 2 == 0:
ans = 1
S[0] = 1
f = 1
else:
ans = 1
S[0] = -1
f = -1
for a in A[1:]:
S += a
if S == 0:
ans += 1
S = -f
else:
if S/abs(S) != f*(-1):
ans += abs(S)+1
S = -f
f *= -1
print(ans)
|
s047173337 | p03739 | u484229314 | 1577635300 | Python | Python (3.4.3) | py | Runtime Error | 187 | 14468 | 554 | N = int(input())
A = [int(_) for _ in input().split()]
ans = 0
s = A[0]
i = 1
while i < N:
a = A[i]
# print(i, s, ans)
sign = (s // abs(s))
abs_min = abs(s) + 1
if a == 0:
ans += abs_min
s = -sign
i += 1
continue
abs_a = abs(a)
a_sign = (a // abs_a)
if a_sign == sign:
A[i] = 0
ans += abs_a
else:
if abs_min <= abs_a:
s = (abs_a - abs(s)) * a_sign
else:
ans += abs_min - abs_a
s = -sign
i += 1
print(ans)
|
s957535708 | p03739 | u095562538 | 1577083289 | Python | Python (3.4.3) | py | Runtime Error | 134 | 14332 | 1170 | # coding: utf-8
# Here your code
N = int(input())
a = [int(i) for i in input().split()]
result_1 = 0
before_sum =a[0]
after_sum =a[0]
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_1 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_1 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_1 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
before_sum =int(-a[0]/abs(a[0]))
after_sum =before_sum
result_2 = 1 + abs(before_sum)
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_2 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_2 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_2 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
print(min(result_1,result_2-1))
|
s910986115 | p03739 | u095562538 | 1577082836 | Python | Python (3.4.3) | py | Runtime Error | 138 | 14332 | 1169 | # coding: utf-8
# Here your code
N = int(input())
a = [int(i) for i in input().split()]
result_1 = 0
before_sum =a[0]
after_sum =a[0]
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_1 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_1 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_1 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
before_sum =int(-a[0]/abs(a[0]))
after_sum =before_sum
result_2 = 1 + abs(before_sum)
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_2 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_2 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_2 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
print(min(result_1,result_2))
|
s276121511 | p03739 | u905582793 | 1576872244 | Python | Python (3.4.3) | py | Runtime Error | 125 | 14468 | 1021 | # coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
sm = 0
ans = 0
ans += abs(ls[0]-x)
k = x
for i in range(1,len(ls)):
sm += k
if sm > 0:
if abs(ls[i]) <= sm or sm < ls[i]:
ans += abs(ls[i]+sm+1)
k = -sm-1
else:
k = ls[i]
if sm < 0:
if abs(ls[i]) <= abs(sm) or sm > ls[i]:
ans += abs(-sm+1-ls[i])
k = -sm+1
else:
k = ls[i]
return ans
anstot = []
if a[0] > 0:
inv = -1
if a[0] < 0:
inv = 1
if a[0] != 0:
anstot.append(operate(a,a[0]))
anstot.append(operate(a,inv))
if a[0] == 0:
for i in range(n):
if a[i] == 0:
a[i] = -1**(i%2)
if a[i] != 0:
r = i
break
anstot.append(operate(r+a,a[0]))
for i in range(r):
a[i] = -1**((1+i)%2)
anstot.append(operate(r+a,a[0]))
print(min(anstot)) |
s294893245 | p03739 | u737321654 | 1576425179 | Python | Python (3.4.3) | py | Runtime Error | 141 | 14468 | 918 | N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count3 = 0
count4 = 0
currentSum = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count3 += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count3 += abs(currentSum) + 1
currentSum = -1
elif A[i] <= 0 and restSum == 0:
count3 += abs(currentSum) + 1
currentSum = 1
currentSum = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count4 += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count4 += abs(currentSum) + 1
currentSum = -1
elif A[i] >= 0 and restSum == 0:
count4 += abs(currentSum) + 1
currentSum = -1
print(count1, count2, count3, count4)
|
s921382804 | p03739 | u814986259 | 1575680453 | Python | Python (3.4.3) | py | Runtime Error | 43 | 14372 | 403 | n=int(input())
a=list(map(int,input().split()))
ans=0
tmp=0
for i in range(N):
tmp+=a[i]
if i%2==0:
if tmp<=0:
ans+=abs(tmp)+1
tmp=1
else:
if tmp>=0:
ans+=abs(tmp)+1
tmp=-1
tmp=0
tmp2=0
for i in range(N):
tmp+=a[i]
if i %2==0:
if tmp>=0:
tmp2+=abs(tmp)+1
tmp=-1
else:
if tmp<=0:
tmp2+=abs(tmp)+1
tmp=1
print(min(ans,tmp))
|
s648905803 | p03739 | u703442202 | 1575251724 | Python | Python (3.4.3) | py | Runtime Error | 93 | 14468 | 705 | n = int(input())
a_list = list(map(int,input().split()))
def greeed(sum_a,now_a,next_a,count):
if now_a >= 0 and next_a >= 0:
next_a = -1
count += next_a + 1
elif now_a < 0 and next_a <0:
next_a = 0
count += next_a
if sum_a + next_a ==0:
if next_a >=0:
next_a += 1
count += 1
else :
next_a = next_a -1
count+= 1
return count
count = 0
if a_list[0] ==0:
if a_list[1]>=0:
a_list = -1
count+= 1
else :
a_list = 1
count +=1
sum_a = a_list[0]
for i in range(0,n-1):
now_a = a_list[i]
next_a = a_list[i + 1]
count = greeed(sum_a,now_a,next_a,count)
print(count)
|
s769489160 | p03739 | u223904637 | 1574474256 | Python | PyPy3 (2.4.0) | py | Runtime Error | 177 | 38384 | 443 | n=int(input())
l=list(map(int,input().split()))
l.append(0)
a1=0
b1=0
a2=0
b2=0
for i in range(n):
b1+=l[i]
b2+=l[i]
if i%2==0:
if b1>=0:
a1+=abs(-1-b1)
b1=-1-l[i+1]
if b2=<0:
a2+=abs(1-b2)
b2=1-l[i+1]
else:
if b1<=0:
a1+=abs(1-b1)
b1=1-l[i+1]
if b2>=0:
a2+=abs(-1-b2)
b2=-1-l[i+1]
print(min(a1,a2)) |
s995356544 | p03739 | u214434454 | 1574053898 | Python | PyPy3 (2.4.0) | py | Runtime Error | 223 | 63216 | 754 | n = int(input())
a = list(map(int, input().split()))
a2 = a.copy()
ans1 = 0 #偶数インデックスが正
for i in range(n):
s1 = sum(a[:i+1])
if i % 2 == 0:
if s1 <= 0:
x = abs(s1) + 1
ans1 += x
a[i] += x
else:
continue
else:
if s1 >= 0:
x = abs(s1) + 1
ans1 += x
a[i] -= x
ans2 = 0 #偶数インデックスが負
for i in range(n):
s2 = sum(a2[:i+1])
if i % 2 == 1:
if s2 <= 0:
x = abs(s2) + 1
ans2 += x
a2[i] += x
else:
continue
else:
if s2 >= 0:
x = abs(s2) + 1
ans2 += x
a2[i] -= x
print(min(ans1, ans2)) |
s907222240 | p03739 | u846634344 | 1573803188 | Python | PyPy3 (2.4.0) | py | Runtime Error | 220 | 63216 | 800 | def decision(i):
sum_for_i = sum(A[:i+1]) # 0~i までの sum
sum_before_i = sum(A[:i]) # 0~i-1までの sum
if sum_for_i == 0:
if sum_before_i <0:
return 1
if sum_before_i >0:
return 2
if sum_before_i > 0 and sum_for_i >0:
return 3
if sum_before_i < 0 and sum_for_i <0:
return 4
return 0
n = int(input())
original_A = [int(x) for x in input().split()]
A = original_A.copy()
for i in range(1, n):
result = decision(i)
if result == 0:
continue
elif result == 1:
A[i] += 1
elif result == 2:
A[i] -= 1
elif result == 3:
A[i] -= (abs((sum(A[:i+1]))+1))
elif result == 4:
A[i] += (abs(sum(A[:i+1]))+1)
print(sum([abs(original_A[x] - A[x]) for x in range(n)])) |
s006012773 | p03739 | u134712256 | 1572643514 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 483 | n = int(input())
a = [int(i) for i in input().split()]
s0 = a[0]
count=0
i=0
while a[i]!=0and i!=n-1:i++
if a[i]>0:
s0+=1
count+=1
else:
s0-=1
count+=1
for i in range(1,n):
s1 = s0+a[i]
if s0*s1>=0:
if s1>0:
a[i]-=(abs(s1)+1)
count+=(abs(s1)+1)
elif s1<0:
a[i]+=(abs(s1)+1)
count+=(abs(s1)+1)
elif s1==0:
if s0>0:
a[i]-=1
count+=1
elif s0<0:
a[i]+=1
count+=1
s0 += a[i]
print(count) |
s183441698 | p03739 | u134712256 | 1572643433 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 473 | n = int(input())
a = [int(i) for i in input().split()]
s0 = a[0]
count=0
i=0
while a[i]!=0:i++
if a[i]>0:
s0+=1
count+=1
else:
s0-=1
count+=1
for i in range(1,n):
s1 = s0+a[i]
if s0*s1>=0:
if s1>0:
a[i]-=(abs(s1)+1)
count+=(abs(s1)+1)
elif s1<0:
a[i]+=(abs(s1)+1)
count+=(abs(s1)+1)
elif s1==0:
if s0>0:
a[i]-=1
count+=1
elif s0<0:
a[i]+=1
count+=1
s0 += a[i]
print(count) |
s603603842 | p03739 | u626468554 | 1571778650 | Python | Python (3.4.3) | py | Runtime Error | 91 | 14468 | 486 | n = int(input())
a = list(map(int,input().split()))
ttl = a[0]
cst = 0
if a[0]>0:
flg = 1
elif a[0]<0:
flg = -1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst += memo
flg *= -1
ttl = a[0]
cst2 = 0
print(min(cst,cst2))
|
s558581474 | p03739 | u626468554 | 1571778118 | Python | Python (3.4.3) | py | Runtime Error | 142 | 14332 | 931 | n = int(input())
a = list(map(int,input().split()))
ttl = a[0]
cst = 0
if a[0]>0:
flg = 1
elif a[0]<0:
flg = -1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst += memo
flg *= -1
ttl = a[0]
cst2 = 0
if a[0]>0:
flg = -1
cst2 += abs(ttl)+1
ttl += 0-ttl-1
elif a[0]<0:
flg = 1
cst2 += abs(ttl)+1
ttl += 0-ttl+1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst2 += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst2 += memo
flg *= -1
print(min(cst,cst2))
|
s953822685 | p03739 | u626468554 | 1571778076 | Python | Python (3.4.3) | py | Runtime Error | 318 | 14332 | 932 | n = int(input())
a = list(map(int,input().split()))
ttl = a[0]
cst = 0
if a[0]>0:
flg = 1
elif a[0]<0:
flg = -1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst += memo
flg *= -1
ttl = a[0]
cst2 = 0
if a[0]>0:
flg = -1
cst2 += abs(ttl)+1
ttl += 0-ttl-1
elif a[0]<0:
flg = 1
cst2 += abs(ttl)+1
ttl += 0-ttl+1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst2 += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst2 += memo
flg *= -1
print(cst2," ",ttl)
|
s358070668 | p03739 | u690781906 | 1569847039 | Python | Python (3.4.3) | py | Runtime Error | 104 | 14464 | 715 | n = int(input())
a = list(map(int, input().split()))
ans = 0
if a[0] > 0:
op = "+"
elif a[0] < 0:
op = "-"
elif a[0] == 0:
ans += 1
if a[1] > 0:
a[0] = -1
else:
a[0] = 1
total = a[0]
for i in range(1, n):
if (total+a[i]) >= 0 and op == "+":
ans += abs((-1 - total) - a[i])
total = -1
op = "-"
elif (total+a[i]) < 0 and op == "-":
ans += abs((1 - total) - a[i])
total = 1
op = "+"
elif total+a[i] == 0:
ans += 1
if a[1] > 0:
a[0] = -1
else:
a[0] = 1
else:
total += a[i]
if op == "+":
op = "-"
else:
op = "+"
print(ans) |
s938825250 | p03739 | u780962115 | 1569253875 | Python | Python (3.4.3) | py | Runtime Error | 18 | 2940 | 1103 | n=int(input())
N=list(map(int,input().split()))
emptylists=[]
number=0
for i in range(n):
number+=N[i]
emptylists.append(number)
ans1=0
use=0
for i in range(n):
#偶数番が正、奇数番が負の時
if i%2==0:
#奇数番め
if emptylists[i]+use<0:
continue
if emptylists[i]+use>=0:
ans1+=emptylists[i]+1
use=use-emptylists[i]-1
if i%2!=0:
#偶数番め
if emptylists[i]+use>0:
continue
if emptylists[i]+use<=0:
ans1+=1-emptylists[i]-use
use=use+1-emptylists[i]
ans2=0
uses=0
for i in range(n):
#偶数番が負、奇数番が正の時
if i%2!=0:
#ぐう数番め
if emptylists[i]<0:
continue
if emptylists[i]>=0:
ans2+=emptylists[i]+1
uses=uses-emptylists[i]-1
if i%2==0:
#き数番め
if emptylists[i]>0:
continue
if emptylists[i]<=0:
ans2+=1-emptylists[i]-uses
uses=uses+1-emptylists[i]
print(min(ans1,ans2)) |
s996107116 | p03739 | u785470389 | 1568872890 | Python | Python (3.4.3) | py | Runtime Error | 85 | 14468 | 482 | n = int(input())
a = [] * n
a = list(map(int, input().split()))
if a[0] > 0:
symbol = "plus"
elif a[0] < 0:
symbol = "minus"
sum1 = a[0]
count = 0
for i in range(1, n):
sum1 += a[i]
if symbol == "plus" and sum1 >= 0:
operation = sum1 + 1
count += operation
sum1 = -1
elif symbol == "minus" and sum1 <= 0:
operation = abs(sum1) + 1
count += operation
sum1 = 1
if symbol == "plus":
symbol = "minus"
else:
symbol = "plus"
print(count)
|
s849966254 | p03739 | u785470389 | 1568872614 | Python | Python (3.4.3) | py | Runtime Error | 90 | 14468 | 494 | n = int(input())
a = [] * n
a = list(map(int, input().split()))
if a[0] > 0:
symbol = "plus"
elif a[0] < 0:
symbol = "minus"
sum1 = a[0]
count = 0
for i in range(1, n):
sum1 += a[i]
if symbol == "plus" and sum1 >= 0:
operation = sum1 + 1
count += operation
sum1 -= operation
elif symbol == "minus" and sum1 <= 0:
operation = abs(sum1) + 1
count += operation
sum1 += operation
if symbol == "plus":
symbol = "minus"
else:
symbol = "plus"
print(count) |
s687616069 | p03739 | u266874640 | 1568342487 | Python | PyPy3 (2.4.0) | py | Runtime Error | 186 | 38384 | 863 | import numpy as np
import copy
N = int(input())
a = list(map(int,input().split()))
a = np.cumsum(a)
ans1 = 0
ans2 = 0
b = copy.copy(a)
for i in range(N):
if i % 2 == 0:
if a[i] > 0:
pass
else:
ans1 += abs(a[i]) + 1
a[i+1:] = list(map(lambda n:n+abs(a[i])+1, a[i+1:]))
else:
if a[i] < 0:
pass
else:
ans1 += abs(a[i]) + 1
a[i+1:] = list(map(lambda n:n-(abs(a[i])+1), a[i+1:]))
for i in range(N):
if i % 2 == 1:
if b[i] > 0:
pass
else:
ans2 += abs(b[i]) + 1
b[i+1:] = list(map(lambda n:n+abs(b[i])+1, b[i+1:]))
else:
if b[i] < 0:
pass
else:
ans2 += abs(b[i]) + 1
b[i+1:] = list(map(lambda n:n-(abs(b[i])+1), b[i+1:]))
print(min(ans1,ans2))
|
s679455493 | p03739 | u009348313 | 1568255994 | Python | PyPy3 (2.4.0) | py | Runtime Error | 169 | 38256 | 353 | N = int(input())
A = list(map(int,input().split()))
ans = 0
s = A[0]
if s > 0:
flag = 1
elif s < 0:
flag = -1
elif if A[1] < 0:
flag = 1
ans += 1
else:
flag = -1
ans += 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)
|
s076174967 | p03739 | u413165887 | 1567139532 | Python | Python (3.4.3) | py | Runtime Error | 42 | 14332 | 1339 | n = int(input())
a = list(map(int, input().split(' ')))
result = 0
h = 0
if a[0] == 0:
counter += 1
result += 1
else:
counter += a[0]
for i in range(1, n):
if counter < 0:
counter += a[i]
if counter == 0:
counter += 1
result +=1
elif counter > 0:
continue
else:
result += 1-counter
counter = 1
else:
counter += a[i]
if counter == 0:
counter -= 1
result += 1
elif counter < 0:
continue
else:
result += counter+1
counter = -1
out = []
out.append(result)
result = 0
counter = 0
if a[0] == 0:
counter -= 1
result +=1
elif a[0]< 0:
counter += 1
result += 1-a[0]
else:
counter -= 1
result += 1+a[0]
for i in range(1, n):
if counter < 0:
counter += a[i]
if counter == 0:
counter += 1
result += 1
elif counter > 0:
continue
else:
result += 1-counter
counter = 1
else:
counter += a[i]
if counter == 0:
counter -= 1
result +=1
elif counter < 0:
continue
else:
result += counter+1
counter = -1
out.append(result)
print(min(out)) |
s150289134 | p03739 | u271934630 | 1566960948 | Python | Python (3.4.3) | py | Runtime Error | 51 | 14332 | 357 | n = int(input())
a = [int(x) for x in input().split()]
def check(a, t):
ans = 0
x = 0
for i in a:
x += i
if t == True and x < 1:
ans += 1 - x
x = 1
elif t == False and x > -1:
ans += x + 1
x = -1
t = not t
return ans
print(min(chk(a, True), chk(a, False)))
|
s002001444 | p03739 | u271934630 | 1566960874 | Python | PyPy3 (2.4.0) | py | Runtime Error | 222 | 63216 | 357 | n = int(input())
a = [int(x) for x in input().split()]
def check(a, t):
ans = 0
x = 0
for i in a:
x += i
if t == True and x < 1:
ans += 1 - x
x = 1
elif t == False and x > -1:
ans += x + 1
x = -1
t = not t
return ans
print(min(chk(a, True), chk(a, False)))
|
s749388315 | p03739 | u076917070 | 1566875771 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1024 | import sys
input=sys.stdin.readline
input = open(sys.argv[1], "r").readline
def main():
N = int(input())
A = list(map(int, input().split()))
tmp = A.copy()
mi = -1
for j in range(2):
A = tmp.copy()
s = 0
n = 0
if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする
if A[0] > 0:
n += abs(A[0] +1)
A[0] = -1
else: # 奇数番目までの和が正 ⇒ A[0] を正にする
if A[0] < 0:
n += abs(A[0] -1)
A[0] = 1
s = A[0]
# print(A)
for i in range(1,N):
if s * (s+A[i]) >= 0:
if s < 0:
n += abs(-s+1 -A[i])
A[i] = -s+1
else:
n += abs(-s-1 -A[i])
A[i] = -s-1
s += A[i]
if mi == -1:
mi = n
else:
mi = min(mi, n)
print(mi)
if __name__ == '__main__':
main()
|
s722958886 | p03739 | u450904670 | 1566779082 | Python | PyPy3 (2.4.0) | py | Runtime Error | 229 | 63216 | 723 | n = int(input())
a = list(map(int, input().split()))
import numpy as np
na = np.array(a).cumsum()
cnt = 0
if(na[0] > 0):
for i in range(n):
delta = abs(na[i]) + 1
if(i % 2 == 0 and na[i] <= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] += delta
elif(i % 2 == 1 and na[i] >= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] -= delta
else:
na[i]
else:
for i in range(n):
delta = abs(na[i]) + 1
if(i % 2 == 1 and na[i] <= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] += delta
elif(i % 2 == 0 and na[i] >= 0):
cnt = cnt + delta
for j in range(i, n):
na[j] -= delta
else:
na[i]
print(cnt)
|
s227577833 | p03739 | u677523557 | 1564703047 | Python | Python (3.4.3) | py | Runtime Error | 101 | 14468 | 562 | N = int(input())
A = list(map(int, input().split()))
def count(s):
ans = 0
for i, a in enumerate(A):
if i == 0:
continue
if s < 0:
if 1-s-a >= 0:
ans += 1-s-a
s = 1
else:
s += a
elif s > 0:
if s+a+1 >= 0:
ans += s+a+1
s = -1
else:
s += a
return ans
b = A[0]
ans1 = count(b)
if b > 0:
ans2 = count(-1)+b+1
elif b < 0:
ans2 = count(1)+1-b
print(min(ans1, ans2)) |
s927040097 | p03739 | u201660334 | 1564203798 | Python | Python (3.4.3) | py | Runtime Error | 98 | 14332 | 261 | n = int(input())
a = list(map(int, input().split()))
count = 0
sum_a = a[0]
for i in range(n - 1):
sum_a2 = sum_a + a[i + 1]
if sum_a * sum_a2 >= 0:
count += abs(sum_a2) + 1
sum_a2 = - abs(sum_a) // sum_a
sum_a = sum_a2
print(count) |
s593519625 | p03739 | u413019025 | 1562799464 | Python | Python (3.4.3) | py | Runtime Error | 123 | 14468 | 1136 | #!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
def read_h(typ=int):
return list(map(typ, input().split()))
def read_v(n, m=1, typ=int):
return [read_h() if m > 1 else typ(input()) for _ in range(n)]
def calc_num_op(cumul, a):
assert cumul != 0
tmp_cumul = cumul + a
# print('current cumul:', cumul)
# print('current a:', a)
# print('bare cumul:', tmp_cumul)
if (cumul < 0 and tmp_cumul <= 0):
return abs(-cumul - a + 1), 1
if (cumul > 0 and tmp_cumul >= 0):
return abs(-cumul - a - 1), -1
return 0, tmp_cumul
def solve(arr, sign):
num_op = 0
cumul = arr[0]
for a in arr[1:]:
delta, cumul = calc_num_op(cumul, a)
# print('delta:', delta)
# print('modified cumul:', cumul)
num_op += delta
sign = '+' if sign == '-' else '-'
return num_op
def main():
_ = read_h()
arr = read_h()
op_plus = solve(arr, '+')
# print('op plus:', op_plus)
op_minus = solve(arr, '-')
# print('op minus:', op_minus)
print(min(op_plus, op_minus))
if __name__ == '__main__':
main()
|
s500685212 | p03739 | u633914031 | 1562652468 | Python | Python (3.4.3) | py | Runtime Error | 175 | 14332 | 956 | 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):
wa+=a[i]
if i == 0:
a[i]=int(a[i]/abs(a[i])*(-1))
cnt+=abs(a[i])+1
wa=a[i]
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)) |
s874806197 | p03739 | u633914031 | 1562651993 | Python | Python (3.4.3) | py | Runtime Error | 185 | 14332 | 951 | 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):
wa+=a[i]
if i == 0:
a[i]=a[i]/abs(a[i])*(-1)
cnt+=abs(a[i])+1
wa=a[i]
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)) |
s440630749 | p03739 | u633914031 | 1562651760 | Python | Python (3.4.3) | py | Runtime Error | 264 | 14332 | 935 | 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)) |
s442277787 | p03739 | u633914031 | 1562651343 | Python | Python (3.4.3) | py | Runtime Error | 259 | 14084 | 929 | 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)) |
s673797089 | p03739 | u416773418 | 1560800292 | Python | Python (3.4.3) | py | Runtime Error | 141 | 14468 | 381 | n=int(input())
a=list(map(int,input().split()))
sm=a[0]
ans=[0, 0]
for i in range(n-1):
sm1=sm+a[i+1]
if sm*sm1>=0:
ans[0]+=abs(sm1)+1
sm//=abs(sm)*(-1)
else:
sm=sm1
sm=-1
ans[1]+=abs(a[0])+1
for i in range(n-1):
sm1=sm+a[i+1]
if sm*sm1>=0:
ans[1]+=abs(sm1)+1
sm//=abs(sm)*(-1)
else:
sm=sm1
print(min(ans)) |
s155523434 | p03739 | u416773418 | 1560800015 | Python | Python (3.4.3) | py | Runtime Error | 89 | 14332 | 207 | n=int(input())
a=list(map(int,input().split()))
sm=a[0]
ans=0
for i in range(n-1):
sm1=sm+a[i+1]
if sm*sm1>=0:
ans+=abs(sm1)+1
sm//=abs(sm)*(-1)
else:
sm=sm1
print(ans) |
s241831298 | p03739 | u463655976 | 1559996395 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3060 | 199 | N = input()
a = map(int, input().split())
def f(x):
cnt = 0
cur = 0
for ai in a:
cur += ai
cur *= x
if cur < 0:
cnt += 1 - ai
x *= -1
print(cnt)
print(min(f(1), f(-1)) |
s895794717 | p03739 | u551909378 | 1558986785 | Python | Python (3.4.3) | py | Runtime Error | 195 | 14468 | 799 | N = int(input())
a = [int(x) for x in input().split()]
A = a[0]
asum = [A]
if A == 0:
ansp,ansm,tempp,tempm = 1,1,1,-1
elif A < 0:
ansp,ansm,tempp,tempm = -A + 1, 0, -A + 1, 0
else:
ansp,ansm,tempp,tempm = 0, A2 + 1, 0, -A2 - 1
for i in range(1,N):
A = asum[i-1] + a[i]
asum += [A]
A1 = A + tempp
if A1 == 0:
ansp += 1
tempp += ((i%2)*2-1)
elif i%2==0 and A1 < 0:
ansp += -A1 + 1
tempp += -A1 + 1
elif i%2!=0 and A1 > 0:
ansp += A1 + 1
tempp += -A1 - 1
A2 = A + tempm
if A2 == 0:
ansm += 1
tempm += -((i%2)*2-1)
elif i%2!=0 and A2 < 0:
ansm += -A2 + 1
tempm += -A2 + 1
elif i%2==0 and A2 > 0:
ansm += A2 + 1
tempm += -A2 - 1
print(min(ansp,ansm)) |
s238424992 | p03739 | u391731808 | 1557620164 | Python | Python (3.4.3) | py | Runtime Error | 128 | 14468 | 404 | N = int(input())
*A, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
S = 1
ans1 = abs(S)+1
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans1 += abs(S1)+1
S1 = -S//abs(S)
S = S1
ans2 = 0
S = A[0]
if S >= 0:
S = -1
ans2 = abn(S)+1
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans2 += abs(S1)+1
S1 = -S//abs(S)
S = S1
print(min(ans1,ans2)) |
s704420737 | p03739 | u391731808 | 1557619726 | Python | Python (3.4.3) | py | Runtime Error | 88 | 14332 | 182 | N = int(input())
*A, = map(int,input().split())
ans = 0
S = A[0]
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans += abs(S1)+1
S1 = -S//abs(S)
S = S1
print(ans) |
s188481262 | p03739 | u059436995 | 1556332758 | Python | Python (3.4.3) | py | Runtime Error | 94 | 14332 | 329 | n = int(input())
A = list(map(int, input().split()))
cnt = 0
w = A[0]
for i in range(n - 1):
nw = w + A[i + 1]
if w > 0:
if nw >= 0:
cnt += nw + 1
chg = -(nw + 1)
else:
if nw <= 0:
cnt += 1 - nw
chg = 1 - nw
w = nw + chg
chg = 0
print(cnt) |
s255774848 | p03739 | u059436995 | 1556331183 | Python | Python (3.4.3) | py | Runtime Error | 96 | 14332 | 335 | n = int(input())
A = list(map(int, input().split()))
cnt = 0
w = A[0]
for i in range(n - 1):
nw = w + A[i + 1]
if w > 0:
if nw >= 0:
cnt += nw + 1
chg = -(nw + 1)
elif w < 0:
if nw <= 0:
cnt += 1 - nw
chg = 1 - nw
w = nw + chg
chg = 0
print(cnt) |
s364009713 | p03739 | u059436995 | 1556327722 | Python | Python (3.4.3) | py | Runtime Error | 706 | 14468 | 678 | n = int(input())
A = list(map(int, input().split()))
def ssa(n):
arr = A
memo =['inf'] * (n + 1)
def _ssa(n):
if n == 0:
return arr[0]
if memo[n] != 'inf':
return memo[n]
memo[n] = _ssa(n -1) + arr[n]
return memo[n]
return _ssa(n)
cnt = 0
chg = 0
for i in range(n - 1):
if ssa(i) + chg > 0:
if ssa(i + 1) + chg >= 0:
cnt += ssa(i + 1) + chg + 1
chg += -(ssa(i + 1)+ chg + 1)
elif ssa(i) + chg < 0:
if ssa(i + 1) + chg <= 0:
cnt += 1 - ssa(i + 1) - chg
chg += 1 - ssa(i + 1) - chg
if ssa(n - 1) + chg == 0:
cnt +=1
print(cnt)
|
s347678689 | p03739 | u893132811 | 1556300502 | Python | Python (3.4.3) | py | Runtime Error | 153 | 14464 | 848 | n = int(input())
A = list(map(int, input().split()))
ans1 = 0
sum = A[0]
for a in A[1:]:
if (sum + a) * sum < 0:
sum += a
else:
fugo = sum // abs(sum)
nextsum = - fugo
#print('fugo={}, nextsum={}'.format(fugo, nextsum))
a_should_be = nextsum - sum
dif = abs(a_should_be - a)
#print('a_shoule_be={}, dif={}'.format(a_should_be, dif))
sum = nextsum
ans1 += dif
#print(ans1)
#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = - (A[0] // abs(A[0]))
for a in A[1:]:
if (sum + a) * sum < 0:
sum += a
else:
fugo = sum // abs(sum)
nextsum = - fugo
a_should_be = nextsum - sum
dif = abs(a_should_be - a)
sum = nextsum
ans2 += dif
#print(ans2)
print(min(ans1, ans2))
|
s890642896 | p03739 | u893132811 | 1556299078 | Python | Python (3.4.3) | py | Runtime Error | 154 | 14468 | 727 | n = int(input())
A = list(map(int, input().split()))
ans1 = 0
sum = A[0]
for a in A[1:]:
if (sum + a) * sum < 0:
sum += a
else:
fugo = sum // abs(sum)
nextsum = - fugo
a_should_be = nextsum - sum
dif = abs(a_should_be - a)
sum += a_should_be
ans1 += dif
#print(ans1)
#A[0]を反転したほうがいいパターンの処理
ans2 = A[0] + 1
sum = - (A[0] // abs(A[0]))
for a in A[1:]:
if (sum + a) * sum < 0:
sum += a
else:
fugo = sum // abs(sum)
nextsum = - fugo
a_should_be = nextsum - sum
dif = abs(a_should_be - a)
sum += a_should_be
ans2 += dif
#print(ans2)
print(min(ans1, ans2))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.