Problem:
Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1

Solution in Rust (1.42.0) (judge verdict: Accepted)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
//        let mut s = {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// x ** n % mods を求める
pub fn mod_pow(x: i64, n: i64, mods: i64) -> i64 {
    if n == 0 {
        return 1;
    }
    let mut res = mod_pow(x * x % mods, n / 2, mods);
    if n % 2 == 1 {
        res = res * x % mods;
    }
    return res;
}

// (1/n) % mods を求める
pub fn mod_inv(n: i64, mods: i64) -> i64 {
    return mod_pow(n, mods-2, mods);
}

// nCk % mods を求める
pub fn mod_comb(n: i64, k: i64, mods: i64) -> i64 {
    let sum1 = (n-k+1..n+1).fold(1, |ac, x| ac * x % mods);
    let sum2 = (1..k+1).fold(1, |ac, x| ac * x % mods);
    return sum1 * mod_inv(sum2, mods) % mods;
}

fn combination(n: usize, k: usize) -> Vec<Vec<usize>> {
    let mut ans = Vec::new();
    if k == 1 {
        for i in 1..n+1 {
            let mut tmp = Vec::new();
            tmp.push(i);
            ans.push(tmp);
        }
//        println!("{:?}, {}, {}", ans, n, k);
        ans
    }
    else {
        ans = combination(n-1, k-1);
        let mut newans = Vec::new();
        for iter in ans {
            let max = iter.iter().max().unwrap();
            for j in *max+1..n+1 {
                let mut newtmp = iter.clone();
                newtmp.push(j);
                newans.push(newtmp);
            }
        }
//        println!("{:?}, {}, {}", newans, n, k);
        newans
    }
}

use std::cmp;

fn factorial(a: usize) -> usize {
    let mut m = a;
    let mut ans = 1;
    while m > 1 {
        ans *= m;
        m -= 1;
    };
    ans
}
 
fn multi_choose(size: usize, max_value: usize) -> Vec<Vec<usize>> {
    let len = factorial(size + max_value - 1) / factorial(size) / factorial(max_value - 1);
    let mut ans = Vec::new();
    ans.push(vec![max_value; size]);
    let mut hold = vec![max_value; size];
    for _ in 0..len-1 {
        let mut v = 0;
        loop {
            hold[v] -= 1;
            if hold[v] == 0 {
                hold[v] = max_value;
                v += 1;
                continue;
            }
            break;
        }
        for i in 1..size {
            hold[size - i - 1] = cmp::min(hold[size - i - 1], hold[size - i]);
        }
        ans.push(hold.to_vec());
    }
    ans
}

//use combinations::Combinations;

fn main() {
    input!{
        n: usize,
        m: usize,
        q: usize,
        sabcd: [[usize; 4]; q],
    }
    //    let it: Vec<_> = Combinations::new((1..n+m).collect(), n).collect();
    let it = combination(n+m-1, n);
//    println!("{:?}", combination(4, 3));
//    let it = multi_choose(n, m);
    let mut max = 0;
    let mut tmp;
   let mut a;
   // for iter in it.iter() {
   //     println!("{:?}", it);
    for iter in it {
        a = vec![0; n];
        a[0] = iter[0];
        for i in 1..n {
            a[i] = a[i-1] + iter[i] - iter[i-1] - 1; 
        }
        tmp = 0;
        for j in 0..q {
            if a[sabcd[j][1]-1] - a[sabcd[j][0]-1] == sabcd[j][2] {
//            if iter[sabcd[j][1]-1] - iter[sabcd[j][0]-1] == sabcd[j][2] {
                tmp = tmp + sabcd[j][3];
            }
        }
        if tmp > max {
            max = tmp;
        }
    }
    println!("{}", max);
}