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++ (GCC 9.2.1) (judge verdict: Accepted)

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author zjsdut
 */

#ifndef ALGO_UTILS
#define ALGO_UTILS
#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>
template<typename A, typename B>
bool chkmin(A &a, const B &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template<typename A, typename B>
bool chkmax(A &a, const B &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using vl = std::vector<ll>;
using vb = std::vector<bool>;
using vi = std::vector<int>;
using pii = std::pair<int, int>;
using pli = std::pair<ll, int>;
using pil = std::pair<int, ll>;
using pll = std::pair<ll, ll>;
using vpii = std::vector<pii>;
template<typename T>
using vv = std::vector<std::vector<T>>;
#define rng3(i, a, b) for (std::common_type<decltype(a), decltype(b)>::type i = a; i < b; ++i)
#define rng2(i, n) rng3(i, 0, n)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rng(...) GET_MACRO(__VA_ARGS__, rng3, rng2)(__VA_ARGS__)
#define up(i, a, b) for (std::common_type<decltype(a), decltype(b)>::type i = a; i <= b; ++i)
#define down(i, b, a) for (std::common_type<decltype(a), decltype(b)>::type i = b; i >= a; i--)
#define TOKENPASTE(x, y) x##y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define rep(n)                                                                                     \
  for (int TOKENPASTE2(_iter_, __LINE__) = 0, TOKENPASTE2(_num_, __LINE__) = n;                    \
       TOKENPASTE2(_iter_, __LINE__) < TOKENPASTE2(_num_, __LINE__);                               \
       ++TOKENPASTE2(_iter_, __LINE__))
#define FOR(x, cont) for (const auto &x : cont)
#define For(x, cont) for (auto &x : cont)
#define ALL(x) begin(x), end(x)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ep emplace
#define SZ(x) (int) (x).size()
#define UNIQ(vec) (vec).erase(std::unique(all(vec)), std::end(vec))
#define LB(cont, x) int(std::lower_bound(all(cont), x) - std::begin(cont))
#define UB(cont, x) int(std::upper_bound(all(cont), x) - std::begin(cont))
template<typename T, typename U = std::less<T>>
using pq = std::priority_queue<T, std::vector<T>, U>;
template<typename T, typename U>
T ceil(T x, U y) {
  assert(y > 0);
  if (x > 0) x += y - 1;
  return x / y;
}

template<typename T, typename U>
T floor(T x, U y) {
  assert(y > 0);
  if (x < 0) x -= y - 1;
  return x / y;
}
#endif
#ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_
#define JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_
#include <iostream>
#include <iomanip>

struct fast_ios {
  fast_ios() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(10);
  };
} fast_ios_;

template<typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &vec) {
  for (auto &x : vec)
    stream >> x;
  return stream;
}

template<typename T, typename U>
std::istream &operator>>(std::istream &in, std::pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}

void scan() {}

template<class T, class... Args>
void scan(T &a, Args &... rest) {
  std::cin >> a;
  scan(rest...);
}

template<typename T>
std::ostream &operator<<(std::ostream &stream, const std::vector<T> &vec) {
  bool first = true;
  for (const T &t : vec) {
    if (first) first = false;
    else std::cout << ' ';
    std::cout << t;
  }
  return stream;
}

template<typename T, typename U>
std::ostream &operator<<(std::ostream &out, const std::pair<T,U> &p) {
  out << p.first << ' ' << p.second;
  return out;
}

template<typename T>
void print(const std::vector<std::vector<T>> &t) {
  for (const auto &row : t) {
    std::cout << row << '\n';
  }
}

template<typename T>
void print(const T &t) { std::cout << t << ' '; }

template<typename T, typename... Args>
void print(const T &t, const Args &... rest) {
  print(t);
  print(rest...);
}

template<typename T>
void println(const T &t) { std::cout << t << '\n'; }

template<typename T, typename... Args>
void println(const T &t, const Args &... rest) {
  print(t);
  println(rest...);
}
#endif //JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_

#include <functional>
using namespace std;
class CManyRequirements {
public:
  static void solve(istream&, ostream&) {
    int n, m, q;
    scan(n,m,q);
    vi a(q),b(q),c(q),d(q);
    rng (i,0,q) scan(a[i],b[i],c[i],d[i]);

    int best = 0;
    vi A(n+1);
    A[0] = 1;
    function<void(int)> dfs = [&](int i) {
      if (i == n + 1) {
        int sum = 0;
        rng (j, 0, q) {
          if (A[b[j]] - A[a[j]] == c[j])
            sum += d[j];
        }
        chkmax(best, sum);
        return;
      }
      up (j, A[i-1], m) {
        A[i] = j;
        dfs(i+1);
      }
    };
    dfs(1);
    println(best);
//    int f = 1;
//    up (i, 2, 10) f*=i; println(f);
  }
};
/*
 * Make sure that your laptop is plugged in or the build may be slow.
 */

#define in std::cin
#define out std::cout
int main() {
  CManyRequirements solver;
  solver.solve(in, out);
  return 0;
}