Scanf.scanf "%d %d" (fun n t ->
    let rec loop i prev acc =
        if i = n then acc + t else
            let ti = Scanf.scanf " %d" (fun t -> t) in
            let acc = if prev + t <= ti then acc + t else acc + ti - prev in
            loop (i + 1) ti acc
    in
    loop 0 0 0 |> Printf.printf "%d\n"
)