let calc = function | [a;_;c;d;] -> max (a+c) (a+d) | [a;_;c;] -> a+c | [a;_;] | [a] -> a | _ -> failwith "unattainable" let rec f3 l n = match List.length l with | 0 -> n | 1 -> n+l.[0] | _ -> let A = calc <| List.truncate 4 l let B = calc <| (List.skip 1 >> List.truncate 3) l let p = if A < B then 1 else 0 let pp = if List.length l < (p+3) then List.length l else p+2 f3 (List.skip (pp) l) (l.[p]+n) stdin.ReadLine() |> ignore let l = stdin.ReadLine().Split([|' '|]) |> Seq.map (int) |> Seq.toList f3 l 0 |> (printfn "%d")