let max_nonoverlap intervals =
  let arr = Array.of_list intervals in
  let n = Array.length arr in
  let sorted = Array.make n (0, 0) in
  for i = 0 to n - 1 do
    sorted.(i) <- arr.(i)
  done;
  for i = 0 to n - 1 do
    for j = 0 to n - 2 - i do
      let (_, e1) = sorted.(j) in
      let (_, e2) = sorted.(j + 1) in
      if e1 > e2 then begin
        let t = sorted.(j) in
        sorted.(j) <- sorted.(j + 1);
        sorted.(j + 1) <- t
      end
    done
  done;
  let count = ref 0 in
  let last_end = ref (-1000000) in
  for i = 0 to n - 1 do
    let (s, e) = sorted.(i) in
    if s >= !last_end then begin
      count := !count + 1;
      last_end := e
    end
  done;
  !count
;;

max_nonoverlap [(1, 4); (3, 5); (0, 6); (5, 7); (3, 8); (5, 9); (6, 10); (8, 11); (8, 12); (2, 13); (12, 14)]
