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 Rust (1.15.1) (judge verdict: Runtime Error)

/*------------------------------------------------------------------*/
/*------------------------- begin template -------------------------*/
/*------------------------------------------------------------------*/

// ref: tanakh <https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8>
// diff: using Parser
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut parser = Parser::from_str($s);
        input_inner!{parser, $($r)*}
    };
    (parser = $parser:ident, $($r:tt)*) => {
        input_inner!{$parser, $($r)*}
    };
    (new_stdin_parser = $parser:ident, $($r:tt)*) => {
        let stdin = std::io::stdin();
        let reader = std::io::BufReader::new(stdin.lock());
        let mut $parser = Parser::new(reader);
        input_inner!{$parser, $($r)*}
    };
    ($($r:tt)*) => {
        input!{new_stdin_parser = parser, $($r)*}
    };
}

macro_rules! input_inner {
    ($parser:ident) => {};
    ($parser:ident, ) => {};
    ($parser:ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($parser, $t);
        input_inner!{$parser $($r)*}
    };
}

macro_rules! read_value {
    ($parser:ident, ( $($t:tt),* )) => {
        ( $(read_value!($parser, $t)),* )
    };
    ($parser:ident, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($parser, $t)).collect::<Vec<_>>()
    };
    ($parser:ident, chars) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()
    };
    ($parser:ident, usize1) => {
        read_value!($parser, usize) - 1
    };
    ($parser:ident, $t:ty) => {
        $parser.next::<$t>().expect("Parse error")
    };
}

use std::io;
use std::io::BufRead;
use std::str;

// ref: tatsuya6502 <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e>
// ref: wariuni <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e#comment-7040a5ae96305e884eb9>
// diff: using std::io::BufRead::fill_buf()
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}

impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}

impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
}

fn main() {
    input! {
        new_stdin_parser = parser,
    }
    let res = solve(&mut parser);
    print!("{}", res);
}

/*------------------------------------------------------------------*/
/*-------------------------- end template --------------------------*/
/*------------------------------------------------------------------*/

fn f(xs: &Vec<i64>) -> (i64, i64) {
    let mut sum = xs.iter().sum::<i64>();
    let mut odd_count = 0;
    if xs[0] % 2 == 1 {
        odd_count += 1;
    }
    for i in 1..xs.len() {
        if (xs[i - 1] + xs[i]) % 2 == 1 {
            odd_count += 1;
        }
    }
    if xs[xs.len() - 1] % 2 == 1 {
        odd_count += 1;
    }
    (sum, max(0, odd_count / 2 - 1))
}

use std::cmp::*;
use std::fmt::Write;
fn solve<R: BufRead>(parser: &mut Parser<R>) -> String {
    let mut buf = String::new();
    input! {
        parser = parser,
        n: usize,
        xs: [i64; n],
    }
    let sum = xs.iter().sum::<i64>();
    let mut zss = vec![];
    let mut zs = vec![];
    let mut prev = -1;
    let mut left = 0;
    let mut right = 0;
    for i in 0..n {
        if xs[i] != 0 {
            left = i;
            break;
        }
    }
    for i in (0..n).rev() {
        if xs[i] != 0 {
            right = i;
            break;
        }
    }
    for i in left..right + 1 {
        let x = xs[i];
        // eprintln!("i={}", i);
        if x == 0 {
            if prev != 0 {
                zss.push(zs.clone());
                zs.clear();
                zs = vec![1];
            } else {
                zs[0] += 1;
            }
        } else {
            if prev == 0 {
                zss.push(zs.clone());
                zs.clear();
            }
            zs.push(x);
        }
        prev = x;
    }
    zss.push(zs);
    // eprintln!("{:?}", zss);
    let mut valid_zeros = vec![true; zss.len()];
    {
        let mut left_zero_count = 0;
        let mut left_sum = 0;
        for i in 0..zss.len() {
            if i % 2 == 0 {
                left_sum += zss[i].iter().sum::<i64>();
            } else {
                left_zero_count += zss[i][0];
                if left_zero_count >= left_sum {
                    left_sum = 0;
                    left_zero_count = 0;
                    valid_zeros[i] = false;
                }
            }
        }
    }
    {
        let mut right_zero_count = 0;
        let mut right_sum = 0;
        for i in (0..zss.len()).rev() {
            if i % 2 == 0 {
                right_sum += zss[i].iter().sum::<i64>();
            } else {
                right_zero_count += zss[i][0];
                if right_zero_count >= right_sum {
                    right_sum = 0;
                    right_zero_count = 0;
                    valid_zeros[i] = false;
                }
            }
        }
    }
    // eprintln!("{:?}", valid_zeros);
    let mut yss = vec![];
    let mut ys = vec![];
    let mut pena = 0;
    for i in 0..zss.len() {
        if i % 2 == 0 {
            for &z in &zss[i] {
                ys.push(z);
            }
        } else {
            if valid_zeros[i] {
                for _ in 0..zss[i][0] {
                    ys.push(1);
                }
                pena += zss[i][0];
            } else {
                yss.push((ys.clone(), pena));
                ys.clear();
                pena = 0;
            }
        }
    }
    if !ys.is_empty() {
        yss.push((ys, pena));
    }
    // eprintln!("{:?}", yss);
    let mut res = 1 << 60;
    for y in &yss {
        let ref ys = y.0;
        let ref pena = y.1;
        let (s, cut) = f(ys);
        // eprintln!("s={}, cut={}, pena={}", s, cut, pena);
        res = min(res, sum - s + cut + pena);
    }
    writeln!(buf, "{}", res);
    buf
}