import sys

input = sys.stdin.readline

import numpy as np



N = int(eval(input()))

A = [int(eval(input())) for _ in range(N)]



p1 = 0

p2 = 0

P1 = [0]

P2 = [0]

for a in A:

    if a == 0:

        p1 -= 2

        p2 += 1

    elif a % 2 == 0:

        p2 -= 1

    elif a % 2 == 1:

        p1 -= 1

        p2 += 1

    p1 += a

    P1.append(p1)

    P2.append(p2)



dp = [P1[N], P1[N]+P2[N], -P1[N], 0]



for i in reversed(list(range(N))):

    dp[0] = max(dp[0], P1[i])

    dp[1] = max(dp[1], P2[i]+dp[0])

    dp[2] = min(dp[2], P2[i]-dp[1])

    dp[3] = min(dp[3], P1[i]+dp[2])



print((dp[3]+sum(A)))

import sys

input = sys.stdin.readline



N = int(eval(input()))

A = [int(eval(input())) for _ in range(N)]



dp = [[0]*5 for _ in range(N+1)]



for i, a in enumerate(A):

    b = a%2 if a > 0 else 2

    dp[i+1][0] = dp[i][0] + a

    dp[i+1][1] = min(dp[i][1], dp[i][0]) + b

    dp[i+1][2] = min([dp[i][2], dp[i][1], dp[i][0]]) + (a+1)%2

    dp[i+1][3] = min([dp[i][3], dp[i][2], dp[i][1], dp[i][0]]) + b

    dp[i+1][4] = min(dp[i]) + a



ans = min([dp[N][4], dp[N][3], dp[N][2]])

print(ans)
import sys

input = sys.stdin.readline
import numpy as np

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
p1 = 0
p2 = 0
P1 = [0]
P2 = [0]
for a in A:
    if a == 0:
        p1 -= 2
        p2 += 1
    elif a % 2 == 0:
        p2 -= 1
    elif a % 2 == 1:
        p1 -= 1
        p2 += 1
    p1 += a
    P1.append(p1)
    P2.append(p2)
dp = [P1[N], P1[N] + P2[N], -P1[N], 0]
for i in reversed(list(range(N))):
    dp[0] = max(dp[0], P1[i])
    dp[1] = max(dp[1], P2[i] + dp[0])
    dp[2] = min(dp[2], P2[i] - dp[1])
    dp[3] = min(dp[3], P1[i] + dp[2])
print((dp[3] + sum(A)))

import sys

input = sys.stdin.readline
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
dp = [[0] * 5 for _ in range(N + 1)]
for i, a in enumerate(A):
    b = a % 2 if a > 0 else 2
    dp[i + 1][0] = dp[i][0] + a
    dp[i + 1][1] = min(dp[i][1], dp[i][0]) + b
    dp[i + 1][2] = min([dp[i][2], dp[i][1], dp[i][0]]) + (a + 1) % 2
    dp[i + 1][3] = min([dp[i][3], dp[i][2], dp[i][1], dp[i][0]]) + b
    dp[i + 1][4] = min(dp[i]) + a
ans = min([dp[N][4], dp[N][3], dp[N][2]])
print(ans)

-import numpy as np - -p1 = 0 -p2 = 0 -P1 = [0] -P2 = [0] -for a in A: -    if a == 0: -        p1 -= 2 -        p2 += 1 -    elif a % 2 == 0: -        p2 -= 1 -    elif a % 2 == 1: -        p1 -= 1 -        p2 += 1 -    p1 += a -    P1.append(p1) -    P2.append(p2) -dp = [P1[N], P1[N] + P2[N], -P1[N], 0] -for i in reversed(list(range(N))): -    dp[0] = max(dp[0], P1[i]) -    dp[1] = max(dp[1], P2[i] + dp[0]) -    dp[2] = min(dp[2], P2[i] - dp[1]) -    dp[3] = min(dp[3], P1[i] + dp[2]) -print((dp[3] + sum(A))) +dp = [[0] * 5 for _ in range(N + 1)] +for i, a in enumerate(A): +    b = a % 2 if a > 0 else 2 +    dp[i + 1][0] = dp[i][0] + a +    dp[i + 1][1] = min(dp[i][1], dp[i][0]) + b +    dp[i + 1][2] = min([dp[i][2], dp[i][1], dp[i][0]]) + (a + 1) % 2 +    dp[i + 1][3] = min([dp[i][3], dp[i][2], dp[i][1], dp[i][0]]) + b +    dp[i + 1][4] = min(dp[i]) + a +ans = min([dp[N][4], dp[N][3], dp[N][2]]) +print(ans)