Buggy python submission (verdict: Runtime Error):
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] and f==1:
    s0+=O[i]
    A=A[i+1:]

  if A[i]==1:
    f=1
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)) 


Fixed submission:
n=int(input())
zot=lambda x:0 if x==0 else (x+1)%2+1
A=[int(input()) for i in range(n)]
D=[0,0,0,0,0]

for a in A:
  d0 = D[0] + a
  d1 = min(D[:2])+ [2,1,0][zot(a)]
  d2 = min(D[:3])+ [1,0,1][zot(a)]
  d3 = min(D[:4])+ [2,1,0][zot(a)]
  d4 = min(D) + a
  D=[d0,d1,d2,d3,d4]
print(min(D))

Buggy python submission (verdict: Runtime Error):
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] and f==1:
    s0+=O[i]
    A=A[i+1:]

    break
  if A[i]==1:
    f=1
print(min(s,s0)) 


Fixed submission:
n=int(input())
zot=lambda x:0 if x==0 else (x+1)%2+1
A=[int(input()) for i in range(n)]
D=[0,0,0,0,0]

for a in A:
  d0 = D[0] + a
  d1 = min(D[:2])+ [2,1,0][zot(a)]
  d2 = min(D[:3])+ [1,0,1][zot(a)]
  d3 = min(D[:4])+ [2,1,0][zot(a)]
  d4 = min(D) + a
  D=[d0,d1,d2,d3,d4]
print(min(D))

Buggy python submission (verdict: Runtime Error):
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(n):
  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] and f==1:
    s0+=O[i]
    A=A[i+1:]

    break
  if A[i]==1:
    f=1
print(min(s,s0)) 

Fixed submission:
n=int(input())
zot=lambda x:0 if x==0 else (x+1)%2+1
A=[int(input()) for i in range(n)]
D=[0,0,0,0,0]

for a in A:
  d0 = D[0] + a
  d1 = min(D[:2])+ [2,1,0][zot(a)]
  d2 = min(D[:3])+ [1,0,1][zot(a)]
  d3 = min(D[:4])+ [2,1,0][zot(a)]
  d4 = min(D) + a
  D=[d0,d1,d2,d3,d4]
print(min(D))

Buggy python submission (verdict: Runtime Error):
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


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(n):
  if Z[i]>=R[i]:
    s+=sum(A[i:])
    A=A[:i]
    break

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] and f==1:
    s0+=O[i]
    A=A[i+1:]

    break
  if A[i]==1:
    f=1
print(min(s,s0))  

Fixed submission:
n=int(input())
zot=lambda x:0 if x==0 else (x+1)%2+1
A=[int(input()) for i in range(n)]
D=[0,0,0,0,0]

for a in A:
  d0 = D[0] + a
  d1 = min(D[:2])+ [2,1,0][zot(a)]
  d2 = min(D[:3])+ [1,0,1][zot(a)]
  d3 = min(D[:4])+ [2,1,0][zot(a)]
  d4 = min(D) + a
  D=[d0,d1,d2,d3,d4]
print(min(D))