let () = Scanf.scanf "%d %d\n0 " @@ fun n t ->
  let ts = Array.init (n - 1) @@ fun _ ->
    Scanf.scanf "%d " @@ fun t -> t in
  Array.fold_left (fun (acc, prev) t -> (t - prev :: acc, t)) ([], 0) ts
  |> fst
  |> List.map (min t)
  |> List.fold_left ( + ) t
  |> Printf.printf "%d\n"

