let min_path_triangle rows =
  let n = List.length rows in
  let dp = Array.make n 0 in
  let last = List.nth rows (n - 1) in
  let i = ref 0 in
  List.iter (fun x -> dp.(!i) <- x; i := !i + 1) last;
  for r = n - 2 downto 0 do
    let row = Array.of_list (List.nth rows r) in
    for c = 0 to Array.length row - 1 do
      dp.(c) <- row.(c) + (if dp.(c) < dp.(c + 1) then dp.(c) else dp.(c + 1))
    done
  done;
  dp.(0)

;;

min_path_triangle [[2]; [3; 4]; [6; 5; 7]; [4; 1; 8; 3]]
