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

Solution in PyPy3 (2.4.0) (judge verdict: Wrong Answer)

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):
    tmp = 0
    tmp =gu+zero+hki+hzero*2
    res = min(tmp,res)
    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):
    tmp = 0
    tmp =gu+zero+hki+hzero*2
    res = min(tmp,res)
    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 += 1
        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):
    tmp = 0
    tmp =ki+zero*2+hki+hzero*2
    res = min(tmp,res)
    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))