let rec range a b = if a >= b then [] else a::(range (a+1) b) let rec sum = function [] -> 0 | n::ns -> n + (sum ns) let solve ls = let rec inner ls prev cur must = match ls with | [] -> cur @ must | x::xs -> if cur == [] then inner xs prev [x] must else if x > (List.hd cur) then inner xs cur (x::prev) must else inner xs [] [] cur in inner ls [] [] [] let _ = let _ = int_of_string @@ read_line() in let inputs = String.split_on_char ' ' (read_line ()) in inputs |> List.map (fun s -> int_of_string s) |> solve |> sum |> print_int