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):
from itertools import *
n, m, q = map(int, input().split())

a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q

for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    a[i], b[i] = a[i] - 1, b[i] - 1

p_tmp = [0] * m
for i in range(m):
    p_tmp[i] = i

p = combinations_with_replacement(p_tmp, n)

ans = 0
for x in p:
    score = 0
    for i in range(q):
        if x[b[i]] - x[a[i]] == c[i]:
            score += d[i]

    ans = max(ans, score)

print(ans)

Solution (Python):
n, m, q = map(int, input().split())
abcd = []
for _ in range(q):
    abcd.append(tuple(map(int, input().split())))
ans = 0
from itertools import combinations_with_replacement
for A in combinations_with_replacement(list(range(1, m+1)), n):
    suma = 0
    for a,b,c,d in abcd:
        if A[b-1] - A[a-1] == c:
            suma += d
    ans = max(ans, suma)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

ans=0
for A in combinations_with_replacement(list(range(1,m+1)),n):

    asum=0
    for a,b,c,d in abcd:
        if A[b-1]-A[a-1]==c:
            asum+=d
    ans=max(ans,asum)
print(ans)

Solution (Python):
n, m, q = map(int, input().split())
a_list = []
b_list = []
c_list = []
d_list = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    a_list.append(a)
    b_list.append(b)
    c_list.append(c)
    d_list.append(d)

import itertools

ans = 0
h_list = [i+1 for i in range(m)]
for A in itertools.combinations_with_replacement(h_list, n):
    
    max_d = 0
    for a, b, c, d in zip(a_list, b_list, c_list, d_list):
        if A[b-1]-A[a-1] == c:
            max_d += d
    ans = max(ans, max_d)

print(ans)

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from itertools import combinations_with_replacement
def resolve():
    n, m, q = map(int, input().split())
    ABCD = [tuple(map(int, input().split())) for _ in range(q)]
    ans = 0
    for I in combinations_with_replacement(range(m), n):
        score = 0
        for a, b, c, d in ABCD:
            if I[b - 1] - I[a - 1] == c:
                score += d
        ans = max(ans, score)
    print(ans)
resolve()

Solution (Python):
from itertools import combinations_with_replacement as cr
n,m,q=map(int,input().split())
s=[list(map(int,input().split()))for i in range(q)]
f=0
for i in cr([i for i in range(1,m+1)],n):
    e=0
    for a,b,c,d in s:
        if i[b-1]-i[a-1]==c:
            e+=d
    if f<e:f=e
print(f)

Solution (Python):
import itertools

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

for i in itertools.combinations_with_replacement(range(1,M+1),N):
    ans2 = 0
    for t in A:
        if i[t[1]-1] - i[t[0]-1] == t[2]:
            ans2 += t[3]

    ans = max(ans,ans2)

print(ans)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
A = [0] * Q
B = [0] * Q
C = [0] * Q
D = [0] * Q

for i in range(Q):
    a, b, c, d = map(int, input().split())
    A[i] = a
    B[i] = b
    C[i] = c
    D[i] = d
X = list(itertools.combinations_with_replacement([a for a in range(1,M+1)],N))

cnt = [0] * (len(X))
for k in range(len(X)):
    for l in range(Q):
        if X[k][B[l] - 1] - X[k][A[l] - 1] == C[l]:
            cnt[k] += D[l]

print(max(cnt))

Solution (Python):
N, M, Q = map(int, input().split())
a, b, c, d = [0] * Q, [0] * Q, [0] * Q, [0] * Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

mx = 0

def dfs(Aary):
    global mx

    if len(Aary) == N + 1:
        scr = score(Q, Aary, a, b, c, d)
        if mx < scr:
            mx = scr
        return

    Aary.append(Aary[-1])
    while Aary[-1] <= M:
        dfs(Aary.copy())
        Aary[-1] += 1

def score(Q, A, a, b, c, d):
    ans = 0
    for i in range(Q):
        if A[b[i]] - A[a[i]] == c[i]:
            ans += d[i]

    return ans

dfs( [1] )

print(mx)

Solution (Python):
import itertools
n,m,q=list(map(int,input().split()))
abcd=[[] for i in range(q)]
for i in range(q):
    abcd[i]=list(map(int,input().split()))
ans=0
l=list(itertools.combinations_with_replacement([a for a in range(1,m+1)],n))
x=(len(l))
for i in range(x):
    e=l[i]
    
    wa=0
    for t in range(q):
        if e[abcd[t][1]-1]-e[abcd[t][0]-1]==abcd[t][2]:
            wa+=abcd[t][3]
    ans=max(ans,wa)
print(ans)

Solution (Python):
import itertools

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

def get_sum(A, abcd_array):
    sum = 0
    for abcd in abcd_array:
        a, b, c, d = abcd
        if A[b-1] - A[a-1] == c:
            sum += d
    return sum

candidate_array = list(itertools.combinations_with_replacement(range(1,m+1), n))
ans = 0
for A in candidate_array:
    ans = max(ans, get_sum(A, abcd_array))

print(ans)

Solution (Python):
import itertools

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

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

max_point = 0
for each in lists:
  point = 0
  for i in range(q):
    l = lst[i]
    a = l[0] - 1
    b = l[1] - 1
    c = l[2]
    if each[b] - each[a] == c:
      point += l[3]
  if point > max_point:
    max_point = point
print(max_point)

Solution (Python):
def main():
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]

    def dfs(A):
        if len(A) == N:
            ret = 0
            for (a, b, c, d) in T:
                if A[b-1] - A[a-1] == c:
                    ret += d
            return ret
        ret = 0
        s = A[-1] if A else 1
        for a in range(s, M+1):
            ret = max(ret, dfs(A + [a]))
        return ret

    print(dfs([]))


if __name__ == '__main__':
    main()

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

a_ls=[]
b_ls=[]
c_ls=[]
d_ls=[]

for vnwrjvnwakn in range(Q):
	abcd=list(map(int, input().split()))
	a_ls+=[abcd[0]]
	b_ls+=[abcd[1]]
	c_ls+=[abcd[2]]
	d_ls+=[abcd[3]]



import itertools
As=itertools.combinations_with_replacement(range(1,M+1),N)
ans=0
for A in As:
	score=0
	Als=list(A)
	for i in range(Q):
		if Als[b_ls[i]-1]-Als[a_ls[i]-1]==c_ls[i]:
			score+=d_ls[i]
	
	ans=max(ans,score)


print(ans)

Solution (Python):
number=list(map(int,input().split()))
n,m,q=number[0],number[1],number[2]
nums=[]
for i in range(q):
    tmp=list(map(int,input().split()))
    nums.append(tmp)
answer=0
mid=0
List1=[]
for i in range(m):
  List1.append(i)
import itertools
for i in list(itertools.combinations_with_replacement(List1,n)):
    mid=0
    for j in range(q):
        if i[nums[j][1]-1]-i[nums[j][0]-1]==nums[j][2]:
            mid+=nums[j][3]
    answer=max(mid,answer)
    
print(answer)

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

ans = 0

import itertools
for con in itertools.combinations_with_replacement(list(range(m)), n):
    tmp = 0
    for i in l:
        a,b,c,d = i
        if con[b-1] - con[a-1] == c:
            tmp += d
    ans = max(ans, tmp)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement as comp_rplc

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

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

ans = 0

for seq in comp_rplc(range(1, m + 1), n):
  score = 0
  for a, b, c, d in req:
    if seq[b - 1] - seq[a - 1] == c:
      score += d
    
  ans = max(ans, score)

print(ans)

Solution (Python):
import itertools
N,M,Q=map(int,input().split())
List = []
for i in range (Q):
  List.append(list(map(int, input().split())))
List1 = []
res = 0
mid = 0
for i in range(M):
  List1.append(i)
x = itertools.combinations_with_replacement(List1, N)
tryList= list(x)

for tryElement in tryList:
  mid = 0
  for j in range(Q):
    if tryElement[List[j][1]-1]-tryElement[List[j][0]-1]==List[j][2]:
      mid += List[j][3]
  res = max(mid,res)
print(res)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
N, M, Q = map(int, input().split())
l = []
for _ in range(Q):
    l.append(list(map(int, input().split())))
ans = 0
def dfs(n):
    if len(n) == N+1:
        global ans
        tmp = 0
        for i in range(Q):
            abi = n[l[i][1]]
            aai = n[l[i][0]]
            c = l[i][2]
            d = l[i][3]
            if abi -aai == c:
                tmp += d
        ans = max(ans, tmp)
    else:
        for i in range(n[-1], M+1):
            tmp2 = n[:]
            tmp2.append(i)
            dfs(tmp2)
dfs([1])
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(Q)]
ans = 0

for arr in combinations_with_replacement(range(1, M+1), N):
    tmp = 0
    arr = list(arr)
    for a, b, c, d in X:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    ans = max(ans, tmp)

print(ans)

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

import itertools
for con in itertools.combinations_with_replacement(list(range(m)),n):
    tmp = 0
    for i in query:
        a,b,c,d = i
        if con[b-1] - con[a-1] == c:
            tmp += d
    ans = max(ans, tmp)

print(ans)

Solution (Python):
import itertools

n,m,q = list(map(int,input().split()))
abcd = [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 i in range(q):
        a,b,c,d = abcd[i]
        if A[b-1] - A[a-1] == c:
            tmp += d
    ans = max(tmp, ans)

print(ans)

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


def main():
  N, M, Q = list(map(int, input().split()))
  cands = itertools.combinations(range(N+M-1), N)
  Qs = [tuple(map(int, input().split())) for _ in range(Q)]

  seq = [0]*N
  max_ = 0
  for cand in cands:
    temp_max = 0
    for i in range(N):
      seq[i] = cand[i] - i

    for a, b, c, d in Qs:
      if seq[b-1] - seq[a-1] == c:
        temp_max += d
    max_ = max(max_, temp_max)

  print(max_)


if(__name__ == '__main__'):
  main()

Solution (Python):
import itertools
n,m,q=map(int,input().split())
L=list(itertools.combinations_with_replacement(range(m),n))
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
for i in range(q):
    a[i],b[i],c[i],d[i]=map(int,input().split())
    a[i]-=1
    b[i]-=1
ans=0
for l in L:
    tmp=0
    for i in range(q):
        if l[b[i]]-l[a[i]]==c[i]:
            tmp+=d[i]
    ans=max(ans,tmp)
print(ans)

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

def dfs(A, head):
  if len(A) == N:
    score = 0
    for a, b, c, d in cond:
      if A[b] - A[a] == c:
        score += d
    return score
  ans = 0
  for n in range(head, M + 1):
    ans = max(ans, dfs(A + [n], n))
  return ans
print(dfs([1], 1))

Solution (Python):
import itertools
n,m,q=map(int,input().split())
a=[]
for _ in range(q):
  b=list(map(int,input().split()))
  a.append(b)
  
g=list(range(1,m+1))
c=[]
for v in itertools.combinations_with_replacement(g,n):
  c.append(v)
  
ans1=[]
ans=0
for x in range(len(c)):
  d=c[x]
  ans1=0
  for r in range(q):
    a1=a[r][0]-1
    b1=a[r][1]-1
    c1=a[r][2]
    d1=a[r][3]
    if d[b1]-d[a1]==c1:
      ans1+=d1
      
  ans=max(ans,ans1)
  
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):
import itertools
n,m,q=map(int,input().split())
a=[]
for _ in range(q):
  b=list(map(int,input().split()))
  a.append(b)
  
g=list(range(1,m+1))
c=[]
for v in itertools.combinations_with_replacement(g,n):
  c.append(v)
  
ans1=[]
ans=0
for x in range(len(c)):
  d=c[x]
  ans1=0
  for r in range(q):
    a1=a[r][0]-1
    b1=a[r][1]-1
    c1=a[r][2]
    d1=a[r][3]
    if d[b1]-d[a1]==c1:
      ans1+=d1
      
  ans=max(ans,ans1)
  
print(ans)

Solution (Python):
from itertools import combinations as combi

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



max_num = 0
for l in combi(range(M-1+N), N):
    x = [t-s for t, s in zip(l, range(N))]
    
    score = sum(d for a, b, c, d in ABCD if x[b-1]-x[a-1] == c)
    max_num = max(max_num, score)

print(max_num)

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

def score(Ans):
    t = 0
    for ai,bi,ci,di in zip(A,B,C,D):
        if Ans[bi]-Ans[ai]==ci:
            t += di
    return t

def dfs(Ans):
    if len(Ans)==n:
        return score(Ans)
    ans = 0
    prev_last = Ans[-1] if len(Ans)>0 else 0
    for v in range(prev_last, m):
        Ans.append(v)
        ans = max(ans, dfs(Ans))
        Ans.pop()
    return ans

n, m, q = map(int, readline().split())
A,B,C,D = [0]*q, [0]*q, [0]*q, [0]*q
for i in range(q):
    a,b,C[i],D[i] = map(int, readline().split())
    A[i] = a-1
    B[i] = b-1

print(dfs([]))

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

ans = 0
for i in combinations_with_replacement(range(1,m+1), n):
  t = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]: t += j[3]
  ans = max(ans, t)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

N,M,Q=map(int,input().split())

A,B,C,D=[],[],[],[]

for i in range(Q):
    a,b,c,d=list(map(int,input().split()))
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

Z=combinations_with_replacement(range(1,M+1),N)

M=0
for K in Z:
    X=0
    L=["*"]+list(K)
    for i in range(Q):
        if L[B[i]]-L[A[i]]==C[i]:
            X+=D[i]
    M=max(M,X)
print(M)

Solution (Python):
from collections import deque
N, M, Q = map(int, input().split())
I = [list(map(int, input().split())) for _ in range(Q)]

que = deque()
ans = 0
for i in range(1, M+1):
    que.append([i])

while que:
    seq = que.popleft()
    
    if len(seq) == N:
        p = 0
        for i in range(Q):
            if seq[I[i][1]-1] - seq[I[i][0]-1] == I[i][2]:
                p += I[i][3]
        ans = max(ans, p)
    else:
        for i in range(seq[-1], M+1):
            seq_next = seq + [i]
            que.append(seq_next)

print(ans)

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

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

def dfs(seq):
    if len(seq) == n:
        yield seq
    else:
        if seq:
            last = seq[-1]
        else:
            last = 1
        for i in range(last, m+1):
            new_seq = seq.copy()
            new_seq.append(i)
            yield from dfs(new_seq)

seq = dfs([])
ans = 0
for s in seq:
    tmp_ans = 0
    for a, b, c, d in abcd:
        if s[b-1] - s[a-1] == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
print(ans)

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

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

def dfs(seq):
    if len(seq) == n:
        yield seq
    else:
        if seq:
            last = seq[-1]
        else:
            last = 1
        for i in range(last, m+1):
            new_seq = seq.copy()
            new_seq.append(i)
            yield from dfs(new_seq)

seq = dfs([])
ans = 0
for s in seq:
    tmp_ans = 0
    for a, b, c, d in abcd:
        if s[b-1] - s[a-1] == c:
            tmp_ans += d
    ans = max(ans, tmp_ans)
print(ans)

Solution (Python):
import sys
import itertools


def resolve(in_):
    n, m, q = map(int, next(in_).split())
    abcd = tuple(tuple(map(int, row.split())) for row in in_)
    

    ans = 0
    for seq_a in itertools.combinations_with_replacement(range(1, m + 1), n):
        
        point = 0
        for a, b, c, d in abcd:
            if seq_a[b - 1] - seq_a[a - 1] == c:
                point += d
        ans = max(ans, point)

    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import itertools

n, m, q = [int(x) for x in stdin.readline().rstrip().split()]

A = []
for i in range(q):
    A.append([int(x) for x in stdin.readline().rstrip().split()])

nums = [x for x in range(1,m+1)]

max_point = 0
for balls in itertools.combinations_with_replacement(nums, n):
    point = 0
    for i in range(q):
        if balls[A[i][1]-1] - balls[A[i][0]-1] == A[i][2]: point = point + A[i][3]
    max_point = max([max_point, point])

print(max_point)

Solution (Python):
import sys
import math
from collections import defaultdict,deque
from itertools import permutations
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()
sys.setrecursionlimit(10**6)
"""========main code==============="""


def check(lol,x):
    if(len(lol)==n):
        tp=0
        for j in v:
            a=j[0]
            b=j[1]
            c=j[2]
            d=j[3]
            if(lol[b-1]-lol[a-1]==c):
                tp+=d
            
        return tp
    k=0         
    for i in range(x,m+1):
        yo=lol[:]
        yo.append(i);
        k=max(k,check(yo,i))
    return k
    


n,m,q=ml()
lol=[]
for i in range(1,m+1):
    lol.append(i)


v=[]

for i in range(q):
   tmp=ll()
   v.append(tmp)



lol=[1]
print(check(lol,1))

Solution (Python):
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

ans = 0
for v in combinations_with_replacement(list(range(1, m + 1)), n):
    sm = 0
    for i in range(q):
        if v[b[i] - 1] - v[a[i] - 1] == c[i]:
            sm += d[i]
    ans = max(ans, sm)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
res = 0


def resolve():
    def dfs(v, A):
        global res
        if len(A) == n:
            total = 0
            for a, b, c, d in query:
                if A[b - 1] - A[a - 1] == c:
                    total += d
            res = max(res, total)
        else:
            for i in range(v, m + 1):
                A.append(i)
                dfs(i, A)
                A.pop()

    n, m, q = map(int, input().split())
    query = [list(map(int, input().split())) for _ in range(q)]
    dfs(1, [])
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import itertools

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







l = range(1, M + 1)
nCr = list(itertools.combinations_with_replacement(l, N))



ans = 0
for A in nCr:
    
    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)

Solution (Python):
def INT():
    return int(input())

def MI():
    return map(int, input().split())

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

def l_s(L):
    return "".join(map(str, L))

from itertools import combinations_with_replacement as cwr

N, M, Q = MI()
comb = [LI() for _ in range(Q)]
sequence = list(range(1, M + 1))
ans = 0

for A in cwr(sequence, N):
    tmp = 0
    A = list(A)
    for abcd in comb:
        a, b, c, d = abcd[0], abcd[1], abcd[2], abcd[3]
        if A[b - 1] - A[a - 1] == c:
            tmp += d
            
    ans = max(ans, tmp)
    
print(ans)

Solution (Python):
n,m,q = [int(_) for _ in input().split()]
a = []
b = []
c = []
d = []

for q in range(q):
  tmplist = [int(_) for _ in input().split()]
  a.append(tmplist[0])
  b.append(tmplist[1])
  c.append(tmplist[2])
  d.append(tmplist[3])




result=[]
def score(A):
  max_score = 0
  for i in range(q+1):
    if A[b[i]-1] - A[a[i]-1] != c[i]: 
      continue
    else:
      max_score+= d[i]
  return max_score

def dfs(A):
  if len(A) == n+1: 
    return score(A)
  result = 0
  past_data = A[-1]
  for v in range(past_data,m+1):
    A.append(v)
    result = max(result,dfs(A))
    A.pop()
  return result

print(dfs([1]))

Solution (Python):
import itertools

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

nCr = list(itertools.combinations_with_replacement(range(1, M + 1), N))
ans = 0

for A in nCr:
    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)

Solution (Python):
n, m, q = map(int, input().split())
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

score = []



import copy
def dfs(A):
    if len(A) == n+1:
        
        tmp = 0
        
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                 tmp += d[i]
        score.append(tmp)
        return
    else:
        tmp = A[-1]
        arr = copy.copy(A)
        arr.append(tmp)
        while(arr[-1] <= m):
            dfs(arr)
            arr[-1] += 1  
dfs([1])

print(max(score))

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
l = [i for i in range(1, M+1)]
qus = []
As = []
for _ in range(Q):
    b = list(map(int, input().split()))
    qus.append(b)
for v in itertools.combinations_with_replacement(l, N):
    a = list(v)
    A = 0
    for q in qus:
        pre = a[q[1]-1] - a[q[0]-1]
        if pre == q[2]:
            A += q[3]
    As.append(A)
print(max(As))

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
a = []
b = []
c = []
d = []
for q in range(Q):
    aq, bq, cq, dq = map(int, input().split())
    a.append(aq)
    b.append(bq)
    c.append(cq)
    d.append(dq)

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

Solution (Python):

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


def dfs(x):
    if len(x) == N + 1:
        res = 0
        for a, b, c, d in X:
            if x[b] - x[a] == c:
                res += d
        return res

    res = 0
    for i in range(max(1, x[-1]), M + 1):
        x.append(i)
        res = max(res, dfs(x))
        x.pop()

    return res


print(dfs([0]))

Solution (Python):
import itertools
n,m,q = list(map(int, input().split()))
abcd = [[0] * 4 for _ in range(q)]
for i in range(q):
  abcd[i] = list(map(int, input().split()))

M = itertools.combinations_with_replacement(range(m),n)
ans = 0
for A in M:
  for i in range(1,n):
    if A[i] <= A[i-1]:break
  temp = 0
  for i in range(q):
    a = A[abcd[i][0]-1]
    b = A[abcd[i][1]-1]
    c = abcd[i][2]
    d = abcd[i][3]
    if b - a == c:
      temp += d
  ans = max(ans,temp)

print(ans)

Solution (Python):
n,m,q = map(int,input().split())
A = [0]*q
B = [0]*q
C = [0]*q
D = [0]*q



for i in range(q):
    a,b,c,d = map(int,input().split())
    A[i] = a
    B[i] = b
    C[i] = c
    D[i] = d

walist = []
for i in range(2**(n+m-1)):
    if bin(i).count("1") == n:
        wa = 0
        seq = []
        value = 1
        for j in range(n+m-1):
            if (i>>j)&1 == 1:
                seq += [value]
            else:
                value += 1
        for k in range(q):
            if seq[B[k]-1] - seq[A[k]-1] == C[k]:
                wa += D[k]
        walist += [wa]

print(max(walist))

Solution (Python):
import itertools
n,m,q = map(int, input().split())
p=[[0,0,0,0]]
for i in range(q):
    p.append(list(map(int, input().split())))

ans=0

for tmp in itertools.combinations_with_replacement(range(1,m+1),n):
    tokuten=0
    for gyou in range(1,q+1):
        if tmp[p[gyou][1]-1]-tmp[p[gyou][0]-1]==p[gyou][2]:
            tokuten+=p[gyou][3]
    ans=max(ans,tokuten)

print(ans)

Solution (Python):
N,M,Q = map(int, input().split())
ab = tuple(tuple(map(int, input().split())) for _ in range(Q))
ans = 0
def dfs(A):
    global ans
    if len(A) == N:
        tmp = 0
        for i in range(Q):
            a,b,c,d = ab[i]
            if (A[b-1]-A[a-1]) == c:
                tmp += d
        if tmp > ans:
            ans = tmp


    else:
        last = A[-1] if len(A) > 0 else 1
        for i in range(last, M+1):
            A.append(i)
            dfs(A)
            A.pop()
dfs([])
print(ans)

Solution (Python):
import itertools 
N,M,Q = map(int,input().split())
ans = 0
search_list = list(range(M))
value_list = [[0]*4 for _ in range(Q)]
for i in range(Q):
    value_list[i]=list(map(int,input().split()))
for i in itertools.combinations_with_replacement(search_list,N):
    value = 0
    for test in value_list:
        if(i[test[1]-1]-i[test[0]-1]==test[2]):
            value+=test[3]
    ans = max(ans,value)
print(ans)

Solution (Python):
import itertools
N,M,Q = map(int,input().split())
cc = [list(map(int,input().split())) for i in range(Q)]
a = [i for i in range(1,M+1)]
max_count = -float('inf')
for i in itertools.combinations_with_replacement(a,N):
  count = 0
  for j in cc:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      count += j[3]
  max_count = max(max_count,count)
print(max_count)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,m,q = inpm()
ans = 0
abcd = []
for _ in range(q):
    a,b,c,d = inpm()
    abcd.append((a,b,c,d))

for e in itertools.combinations_with_replacement(range(m),n):
    cnt = 0
    for i in range(q):
        [a,b,c,d] = abcd[i]
        if e[b-1] - e[a-1] == c:
            cnt += d
    ans = max(ans,cnt)
print(ans)

Solution (Python):
def permutations(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutations(data):
                ret.append([i] + j)

        return ret
        

N, M, Q = map(int, input().split())

bou = [1] * N
ball = [2] * (M-1)

junretsu = permutations(bou + ball)

a, b, c, d = [], [], [], []
for i in range(Q):
    A, B, C, D = map(int, input().split())
    a.append(A)
    b.append(B)
    c.append(C)
    d.append(D)

ans = 0
for i in range(len(junretsu)):
    souwa = 0
    check = 0
    A = []
    for j in range(N+M-1):
        if junretsu[i][j] == 1:
            A.append(j - check + 1)
            check += 1
    for j in range(Q):
        if (A[b[j]-1] - A[a[j]-1]) == c[j]:
            souwa += d[j]
    
    ans = max(ans, souwa)

print(ans)

Solution (Python):
import itertools
N,M,Q = map(int,input().split())
list_a = []
for i in range(Q):
  a = list(map(int,input().split()))
  list_a.append(a)
list_num =[]
max_num = 0
for i in range(1,M+1):
  list_num.append(i)
num = list(itertools.combinations_with_replacement(list_num, N))
for i in num:
  count=0
  for j in list_a:
    if i[j[1]-1]-i[j[0]-1]==j[2]:
      count+=j[3]
  max_num = max(count,max_num)
print(max_num)

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

def dfs(depth, tail):
    global ans, seq
    if depth == n:
        s = 0
        for a, b, c, d in abcd:
            a -= 1
            b -= 1
            if seq[b] - seq[a] == c:
                s += d
        ans = max(ans, s)
        return

    for i in range(tail, m + 1):
        seq[depth] = i
        dfs(depth + 1, i)

dfs(0, 1)
print(ans)

Solution (Python):
n,m,q = map(int,input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    a[i] -= 1 
    b[i] -= 1 
    
def score(A):
    x = 0
    for ai,bi,ci,di in zip(a,b,c,d):
        if A[bi] - A[ai] == ci:
            x += di
    return x

def dfs(A):
    
    if len(A) == n:
        
        return score(A)
    res = 0
    
    pre = A[-1] if len(A) > 0 else 0 
    for v in range(pre, m):
        A.append(v) 
        res = max(res,dfs(A))      
        A.pop()     
    return res

print(dfs([]))