Problem:


Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than 0, or a point with coordinate greater than L.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

1 \leq L \leq 2\times 10^5

0 \leq A_i \leq 10^9(1\leq i\leq L)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1



Sample input:
4
1
0
2
3


Sample output:
1

Submission status: Wrong Answer

Solution code:
# D
L = int(input())
A_list = [0]*L
for i in range(L):
    A_list[i] = int(input())

# 0奇
left_min = 0
left = 0
left_min_i_odd = -1
for i in range(L):
    if A_list[i] == 0:
        left -= 1
    else:
        if A_list[i] % 2 == 1:
            left += A_list[i]
        else:
            left += A_list[i] - 1
    if left < left_min:
        left_min = left
        left_min_i_odd = i
        
if left_min_i_odd == -1:
    left_min_odd = 0
else:
    left_min_odd = sum(A_list[:(left_min_i_odd+1)])

# 0偶
left_min = 0
left = 0
left_min_i_even = -1
for i in range(L):
    if A_list[i] == 0:
        left -= 2
    else:
        if A_list[i] % 2 == 0:
            left += A_list[i]
        else:
            left += A_list[i] - 1
    if left < left_min:
        left_min = left
        left_min_i_even = i
        
if left_min_i_even == -1:
    left_min_even = 0
else:
    left_min_even = sum(A_list[:(left_min_i_even+1)])

# 奇0
right_min = 0
right = 0
right_min_i_odd = L
for i in range(L-1, -1, -1):
    if A_list[i] == 0:
        right -= 1
    else:
        if A_list[i] % 2 == 1:
            right += A_list[i]
        else:
            right += A_list[i] - 1
    if right < right_min:
        right_min = right
        right_min_i_odd = i
        
if right_min_i_odd == L:
    right_min_odd = 0
else:
    right_min_odd = sum(A_list[right_min_i_odd:])
        
# 0偶
right_min = 0
right = 0
right_min_i_even = L
for i in range(L-1, -1, -1):
    if A_list[i] == 0:
        right -= 2
    else:
        if A_list[i] % 2 == 0:
            right += A_list[i]
        else:
            right += A_list[i] - 1
    if right < right_min:
        right_min = right
        right_min_i_even = i
        
if right_min_i_even == L:
    right_min_even = 0
else:
    right_min_even = sum(A_list[right_min_i_even:])

# 0奇0
# 0奇偶奇0
# 0奇偶0
# 0偶奇0
# 0偶0

res_list_8 = []

for p in range(4):
    if p == 0:
        left_min = left_min_even
        left_i = left_min_i_even
        right_min = right_min_even
        right_i = right_min_i_even
    elif p == 1:
        left_min = left_min_even
        left_i = left_min_i_even
        right_min = right_min_odd
        right_i = right_min_i_odd
    elif p == 2:
        left_min = left_min_odd
        left_i = left_min_i_odd
        right_min = right_min_odd
        right_i = right_min_i_odd
    else:
        left_min = left_min_odd
        left_i = left_min_i_odd
        right_min = right_min_even
        right_i = right_min_i_even
    
    res1 = left_min + right_min
    left_min = 0
    # default 偶
    for i in range(left_i +1, right_i):
        if A_list[i] == 0:
            res1 += 2
        else:
            res1 += A_list[i] % 2
    left = 0
    right = 0
    left_min = 0
    right_min = 0

    for i in range(left_i +1, right_i):
        if A_list[i] == 0:
            left -= 1
        elif A_list[i] % 2 == 0:
            left += 1
        else:
            left -= 1
        if left < left_min:
            left_min = left
            if i == right_i - 1:
                right_min = 1

    if right_min != 1:
        for i in range(right_i-1, left_i, -1):
            if A_list[i] == 0:
                right -= 1
            elif A_list[i] % 2 == 0:
                right += 1
            else:
                right -= 1
            if right < right_min:
                right_min = right
    else:
        right_min = 0
        
    res1 += (left_min+right_min)

    res_list_8.append(res1)
    
# 0偶0
for p in range(4):
    if p == 0:
        left_min = left_min_even
        left_i = left_min_i_even
        right_min = right_min_even
        right_i = right_min_i_even
    elif p == 1:
        left_min = left_min_even
        left_i = left_min_i_even
        right_min = right_min_odd
        right_i = right_min_i_odd
    elif p == 2:
        left_min = left_min_odd
        left_i = left_min_i_odd
        right_min = right_min_odd
        right_i = right_min_i_odd
    else:
        left_min = left_min_odd
        left_i = left_min_i_odd
        right_min = right_min_even
        right_i = right_min_i_even
    
    res1 = left_min + right_min
    left_min = 0
    # default 奇
    for i in range(left_i +1, right_i):
        res1 += (A_list[i]+1) % 2
    left = 0
    right = 0
    left_min = 0
    right_min = 0

    for i in range(left_i +1, right_i):
        if A_list[i] == 0:
            left += 1
        elif A_list[i] % 2 == 0:
            left -= 1
        else:
            left += 1
        if left < left_min:
            left_min = left
            if i == right_i - 1:
                right_min = 1

    if right_min != 1:
        for i in range(right_i-1, left_i, -1):
            if A_list[i] == 0:
                right += 1
            elif A_list[i] % 2 == 0:
                right -= 1
            else:
                right += 1
            if right < right_min:
                right_min = right
    else:
        right_min = 0

    res1 += (left_min+right_min)

    res_list_8.append(res1)
    


print(min(res_list_8[4:]))

Submission status: Wrong Answer

Solution code:
import sys
import copy
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = copy.deepcopy(a)
b = b[::-1]
c = copy.deepcopy(a)
zero = 0
nonzero = 0
x = 0
y= n
for i in range(n):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            x = i+1
    else:
        nonzero += a[i] - a[i] %2
zero = 0
nonzero = 0
for i in range(n-1,x,-1):
    if a[i] == 0:
        zero += 1
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            y = i
    else:
        nonzero += a[i] - (a[i]%2-1)*(-1)
ans = 0
for i in range(x):
    ans += a[i]
for i in range(y+1,n):
    ans += a[i]
for i in range(x,y):
    if a[i] == 0:
        a[i] = 0
        continue
    a[i] = a[i]%2+1
ki = 0
gu = 0
zero = 0
for i in range(x,y):
    if a[i] == 0:
        zero += 1
    elif a[i] == 1:
        gu += 1
    else:
        ki += 1
res = 1<<60
hki = 0
hgu = 0
hzero = 0
tmp = 0
for i in range(x,y+1):
    tmp = 0
    tmp =gu+zero+hki+hzero*2
    res = min(tmp,res)
    if i == n:
        continue
    if a[i] == 0:
        zero -= 1
        hzero+=1
    elif a[i] == 1:
        gu -= 1
        hgu+=1
    else:
        ki -= 1
        hki+=1    
res = min(tmp,res)
a = b
kotae = ans+res
zero = 0
nonzero = 0
x = 0
y= n
for i in range(n):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            x = i+1
    else:
        zero = 0
        nonzero += a[i] - a[i] %2
zero = 0
nonzero = 0
for i in range(n-1,x,-1):
    if a[i] == 0:
        zero += 1
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            y = i
    else:
        nonzero += a[i] - (a[i]%2-1)*(-1)
ans = 0
for i in range(x):
    ans += a[i]
for i in range(y+1,n):
    ans += a[i]
for i in range(x,y):
    if a[i] == 0:
        a[i] = 0
        continue
    a[i] = a[i]%2+1
ki = 0
gu = 0
zero = 0
for i in range(x,y):
    if a[i] == 0:
        zero += 1
    elif a[i] == 1:
        gu += 1
    else:
        ki += 1
res = 1<<60
hki = 0
hgu = 0
hzero = 0
tmp = 0
for i in range(x,y+1):
    tmp = 0
    tmp =gu+zero+hki+hzero*2
    res = min(tmp,res)
    if i == n:
        continue
    if a[i] == 0:
        zero -= 1
        hzero+=1
    elif a[i] == 1:
        gu -= 1
        hgu+=1
    else:
        ki -= 1
        hki+=1    
res = min(tmp,res)
kotae2 = res+ans

a = c
zero = 0
nonzero = 0
x = 0
y= n
for i in range(n):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            x = i+1
    else:
        zero = 0
        nonzero += a[i] - a[i] %2
zero = 0
nonzero = 0
for i in range(n-1,x,-1):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            y = i
    else:
        nonzero += a[i] - a[i]%2
ans = 0
for i in range(x):
    ans += a[i]
for i in range(y+1,n):
    ans += a[i]
for i in range(x,y):
    if a[i] == 0:
        a[i] = 0
        continue
    a[i] = a[i]%2+1
ki = 0
gu = 0
zero = 0
for i in range(x,y):
    if a[i] == 0:
        zero += 1
    elif a[i] == 1:
        gu += 1
    else:
        ki += 1
res = 1<<60
hki = 0
hgu = 0
hzero = 0
tmp = 0
for i in range(x,y+1):
    tmp = 0
    tmp =ki+zero*2+hki+hzero*2
    res = min(tmp,res)
    if i == n:
        continue
    if a[i] == 0:
        zero -= 1
        hzero+=1
    elif a[i] == 1:
        gu -= 1
        hgu+=1
    else:
        ki -= 1
        hki+=1    
res = min(tmp,res)
print(min(kotae,res+ans,kotae2))

Submission status: Wrong Answer

Solution code:
import math

data = input().split()  
l = int(data[0])
a = []
tmp = []
for i in range(l):
    val = int(input())
    if(val == 0):
        a.append(tmp)
        tmp = []
    else:
        tmp.append(val)
a.append(tmp)

size=[]
for i in a:
    size.append(sum(i))

z=sum(size)-max(size)

ind=size.index(max(size))
sanp=0

hant=False
for i in a[ind]:
    if(i%2 == 1 and hant == True):
        sanp+=1
        hant=False
    elif(i%2 == 1 and hant == False):
        hant=True

print(sanp+z)

Submission status: Wrong Answer

Solution code:
def solve():
  N = int(input())
  A = [int(input()) for i in range(N)]

  count = 0
  fp = 0
  for i in range(N):
    a = A[i]
    if a != 0:
      count += a
      continue

    count -= 1
    if count <= 0:
      count = 0
      fp = i + 1

  count = 0
  ep = N

  for i in range(N - 1, -1, -1):
    a = A[i]
    if a != 0:
      count += a
      continue

    count -= 1
    if count <= 0:
      count = 0
      ep = i

  ans = sum(A[:fp]) + sum(A[ep:])
  f = False
  for i in range(fp, ep):
    if A[i] % 2 == 0:
      if f: return ans + 1
      else: f = True

  return ans

print(solve())

Submission status: Wrong Answer

Solution code:
print(list(map(int,open(0).read().split())).count(0))

Submission status: Wrong Answer

Solution code:
L = int(input())
a_list = [int(input()) for _ in range(L)]

extra = 0
for a in a_list[::-1]:
    if a == 0:
        extra += 1
    else:
        break

prev_sum = 0
skip_cnt = 0
ans = 0
start = False
even_cnt = 0
for a in a_list:
    if a == 0:
        skip_cnt += 1
        if skip_cnt >= prev_sum:
            ans = 0
            ans += prev_sum
            start = False
        else:
            if start:
                ans += 1
    else:
        if a % 2 == 0:
            even_cnt += 1
        prev_sum += a
        start = True
ans -= extra
print(ans + even_cnt - 2 if even_cnt >= 2 else ans)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = []
for i in range(L):
    A.append(int(input()))

sum_A = sum(A)
ans = sum_A + 1
base_sum = 0

best_l = -1
best_r = -1

for l in range(L-1):
    penalty = 0
    base_sum = A[l]
    # l_reverse_gain = 0
    # r_reverse_gain = 0
    # max_l_reverse_gain = 0
    # max_r_reverse_gain = 0
    for r in range(l+1, L):
        # if A[l] % 2 == 0:
        #     if A[r] % 2 == 1:
        #         l_reverse_gain -= 1
        #     else:
        #         l_reverse_gain += 1
        base_sum += A[r]
        untouched_sum = sum_A - base_sum
        if penalty + untouched_sum < ans:
            ans = min(ans, penalty + untouched_sum)
            best_l = l
            best_r = r
        if A[r] % 2 == 0:
            penalty += 1

# print(ans)

return_from_l = [0]
return_from_r = [0]
for i in range(best_l+1, best_r):
    if A[i] % 2 == 1:
        return_from_l.append(return_from_l[len(return_from_l)-1] + 1)
    else:
        return_from_l.append(return_from_l[len(return_from_l)-1] - 1)

for i in range(best_r-1, best_l, -1):
    if A[i] % 2 == 1:
        return_from_r.append(return_from_r[len(return_from_r)-1] + 1)
    else:
        return_from_r.append(return_from_r[len(return_from_r)-1] - 1)

# print(return_from_l)
# print(return_from_r)

return_from_r = list(return_from_r[::-1])

# print(best_l, best_r)

best_return_l = -1
l_cost_change = 0
best_return_r = L+1
r_cost_change = 0
if A[best_l] % 2 == 0:
    for i, cost in enumerate(return_from_l):
        if cost < l_cost_change:
            l_cost_change = cost
            best_return_l = i
if A[best_r] % 2 == 0:
    for i, cost in enumerate(return_from_r):
        if cost < r_cost_change:
            r_cost_change = cost
            best_return_r = i
if best_return_r <= best_return_l:
    cost_change = min(l_cost_change, r_cost_change)
else:
    cost_change = l_cost_change + r_cost_change

ans += cost_change

print(ans)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())

l = [int(input()) for i in range(n)]

s = sum(l)

if s % 2 == 0:
	s1 = s - 1
	s2 = s + 1
else:
	s1 = s
	s2 = s

res1 = abs(s1 - int(round(s1/n))*n)
res2 = abs(s2 - int(round(s2/n))*n)
print(min(res1, res2))

Submission status: Wrong Answer

Solution code:
import sys
import copy
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = copy.deepcopy(a)
b = b[::-1]
c = copy.deepcopy(a)
zero = 0
nonzero = 0
x = 0
y= n
for i in range(n):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            x = i+1
    else:
        nonzero += a[i] - a[i] %2
zero = 0
nonzero = 0
for i in range(n-1,-1,-1):
    if a[i] == 0:
        zero += 1
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            y = i
    else:
        nonzero += a[i] - (a[i]%2-1)*(-1)
ans = 0
for i in range(x):
    ans += a[i]
for i in range(y+1,n):
    ans += a[i]
for i in range(x,y):
    if a[i] == 0:
        a[i] = 0
        continue
    a[i] = a[i]%2+1
ki = 0
gu = 0
zero = 0
for i in range(x,y):
    if a[i] == 0:
        zero += 1
    elif a[i] == 1:
        gu += 1
    else:
        ki += 1
res = 1<<60
hki = 0
hgu = 0
hzero = 0
tmp = 0
for i in range(x,y+1):
    tmp = 0
    tmp =gu+zero+hki+hzero*2
    res = min(tmp,res)
    if i == n:
        continue
    if a[i] == 0:
        zero -= 1
        hzero+=1
    elif a[i] == 1:
        gu -= 1
        hgu+=1
    else:
        ki -= 1
        hki+=1    
res = min(tmp,res)
a = b
kotae = ans+res
zero = 0
nonzero = 0
x = 0
y= n
for i in range(n):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            x = i+1
    else:
        zero = 0
        nonzero += a[i] - a[i] %2
zero = 0
nonzero = 0
for i in range(n-1,-1,-1):
    if a[i] == 0:
        zero += 1
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            y = i
    else:
        nonzero += a[i] - (a[i]%2-1)*(-1)
ans = 0
for i in range(x):
    ans += a[i]
for i in range(y+1,n):
    ans += a[i]
for i in range(x,y):
    if a[i] == 0:
        a[i] = 0
        continue
    a[i] = a[i]%2+1
ki = 0
gu = 0
zero = 0
for i in range(x,y):
    if a[i] == 0:
        zero += 1
    elif a[i] == 1:
        gu += 1
    else:
        ki += 1
res = 1<<60
hki = 0
hgu = 0
hzero = 0
tmp = 0
for i in range(x,y+1):
    tmp = 0
    tmp =gu+zero+hki+hzero*2
    res = min(tmp,res)
    if i == n:
        continue
    if a[i] == 0:
        zero -= 1
        hzero+=1
    elif a[i] == 1:
        gu -= 1
        hgu+=1
    else:
        ki -= 1
        hki+=1    
res = min(tmp,res)
kotae2 = res+ans

a = c
zero = 0
nonzero = 0
x = 0
y= n
for i in range(n):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            x = i+1
    else:
        zero = 0
        nonzero += a[i] - a[i] %2
zero = 0
nonzero = 0
for i in range(n-1,-1,-1):
    if a[i] == 0:
        zero += 2
        if nonzero <= zero:
            zero = 0
            nonzero = 0
            y = i
    else:
        nonzero += a[i] - a[i]%2
ans = 0
for i in range(x):
    ans += a[i]
for i in range(y+1,n):
    ans += a[i]
for i in range(x,y):
    if a[i] == 0:
        a[i] = 0
        continue
    a[i] = a[i]%2+1
ki = 0
gu = 0
zero = 0
for i in range(x,y):
    if a[i] == 0:
        zero += 1
    elif a[i] == 1:
        gu += 1
    else:
        ki += 1
res = 1<<60
hki = 0
hgu = 0
hzero = 0
tmp = 0
for i in range(x,y+1):
    tmp = 0
    tmp =ki+zero*2+hki+hzero*2
    res = min(tmp,res)
    if i == n:
        continue
    if a[i] == 0:
        zero -= 1
        hzero+=1
    elif a[i] == 1:
        gu -= 1
        hgu+=1
    else:
        ki -= 1
        hki+=1    
res = min(tmp,res)
print(min(kotae,res+ans,kotae2))

Submission status: Wrong Answer

Solution code:
L = int(input())
a_list = [int(input()) for _ in range(L)]

extra = 0
for a in a_list[::-1]:
    if a == 0:
        extra += 1
    else:
        break

prev_sum = 0
skip_cnt = 0
ans = 0
start = False
even_cnt = 0
for a in a_list:
    if a == 0:
        skip_cnt += 1
        if skip_cnt >= prev_sum:
            ans = 0
            ans += prev_sum
            start = False
        else:
            if start:
                ans += 1
    else:
        if a % 2 == 0:
            even_cnt += 1
        prev_sum += a
        start = True
ans -= extra
print(ans + 1 if even_cnt >= 2 else ans)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [0 for i in range(L+1)]
B = [0 for i in range(L+1)]

for i in range(L):
	A[i] = int(input())
	B[i] = A[i] % 2

result = 10**10
mem = 0

for i in range(L+1):
	if A[i] == 0:
		culc = sum(A[0:mem]) + sum(A[i:])
		b0 = sum(B[mem:i+1])
		b = b0
		memm = mem
		for j in range(mem,i):
			if B[j]==0:
				b = sum(B[mem:memm])+sum(B[j:i+1])
				b0 = min(b0,b)
				memm = j
		culc = culc + b0
		result = min(result,culc)
		mem = i

print(result)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8

import copy
import numpy as np
import random
import itertools

l = int(input())



a = []
for i in range(l):
    a.append(int(input()))

source = list(range(l+1))

combinations = list(itertools.combinations(source,2))

answer = 10**10
# print(combinations)
for combination in combinations:
    a_tem = copy.deepcopy(a)
    goal = combination[1]
    start = combination[0]
    position = start
    # print(start,goal)
    # while True:
    #     a_tem[position] -= 1
    #     position -= 1
    #     if position == start:
    #         break
    #
    # position = goal
    while True:
        if a_tem[position] >= 2:
            if a_tem[position] % 2 == 1:
                a_tem[position] = 1
            else:
                a_tem[position] = 0
        else:
            if a_tem[position] == 0:
                a_tem[position] = 2

        position += 1
        if position == goal:
            break

    com_source = list(range(goal-start))
    combinations2 = itertools.combinations(com_source,2)
    # print(a_tem)
    if sum(a_tem)-l >= answer:
        pass
    else:
        for combination2 in combinations2:
            count = 0
            a_np = np.array(a_tem)
            # print(a_np)
            start_2 = combination2[0]
            goal_2 = combination2[1]
            start_2 += start
            goal_2 += start
            a_np[start_2:goal_2] -= 1
            # print(a_np)
            for i in range(l):
                if a_np[i] == -1:
                    count += 1
                else:
                    count += a_np[i]
                if count >= answer:
                    break
            answer = min(answer,count)


print(answer)

Submission status: Wrong Answer

Solution code:
L = int(input())
a_list = [int(input()) for _ in range(L)]

extra = 0
for a in a_list[::-1]:
    if a == 0:
        extra += 1
    else:
        break

prev_sum = 0
skip_cnt = 0
ans = 0
start = False
even_cnt = 0
for a in a_list:
    if a == 0:
        skip_cnt += 1
        if skip_cnt >= prev_sum:
            ans = 0
            ans += prev_sum
            prev_sum = 0
            start = False
        else:
            if start:
                ans += 1
    else:
        if a % 2 == 0:
            even_cnt += 1
        prev_sum += a
        start = True
ans -= extra
print(ans if even_cnt >= 2 else ans)

Submission status: Wrong Answer

Solution code:
def calc(A):
    tmp = [not(a%2) for a in A]
    tl = 0
    max_tl = 0
    for tf in tmp:
        if tf:
            tl += 1
        else:
            max_tl = max(max_tl, tl)
            tl = 0
    return sum(tmp), max_tl

L = int(input())
A = [0 for i in range(L)]
ans = 0
max_tl = 0
zs, ze, zl = 0, 0, 0
for i in range(L):
    Ai = int(input())
    A[i] = Ai
    if Ai == 0:
        A[i] = 1
        if zl == 0:
            zs = i
        zl += 1
        continue
    elif zl != 0:
        sub_sum = sum(A[ze:zs])
        if sub_sum > zl:
            tmp_sum, tl = calc(A[ze:zs])
            max_tl = max(max_tl, tl)
            ans += tmp_sum
        else:
            ans += zl
        ze = i
        zl = 0

tmp_sum, tl = calc(A[ze:])
max_tl = max(max_tl, tl)
ans += tmp_sum
print(ans-max_tl)

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = []
evod0 = []
even = [0]
odds = [0]
g0 = []
for i in range(N):
    tmp = int(input())
    arr.append(tmp)
    if tmp%2:
        evod0.append(1)
        even.append(even[-1]+1)
        odds.append(odds[-1])
    elif tmp>0:
        evod0.append(2)
        even.append(even[-1])
        odds.append(odds[-1]+1)
    else:
        evod0.append(0)
        g0.append(i)
        even.append(even[-1]+1)
        odds.append(odds[-1]+2)
ans = sum(arr)
#0を消す
for i in range(N-1):
    tmp = min(even[i+1]+odds[N] - odds[i+1],odds[i+1] + even[N] - even[i+1])
    ans = min(ans,tmp)
tmp = min(even[N],odds[N])
ans = min(ans,tmp)
#0を消さない
ans0 = 0
if g0:
#左を全部0にする
    ans0 += sum(arr[:g0[0]])
    a0min = sum(arr[g0[-1]+1:])
    for i in range(g0[-1]+1,N-1):
        tmp = min(even[i+1] - even[g0[-1]+1] + odds[N] - odds[i+1],odds[i+1] - odds[g0[-1]+1] + even[N] - even[i+1])
        a0min = min(a0min,tmp)
    tmp = min(even[N]-even[g0[-1]+1],odds[N]-odds[g0[-1]+1])
    a0min = min(a0min,tmp)
    ans0 += a0min
#右を全部0に
    right = sum(arr[g0[-1]:])
    rmin = sum(arr[:g0[0]])
    for i in range(0,g0[0]):
        tmp = min(even[i+1]+odds[g0[0]] - odds[i+1],odds[i+1] + even[g0[0]] - even[i+1])
        rmin = min(rmin,tmp)
    tmp = min(even[g0[0]],odds[g0[0]])
    rmin = min(tmp,rmin)
    right += rmin
    print(min(ans,ans0,right))
    exit()
print(ans)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [int(input()) for i in range(L)]

Asum = [0]
for i, a in enumerate(A):
    Asum.append(Asum[i] + a)

Emin = [0 for i in range(L + 1)]
Zmin = [0 for i in range(L + 1)]

for i in range(L)[:: -1]:
    a = A[i]
    if a == 0:
        Emin[i] = 1 + Emin[i + 1]
        Zmin[i] = 1 + min(Emin[i + 1], Zmin[i + 1])
    elif a % 2 == 0:
        Emin[i] = Emin[i + 1]
        Zmin[i] = 1 + min(Emin[i + 1], Zmin[i + 1])
    else:
        Emin[i] = 1 + Emin[i + 1]
        Zmin[i] = min(Emin[i + 1], Zmin[i + 1])
    Emin[i] = min(Emin[i], Asum[L] - Asum[i])
    Zmin[i] = min(Zmin[i], Asum[L] - Asum[i])


A.reverse()

Asum = [0]
for i, a in enumerate(A):
    Asum.append(Asum[i] + a)

Emin2 = [0 for i in range(L + 1)]
Zmin2 = [0 for i in range(L + 1)]

for i in range(L)[:: -1]:
    a = A[i]
    if a == 0:
        Emin2[i] = 1 + Emin2[i + 1]
        Zmin2[i] = 1 + min(Emin2[i + 1], Zmin2[i + 1])
    elif a % 2 == 0:
        Emin2[i] = Emin2[i + 1]
        Zmin2[i] = 1 + min(Emin2[i + 1], Zmin2[i + 1])
    else:
        Emin2[i] = 1 + Emin2[i + 1]
        Zmin2[i] = min(Emin2[i + 1], Zmin2[i + 1])
    Emin2[i] = min(Emin2[i], Asum[L] - Asum[i])
    Zmin2[i] = min(Zmin2[i], Asum[L] - Asum[i])

ans = 3000000
for i in range(L + 1):
    ans = min(ans, min(Emin[i], Zmin[i]) + min(Emin2[L - i], Zmin2[L - i]))

print(ans)

Submission status: Wrong Answer

Solution code:
L = int(input())
a_list = [int(input()) for _ in range(L)]

extra = 0
for a in a_list[::-1]:
    if a == 0:
        extra += 1
    else:
        break

prev_sum = 0
skip_cnt = 0
ans = 0
start = False
even_cnt = 0
for a in a_list:
    if a == 0:
        skip_cnt += 1
        if skip_cnt >= prev_sum:
            ans = 0
            ans += prev_sum
            prev_sum = 0
            start = False
        else:
            if start:
                ans += 1
    else:
        if a % 2 == 0:
            even_cnt += 1
        prev_sum += a
        start = True
ans -= extra
print(ans + 1 if even_cnt >= 2 else ans)

Submission status: Wrong Answer

Solution code:

def main():
    buf = input()
    L = int(buf)
    A = []
    for i in range(L):
        buf = input()
        A.append(int(buf))
    print(min(calc_move_count(A), calc_move_count(list(reversed(A)))))

def calc_move_count(A):
    stone_diff = []
    for i in A:
        if i == 0:
            stone_diff.append(1)
        elif i % 2 == 0:
            stone_diff.append(-1)
        else:
            stone_diff.append(0)
    no_left_sum = []
    for i in reversed(stone_diff):
        if not no_left_sum:
            no_left_sum.append(abs(i))
        else:
            no_left_sum.append(no_left_sum[-1] + abs(i))
    no_left_sum = list(reversed(no_left_sum))
    no_left_sum.append(0)
    move_left = [0]
    for i in A:
        if not move_left:
            move_left.append(i)
        else:
            move_left.append(move_left[-1] + i)
    move_right = []
    for i in reversed(A):
        if not move_right:
            move_right.append(i)
        else:
            move_right.append(move_right[-1] + i)
    move_right = list(reversed(move_right))
    move_right.append(0)
    dp = [[0, no_left_sum[0], move_right[0]]]
    for i in range(len(A)):
        l = []
        l.append(min(move_left[i+1], dp[-1][0]+abs(stone_diff[i]+1)))
        l.append(min(move_left[i+1], dp[-1][0]+abs(stone_diff[i]+1), dp[-1][1]+abs(stone_diff[i])-no_left_sum[i]) + no_left_sum[i+1])
        l.append(min(dp[-1][2], l[1] + move_right[i+1] - no_left_sum[i+1]))
        dp.append(l)
    return dp[-1][2]


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
l=int(input())
a=[]
for k in range(l):
    a.append(int(input()))

print(a)
ans=9999
i=0
j=l-1
for k in range(j-i+1):
    k+=i
    n=0
    for t in range(k-i):
        t+=i
        if a[t] == 0:
            n+=1
    for t in range(j-k+1):
        t+=k
        if not a[t] == 0:
            n+=1
    m=l-n
    if ans>m:
        ans=m
    if ans>n:
        ans=n
print(ans)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8

import copy
import numpy as np
import random
import itertools

l = int(input())



a = []
for i in range(l):
    a.append(int(input()))

source = list(range(l+1))

combinations = list(itertools.combinations(source,2))

answer = 10**10
# print(combinations)
for combination in combinations:
    a_tem = copy.deepcopy(a)
    goal = combination[1]
    start = combination[0]
    position = start
    # print(start,goal)
    # while True:
    #     a_tem[position] -= 1
    #     position -= 1
    #     if position == start:
    #         break
    #
    # position = goal
    while True:
        if a_tem[position] >= 2:
            if a_tem[position] % 2 == 1:
                a_tem[position] = 1
            else:
                a_tem[position] = 0
        else:
            if a_tem[position] == 0:
                a_tem[position] = 2

        position += 1
        if position == goal:
            break

    com_source = list(range(goal-start))
    combinations2 = itertools.combinations(com_source,2)
    for combination2 in combinations2:
        count = 0
        a_np = np.array(a_tem)
        # print(a_np)
        start_2 = combination2[0]
        goal_2 = combination2[1]
        start_2 += start
        goal_2 += start
        a_np[start_2:goal_2] -= 1
        # print(a_np)
        for i in range(l):

            if a_np[i] == -1:
                count += 1
            else:
                count += a_np[i]
            if count >= answer:
                break

        answer = min(answer,count)


print(answer)

Submission status: Wrong Answer

Solution code:
L = int(input())
a_list = [int(input()) for _ in range(L)]

extra = 0
for a in a_list[::-1]:
    if a == 0:
        extra += 1
    else:
        break

prev_sum = 0
skip_cnt = 0
ans = 0
start = False
even_cnt = 0
for a in a_list:
    if a == 0:
        skip_cnt += 1
        if skip_cnt >= prev_sum:
            ans = 0
            ans += prev_sum
            prev_sum = 0
            start = False
        else:
            if start:
                ans += 1
    else:
        if a % 2 == 0:
            even_cnt += 1
        skip_cnt = 0
        prev_sum += a
        start = True
ans -= extra
print(ans + 1 if even_cnt >= 2 else ans)

Submission status: Wrong Answer

Solution code:
l=int(input())
a=[]
for k in range(l):
    a.append(int(input()))

print(a)
ans=9999
i=0
j=l-1
for k in range(j-i+1):
    k+=i
    n=0
    m=0
    for t in range(k-i):
        t+=i
        if a[t] == 0:
            n+=1
        else:
            m+=1
    for t in range(j-k+1):
        t+=k
        if a[t] == 0:
            m+=1
        else:
            n+=1
    if ans>m:
        ans=m
    if ans>n:
        ans=n
print(ans)

Submission status: Wrong Answer

Solution code:
l=int(input())
a=[]
for k in range(l):
    t = int(input())
    if not t == 0:
        a.append(t)
    else: 
        a.append(2)

print(a)
ans=9999
i=0
j=l-1
for k in range(j-i+1):
    k+=i
    n=0
    m=0
    for t in range(k-i):
        t+=i
        if a[t] % 2 == 0:
            n+=1
        else:
            m+=1
    for t in range(j-k+1):
        t+=k
        if a[t] % 2 == 0:
            m+=1
        else:
            n+=1
    if ans>m:
        ans=m
    if ans>n:
        ans=n
print(ans)