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:
n=int(input())
A=[int(input()) for i in range(n)]
s=0

Z=[]
R=[]
z=0
r=0
for a in A:
  if a==0:
    z+=1
  r+=a
  Z.append(z)
  R.append(r)

for i in range(n-1,-1,-1):
  if Z[i]>=R[i]:
    s+=sum(A[:i+1])
    A=A[i+1:]
    
    break

if A==[]:
  print(s)
  exit()
  
Z=[]
R=[]
z=0
r=0
for a in A[::-1]:
  if a==0:
    z+=1
  r+=a
  Z.append(z)
  R.append(r)
Z=Z[::-1]
R=R[::-1]

for i in range(len(A)):
  if Z[i]>=R[i]:
    s+=sum(A[i:])
    A=A[:i]
    break

if A==[]:
  print(s)
  exit()

for i in range(len(A)):
  if A[i]==0:
    s+=1
    A[i]=1
s0=s

O=[]
E=[]
o=0
e=0
for a in A:
  if a%2==0:
    e+=1
  else:
    o+=1
  E.append(e)
  O.append(o)
l=0
f=0

for i in range(len(A)-1,-1,-1):
  if E[i]>O[i] and f==1:
    s+=O[i]
    A=A[i+1:]

    break
  if A[i]==1:
    f=1
O=[]
E=[]
o=0
e=0
for a in A[::-1]:
  if a%2==0:
    e+=1
  else:
    o+=1
  E.append(e)
  O.append(o)
l=0
f=0
E=E[::-1]
O=O[::-1]
for i in range(len(A)):
  if E[i]>O[i]:
    s+=O[i]
    A=A[i+1:]

    break


O=[]
E=[]
o=0
e=0
for a in A[::-1]:
  if a%2==0:
    e+=1
  else:
    o+=1
  E.append(e)
  O.append(o)
l=0
f=0
E=E[::-1]
O=O[::-1]
for i in range(len(A)):
  if E[i]>O[i]:
    s0+=O[i]
    A=A[i+1:]

    break
O=[]
E=[]
o=0
e=0
for a in A:
  if a%2==0:
    e+=1
  else:
    o+=1
  E.append(e)
  O.append(o)
l=0
f=0

for i in range(len(A)-1,-1,-1):
  if E[i]>O[i]:
    s0+=O[i]
    A=A[i+1:]

    break

print(min(s,s0))

Submission status: Wrong Answer

Solution code:
l = int(input())
a = []

for _ in range(l):
    a.append(int(input()))
b = -1
remaina = a

sum = a.count(0)
for i in range(a.count(0)):
    subl = remaina[b+1:remaina.index(0)]
    b = remaina.index(0)
    remaina = remaina[b+1:len(remaina)]
    if(len(subl)>0):
        while(not(subl[0]%2==0 and subl[len(subl)-1]%2==0)):
            if(subl[0]%2==1):
                subl = subl[1:len(subl)]
            if (len(subl) == 0):
                subl = [1]
            if(subl[len(subl)-1]%2==1):
                subl = subl[0:-1]
            if(len(subl)==0):
                subl = [0]
        sum += len([i for i in subl if i % 2 == 0]) - 1

sum += len([i for i in remaina if i % 2 == 0]) - 1

if(a[len(a)-1]==0):
    sum = sum -1

print(sum)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 22:18:08 2019

@author: maezawa
"""

l = int(input())
a = []
for _ in range(l):
    a.append(int(input()))
    
cnteven = 0
cntgroup = 0
cntzero = 0
cntzeros = 0
cntop = 0
for i in range(l):
    if a[i] != 0:
        cntgroup += a[i]
        cntzero = 0
        if a[i] % 2 == 0:
            cnteven += 1
    else:
        cntzero += 1
        cntzeros += 1
        if cntgroup <= cntzero:
            cntop = cntgroup
            cnteven = 0
if a[-1] == 0:
    cntzeros -=1
if cnteven >=2:
    ans1 = cnteven-2 + cntop + cntzeros
else:
    ans1 = cntop + cntzeros

cnteven = 0
cntgroup = 0
cntzero = 0
cntzeros = 0
cntop = 0
for i in reversed(range(l)):
    if a[i] != 0:
        cntgroup += a[i]
        cntzero = 0
        if a[i] % 2 == 0:
            cnteven += 1
    else:
        cntzero += 1
        cntzeros += 1
        if cntgroup <= cntzero:
            cntop = cntgroup
            cnteven = 0
if a[0] == 0:
    cntzeros -= 1
if cnteven >= 2:
    ans2 = cnteven-2 + cntop + cntzeros
else:
    ans2 = cntop + cntzeros

print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

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

#前処理
for i in range(l):
    a[i] = a[i] % 2

num = 0
for i in range(l):
    if a[i] == 1:
        num += 1
    num = num << 1

#ビット演算
min_ans = 2 ** 30
for i in range(l+1):
    for j in range(l+1):
        if i > j:
            p = int(2**(i-j+1))
            fl = ((p-1) << (j))
            ans = bin(num ^ fl).count('1')

            if ans < min_ans:
                min_ans = ans

print(min_ans)

Submission status: Wrong Answer

Solution code:
l=int(input())
a=[int(input()) for x in range(l)]
reduce=[-1*x + (x+1)%2 for x in a]
odds=[(x+1)%2 for x in a]
odds[0]=0
odds[l-1]=0
def searchleft(idx,reduce):
    if idx==0:return 0,idx
    nowidx=idx-1
    nowsum=reduce[nowidx]
    minsum=reduce[nowidx]
    minidx=nowidx
    if nowsum>0:return 0,idx
    while nowidx>0:
        nowidx-=1
        nowsum+=reduce[nowidx]
        if nowsum<minsum:
            minsum=nowsum
            minidx=nowidx
    return minsum,max(0,minidx)
def searchright(idx,reduce):
    if idx==l-1:return 0,idx
    nowidx=idx+1
    nowsum=reduce[nowidx]
    minsum=reduce[nowidx]
    minidx=nowidx
    if nowsum>0:return 0,idx
    while nowidx<l-1:
        nowidx+=1
        nowsum+=reduce[nowidx]
        if nowsum<=minsum:
            minsum=nowsum
            minidx=nowidx
    return minsum,min(l-1,minidx)

def calc(i,j,a):
    asum=a[:i]
    if j<l-1:asum+=a[j+1:]
    sans=sum(asum)
    for num in range(i+1,j):
        sans+=odds[num]
    return sans
jiku=max(a)
roop=a.count(jiku)
before=0
data=dict()
for i in range(roop):
    idx=a[before:].index(jiku)
    before=idx+1
    A,B=searchleft(idx,reduce)
    C,D=searchright(idx,reduce)
    data[(B,D)]=A+C

minsum=min(data.values())
ans=l
for k in data.keys():
    if data[k]==minsum:
        sans=calc(k[0],k[1],a)
        if ans>sans:ans=sans
print(ans)

Submission status: Wrong Answer

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
L = int(input())
A = [int(input()) for _ in  range(L)]


fromEnd = []

i = 0
while i < L:
    if A[i] != 0:
        start = i
        while i < L and A[i] != 0:
            i += 1
        end = i-1 if i != start else start
        fromEnd.append((start, end, sum(A[start:end+1])))
    i += 1


# start forward

def getAns(start,end):
    ans = float("inf")
    mode = 2 % 2
    sum_forward = sum(A)

    for i in range(start, L):
        sum_forward = min(sum_forward, ans)
        if   A[i] % 2 ==  1 and mode == A[i] % 2:
            sum_forward -= A[i]
        elif A[i] % 2 ==  1 and mode != A[i] % 2:
            mode = A[i] % 2
            sum_forward -= A[i]
        elif A[i] == 0:
            sum_forward += 1
            mode = -1

        elif A[i] % 2 == 0 and mode == A[i] % 2 :
            sum_forward -= A[i]

        elif A[i] % 2 == 0 and mode != A[i] % 2 :
            sum_forward -= A[i] - 1
            mode = A[i] % 2 
        
        if i == L-1 and A[i] %2 == 0 and mode != A[i] %2:
            sum_forward -= 1

    ans = sum_forward
    mode = 2 % 2
    sum_backward = sum(A)


    for i in range(end, -1, -1):
        ans = min(sum_backward, ans)
        if   A[i] % 2 ==  1 and mode == A[i] % 2:
            sum_backward -= A[i]
        elif A[i] % 2 ==  1 and mode != A[i] % 2:
            mode = A[i] % 2
            sum_backward -= A[i]

        elif A[i] == 0:
            sum_backward += 1
            mode = -1

        elif A[i] % 2 == 0 and mode == A[i] % 2 :
            sum_backward -= A[i]
        elif A[i] % 2 == 0 and mode != A[i] % 2 :
            sum_backward -= A[i] - 1
            mode = A[i] % 2 
        if i == 0 and A[i] %2 == 0 and mode != A[i] %2:
            sum_backward -= 1

    ans = min(sum_backward, sum_forward)
    # print(ans)
    return ans

ans = float("inf")    
for t in fromEnd:
    start,end , _ = t
    ans = min(getAns(start,end), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
L = int(input())
A = []
for i in range(L):
    A.append(int(input()))
    
A = np.array(A)
m = A.argmax()
re = 0
for i in range(m, L):
    if i == L -1:
        if A[i] % 2 == 1:
            re += 1
    elif A[i] == 0:
        re += sum(A[i:L])
        if A[i-1] % 2 == 1:
            re += 1
            
for i in range(m-1, -1, -1):
    if i == 0:
        if A[i] % 2 == 1:
            re += 1
    elif A[i] == 0:
        re += sum(A[0:i])
        if A[i+1] % 2 == 1:
            re += 1
        break

print(re)

Submission status: Wrong Answer

Solution code:
L = int(input())
stones_count = []
for i in range(0, L):
    A = int(input())
    if A % 2 == 1:
        stones_count.append(1)
    elif A == 0:
        stones_count.append(0)
    else:
        stones_count.append(2)

sub_count_1 = []
sub_count_2 = []
count_1 = 0
count_2 = 0
for i in range(0, L):
    if stones_count[i] == 0:
        if count_1 != 0:
            sub_count_1.append(count_1)
        if count_2 != 0:
            sub_count_2.append(count_2)
        count_1 = 0
        count_2 = 0
    elif stones_count[i] == 1:
        if count_2 != 0:
            sub_count_2.append(count_2)
        count_2 = 0
        count_1 += 1
    else:
        count_1 += 1
        count_2 += 1

if count_1 != 0:
    sub_count_1.append(count_1)
if count_2 != 0:
    sub_count_2.append(count_2)

print(len(sub_count_1) + len(sub_count_2) - 2)

Submission status: Wrong Answer

Solution code:
# 600 points
import copy

# -*- coding: utf-8 -*-
L = int(input())
b_row = []

res_min = 0

an = 0

for _ in range(L):
    b_row.append(int(input()))

for k in range(L - 1):
    for j in range(k, L):

        row = copy.copy(b_row)

        if j == k:

            an = sum(row) - row[j]

        elif j == k + 1:

            an = sum(row) - row[k] - row[j] + row[k] % 2

        else:
            an += (row[j-1] + 1) % 2 - row[j]

        if k == 0 and j == 1:
            res_min = an
        else:
            if an < res_min:
                res_min = an

print(res_min)

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)
d = copy.deepcopy(a)
e = 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)
kotae3 = res + ans



a = d
kotae = ans+res
zero = 0
nonzero = 0
x = 0
y= n
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)
zero = 0
nonzero = 0
for i in range(y):
    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

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)
kotae4 = res+ans

a = e
zero = 0
nonzero = 0
x = 0
y= n
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)
zero = 0
nonzero = 0
for i in range(y):
    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
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)
kotae5 = res+ans
print(min(kotae,kotae2,kotae3,kotae4,kotae5))

Submission status: Wrong Answer

Solution code:
L = int(input())

#ゼロで区切られた島ごとに記録
S = []
D = []
R = []

s = 0
d = 1
r = 0
last_a = 0
for i in range(L):
  a = int(input())
  if (0 == a):
    if (0 < s) :
      S.append(s)
      D.append(d)
      R.append(r)
      s = 0
      d = 1
      r = 0
    else:
      d += 1
  else:
    s += a
    r += (a % 2)
    last_a = a

if (0 < s):
  S.append(s)
  R.append(r)
  D.append(d)

if (0 < last_a):
  if ((last_a % 2) == 0):
    if (2 <= R[-1]):
      R[-1] -= 2
#print(S,R,D)

res = 0
mode = 0
for (s, d, r) in zip(S, D, R):
  if (0 == mode):
    if (s <= d):
      mode = 1
    res += r
  else:
    res += s

res_r = 0
mode = 0
for (s, d, r) in zip(reversed(S), reversed(D), reversed(R)):
  if (0 == mode):
    if (s <= d):
      mode = 1
    res_r += r
  else:
    res_r += s

print(min(res, res_r))

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [int(input()) for _ in range(L)]
B = A[::-1]

left_min_1 = [0 for _ in range(L+1)]
right_min_1 = [0 for _ in range(L+1)]
for i in range(L+1):
    # 左が一方方向系
    if i > 1:
        left_min_1[i] = left_min_1[i-1] + (A[i-1]+1) % 2
for i in range(L+1)[::-1]:
    if i < L:
        if A[i] != 0:
            right_min_1[i] = right_min_1[i+1] + (A[i]) % 2
        else:
            right_min_1[i] = min(right_min_1[i+1] + 2,sum(A[i+1:]))

total_1 = [left + right for left, right in zip(left_min_1, right_min_1)]
#print(left_min_1)
#print(right_min_1)
#print("total_1")
#print(total_1)
min_1 = min(total_1)

left_min_2 = [0 for _ in range(L+1)]
right_min_2 = [0 for _ in range(L+1)]
for i in range(L+1):
    # 左が一方方向系
    if i > 1:
        left_min_2[i] = left_min_2[i-1] + (B[i-1]+1) % 2
for i in range(L+1)[::-1]:
    if i < L-1:
        if B[i] != 0:
            right_min_2[i] = right_min_2[i+1] + (B[i]) % 2
        else:
            right_min_2[i] = min(right_min_2[i+1] + 2,sum(B[i+1:]))
total_2 = [left + right for left, right in zip(left_min_2, right_min_2)]
min_2 = min(total_2)
print(min(min_1, min_2))

Submission status: Wrong Answer

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

t=0
for i in range(l):

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

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))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [0] * n
for i in range(0,n):
    A[i] = int(input())
ans = 0
res  =0
for i in A:
    if i>res:
        ans += i-res
    res = 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
prev_even = False

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 and start:
            prev_even = True
        else:
            if prev_even:
                even_cnt += 1
            prev_even = False
        prev_sum += a
        start = True
ans -= extra
print(ans + even_cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(L, A):
    c = 0
    for a in A:
        if a == 0:
            c += 1
        else:
            break
    A = A[c:]

    c = 0
    for i in range(len(A)-1, 0, -1):
        a = A[i]
        if a == 0:
            c += 1
        else:
            break
    if c != 0:
        A = A[:-c]

    B = []
    S = []
    f = True
    for a in A:
        if a == 0 and f:
            S[-1] += 1
        elif a == 0 and not f:
            f = True
            S.append(1)
        elif a != 0 and f:
            f = False
            B.append([a])
        else:
            B[-1].append(a)
        

    S.append(0)
    

    C = []
    D = []
    E = []
    for b in B:
        c = 0
        d = 0
        e = []
        for b_ in b:
            c += (b_ + 1) % 2
            d += b_
            e.append(b_ % 2)
        
        o = sum(e)
        even = len(e) - o
        for i in range(len(e)):
            if e[i] != 1:
                break
            o -= 1
        for i in range(len(e)-1, -1, -1):
            if e[i] != 1:
                break
            o -= 1
        # print(o, even)
        E.append(min(o, max(0, even-1)))
        C.append(c)
        D.append(d)
        
    # print(A)
    # print(B)
    # print(S)
    # print(C)
    # print(D)
    # print(E)


    ans =  0
    CS = sum(C)
    # SS = sum(S)
    for i in range(len(C)):
        error_print('+')
        if D[i] - CS > S[i]:
            if S[i] == 0:
                ans += E[i]
            
            else:
                ans += C[i]
                ans += S[i]
            
        else:
            error_print('-')
            ans += D[i]
        CS -= C[i]
    return ans


def main():
    L = read_int()
    A = [read_int() for _ in range(L)]
    print(slv(L, A))


if __name__ == '__main__':
    main()