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 (Python):
n,m,q = map(int,input().split())
g = []
for i in range(q):
  a,b,c,d = map(int,input().split())
  g.append([a,b,c,d])
MAX = 0
def check(x):
  ten = 0
  for i in range(q):
    a,b,c,d = g[i]
    if x[b-1]-x[a-1] == c:
      ten += d
  return ten
def dfs(s):
  global MAX
  if len(s) == n:
    MAX = max(check(s),MAX)
    return
  for i in range(s[-1],m+1):
    s.append(i)
    dfs(s)
    s.pop()
for i in range(1,m+1):
  dfs([i])
print(MAX)

Solution (Python):
n, m, q = map(int, input().split())
req = []
[req.append(tuple(map(int, input().split()))) for _ in range(q)]
a = [0 for _ in range(n)]

def check():
    res = 0
    for i in range(q):
        ra, rb, rc, rd = req[i]
        if a[rb - 1] - a[ra - 1] == rc:
            res += rd
    return res

def set_a(idx: int, au: int):
    ans = 0
    for i in range(au, m + 1):
        a[idx] = i
        if idx + 1 < n:
            ans = max(set_a(idx + 1, i), ans)
        else:
            ans = max(check(), ans)
    return ans

print(set_a(0, 1))

Solution (Python):
import itertools
N,M,Q = map(int,input().split())

Sd = [list(map(int, input().split())) for i in range(Q)]
A = [1] * N

def point(ablock,A):
    p = 0
    for qlist in ablock:
        if(A[qlist[1]-1] - A[qlist[0]-1] == qlist[2]):
            p += qlist[3]
    return p

comb = itertools.combinations_with_replacement(range(1, M+1), N)

ans = 0
for c in comb:
    sug = point(Sd, c)
    if ans < sug:
        ans = sug

print(ans)

Solution (Python):
[N,M,Q] = list(map(int,input().split()))

abcd = []
for i in range(Q):
    abcd.append(list(map(int,input().split())))

from itertools import permutations, combinations,combinations_with_replacement,product
AA=range(1,M+1)
CM = list(combinations_with_replacement(AA,N))

wa = 0
A = [0]*N
dammy=[]
for i in range(len(CM)):
    A = CM[i]
    for j in range(Q):
        
        if (A[abcd[j][1]-1] - A[abcd[j][0]-1]) == abcd[j][2]:
            wa+=abcd[j][3]
    dammy.append(wa)
    wa =0

print(max(dammy))

Solution (Python):
import itertools
n,m,q=map(int,input().split())
abcd=[list(map(int,input().split())) for _ in range(q)]
ans=0
for i in itertools.combinations_with_replacement(range(1,m+1),n):
    total=0
    for j in range(q):
        if i[abcd[j][1]-1]-i[abcd[j][0]-1]==abcd[j][2]:
            total+=abcd[j][3]
        ans=max(ans,total)
print(ans)

Solution (Python):
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

    N,M,Q = LMIIS()
    A = [0]*N

    query = [0]*Q
    for i in range(Q):
        a,b,c,d = LMIIS()
        query[i] = (a,b,c,d)

    from itertools import combinations_with_replacement
    max_score = 0
    for T in combinations_with_replacement(range(1,M+1),N):
        tmp_score = 0
        for a,b,c,d in query:
            if T[b-1] - T[a-1] == c:
                tmp_score += d
        max_score = max(max_score, tmp_score)
    print(max_score)







if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

n, m, q = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for _ in range(q)]

s = ['1']
s_list = []

def dfs(s):
    last_s = int(s[-1])
    for i in range(last_s, m + 1):
        t = s[:]
        t.append(str(i))
        if len(t) == n + 1:
            s_list.append(t[1:])
        else:
            dfs(t)

dfs(s)

ans = 0

for s in s_list:
    res = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if int(s[b - 1]) - int(s[a - 1]) == c:
            res += d
    ans = max(res, ans)

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
import itertools
N,M,Q=map(int,input().split())
T=[]

H=0
for y in range(Q):
    a,b,c,d=map(int,input().split())
    t=(a,b,c,d)
    T.append(t)
A=[x+1 for x in range(M)]
B=list(itertools.combinations_with_replacement(A,N))
C=len(B)
for z in range(C):
    E=B[z]
    G=0
    for l in range(Q):
        F=T[l]
        g=F[0]-1
        h=F[1]-1
        i=F[2]
        if E[h]-E[g]==i:
            G+=F[3]
    if G>=H:
        H=G
print(H)

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0

for i in itertools.combinations_with_replacement(range(1, M + 1), N):
    total = 0
    for j in range(Q):
        if i[abcd[j][1] - 1] - i[abcd[j][0] - 1] == abcd[j][2]:
            total += abcd[j][3]
    ans = max(ans, total)

print(ans)

Solution (Python):
import itertools

n, m, q = map(int, input().split())
li = [tuple(map(int, input().split())) for i in range(q)]

A_li = tuple(itertools.combinations_with_replacement(range(m), n))

score = 0

for A in A_li:
  tmp = 0
  for nums in li:
    ai =  nums[0]-1
    bi = nums[1]-1
    ci = nums[2]
    di = nums[3]
    
    if A[bi] - A[ai] == ci:
      tmp += di
  score = max(score, tmp)
  
print(score)

Solution (Python):
import itertools

n, m, q = map(int, input().split())

As = list(itertools.combinations_with_replacement(range(1,m+1), n))

ans = [0] * len(As)
for _ in range(q):
  a, b, c, d = map(int, input().split())
  for i, A in enumerate(As):
    if A[b-1] - A[a-1] == c:
      ans[i] += d

print(max(ans))

Solution (Python):
import itertools

t_list = []
n, m, q = map(int, input().split())
for i in range(q):
    t_list.append(list(map(int, input().split())))

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)

Solution (Python):
n,m,q=map(int,input().split())
arr=[list(map(int,input().split())) for i in range(q)]
ans=0
for i1 in range(1,m+1):
    for i2 in range(i1,m+1):
        for i3 in range(i2,m+1):
            for i4 in range(i3,m+1):
                for i5 in range(i4,m+1):
                    for i6 in range(i5,m+1):
                        for i7 in range(i6,m+1):
                            for i8 in range(i7,m+1):
                                for i9 in range(i8,m+1):
                                    for i10 in range(i9,m+1):
                                        tmp=0
                                        arrs=[0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for a,b,c,d in arr:
                                            if arrs[b]-arrs[a]==c:
                                                tmp+=d
                                        ans=max(ans,tmp)
print(ans)

Solution (Python):
def examA():
    K = I()
    A, B = LI()
    for i in range(A,B+1):
        if i%K==0:
            print("OK")
            return
    print("NG")
    return

def examB():
    X = I()
    now = 100
    ans = 0
    while(now<X):
        ans += 1
        now += now//100
    print(ans)
    return

def examC():
    N, M, Q = LI()
    A = [LI()for _ in range(Q)]
    A.sort()
    dp = [[0]*M for _ in range(N)]
    for i in range(Q):
        a, b, c, d = A[i]
        a -= 1; b -= 1
        for j in range(M):
            dp[b][j] = max(dp[b][j],0)


    ans = 0
    print(ans)
    return


def examC2():
    N, M, Q = LI()
    A = [LI()for _ in range(Q)]
    C = list(itertools.combinations_with_replacement(range(1, M + 1), N))
    
    ans = 0
    for L in C:
        cur = 0
        for a, b, c, d in A:
            if (L[b-1]-L[a-1]==c):
                cur += d
        ans = max(cur,ans)
    print(ans)
    return

def examD():
    A, B, N = LI()
    if N>=B-1:
        ans = (A*(B-1))//B
    else:
        ans = (A*N)//B
    print(ans)
    return

def examE():
    N, M = LI()
    ans = []
    if N%2==1:
        for i in range(N // 2):
            ans.append((i + 1, N - i))
    else:
        flag = False
        for i in range(N // 2):
            if N-i-(i+1)<=N//2:
                flag = True
            if flag:
                ans.append((i+1,N-i-1))
            else:
                ans.append((i + 1, N - i))
    
    for v in ans[:M]:
        print(" ".join(map(str,v)))
    return

def examF():
    N = I()
    A = LI()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        u, v = LI()
        u -= 1; v -= 1
        V[u].append(v)
        V[v].append(u)
    dp = [-1]*N
    dp[0] = 1
    LIS = deque()
    LIS.append(A[0])
    def dfs(s):
        
        for i in V[s]:
            if dp[i]!=-1:
                continue
            flag = []
            if A[i] > LIS[-1]:
                LIS.append(A[i])
                flag = False
            else:
                flag = [bisect.bisect_left(LIS, A[i]),LIS[bisect.bisect_left(LIS, A[i])]]
                LIS[flag[0]] = A[i]
            dp[i] = len(LIS)
            dfs(i)
            
            if not flag:
                LIS.pop()
            else:
                LIS[flag[0]] = flag[1]
        return
    dfs(0)
    ans = dp
    for v in ans:
        print(v)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC2()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
import itertools
n, m, q = map(int, input().split())

ql = [list(map(int, input().split())) for i in range(q)]

ans=0

for c in itertools.combinations_with_replacement(range(1, m + 1), n):
    a=0
    for j in ql:
        if c[j[1]-1] - c[j[0]-1] == j[2]:
            a += j[3]
    if ans < a:
        ans = a
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

N, M, Q = map(int, input().split())
Q = [tuple(map(int, input().split())) for _ in range(Q)]

res = 0

for A in combinations_with_replacement(range(1, M + 1), N):
    point = 0
    for a, b, c, d in Q:
        if A[b - 1] - A[a - 1] == c:
            point += d
    res = max(point, res)

print(res)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def backtrack(n, ml, mh, tmp=[]):
    if n == 0:
        yield tmp
        return

    for i in range(ml, mh + 1):
        yield from backtrack(n - 1, i, mh, tmp + [i])


def main():
    def count(ca):
        return sum(d for a, b, c, d in abcd if ca[b - 1] - ca[a - 1] == c)
    N, M, Q = inpl()
    abcd = [inpl() for _ in range(Q)]
    print(max(count(c) for c in backtrack(N, 1, M)))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def backtrack(n, ml, mh, tmp=[]):
    if n == 0:
        yield tmp
        return

    for i in range(ml, mh + 1):
        yield from backtrack(n - 1, i, mh, tmp + [i])


def main():
    def count(ca):
        return sum(d for a, b, c, d in abcd if ca[b - 1] - ca[a - 1] == c)
    N, M, Q = inpl()
    abcd = [inpl() for _ in range(Q)]
    print(max(count(c) for c in backtrack(N, 1, M)))


if __name__ == '__main__':
    main()

Solution (Python):
n, m, q = 0, 0, 0
Q = []

def main():
    global n, m, q
    n, m, q = map(int, input().split())
    for _ in range(q):
        a, b, c, d = map(int, input().split())
        a, b = a - 1, b - 1
        Q.append([a, b, c, d])
    ans = solve()
    print(ans)

def solve():
    ans = 0
    for i in range(1, m + 1):
        ans = max(ans, dfs([i], 0))
    return ans

def dfs(A, ans):
    
    if len(A) == n:
        tmp = sum([Q[i][3] for i in range(q) if A[Q[i][1]] - A[Q[i][0]] == Q[i][2]])
        

        return max(ans, tmp)

    A.append(A[-1])
    while A[-1] <= m:
        ans = max(ans, dfs(A, ans))
        A[-1] += 1
    A.pop()
    
    return ans

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations_with_replacement as cwr

n, m, q  = map(int, input().split())
scores = [[int(j) for j in input().split()] for _ in range(q)]

combs = cwr(range(1, m+1), n)
ans = 0
for comb in combs:
  cand = 0
  for a, b, c, d in scores:
    if comb[b-1] - comb[a-1] == c:
      cand += d
  ans = max(ans, cand)
print(ans)

Solution (Python):
import itertools
n, m, q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
Max = 0
for A in itertools.combinations_with_replacement(range(1, m + 1), n):
    
    score = 0
    for ai, bi, ci, di in abcd:
        if A[bi - 1] - A[ai - 1] == ci:
            score += di
    Max = max(Max, score)
    
print(Max)

Solution (Python):
n,m,q = map(int,input().split())
kouho = [int(i) for i in range(1,m+1)]
kouho_list = [1]*(n+1)
Input = []
for i in range(q):
    tmp = [int(i) for i in input().split()]
    Input.append(tmp)

def rec(N,Min):
    global ans
    if N==1:
        ans =0
    if N==n:
        score = 0
        for i in range(q):
            a,b,c,d = Input[i][0],Input[i][1],Input[i][2],Input[i][3]
            if kouho_list[b-1]-kouho_list[a-1] == c:
                score += d
        ans = max(ans,score)
        return 0
    for i in range(Min,m+1):
        kouho_list[N] = i
        rec(N+1,i)
rec(1,1)
print(ans)

Solution (Python):



from itertools import combinations_with_replacement
def main():
    N, M, Q = map(int, input().split())
    Abcd = [list(map(int,input().split())) for i in range(Q)]
    L = list(combinations_with_replacement(range(1,M+1), N))
    ret = 0

    for l in L:
        pt = 0
        for abcd in Abcd:
            if l[abcd[1]-1] - l[abcd[0]-1] == abcd[2]:
                pt += abcd[3]
        ret = max(ret, pt)
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, M, Q = rl()
A, B, C, D = [], [], [], []
for i in range(Q):
	a, b, c, d = rl()
	A.append(a-1)
	B.append(b-1)
	C.append(c)
	D.append(d)

def check(nums):
	for i in range(len(nums)-1):
		if nums[i] > nums[i+1]:
			return False
	return True

def f(nums):
	s = 0
	for i in range(Q):
		if nums[B[i]]-nums[A[i]] == C[i]:
			s += D[i]
	return s

nums = [0] * N
def myiter(M, N, i=0):
	if i == N:
		yield nums
		return
	prev = 1
	if i > 0:
		prev = nums[i-1]
	for j in range(prev,M+1):
		nums[i] = j
		for y in myiter(M, N, i+1):
			yield y


ans = 0
for nums in myiter(M, N):
	if check(nums):
		n = f(nums)
		ans = max(ans, n)
print(ans)

Solution (Python):
N,M,Q = map(int,input().split())
lis = []
i = 0
ai,bi,dist,poin = [],[],[],[]
while True:
    try:
        lis.append(list(map(int,input().split())))
        ai.append(lis[i][0])
        bi.append(lis[i][1])
        dist.append(lis[i][2])
        poin.append(lis[i][3])
        i+=1

    except:
        break;
        

        
def point(num):
  tok = 0
  for i in range(Q):
    
    if(num[bi[i]-1] - num[ai[i]-1] == dist[i]):
      tok += poin[i]
  return tok



def UpdateA(num):
  NewA = num
  flag = 0
  i = N-1
  while(flag == 0 and i >= 0):
    NewA[i] += 1
    if(NewA[i] > M):
      i -= 1
      continue
    else:
      j=i+1
      while(j<N):
        NewA[j] = NewA[j-1]
        j+=1
      flag = 1
  
  return NewA
  
maxi = 0
A = [1 for i in range(N)]
while(1):
  thisp = point(A)
  
  if(thisp > maxi):
    maxi = thisp
    
    if(maxi == sum(poin)):
      break
  A = UpdateA(A)
  if(sum(A) > N*M):
    break

print(maxi)

Solution (Python):
import sys
import itertools

N, M, Q = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(Q)]

max_value = 0

for A in itertools.combinations_with_replacement(range(1, M+1), N):
    tmp_value = 0
    for i in range(len(S)):
        if S[i][1] > len(A) or S[i][0] > len(A):
            continue

        if A[S[i][1] - 1] - A[S[i][0] - 1] == S[i][2]:
            tmp_value += S[i][3]

    if tmp_value > max_value:
        max_value = tmp_value
print(max_value)

Solution (Python):
from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
A = combinations_with_replacement(range(m), n)
L = []

for _ in range(q):
    L.append(list(map(int, input().split())))

r = 0
for a in A:
    t = 0
    for l in L:
        if a[l[1] - 1] - a[l[0] - 1] == l[2]:
            t += l[3]
    if t > r:
        r = t
print(r)

Solution (Python):

import sys
import itertools
import fractions
import copy
import bisect
import math
import numpy as np
from itertools import combinations_with_replacement


from operator import itemgetter
from sys import stdin

from operator import mul
from functools import reduce
from collections import Counter





j=0
k=0
n=3
r=1
a=[0]*5
b=[]






"1行1つの整数を入力を取得し、整数と取得する"




"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

for i in number_list:
    print(number_list[i-1])

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''



"正方行列にstr型の値を入力"
'''




'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"


'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''











'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''




"aという変数に入っているものを大文字にして出力"















'''
コンビネーションの組み合わせの中身を出力する形式
for i in itertools.combinations(brray, 2)
combinationsをpermutationsにすれば順列になる
今回なら(abc133B)
入力
1 2
5 5
-2 8
出力
[[1, 2], [5, 5], [-2, 8]]
もちろん一次元リストでも使えるし
何よりiもリストのように使えるので便利
'''


'''
def cmb(n,r):
    
    r= min(n-r,r)
    
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    
    
    

    under = reduce(mul, range(1, r+1))
    
    
    
    

    return over // under
    
    
    
    
a = cmb(n, r)

'''

'''




for i in a.most_common(n):print(i)





 


'''









'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''
















input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
'''
講評：検索対象の数列を全て数え上げる発想まではあっていた
　　　出来ればそれを実装できればなお良しだった

以下の違いを意識しよう
combinations() p, r 長さrのタプル列、ソートされた順で重複なし

combinations_with_replacement() p, r 長さrのタプル列、ソートされた順で重複あり


使用例 　　　　　　　　　　　                  出力　　
combinations('ABCD', 2)　　　　　　　　　　　　AB AC AD BC BD CD

combinations_with_replacement('ABCD', 2)　　 AA AB AC AD BB BC BD CC CD DD
'''

def main():
    N, M, Q = map(int, input().split())
    abcd = [None] * Q
    for i in range(Q):
        abcd[i] = tuple(map(int, input().split()))
        
    ans = 0
    for A in combinations_with_replacement(range(1, M+1), N):
        
        
        score = 0
        for q in abcd:
            
            
            
            a, b, c, d = q
            
            if A[b-1] - A[a-1] == c:
                score += d
        if score > ans:
            ans = score

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
cond = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    a -= 1
    b -= 1
    cond.append((a, b, c, d))


def calc(l):
    score = 0
    for a, b, c, d in cond:
        if l[b] - l[a] == c:
            score += d
    return score


ans = 0
l = [0] * n
l[0] = 1


def dfs(l_len):
    global l, ans
    if l_len == n:
        ans = max(ans, calc(l))
        return

    for i in range(l[l_len - 1], m + 1):
        l[l_len] = i
        dfs(l_len + 1)


dfs(1)
print(ans)

Solution (Python):
N, M, Q = map(int, input().split())  
Q = [list(map(int, input().split())) for i in range(Q)]  

import itertools
A = 0
for i in itertools.combinations_with_replacement(range(M),N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        if score > A:
            A = score

print(A)

Solution (Python):
N, M, Q = map(int, input().split())  
Q = [list(map(int, input().split())) for i in range(Q)]  

import itertools
A = []
for i in itertools.combinations_with_replacement(range(M),N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        A.append(score)

print(max(A))

Solution (Python):
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
cond = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    a -= 1
    b -= 1
    cond.append((a, b, c, d))


def calc(l):
    score = 0
    for a, b, c, d in cond:
        if l[b] - l[a] == c:
            score += d
    return score


ans = 0
for l in combinations_with_replacement(range(1, m + 1), n):
    cand = calc(l)
    ans = max(ans, cand)

print(ans)

Solution (Python):
n,m,q=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(q)]
ans=0
for i1 in range(1,m+1): 
  for i2 in range(i1,m+1):
    for i3 in range(i2,m+1):
      for i4 in range(i3,m+1):
        for i5 in range(i4,m+1):
          for i6 in range(i5,m+1):
            for i7 in range(i6,m+1):
              for i8 in range(i7,m+1):
                for i9 in range(i8,m+1):
                  for i10 in range(i9,m+1):
                    tmp=0
                    arrs=[0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                    for a,b,c,d in arr: 
                      if arrs[b]-arrs[a]==c:
                        tmp+=d
                    ans=max(ans,tmp)
print(ans)

Solution (Python):
def f(n, m, q, requirements):
    A = [1 for i in range(n + 1)]
    ans = 0
    while True:
        
        score = 0
        for requirement in requirements:
            a, b, c, d = requirement
            if A[b] - A[a] == c:
                score += d
        ans = max(ans, score)
        
        j = n
        while j > 0:
            if A[j] < m:
                A[j] += 1
                for i in range(j + 1, n + 1):
                    A[i] = A[j]
                break
            j -= 1
        if j == 0:
            break
    return ans


if __name__ == '__main__':
    n, m, q = map(int, input().split())
    requirements = []
    for i in range(q):
        requirements.append(tuple(map(int, input().split())))
    print(f(n, m, q, requirements))

Solution (Python):
import itertools
N, M, Q = map(int, input().split())

K= [list(map(int, input().split())) for i in range(Q)]

ans = 0
for A in itertools.combinations_with_replacement([i for i in range(1, M + 1)], N):
    ans1 = 0
    for i in range(Q):
        a, b, c, d = K[i]
        if (A[b - 1] - A[a - 1] == c):
            ans1 += d
        ans = max(ans, ans1)
print(ans)

Solution (Python):
import itertools
import copy
import math


N, M, Q = map(int, input().split())
L = []
for _ in range(Q):
    a, b, c, d = map(int, input().split())
    L.append((a-1, b-1, c, d))


ans = 0
for a1 in range(1, M+1):
    for a2 in range(a1, M+1):
        for a3 in range(a2, M+1):
            for a4 in range(a3, M+1):
                for a5 in range(a4, M+1):
                    for a6 in range(a5, M+1):
                        for a7 in range(a6, M+1):
                            for a8 in range(a7, M+1):
                                for a9 in range(a8, M+1):
                                    for a10 in range(a9, M+1):
                                        seq = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
                                        t = 0
                                        for a, b, c, d in L:
                                            if seq[b]-seq[a] == c:
                                                t += d
                                        
                                        ans = max(ans, t)


print(ans)

Solution (Python):
N, M, Q = map(int, input().split())  
Q = [list(map(int, input().split())) for i in range(Q)]  

A = []
for i in range(1,M+1):
    A.append(i)

B = []
import itertools
for i in itertools.combinations_with_replacement(A,N):
    B.append(i)

C = []
for i in range(len(B)):
    score = 0
    for j in range(len(Q)):
        if B[i][Q[j][1]-1] - B[i][Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        C.append(score)

print(max(C))

Solution (Python):

import itertools
n, m, q = map(int,input().split())
abcd = []
for i in range(q):
    abcd.append([int(i) for i in input().split()])
"""

A = []
def func(B, n):
    if len(B) == n:
        A.append(B)
        return
    elif len(B) == 0:
        for i in range(1, m+1):
            func(B + [i], n)
    else:
        for i in range(B[-1], m+1):
            func(B + [i], n)
print(A)
"""

a = [0 for _ in range(q)]
b = [0 for _ in range(q)]
c = [0 for _ in range(q)]
d = [0 for _ in range(q)]
for i in range(q):
    a[i] = abcd[i][0]
    b[i] = abcd[i][1]
    c[i] = abcd[i][2]
    d[i] = abcd[i][3]

A = (list(itertools.combinations_with_replacement(range(1, m + 1), n)))


ans = 0
for i in range(len(A)):
    tmp = 0
    
    for j in range(q):
        if (A[i][b[j]-1] - A[i][a[j]-1]) == c[j]:
            tmp += d[j]
    
    if ans < tmp:
        ans = tmp
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log
from heapq import heapify, heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from decimal import Decimal
inf = float('inf')
MOD = 1000000007
def INT_(n): return int(n) - 1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')

n, m, q = MI()
abcd = LLIN(q)

def main():
    data = list(range(1, m+1))
    arr = combinations_with_replacement(data, n)
    ans = 0
    for A in arr:
        tmp = 0
        for a,b,c,d in abcd:
            if A[b-1] - A[a-1] == c:
                tmp += d
            
        ans = max(ans, tmp)
    
    print(ans)
    

if __name__ == "__main__":
    main()

Solution (Python):
n,m,q=map(int,input().split())
abcd=[list(map(int,input().split())) for _ in range(q)]

def dfs():
  todo=[[]]
  ret=[]
  while len(todo)>0:
    t=todo.pop()
    if len(t)==n:
      ret.append(t)
    else:
      l=t[-1] if len(t)>0 else 1
      for i in range(l,m+1):
        tc=t.copy()
        tc.append(i)
        todo.append(tc)
  return ret
def point(s):
  ret=0
  for a,b,c,d in abcd:
    if s[b-1]-s[a-1]==c:
      ret+=d
  return ret
a=dfs()
ans=0
for ai in a:
  ans=max(ans,point(ai))
print(ans)

Solution (Python):


import itertools
N, M, Q = map(int, input().split())
pair = [list(map(int, input().split())) for i in range(Q)]
ans = 0

for A in itertools.combinations_with_replacement(range(1, M+1), N):
    tmp = 0
    for p in pair:
        
        if A[p[1] - 1] - A[p[0] - 1] == p[2]:
            tmp += p[3]
            ans = max(ans, tmp)
            
            

print(ans)

Solution (Python):
N,M,Q = map(int,input().split())
cond_list = []
for i in range(Q) :
    cond_list.append(list(map(int,input().split())))




max_score = 0

for A1 in range(1,M+1) :
    for A2 in range(A1, M+1) :
        for A3 in range(A2, M+1) :
            for A4 in range(A3, M+1) :
                for A5 in range(A4, M+1) :
                    for A6 in range(A5, M+1) :
                        for A7 in range(A6, M+1) :
                            for A8 in range(A7, M+1) :
                                for A9 in range(A8, M+1) :
                                    for A10 in range(A9, M+1) :
                                        tmpA = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10]
                                        tmp_score = 0
                                        for a,b,c,d in cond_list :
                                            if (tmpA[b-1] - tmpA[a-1] == c) :
                                                tmp_score += d
                                        if (max_score < tmp_score) :
                                            max_score = tmp_score

print(max_score)

Solution (Python):
n,m,q=map(int,input().split())
req=[tuple(map(int,input().split())) for i in range(q)]
ans=0
def construction(arr):
    global ans
    if len(arr)==n:
        ret=0
        for i in range(q):
            if arr[req[i][1]-1]-arr[req[i][0]-1]==req[i][2]:ret+=req[i][3]
        ans=max(ret,ans)
        return
    if len(arr)==0:
        for i in range(m):
            construction(arr+[i])
        return
    for i in range(arr[-1],m):
        construction(arr+[i])
    return
construction([])
print(ans)