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# (Mono-mcs 6.8.0.105) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace ABC165
{
    public class Solve
    {
        public static void Main(string[] args)
        {
            new ABC_C();
        }
    }
    public class ABC_A
    {
        public ABC_A()
        {
            //飛距離をKの倍数にすること
            var k = Ex.ReadInt;
            var l = Ex.ReadIntArray();
            //飛距離の範囲はA以上B以下
            var a = l[0];
            var b = l[1];
            bool ans = false;

            for (int i = a; i <= b; i++)
            {
                if (i % k == 0)
                {
                    ans = true;
                    break;
                }
            }

            var str = ans ? "OK" : "NG";
            str.Cw();
        }
    }
    public class ABC_B
    {
        public ABC_B()
        {
            var x = Ex.ReadLong;
            long ans = 0;
            long i = 100;


            while (i < x)
            {
                i += (long)Math.Floor(i * .01);
                ans++;
            }



            ans.Cw();
        }
    }
    public class ABC_C
    {
        //A は、長さ N の正整数列である。
        int n;
        //1 ≤ A 1 ≤ A 2 ≤ ⋯ ≤ A N ≤ M
        int m;
        long q;
        List<long[]> arr = new List<long[]>();
        long[] A = new long[11];

        //A b i − A a i = c i を満たすような i についての、 d i の総和 (そのような i が存在しないときは 0 )
        //A の得点の最大値を求めてください。
        long ans = 0;

        public ABC_C()
        {
            //正整数 N , M , Q と、 4 つの整数の組 ( a i , b i , c i , d i ) Q 組
            var l = Ex.ReadLongArray();
            n = (int)l[0];
            m = (int)l[1];
            q = l[2];


            for (int i = 0; i < q; i++)
            {
                var lf = Ex.ReadLongArray();
                arr.Add(lf);
            }

            A[0] = 1;
            dfs(1);
            ans.Cw();
        }

        void dfs(int index)
        {
            if (index > n)
            {
                long now = 0;
                foreach (var v in arr)
                {
                    var a = v[0] - 1;
                    var b = v[1] - 1;
                    var c = v[2];
                    var d = v[3];

                    if ((A[b] - A[a]) == c) now += d;
                }

                ans = Math.Max(ans, now);

                return;
            }

            var num = A[index - 1];
            A[index] = num;
            dfs(index + 1);

            while (num < m)
            {
                num++;
                A[index] = num;
                dfs(index + 1);
            }
        }
    }
    public class ABC_D
    {
        long a;
        long b;
        long n;
        public ABC_D()
        {
            // N以下の非負整数
            // floor(Ax/B)−A×floor(x/B)
            // floor(t)とは、実数t以下の最大の整数
            var l = Ex.ReadLongArray();
            a = l[0];
            b = l[1];
            n = l[2];

            var num = Math.Max(0, Math.Min(b - 1, n));
            var ans = (long)Math.Floor(num * a / (double)b);
            ans.Cw();
        }
    }
    public class ABC_E
    {
        public ABC_E()
        {
        }
    }
    public class ABC_F
    {
        public ABC_F()
        {
        }
    }
    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }

        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static string yesno(this bool b) { return b ? "yes" : "no"; }
        public static string YesNo(this bool b) { return b ? "Yes" : "No"; }
        public static string YESNO(this bool b) { return b ? "YES" : "NO"; }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
    }
}