let daily_temperatures temps =
  let n = Array.length temps in
  let answer = Array.make n 0 in
  let stack = ref [] in
  for i = 0 to n - 1 do
    let cont = ref true in
    while !cont do
      match !stack with
      | top :: rest when temps.(top) < temps.(i) ->
        answer.(top) <- i - top;
        stack := rest
      | _ -> cont := false
    done;
    stack := i :: !stack
  done;
  let sum = ref 0 in
  for i = 0 to n - 1 do
    sum := !sum + answer.(i)
  done;
  !sum

;;

daily_temperatures [| 73; 74; 75; 71; 69; 72; 76; 73 |]
