let rec repcomb acc xs n ys =
  match n, ys with
  | 0, _ -> List.rev xs :: acc
  | _, [] -> acc
  | n, y :: ys' -> repcomb (repcomb acc xs n ys') (y :: xs) (n - 1) ys
(* 与えられたリストから重複を許してn要素を選ぶ組み合わせを列挙 *)
let repcomb : int -> 'a list -> 'a list list = fun n xs -> repcomb [] [] n xs

let () = Scanf.scanf "%d %d %d\n" @@ fun n m q ->
  let abcds = Array.init q @@ fun _ ->
    Scanf.scanf "%d %d %d %d\n" @@ fun a b c d -> a, b, c, d in
  Printf.printf "%d\n" @@
  List.fold_left max min_int @@
  List.map (fun as_ ->
    Array.fold_right (fun (a, b, c, d) -> ( + ) @@
      if as_.(b - 1) - as_.(a - 1) = c then d else 0) abcds 0) @@
  List.map Array.of_list @@
  repcomb n @@
  List.init m succ

