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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	aa := make([]int, n)
	for i := 0; i < n; i++ {
		aa[i] = getNextInt(scanner)
		if aa[i] == 0 {
			continue
		}
		aa[i] = 2 - aa[i]%2
	}

	dp := make([][5]int, n+1)
	for i := 1; i <= n; i++ {
		for ii := 0; ii < 5; ii++ {
			dp[i][ii] = math.MaxInt32
		}
	}
	for i := 0; i < n; i++ {
		for ii := 0; ii < 5; ii++ {
			if dp[i][ii] == math.MaxInt32 {
				continue
			}
			for iii := 0; iii <= ii; iii++ {
				switch ii {
				case 0:
					dp[i+1][ii] = minint(dp[i+1][ii], dp[i][iii]+aa[i])
				case 1:
					dp[i+1][ii] = minint(dp[i+1][ii], dp[i][iii]+absint(2-aa[i]))
				case 2:
					dp[i+1][ii] = minint(dp[i+1][ii], dp[i][iii]+absint(1-aa[i]))
				case 3:
					dp[i+1][ii] = minint(dp[i+1][ii], dp[i][iii]+absint(2-aa[i]))
				case 4:
					dp[i+1][ii] = minint(dp[i+1][ii], dp[i][iii]+aa[i])
				}
			}
		}
	}
	ans := math.MaxInt32
	for ii := 0; ii < 5; ii++ {
		ans = minint(ans, dp[n][ii])
	}

	fmt.Fprintln(writer, ans)
}

func minint(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func absint(a int) int {
	if a < 0 {
		return -a
	}
	return a
}