CodeEff/ECCO huggingface dataset:
# Original solution
import itertools
N,M,Q = list(map(int,input().split()))
list1 = [None for i in range(Q)]
for i in range(Q):
  list1[i] = list(map(int,input().split()))

ans = 0
for x in itertools.combinations_with_replacement(list(range(1,M+1)),N):
  score = 0
  x1 = sorted(x)
  score = sum(y[3] for y in list1 if x1[y[1]-1]-x1[y[0]-1] == y[2])
  if score > ans:
    ans = score

print(ans)

# Optimized solution
N,M,Q = list(map(int,input().split()))
list1 = [None for i in range(Q)]

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

ans = [0]
def dfs(x,n2,m1):
  if len(x) == n2:
    score = sum(x1[3] for x1 in list1 if x[x1[1]-1]-x[x1[0]-1] == x1[2])
    if score > ans[0]:
      ans[0] = score
  else:
    for i in range(1,m1+1,1):
      if i >= x[-1]:
        dfs(x+[i],n2,m1)

dfs([1],N,M)
print((ans[0]))
# Original solution
N,M,Q = list(map(int,input().split()))
list1 = [None for i in range(Q)]

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

ans = [0]
def dfs(x,n2,m1):
  if len(x) == n2:
    score = sum(x1[3] for x1 in list1 if x[x1[1]-1]-x[x1[0]-1] == x1[2])
    if score > ans[0]:
      ans[0] = score
  else:
    for i in range(1,m1+1,1):
      if i >= x[-1]:
        dfs(x+[i],n2,m1)

dfs([1],N,M)
print((ans[0]))

# Optimized solution
import itertools
N,M,Q = list(map(int,input().split()))
list1 = [list(map(int,input().split())) for i in range(Q)] 
  
ans = [0]
def dfs(x,n2,m1):
  if len(x) == n2:
    score = sum(x1[3] for x1 in list1 if x[x1[1]-1]-x[x1[0]-1] == x1[2])
    if score > ans[0]:
      ans[0] = score
  else:
    for i in range(1,m1+1,1):
      if i >= x[-1]:
        x.append(i)
        dfs(x,n2,m1)
        x.pop()

dfs([1],N,M)
print((ans[0]))
# Original solution
def solve(string):
    from itertools import combinations_with_replacement
    n, m, q, *abcd = list(map(int, string.split()))
    ans = 0
    for v in combinations_with_replacement(list(range(1, m + 1)), n):
        v = sorted(list(v))
        ans = max(
            ans,
            sum(d if v[b - 1] - v[a - 1] == c else 0 for a, b, c, d in zip(*[iter(abcd)] * 4)))
    return str(ans)


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))


# Optimized solution
def solve(string):
    from itertools import combinations_with_replacement
    n, m, q, *abcd = list(map(int, string.split()))
    ans = 0
    for v in combinations_with_replacement(list(range(1, m + 1)), n):
        ans = max(
            ans,
            sum(d if v[b - 1] - v[a - 1] == c else 0 for a, b, c, d in zip(*[iter(abcd)] * 4)))
    return str(ans)


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))
# Original solution
from collections import *

def dfs(now):
    global ans
    
    if len(now)==N:
        ans_cand = 0
        
        for a, b, c, d in abcd:
            if now[b-1]-now[a-1]==c:
                ans_cand += d
        
        ans = max(ans, ans_cand)
        return
    
    last = now[-1]
    
    for i in range(last, M+1):
        now.append(i)
        dfs(now)
        now.pop()

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

for i in range(1, M+1):
    dfs(deque([i]))

print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(100)
from collections import *

def dfs(now):
    global ans
    
    if len(now)==N:
        cnt = 0
        
        for a, b, c, d in abcd:
            if now[b-1]-now[a-1]==c:
                cnt += d
        
        ans = max(ans, cnt)
        return
    
    for nex in range(now[-1], M+1):
        now.append(nex)
        dfs(now)
        now.pop()

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

for i in range(1, M+1):
    dfs(deque([i]))

print(ans)
# Original solution
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
from itertools import combinations_with_replacement
l = [i for i in range(1, M+1)]
lis = list(combinations_with_replacement(l, N))

for l in lis:
    tmp = 0
    for j in range(Q):
        a, b, c, d = abcd[j]
        if l[b-1]-l[a-1]==c:
            tmp+=d
    ans = max(tmp, ans)

print(ans)

# Optimized solution
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return list(map(int, input().split()))
sys.setrecursionlimit(10**9)

N, M, Q = mapint()
abcd = [list(mapint()) for _ in range(Q)]

from itertools import combinations_with_replacement

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

print(ans)
# Original solution
import sys
import copy
from collections import deque


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

    seq_list = make_sequence(N, M)
    
    max_score = 0
    for i_seq in seq_list:
        score = 0
        for a, b, c, d in ABCD:
            if (i_seq[b-1] - i_seq[a-1]) == c:
                score += d
        
        max_score = max(max_score, score)

    print(max_score)
    return


def make_sequence(N, M):
    seq_list = []
    sequence = [-1] * N

    seq_deque = deque()
    now_ele = 0
    for i in range(M):
        sequence[0] = i
        now_seq = copy.copy(sequence)
        seq_deque.append([now_ele, now_seq])

    while len(seq_deque) > 0:
        pre_ele ,pre_seq = seq_deque.popleft()
        if pre_seq[-1] != -1:
            seq_list.append(pre_seq)
            continue

        end_ele = M - sum(pre_seq[0:pre_ele+1])
        for i in range(end_ele):
            pre_seq[pre_ele+1] = i
            now_seq = copy.copy(pre_seq)
            seq_deque.append([pre_ele+1 ,now_seq])

    for i_seq in seq_list:
        now_value = 1
        for i, i_value in enumerate(i_seq):
            now_value += i_value
            i_seq[i] = now_value

    return seq_list


if __name__ == '__main__':
    main()
    sys.exit(0)

# Optimized solution
import sys
import itertools

def main():
    N, M, Q = list(map(int, input().split()))
    ABCD = [[int(_) for _ in input().split()] for i in range(Q)]
    value_range = list(range(1, M+1))
    seq_list = list(itertools.combinations_with_replacement(value_range, N))

    max_score = 0
    for i_seq in seq_list:
        score = 0
        for a, b, c, d in ABCD:
            if (i_seq[b-1] - i_seq[a-1]) == c:
                score += d

        max_score = max(max_score, score)

    print(max_score)
    return

if __name__ == '__main__':
    main()
    sys.exit(0)
# Original solution
N, M, Q = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(Q)]


def dfs(x):
    if len(x) == N + 1:
        tmp = 0
        for i in range(Q):
            if x[X[i][1]] - x[X[i][0]] == X[i][2]:
                tmp += X[i][3]
        return tmp

    x.append(x[-1])
    res = 0
    while x[-1] <= M:
        res = max(res, dfs(x))
        x[-1] += 1
    x.pop()

    return res

print((dfs([1])))


# Optimized solution
from itertools import combinations_with_replacement

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

ans = 0
for x in combinations_with_replacement(list(range(1, M + 1)), N):
    tmp = 0
    for i in range(Q):
        if x[X[i][1] - 1] - x[X[i][0] - 1] == X[i][2]:
            tmp += X[i][3]
    ans = max(ans, tmp)

print(ans)
# Original solution

N, M, Q = list(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])))


# Optimized solution

N, M, Q = list(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 n in range(x[-1], M + 1):
        x.append(n)
        res = max(res, dfs(x))
        x.pop()
    return res


print((dfs([1])))
# Original solution
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
N, M, Q = LI()

abcd = []

all = 0
for i in range(Q):
    a, b, c, d = LI()
    a -= 1
    b -= 1
    abcd.append([a, b, c, d])
    all += d

abcd = sorted(abcd, key= lambda x:x[3], reverse= True)
ans = 0
if Q <= 2:
    for q in range(Q):
        ans += abcd[q][-1]
    print(ans)
    sys.exit()

def cal_ans(ans, A):
    tmp = 0
    tmp_all = copy.deepcopy(all)
    for i in range(Q):
        a, b, c, d = abcd[i]
        tmp_all -= d
        if A[b] - A[a] == c:
            tmp += d
        else:
            if tmp_all + tmp <= ans:
                return ans
    ans = max(ans, tmp)
    return ans

def n2kdigit(n, k):
    bi=''
    before = 1
    while n!=0:
        if before <= (n % k):
            # print(n)
            bi+=str(n%abs(k))
            before = n % k
            if k<0:n=-(-n//k)
            else:n=n//k
        else:
            return None
    if len(bi) < N:
        return None
    return bi
ans = 0
for i in range((M + 1) ** (N)):
    A = n2kdigit(i, M + 1)
    # print(A)
    if A is not None:
        ans = cal_ans(ans, [int(i) for i in A])
print(ans)

# Optimized solution
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import itertools
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
N, M, Q = LI()

abcd = []

all = 0
for i in range(Q):
    a, b, c, d = LI()
    a -= 1
    b -= 1
    abcd.append([a, b, c, d])
    all += d

abcd = sorted(abcd, key= lambda x:x[3], reverse= True)
ans = 0
if Q <= 2:
    for q in range(Q):
        ans += abcd[q][-1]
    print(ans)
    sys.exit()

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

def n2kdigit(n, k):
    bi=''
    before = 1
    while n!=0:
        if before <= (n % k):
            # print(n)
            bi+=str(n%abs(k))
            before = n % k
            if k<0:n=-(-n//k)
            else:n=n//k
        else:
            return None
    if len(bi) < N:
        return None
    return bi
ans = 0

for i in itertools.combinations_with_replacement(list(range(1, M+1)), N):
    # print(i)
    ans = max(ans, cal_ans(i))
print(ans)
# Original solution
from itertools import combinations_with_replacement
def main():
    n, m, q = list(map(int, input().split()))
    qlist = []
    for _ in range(q):
        qlist.append(tuple(map(int, input().split())))

    nums = [i for i in range(1, m + 1)]
    alist = tuple(combinations_with_replacement(nums, n))

    r = 0
    for al in alist:
        tr = 0
        for eq in qlist:
            eq0 = eq[0] - 1
            eq1 = eq[1] - 1
            eq2 = eq[2]
            eq3 = eq[3]
            if al[eq1] - al[eq0] == eq2:
                tr += eq3
        r = max(r, tr)

    print(r)

if __name__ == '__main__':
    main()


# Optimized solution
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
def main():
    n, m, q = list(map(int, input().split()))
    abcd = []
    for _ in range(q):
        q = tuple(map(int, input().split()))
        abcd.append(q)
    c1 = tuple(combinations_with_replacement(list(range(1,m+1)), n))
    r = 0
    for ce in c1:
        t = 0
        for qe in abcd:
            a, b, c, d = qe
            a -= 1
            b -= 1
            if ce[b] - ce[a] == c:
                t += d
        r = max(r, t)
    print(r)

if __name__ == '__main__':
    main()
# Original solution
# C - Many Requirements
from collections import deque
from typing import Tuple

N: int
M: int
conditions: Tuple[Tuple[int, int, int, int]]


def dfs():
    queue = deque([[1]])
    highest = 0
    while queue:
        seq = queue.popleft()
        if len(seq) == N + 1:
            highest = max(
                highest, sum(d for a, b, c, d in conditions if seq[b] - seq[a] == c)
            )
        else:
            for i in range(seq[-1], M + 1):
                queue.append(seq + [i])
    return highest


def main():
    global N, M, conditions
    N, M, Q, *ABCD = list(map(int, open(0).read().split()))
    conditions = tuple(zip(*[iter(ABCD)] * 4))
    print((dfs()))


if __name__ == "__main__":
    main()


# Optimized solution
# C - Many Requirements
from itertools import combinations_with_replacement


def main():
    N, M, Q, *ABCD = list(map(int, open(0).read().split()))
    conditions = tuple(zip(*[iter(ABCD)] * 4))
    ans = max(
        sum(d for a, b, c, d in conditions if comb[b - 1] - comb[a - 1] == c)
        for comb in combinations_with_replacement(list(range(1, M + 1)), N)
    )
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
N, M, Q = list(map(int, input().split()))

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

l = []
def dfs(lis, y):
  if len(lis) == N:
    l.append(lis)
    return
  else:
    for i in range(y, M+1):
      z = list(lis)
      z.append(i)
      dfs(z, i)
    return
      
dfs([], 1)

ans = 0
for A in l:
  tmp = 0
  for a, b, c, d in query:
    if A[b-1] - A[a-1] == c:
      tmp += d
  ans = max(ans, tmp)

print(ans)

# Optimized solution
from itertools import combinations_with_replacement

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

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

print(ans)
# Original solution
from sys import stdin
from collections import deque
def main():
    #入力
    readline=stdin.readline
    N,M,Q=list(map(int,readline().split()))
    a=[]
    b=[]
    c=[]
    d=[]
    for i in range(Q):
        li=list(map(int,readline().split()))
        a.append(li[0])
        b.append(li[1])
        c.append(li[2])
        d.append(li[3])

    As=[]
    de=[]
    for i in range(1,M+1):
        de.append([i])
    de=deque(de)
    while len(de)>=1:
        x=de.popleft()
        if len(x)==N:
            As.append(x)
        else:
            n=x[-1]
            for i in range(n,M+1):
                y=x[:]
                y.append(i)
                de.append(y)

    max_res=0
    for i in range(len(As)):
        A=As[i]
        res=0
        for j in range(Q):
            if A[b[j]-1]-A[a[j]-1]==c[j]:
                res+=d[j]
        max_res=max(max_res,res)
    print(max_res)
if __name__=="__main__":
    main()

# Optimized solution
from sys import stdin
import itertools
def main():
    #入力
    readline=stdin.readline
    N,M,Q=list(map(int,readline().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]=list(map(int,readline().split()))

    arrays=list(itertools.combinations_with_replacement(list(range(1,M+1)),N))
    max_res=0
    for A in arrays:
        res=0
        for i in range(Q):
            if A[b[i]-1]-A[a[i]-1]==c[i]:
                res+=d[i]
        max_res=max(max_res,res)
    print(max_res)
if __name__=="__main__":
    main()
# Original solution
import itertools
N, M, Q = list(map(int,input().split()))
s = [tuple(map(int, input().split())) for i  in range(Q)]
l = list(range(1,M+1))
ans = []
for v in itertools.combinations_with_replacement(l, N):
    count = 0
    for i in range(Q):
        a, b, c, d = s[i]
        if v[b-1] - v[a-1] == c:
            count += d
    ans.append(count)
print((max(ans)))


# Optimized solution
import itertools
N, M, Q = list(map(int,input().split()))
s = [tuple(map(int, input().split())) for i  in range(Q)]
l = list(range(1,M+1))
ans = [0]
for v in itertools.combinations_with_replacement(l, N):
    count = 0
    for i in range(Q):
        a, b, c, d = s[i]
        if v[b-1] - v[a-1] == c:
            count += d
    ans.append(count)
print((max(0, max(ans))))
# Original solution
#!/usr/bin/env python3
# スペース区切りの整数の入力

from collections import deque

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

# スペース区切りの整数の入力
N, M, Q = list(map(int, input().split()))
#配列の入力
data = [list(map(int, input().split())) for _ in range(Q)]

ans = -1

que = deque()

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


while que:
    seq = que.popleft()

    if len(seq) == N:
        score = calc(seq)
        ans = max(ans, score)
    else:
        for i in range(seq[-1], M+1):
            seq_next = seq + [i]
            que.append(seq_next)

print(ans)


# Optimized solution
#!/usr/bin/env python3
# スペース区切りの整数の入力

def dfs(seq):
    ans = 0
    if len(seq) == N:
        score_ret = 0
        for a, b, c, d in data:
            if seq[b-1] - seq[a-1] == c:
                score_ret += d
        return score_ret
    else:
        for i in range(seq[-1], M+1):
            seq_next = seq + (i,)
            score = dfs(seq_next)
            ans = max(ans, score)
    
    return ans

# スペース区切りの整数の入力
N, M, Q = list(map(int, input().split()))
#配列の入力
data = [list(map(int, input().split())) for _ in range(Q)]

ans = -1
score = dfs((1,))
ans = max(ans, score)
print(ans)
# Original solution
N,M,Q = list(map(int,input().split()))
ls_rq = [list(map(int, input().split())) for i in range(Q)]

#深さ優先探索により各数列を生成する
As=[]
from queue import LifoQueue as lq
S=lq()
S.put([])
while not S.empty():
  a=S.get()
  if len(a)==N:
    As.append(a)
  else:
    if len(a)==0:
      b=1
    else:
      b=a[-1]
    for  i in range(b,M+1):
      S.put(a+[i])

#各数列のスコアを求める
ls_score=[]
for ls in As:
  score=0
  for j in range(Q):
    if ls[ls_rq[j][1]-1]-ls[ls_rq[j][0]-1]==ls_rq[j][2]:
      score+=ls_rq[j][3]
  ls_score.append(score)    
  score=0

#スコアの最大値を求める  
ans=max(ls_score+[0])  
print(ans)  


# Optimized solution
N,M,Q = list(map(int,input().split()))
ls_rq = [list(map(int, input().split())) for i in range(Q)]

#深さ優先探索により各数列を生成する
As=[]
from queue import LifoQueue as lq
S=lq()
S.put([])
while not S.empty():
  a=S.get()
  if len(a)==N:
    As.append(a)
  else:
    if len(a)==0:
      b=1
    else:
      b=a[-1]
    for  i in range(b,M+1):
      S.put(a+[i])

#各数列のスコアを求める
ls_score=[]
for ls in As:
  score=0
  for j in range(Q):
    if ls[ls_rq[j][1]-1]-ls[ls_rq[j][0]-1]==ls_rq[j][2]:
      score+=ls_rq[j][3]
  ls_score.append(score)    
  score=0

#スコアの最大値を求める  
ans=max(ls_score)  
print(ans)
# Original solution
import itertools
n, m, q = list(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)
e = [i+1 for i in range(m)]
ans = 0
for i in itertools.combinations_with_replacement(e,n-1):
	f = list(i)
	f.append(1)
	f.sort()
	w = 0
	for j in range(q):
		if f[b[j]-1]-f[a[j]-1] == c[j]:
			w += d[j]
	ans = max(ans,w)
print (ans)


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

v = []
def dfs(u,ary):
	if len(ary) == n:
		ary.sort()
		cnt = 0
		for j in range(q):
			a,b,c,d = query[j]
			if ary[b-1] - ary[a-1] == c:
				cnt += d
		v.append(cnt)
	else:
		for j in range(u,m+1):
			dfs(j,ary+[j])


dfs(1,[1])

print((max(v)))
# Original solution
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(list(range(1, M+1)), N):
  score = 0
  for i in range(Q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    if A[b-1]-A[a-1] == c:
      score += d
  ans = max(ans,score)
print(ans)


# Optimized solution
import itertools
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
total = []
for A in itertools.combinations_with_replacement(list(range(1, M+1)), N):
  score = 0
  for a,b,c,d in abcd:
    if A[b-1]-A[a-1] == c:
      score += d
    total.append(score)
print((max(total)))
# Original solution
import itertools
def LI():return list(map(int,input().split()))

n,m,q=LI()
abcd=[]
for _ in range(q):
    a,b,c,d=LI()
    a-=1
    b-=1
    abcd.append((a,b,c,d))

# 数列Aの生成
Alist=itertools.combinations_with_replacement(list(range(m)),n)
ans=0
for A in Alist:
    cnt=0
    for a,b,c,d in abcd:
        if A[b]-A[a]==c:
            cnt+=d
    ans=max(ans,cnt)
print(ans)
    

# Optimized solution
# coding: utf-8
# Your code here!
import itertools
def LI():return list(map(int,input().split()))

n,m,q=LI()
abcd=[]
for _ in range(q):
    a,b,c,d=LI()
    a-=1
    b-=1
    abcd.append((a,b,c,d))

# 数列Aの生成
Alist=itertools.combinations_with_replacement(list(range(m)),n)
ans=0
for A in Alist:
    cnt=0
    for a,b,c,d in abcd:
        if A[b]-A[a]==c:
            cnt+=d
    ans=max(ans,cnt)
print(ans)
# Original solution
# coding: utf-8
# Your code here!
import itertools
def LI():return list(map(int,input().split()))

n,m,q=LI()
abcd=[]
for _ in range(q):
    a,b,c,d=LI()
    a-=1
    b-=1
    abcd.append((a,b,c,d))

# 数列Aの生成
Alist=itertools.combinations_with_replacement(list(range(m)),n)
ans=0
for A in Alist:
    cnt=0
    for a,b,c,d in abcd:
        if A[b]-A[a]==c:
            cnt+=d
    ans=max(ans,cnt)
print(ans)
    

# Optimized solution
import itertools
def LI():return list(map(int,input().split()))
n,m,q=LI()
abcd=[]
for _ in range(q):
  a,b,c,d=LI()
  a-=1
  b-=1  
  abcd.append((a,b,c,d))
As=itertools.combinations_with_replacement(list(range(m)),n)
ans=0
for A in As:
  cnt=0
  for a,b,c,d in abcd:
    if A[b]-A[a]==c:cnt+=d
  ans=max(ans,cnt)
print(ans)
# Original solution
f=lambda:list(map(int,input().split()))
n,m,q=f()
lq=[tuple(f()) for _ in range(q)]
ans=0
from itertools import *
for t in combinations_with_replacement(list(range(m)),n):
  s=0
  for a,b,c,d in lq:
    if t[b-1]-t[a-1]==c: s+=d
  ans=max(ans,s)
print(ans)

# Optimized solution
f=lambda:list(map(int,input().split()))
n,m,q=f()
Q=[list(f())for _ in range(q)]
from itertools import*
print((max(sum(d for a,b,c,d in Q if l[b-1]-l[a-1]==c)for l in combinations_with_replacement(list(range(m)),n))))
# Original solution
n,m,q = list(map(int, input().split()))

import itertools
a = []
for i in range(q):
    a.append(input().split())

suma = sum([int(aa[3]) for aa in a])

ans = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n):
    ans_ = 0
    for j in range(q):
        if v[int(a[j][1])-1] - v[int(a[j][0])-1] == int(a[j][2]):
            ans_ += int(a[j][3])
    if ans_ > ans:
        ans = ans_
        if ans == suma:
            break
print(ans)

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

import itertools
a = []
for i in range(q):
    a.append(input().split())

suma = sum([int(aa[3]) for aa in a])

ans = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n-1):
    ans_ = 0
    v = [0]+list(v)
    for j in range(q):
        if v[int(a[j][1])-1] - v[int(a[j][0])-1] == int(a[j][2]):
            ans_ += int(a[j][3])
    if ans_ > ans:
        ans = ans_
        if ans == suma:
            break
print(ans)