Scanf.scanf "%d" (fun l ->
    let a = Array.init l (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let sum = Array.fold_left (+) 0 a in

    let m3 a b c = max a (max b c) in

    let rec loop i e1 o2 e3 mx =
        if i = l then sum - mx else
            let ne1 =
                if a.(i) = 0       then max 0 (e1 - 2) else
                if a.(i) = 1       then max 0 (e1 - 1) else
                if a.(i) mod 2 = 1 then a.(i) - 1 + e1 else
                                        a.(i)     + e1
            in
            let no2 =
                if a.(i) = 0       then max 0  (max o2 e1 - 1) else
                if a.(i) mod 2 = 1 then a.(i) + max o2 e1      else
                                        a.(i) + max o2 e1 - 1
            in
            let ne3 =
                if a.(i) = 0       then max 0  (m3 e1 o2 e3 - 2) else
                if a.(i) = 1       then max 0  (m3 e1 o2 e3 - 1) else
                if a.(i) mod 2 = 1 then a.(i) + m3 e1 o2 e3 - 1  else
                                        a.(i) + m3 e1 o2 e3
            in
            let nmx = max (max ne1 no2) (max ne3 mx) in
            loop (i + 1) ne1 no2 ne3 nmx
    in
    loop 0 0 0 0 0 |> Printf.printf "%d\n"
)