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 rec loop i e1 o2 e3 mx =
        if i = l then sum - mx else
            if a.(i)       = 0 then loop (i + 1) 0 0 0 mx else
            if a.(i)       = 1 then loop (i + 1) 0 (a.(i) + max o2 e1) 0 (max mx (a.(i) + max o2 e1)) else
            if a.(i) mod 2 = 1 then (
                let ne1 = a.(i) - 1 + e1        in
                let no2 = a.(i)     + max o2 e1 in
                let ne3 = a.(i) - 1 + max o2 e3 in
                let nmx = max (max ne1 no2) (max ne3 mx) in
                loop (i + 1) ne1 no2 ne3 nmx
            ) else
                let ne1 = a.(i)     + e1        in
                let no2 = a.(i) - 1 + max o2 e1 in
                let ne3 = a.(i)     + max 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"
)