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 JavaScript (Node.js 12.16.1) (judge verdict: Wrong Answer)

function Main(input) {
	input = input.trim().split("\n").map(function(x) { return x.split(" ")});
	var N = parseInt(input[0][0]);
	var M = parseInt(input[0][1]);
	var Q = parseInt(input[0][2]);
	var ary_abcd = [];
	var tmp = [];
  	var x_max = 0;
	var countX_max = 0;
	var tradeOffList = [];
	var q01ary = [];
	var d = 0;
	var tmpD = 0;
	var vec_A = Array.from({ length: N }, () => undefined); 
	var tmpA;
	for (var i = 1;  i <= Q; i++){
		ary_abcd.push(input[i].map(e => parseInt(e, 10)));
	}
//   console.log( "here");
	for (var i = 1; i < N; i++){
		for (var j = 2; j <= N; j++){
	        countX_max = 0;
			for (var q = 0; q < Q; q++){
				if (ary_abcd[q][0] == i && ary_abcd[q][1] == j){
					tmp.push(ary_abcd[q].slice());
             	    countX_max ++;
             	    if (countX_max === 2){
             	    	tradeOffList.push([tmp.length - 2, tmp.length - 1])
             	    } else if (countX_max === 3){
             	    	tradeOffList[tradeOffList.length - 1].push(tmp.length - 1)
             	    }
				}
			}
          	x_max += Math.min(1, countX_max);
		}
	}
	ary_abcd = tmp.slice(); //ai bi ci diの配列をソート
	
	//数が大きい方から調べる x >= N
	for (var x = N; x <= x_max; x++){
    //   console.log("x",x);
		q01ary = make01Ary(Q, (Q-x), tradeOffList);	
		for (var i = 0; i < q01ary.length; i++){
//		console.log(q01ary.length);
  //              console.log(q01ary[i]);
			tmpD = 0;
          each01check_loop:
			for (var j = 0; j < Q; j++){
                    if (q01ary[i][j] === 0) continue;
                    // console.log("here", q01ary[i][j], j)
					if(vec_A[ary_abcd[j][0] - 1] === undefined && vec_A[ary_abcd[j][1] - 1] === undefined){
						vec_A[ary_abcd[j][0] - 1] = 0;
                        vec_A[ary_abcd[j][1] - 1] = ary_abcd[j][2];
					} else if (vec_A[ary_abcd[j][0] - 1] === undefined) {
						vec_A[ary_abcd[j][0] - 1] = vec_A[ary_abcd[j][1] - 1] - ary_abcd[j][2];
					} else if (vec_A[ary_abcd[j][1] - 1] === undefined) {
						vec_A[ary_abcd[j][1] - 1] = vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2];
					} else {
                        if (vec_A[ary_abcd[j][1] - 1] !== vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2]) { //連立方程式解なし
                            tmpD = 0;
                            // console.log("break");
                            break each01check_loop;	
						} 
                    }
					tmpD +=  	ary_abcd[j][3];
            }
			tmpA = vec_A.filter( function( value ) {
    			return value !== undefined;
                })
            vec_A = Array.from({ length: N }, () => undefined);     
            // console.log(tmpD);
			if ((Math.max.apply(tmpA) - Math.min.apply(tmpA)) < 10 ) {
				d = Math.max(d, tmpD);
// console.log(d, q01ary);
			}
		}
    }
    
    //数が小さい方へx < N
    for (var x = (N - 1); x > 0; x--){
            // console.log("x",x);
        q01ary = make01Ary(Q, (Q-x), tradeOffList);	
            // console.log(q01ary);
		for (var i = 0; i < q01ary.length; i++){
            // console.log(q01ary[i]);
  //              console.log(q01ary[i]);
			tmpD = 0;
          	each01check_loop:
			for (var j = 0; j < Q; j++){
                    // $(vec_A);
                    // $("???");
                    // $(q01ary);
                    // $(i);
                    // $(j);
                    // $(ary_abcd[j]);
                    // console.log(vec_A[ary_abcd[j][0]-1]);
                    // console.log(vec_A[ary_abcd[j][1]-1]);
					if (q01ary[i][j] === 0) continue;
					if(vec_A[ary_abcd[j][0] - 1] === undefined && vec_A[ary_abcd[j][1] - 1] === undefined){
						vec_A[ary_abcd[j][0] - 1] = 0;
                        vec_A[ary_abcd[j][1] - 1] = ary_abcd[j][2];
                        // $(0);
					} else if (vec_A[ary_abcd[j][0] - 1] === undefined) {
                        vec_A[ary_abcd[j][0] - 1] = vec_A[ary_abcd[j][1] - 1] - ary_abcd[j][2];
                        // $(1);
					} else if (vec_A[ary_abcd[j][1] - 1] === undefined) {
                        vec_A[ary_abcd[j][1] - 1] = vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2];
                        // $(2);
					} else {
                        if (vec_A[ary_abcd[j][1] - 1] !== (vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2])) { //連立方程式解なし
                            tmpD = 0;
                            // $(3);
                            // $(vec_A);
                            // $(vec_A[ary_abcd[j][1] - 1]);
                            // $(vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2]);
                            // console.log("break");
							break each01check_loop;	
						} 
					}
					tmpD += ary_abcd[j][3];
            }
            // console.log(tmpD);
			tmpA = vec_A.filter( function( value ) {
    			return value !== undefined;
                })
            vec_A = Array.from({ length: N }, () => undefined);     
			if ((Math.max.apply(tmpA) - Math.min.apply(tmpA)) < 10 ) {
				d = Math.max(d, tmpD);
                            // console.log(d, q01ary[j]);
			}
		}
	}
	console.log(d);  
}   


function compareFunc(a, b) {
	return a - b;
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));


function make01Ary(whole, zero, skip){
	var zeros = Array.from({ length: zero }, () => 0); 
	var ones =  Array.from({ length: (whole - zero) }, () => 1); 
	var v = zeros.concat(ones);
	var ary = []; //配列を入れる配列
  	ary.push(v.slice());
	var tmp;
	var overlapped = 0;
    for (var i = 1; i < (kaijo(whole)/(kaijo(zero)*kaijo(whole-zero))); i++) {
        switch_loop:
        for (var j = 2; j <= whole ; j++){
            for (var k = 1; k < j; k++){
                if (v[whole - j] < v[whole - k]){
                    tmp = v[whole - j];
                    v[whole - j] = v[whole-k];
                    v[whole - k] = tmp;
                    v = v.slice(0, (whole - j + 1)).concat(v.slice(whole - j + 1).sort(compareFunc));
                	for (var ii = 0; ii < skip.length; ii ++){
						overlapped = 0;
                		for (var ij = 0; ij < skip[ii].length; ij++){
                			overlapped += v[skip[ii][ij]];
                            if (overlapped > 1) {
	                      //console.log("here", i);
   		                  break switch_loop;
                  		  }
                		}
                	}
//                  	console.log(overlapped, v);
                    ary.push(v.slice());
                    break switch_loop; 
                }
            }
        }
    }
    return(ary);
}   

function kaijo(num){
    var ans = 1;
    for (var i = 2; i <= num; i++){
        ans *= i;
    } 
    return ans;    
}


function $(text, tmpStyle){
    var str = String(text);
    tmpStyle = (typeof  tmpStyle !== 'undefined') ?  tmpStyle : 0
    if (tmpStyle ===0){
        console.log("////", str);
    }
    if (tmpStyle ===1){
        var l = str.length;
        var lines = Array (l + 1 + 10).join('#');
        var spaces = Array (5).join(' ');
        console.log( lines);
        console.log( "%s%s%s%s%s", '#', spaces, str, spaces, '#');
        console.log( lines);
    }
}