let rec merge xs ys =
  match xs with
  | [] -> ys
  | x :: xs' ->
    match ys with
    | [] -> xs
    | y :: ys' ->
      if x <= y then x :: merge xs' (y :: ys')
      else y :: merge (x :: xs') ys'

;;

let m = merge [1; 4; 7; 10] [2; 3; 5; 8; 9] in
List.fold_left (+) 0 m * List.length m
