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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

//
// ABC165C
// URL : https://atcoder.jp/contests/abc163/tasks/abc165_c
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace AtCoder
{
    class ABC165C
    {
        public static readonly long MOD_CONST = 1000000007;//(long)Math.Pow(10, 9) + 7;


        static void Main(string[] args)
        {
            //出力高速化
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }
        public static int N = 0;
        public static int M = 0;
        public static int Q = 0;
        public static List<long> AList = null;
        public static List<long> BList = null;
        public static List<long> CList = null;
        public static List<long> DList = null;

        public static long ans = 0;
        public static void Solve()
        {
            // 整数配列の入力
            long[] inputLongArray = Console.ReadLine().Split(' ').Select(_inputStr => long.Parse(_inputStr)).ToArray();
            N = (int)inputLongArray[0];//max 10
            M = (int)inputLongArray[1];//max 10
            Q = (int)inputLongArray[2];//max 50

            AList = new List<long>(Q);
            BList = new List<long>(Q);
            CList = new List<long>(Q);
            DList = new List<long>(Q);
            for (int i = 0; i < Q; ++i)
            {
                long[] x = Console.ReadLine().Split(' ').Select(_inputStr => long.Parse(_inputStr)).ToArray();
                AList.Add(x[0]);
                BList.Add(x[1]);
                CList.Add(x[2]);
                DList.Add(x[3]);
            }

            var ReqList = new List<long>(N);

            ReqList.Add(1);
            dfs(ReqList);
            Console.WriteLine(ans);
        }

        public static void dfs(List<long> ReqList)
        {
            if(ReqList.Count == N + 1)//問題文の配列Aが指定の要素数+1溜まったら終了(+1 => ReqList[0]は使わないから)
            {
                //スコアを計算
                var now = calc(ReqList);
                ans = Math.Max(ans, now);
                return;
            }

            //最後尾に今の最後尾の値を詰める.
            //これでAを条件にある要素数まで増やしていく
            ReqList.Add(ReqList.Last());
            while(ReqList.Last() <= M)//最後尾がAの最大値として指定されたMになるまで
            {
                dfs(new List<long>(ReqList) );//直接渡すと、メソッドの先でReqListが変更されるため、複製を渡す
                //スコアが計算されたら最後尾を+1する
                ReqList[ReqList.Count - 1]++;//最後尾+1
                //var s = string.Join(' ', ReqList);
                //Console.WriteLine(s);
            }
        }
        
        /// <summary>
        /// スコア計算
        /// </summary>
        /// <param name="ReqList"></param>
        /// <returns></returns>
        public static long calc(List<long> ReqList)
        {
            long total = 0;
            for (int i = 0; i < AList.Count; ++i)
            {
                //AList, BListの値はReqListのindex(このindexは0からではなく1から始まる)
                long AbiIdx = BList[i];
                long AaiIdx = AList[i];
                long b = ReqList[(int)AbiIdx];
                long a = ReqList[(int)AaiIdx];
                if (b - a == CList[i])
                {
                    total += DList[i];
                }
            }
            return total;
        }

        //---------------------------------------------------------------------------------

        /// <summary>
        /// 初項a, 公差d, 項数nの数列の和
        /// </summary>
        /// <param name="n">求めたいところ</param>
        /// <returns>初項からxまで足した値</returns>
        static long SumSeries(long a, long d, long n)
        {
            return (2 * a + (n-1) * d) * n / 2;
        }
        /// <summary>
        /// 累積和の配列を計算する
        /// Sum[ a[0], a[i] )がs[i+1]に入っている
        /// s[0]  : 0                //一切和を取らない場合
        /// s[1]  : ls[0]            //a[0]   + s[0] = a[0]
        /// s[2]  : ls[1] + s[1]     //a[1]   + a[0]
        /// ...
        /// s[N]  : ls[N-1] + s[N-1] //a[N-1] + ... + a[0]
        /// s[N+1]: ls[N] + s[N]     //a[N]   + ... + a[0]
        /// 
        /// 例: [1, 7)の和(a[1] + ... + a[6])
        /// s[1] = a[0] //[0, 1)
        /// s[7] = a[0] + a[1] + ... + a[6] //[0, 7)
        /// よってs[7] - s[1]);
        /// </summary>
        /// <param name="ls">計算したい配列</param>
        /// <returns>累積和配列</returns>
        static List<long> AggregateSumList(List<long> a)
        {
            var s = new List<long>(a.Count+1);
            if(a.Count == 0)
            {
                return s;
            }
            s.Add(0);//s[0] = 0
            for(int i = 0; i < a.Count; ++i)
            {
                s.Add(a[i] + s[i]);
            }
            return s;
        }


        /// <summary>
        /// 与えられた数が素数かどうかを計算する
        /// </summary>
        /// <param name="num">対象数</param>
        /// <returns>true:素数, false:素数ではない(合成数)</returns>
        public static bool isPrime(int num)
        {
            if (num < 2) return false;
            else if (num == 2) return true;
            else if (num % 2 == 0) return false;

            double sqrtNum = Math.Sqrt(num);
            for (int i = 3; i <= sqrtNum; i += 2)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 順列mPnを計算する
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static long Permutation(long m, long n)
        {
            long ret = 1;
            for(var i = 0; i < n; ++i)
            {
                ret *= m - i;
            }
            return ret;
        }
        /// <summary>
        /// 組み合わせmCnを計算する
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static long Combination(long m, long n)
        {
            if (m == n || n == 0)
            {
                return 1;
            }
            long ans = 1;
            for (var i = 1; i <= n; ++i)
            {
                ans = ans * (m - i + 1) / i;
            }
            return ans;
        }

        static long Gcd(long m, long n)
        {
            if (n == 0) return m;
            return Gcd(n, m % n);
        }

        static long Lcm(long m, long n)
        {
            var gcd = Gcd(m, n);
            //return (m * n) / gcd;
            return m / gcd * n;//桁あふれ対策
        }
    }
}