AtCoder Beginner Contest 165 - Many Requirements

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-----
3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

-----Sample Output-----
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.

Difficulty: EASY

Starter code:

Solutions:
(N, M, Q) = map(int, input().split())
A = list()
lis = list()
ans = 0

def make_list(n, m):
	if n == N:
		A.append(lis.copy())
		return
	for i in range(m, M + 1):
		lis.append(i)
		make_list(n + 1, i)
		lis.pop()
make_list(0, 1)
calc = [list(map(int, input().split())) for _ in range(Q)]
for a in A:
	tmp = 0
	for c in calc:
		if a[c[1] - 1] - a[c[0] - 1] == c[2]:
			tmp += c[3]
	if tmp > ans:
		ans = tmp
print(ans)


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


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)


import itertools
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
print(max((sum((d for (a, b, c, d) in abcd if i[b - 1] - i[a - 1] == c)) for i in itertools.combinations_with_replacement(range(m), n))))


import itertools
(N, M, Q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
Max = 0
for C in itertools.combinations_with_replacement(range(1, M + 1), N):
	score = sum([d for (a, b, c, d) in abcd if C[b - 1] - C[a - 1] == c])
	Max = max(Max, score)
print(Max)


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


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


import itertools

def main():
	(N, M, Q) = list(map(int, input().split()))
	a = []
	b = []
	c = []
	d = []
	for i in range(Q):
		(ai, bi, ci, di) = list(map(int, input().split()))
		a.append(ai)
		b.append(bi)
		c.append(ci)
		d.append(di)
	ans = 0
	for A in list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)):
		score = 0
		for i in range(Q):
			if A[b[i] - 1] - A[a[i] - 1] == c[i]:
				score += d[i]
		if ans < score:
			ans = score
	print(ans)

def __starting_point():
	main()
__starting_point()


from itertools import combinations_with_replacement as cwr
(N, M, Q) = map(int, input().split())
ABCD = []
for _ in range(Q):
	ABCD.append(list(map(int, input().split())))
maxscore = 0
for comb in cwr(range(1, M + 1), N):
	score = 0
	for row in ABCD:
		if comb[row[1] - 1] - comb[row[0] - 1] == row[2]:
			score += row[3]
	maxscore = max(maxscore, score)
print(maxscore)


import itertools
(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for c in itertools.combinations_with_replacement(range(M), N):
	tmp = 0
	for i in range(Q):
		if c[abcd[i][1] - 1] - c[abcd[i][0] - 1] == abcd[i][2]:
			tmp += abcd[i][3]
	ans = max(ans, tmp)
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for n in combinations_with_replacement(range(1, M + 1), N):
	tmp = 0
	for (a, b, c, d) in abcd:
		if n[b - 1] - n[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
abcd_list = [[int(i) for i in input().split()] for _ in range(q)]
ans = 0
for i in itertools.combinations_with_replacement(range(1, m + 1), n):
	A = sorted(i)
	score = 0
	for (a, b, c, d) in abcd_list:
		if A[a - 1] + c == A[b - 1]:
			score += d
	ans = max(ans, score)
print(ans)


(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(q)]
from itertools import combinations_with_replacement as comb_rplc
cmb = comb_rplc(range(1, m + 1), n)
result = 0
for A in cmb:
	tmp = 0
	for (a, b, c, d) in abcd:
		if A[b - 1] - A[a - 1] == c:
			tmp += d
	result = max(tmp, result)
print(result)


import itertools
(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for q in range(Q)]
print(max((sum((d for (a, b, c, d) in abcd if i[b - 1] - i[a - 1] == c)) for i in itertools.combinations_with_replacement(range(M), N))))


from itertools import combinations_with_replacement as cbr
(N, M, Q) = list(map(int, input().split()))
X = []
for _ in range(Q):
	X.append(list(map(int, input().split())))
ans = -1
for p in cbr(range(1, M + 1), N):
	wk = 0
	for x in X:
		(a, b, c, d) = x
		if p[b - 1] - p[a - 1] == c:
			wk += d
	ans = max(wk, ans)
print(ans)


import itertools
from operator import mul
from functools import reduce

def combinations_count(n, r):
	r = min(r, n - r)
	numer = reduce(mul, list(range(n, n - r, -1)), 1)
	denom = reduce(mul, list(range(1, r + 1)), 1)
	return numer // denom
(N, M, Q) = list(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]) = list(map(int, input().split()))
l = list(range(1, M + 1))
h = combinations_count(M + N - 1, N)
H = [0] * h
i = 0
for v in itertools.combinations_with_replacement(l, N):
	H[i] = v
	i += 1
D = [0 for j in range(h)]
for i in range(h):
	for j in range(Q):
		if H[i][b[j] - 1] - H[i][a[j] - 1] == c[j]:
			D[i] += d[j]
print(max(D))


import itertools
(N, M, Q) = [int(i) for i in input().split()]
ABCD = [list(map(int, input().split())) for i in range(Q)]
score = 0
for A in itertools.combinations_with_replacement(range(1, M + 1), N):
	tmp = 0
	for (a, b, c, d) in ABCD:
		if A[b - 1] - A[a - 1] == c:
			tmp += d
	score = max(score, tmp)
print(score)


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


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

def check(A, Q):
	if A[Q[1] - 1] - A[Q[0] - 1] == Q[2]:
		return Q[3]
	else:
		return 0
ans = 0
for i in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
	tmp = 0
	for j in a:
		tmp += check(i, j)
	if ans < tmp:
		ans = tmp
print(ans)


(n, m, q) = map(int, input().split())
from itertools import combinations_with_replacement as com
l = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for z in com(range(m), n):
	now = 0
	for (a, b, c, d) in l:
		if z[b - 1] - z[a - 1] == c:
			now += d
	ans = max(ans, now)
print(ans)


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

def f(B, n):
	ans = 0
	if len(B) < N:
		for i in range(n, M + 1):
			ans = max(ans, f(B + [i], i))
	else:
		for j in range(Q):
			if B[A[j][1] - 1] - B[A[j][0] - 1] == A[j][2]:
				ans += A[j][3]
		return ans
	return ans
print(f([], 1))


from itertools import *
(n, m, q) = map(int, input().split())
aaa = [int(i) for i in range(1, m + 1)]
a = list(combinations_with_replacement(aaa, n))
l = [list(map(int, input().split())) for i in range(q)]
ans = 0
for i in a:
	aa = 0
	for j in l:
		if i[j[1] - 1] - i[j[0] - 1] == j[2]:
			aa += j[3]
	if aa > ans:
		ans = aa
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
T = []
for q in range(Q):
	T.append(list(map(int, input().split())))
A = list(combinations_with_replacement(list(range(1, M + 1)), N))
Alist = [list(a) for a in A]
Max = 0
for a in Alist:
	cost = 0
	for t in T:
		if a[t[1] - 1] - a[t[0] - 1] == t[2]:
			cost += t[3]
	if cost > Max:
		Max = cost
print(Max)


abcd = []
max_ans = 0

def resolve(l):
	total = 0
	for (a, b, c, d) in abcd:
		if l[b - 1] - l[a - 1] == c:
			total += d
	return total

def comb(l, start, end, now, suspend):
	nonlocal max_ans
	if now == suspend:
		max_ans = max([max_ans, resolve(l)])
		return
	for i in range(start, end + 1, 1):
		comb(l + [i], i, end, now + 1, suspend)
(n, m, q) = list(map(int, input().split()))
for i in range(q):
	abcd.append(list(map(int, input().split())))
comb([], 1, m, 0, n)
print(max_ans)


(N, M, Q) = map(int, input().split())
qs = [list(map(int, input().split())) for i in range(Q)]
from itertools import combinations_with_replacement
com = combinations_with_replacement(range(1, M + 1), N)
ans = 0
for A in com:
	tmp = 0
	for (a, b, c, d) in qs:
		if A[b - 1] - A[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


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

def dfs(d):
	if d == N - 1:
		t = 0
		for (a, b, c, d) in R:
			if S[~-b] - S[~-a] == c:
				t += d
		return t
	else:
		m = 0
		for i in range(S[d], M + 1):
			S[d + 1] = i
			m = max(m, dfs(d + 1))
		return m
print(dfs(0))


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
s_list = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for c in combinations_with_replacement(range(1, m + 1), n):
	v = 0
	for s in s_list:
		if c[s[1] - 1] - c[s[0] - 1] == s[2]:
			v += s[3]
	ans = max(ans, v)
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	score = 0
	for i in range(Q):
		(a, b, c, d) = abcd[i]
		if A[b - 1] - A[a - 1] == c:
			score += d
	ans = max(ans, score)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
l = [list(map(int, input().split())) for i in range(q)]
x = 0
for v in itertools.combinations_with_replacement(range(1, m + 1), n):
	t = list(v)
	p = 0
	for i in l:
		p += i[3] * (t[i[1] - 1] - t[i[0] - 1] == i[2])
	x = max(x, p)
print(x)


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

def dfs(seq):
	u = 0
	if len(seq) == n:
		ans = 0
		for (a, b, c, d) in data:
			if seq[b - 1] - seq[a - 1] == c:
				ans += d
		return ans
	else:
		for i in range(seq[-1], m + 1):
			seq_next = seq.copy()
			seq_next.append(i)
			u = max(u, dfs(seq_next))
		return u
data = []
for _ in range(q):
	A = list(map(int, input().split()))
	data.append(A)
print(dfs([1]))


from itertools import combinations_with_replacement
(N, M, Q) = [int(n) for n in input().split()]
X = [[int(n) for n in input().split()] for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	t = 0
	for x in X:
		if A[x[1] - 1] - A[x[0] - 1] == x[2]:
			t += x[3]
	ans = max(ans, t)
print(ans)


from itertools import combinations_with_replacement as c
(n, m, q) = [int(i) for i in input().split()]
abc = [[int(i) for i in input().split()] for _ in range(q)]
aa = list(range(1, m + 1))
ans = 0
for bb in c(aa, n):
	cc = 0
	for (a, b, c, d) in abc:
		if bb[b - 1] - bb[a - 1] == c:
			cc += d
	ans = max(ans, cc)
print(ans)


import numpy as np
import itertools
(N, M, Q) = list(map(int, input().split()))
G = []
for i in range(Q):
	(a, b, c, d) = list(map(int, input().split()))
	G.append([a, b, c, d])
ans = 0
A = np.array(list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)))
n = len(A)
score = np.zeros(n, np.int32)
for (a, b, c, d) in G:
	cond = A[:, b - 1] - A[:, a - 1] == c
	score += d * cond
print(score.max())


import itertools
(n, m, q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
for com in itertools.combinations_with_replacement(range(1, m + 1), n):
	score = sum([d for (a, b, c, d) in abcd if com[b - 1] - com[a - 1] == c])
	if ans < score:
		ans = score
print(ans)


(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):
	tmp = 0
	for (ai, bi, ci, di) in zip(a, b, c, d):
		if A[bi] - A[ai] == ci:
			tmp += di
	return tmp

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


from itertools import combinations_with_replacement as c
(N, M, Q) = map(int, input().split())
L = [tuple(map(int, input().split())) for _ in range(Q)]
print(max([sum((d if S[b - 1] - S[a - 1] == c else 0 for (a, b, c, d) in L)) for S in c(range(1, M + 1), N)]))


import itertools
(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
res = 0
for product in itertools.combinations_with_replacement(range(1, M + 1), N):
	S = 0
	for (a, b, c, d) in abcd:
		if product[b - 1] - product[a - 1] == c:
			S += d
	res = max(res, S)
print(res)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
abcdn = [tuple(map(int, input().split())) for _ in range(Q)]
print(max([sum((d if AN[b - 1] - AN[a - 1] == c else 0 for (a, b, c, d) in abcdn)) for AN in combinations_with_replacement(range(1, M + 1), N)]))


import sys

def IS():
	return sys.stdin.readline().rstrip()

def II():
	return int(IS())

def MII():
	return list(map(int, IS().split()))

def main():
	(n, m, q) = MII()
	query = [MII() for _ in range(q)]

	def calc_point(lst):
		sumv = 0
		for (a, b, c, d) in query:
			if lst[b - 1] - lst[a - 1] == c:
				sumv += d
		return sumv

	def dfs(lst):
		if len(lst) == n:
			return calc_point(lst)
		point = 0
		last_elm = lst[-1]
		for i in range(last_elm, m + 1):
			point = max(point, dfs(lst + [i]))
		return point
	print(dfs([1]))

def __starting_point():
	main()
__starting_point()


import sys
(N, M, Q) = map(int, input().split())
abcd = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
ans = 0
done = set()
todo = [(i,) for i in range(1, M + 1)]
while todo:
	A = todo.pop()
	done.add(A)
	if len(A) == N:
		total = 0
		for (a, b, c, d) in abcd:
			if A[b - 1] - A[a - 1] == c:
				total += d
		ans = max(ans, total)
		continue
	for i in range(A[-1], M + 1):
		nA = A + (i,)
		if not nA in done:
			todo.append(nA)
print(ans)


(n, m, q) = map(int, input().split())
ans = -float('INF')
l = [list(map(int, input().split())) for i in range(q)]
from itertools import combinations_with_replacement as pe
for i in pe(range(m), r=n):
	p = 0
	for (x, y, di, po) in l:
		if i[y - 1] - i[x - 1] == di:
			p += po
	ans = max(p, ans)
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
ls = []
for _ in range(Q):
	(a, b, c, d) = map(int, input().split())
	ls.append((a - 1, b - 1, c, d))
ans = -1
for A in combinations_with_replacement(range(1, M + 1), r=N):
	sm = 0
	for (a, b, c, d) in ls:
		if A[b] - A[a] == c:
			sm += d
	ans = max(ans, sm)
print(ans)


(N, M, Q) = map(int, input().split())
X = [list(map(int, input().split())) for i in range(Q)]
import itertools
a = []
for i in range(1, M + 1):
	a.append(i)
A = list(itertools.combinations_with_replacement(a, N))
ans = 0
for i in range(len(A)):
	K = A[i]
	p = 0
	for (a, b, c, d) in X:
		if K[b - 1] - K[a - 1] == c:
			p += d
	ans = max(p, ans)
print(ans)


from itertools import combinations_with_replacement as comb
(n, m, q) = map(int, input().split())
ans = 0
query = [[int(i) for i in input().split()] for _ in range(q)]
for A in comb(range(1, m + 1), n):
	cnt = 0
	for i in range(q):
		(a, b, c, d) = query[i]
		if A[b - 1] - A[a - 1] == c:
			cnt += d
	ans = max(cnt, ans)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
li = [list(map(int, input().split())) for i in range(q)]
ans = 0
num_li = itertools.combinations_with_replacement(range(1, m + 1), n)
for i in num_li:
	temp = 0
	for j in li:
		(a, b, c, d) = j
		if i[b - 1] - i[a - 1] == c:
			temp += d
	ans = max(ans, temp)
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = list(map(int, input().split()))
Q_ = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	ans = max(ans, sum([q[3] for q in Q_ if A[q[1] - 1] - A[q[0] - 1] == q[2]]))
print(ans)


from itertools import combinations_with_replacement

def main():
	(n, m, q) = list(map(int, input().split()))
	xs = []
	for i in range(q):
		xs.append(list(map(int, input().split())))
	max = 0
	for suretu in combinations_with_replacement(list(range(1, m + 1)), n):
		wa = 0
		for x in xs:
			(a, b, c, d) = x
			if suretu[b - 1] - suretu[a - 1] == c:
				wa += d
		if wa > max:
			max = wa
	print(max)

def __starting_point():
	main()
__starting_point()


(n, m, q) = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(q)]
ans = 0
import itertools
for con in itertools.combinations_with_replacement(range(1, m + 1), n):
	tmpAns = 0
	for lst in arr:
		if con[lst[1] - 1] - con[lst[0] - 1] == lst[2]:
			tmpAns += lst[3]
	ans = max(ans, tmpAns)
print(ans)


from itertools import combinations_with_replacement as H
(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
ret = []
for ls in list(H(range(1, M + 1), N)):
	cand = 0
	for (a, b, c, d) in abcd:
		if ls[b - 1] - ls[a - 1] == c:
			cand += d
	ret.append(cand)
print(max(ret))


from itertools import combinations_with_replacement as combi
(N, M, Q) = map(int, input().split())
a = [0] * 55
b = [0] * 55
c = [0] * 55
d = [0] * 55
for i in range(Q):
	(a[i], b[i], c[i], d[i]) = map(int, input().split())
candidate = combi(range(1, M + 1), N)
ans = 0
for A in candidate:
	score = 0
	for i in range(Q):
		if A[b[i] - 1] - A[a[i] - 1] == c[i]:
			score += d[i]
	ans = max(ans, score)
print(ans)


(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
from itertools import combinations_with_replacement as com
ans = 0
for A in com(range(1, M + 1), N):
	score = 0
	for (a, b, c, d) in abcd:
		if A[b - 1] - A[a - 1] == c:
			score += d
	if ans < score:
		ans = score
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
query = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(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)


from itertools import combinations_with_replacement as c
(N, M, Q) = map(int, input().split())
l1 = list((list(map(int, input().split())) for _ in range(Q)))
ans = 0
for l2 in c(range(1, M + 1), N):
	total = 0
	for i in range(Q):
		if l2[l1[i][1] - 1] - l2[l1[i][0] - 1] == l1[i][2]:
			total += l1[i][3]
	ans = max(ans, total)
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
queue = []
for _ in range(Q):
	queue.append(list(map(int, input().split())))
ans = 0
for A in itertools.combinations_with_replacement(range(1, M + 1), N):
	tmp = 0
	for q in queue:
		if A[q[1] - 1] - A[q[0] - 1] == q[2]:
			tmp += q[3]
	ans = max(ans, tmp)
print(ans)


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


import itertools
(N, M, Q) = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
tl = itertools.combinations_with_replacement(range(1, M + 1), N)
ans = 0
for l in tl:
	t = 0
	for (a, b, c, d) in ABCD:
		if l[b - 1] - l[a - 1] == c:
			t += d
	ans = max(ans, t)
print(ans)


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


import itertools
(N, M, Q) = map(int, input().split())
buff = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in itertools.combinations_with_replacement(range(1, M + 1), N):
	score = 0
	for (a, b, c, d) in buff:
		if A[b - 1] - A[a - 1] == c:
			score += d
	ans = max(ans, score)
print(ans)


import itertools
(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())
ans = 0
for C in itertools.combinations_with_replacement(range(1, m + 1), n):
	x = 0
	for i in range(q):
		if C[b[i] - 1] - C[a[i] - 1] == c[i]:
			x += d[i]
	ans = max(ans, x)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
h = []
for i in range(1, m + 1):
	h.append(i)
ans = 0
for j in itertools.combinations_with_replacement(h, n):
	temp = 0
	for (a, b, c, d) in abcd:
		if j[b - 1] - j[a - 1] == c:
			temp += d
	ans = max(ans, temp)
print(ans)


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
Q = [list(map(int, input().split())) for i in range(q)]
ans = 0
for s in combinations_with_replacement(range(1, m + 1), n):
	ans_ = 0
	for k in Q:
		(a, b, c, d) = k
		if s[b - 1] - s[a - 1] == c:
			ans_ += d
	ans = max(ans_, ans)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
l = set(range(1, m + 1))
h = itertools.combinations_with_replacement(l, n)
ans = 0
for i in h:
	tmp = 0
	for (a, b, c, d) in abcd:
		if i[b - 1] - i[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


import itertools
(N, M, Q) = list(map(int, input().split()))
src = []
for q in range(Q):
	src.append(list(map(int, input().split())))
ML = [n + 1 for n in range(M)]
X = list(itertools.combinations_with_replacement(ML, N))
L = []
for x in X:
	a = 0
	for s in src:
		if x[s[1] - 1] - x[s[0] - 1] == s[2]:
			a += s[3]
	L.append(a)
print(max(L))


from itertools import combinations_with_replacement as comb_rplc
(N, M, Q) = map(int, input().split())
array = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for seq in comb_rplc(range(1, M + 1), N):
	score = 0
	for (a, b, c, d) in array:
		if seq[b - 1] - seq[a - 1] == c:
			score += d
	ans = max(score, ans)
print(ans)


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))


import bisect, collections, copy, itertools, math, string
import sys

def I():
	return int(sys.stdin.readline().rstrip())

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def S():
	return sys.stdin.readline().rstrip()

def LS():
	return list(sys.stdin.readline().rstrip().split())

def main():
	(n, m, q) = LI()
	require = [LI() for _ in range(q)]
	itr = itertools.combinations_with_replacement(list(range(1, n + 2)), m - 1)
	ans = 0
	for i in itr:
		lst = [0 for _ in range(n + 2)]
		lst[0] = 1
		cnt = 0
		for j in i:
			lst[j] += 1
		lst = list(itertools.accumulate(lst))
		for re in require:
			if lst[re[1]] - lst[re[0]] == re[2]:
				cnt += re[3]
		ans = max(cnt, ans)
	print(ans)
main()


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

def dfs(i, j):
	if i == 0:
		alist = []
		for k in range(j):
			alist.append([k + 1])
		return alist
	elif i > 0 and i <= n:
		blist = []
		for k in dfs(i - 1, j):
			last = k[-1]
			for l in range(last, j + 1):
				clist = k.copy()
				clist.append(l)
				blist.append(clist)
		return blist
ans = 0
for alist in dfs(n - 1, m):
	cnt = 0
	for i in range(q):
		if alist[abcd[i][1] - 1] - alist[abcd[i][0] - 1] == abcd[i][2]:
			cnt += abcd[i][3]
	ans = max(ans, cnt)
print(ans)


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):
	temp = 0
	for p in pair:
		if A[p[1] - 1] - A[p[0] - 1] == p[2]:
			temp += p[3]
			ans = max(ans, temp)
print(ans)


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


import itertools
((n, m, q), *r) = [[*map(int, t.split())] for t in open(0)]
ans = 0
for i in list(itertools.combinations_with_replacement(range(1, m + 1), n)):
	sub = 0
	for (a, b, c, d) in r:
		sub += d * (i[b - 1] - i[a - 1] == c)
	ans = max(ans, sub)
print(ans)


from collections import deque
(N, M, Q) = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(Q)]
que = deque()
for i in range(1, M + 1):
	que.append([i])
ans = 0
while que:
	check = que.popleft()
	tmp = 0
	if len(check) == N:
		for A in lst:
			if check[A[1] - 1] - check[A[0] - 1] == A[2]:
				tmp += A[3]
		ans = max(ans, tmp)
	else:
		for j in range(check[-1], M + 1):
			que.append(check + [j])
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
listA = itertools.combinations_with_replacement(range(1, M + 1), N)
ans = 0
listQ = []
for q in range(Q):
	listQ += [list(map(int, input().split()))]
for i in listA:
	anssub = 0
	for (a, b, c, d) in listQ:
		if i[b - 1] - i[a - 1] == c:
			anssub += d
	ans = max(ans, anssub)
print(ans)


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


import itertools
(N, M, Q) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(Q)]
li = []
for B in itertools.combinations_with_replacement(range(1, M + 1), N):
	t = 0
	for a in A:
		if B[a[1] - 1] - B[a[0] - 1] == a[2]:
			t += a[3]
	li.append(t)
print(max(li))


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
point_max = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	point = 0
	for (a, b, c, d) in ABCD:
		if A[b - 1] - A[a - 1] == c:
			point += d
	point_max = max(point, point_max)
print(point_max)


(N, M, Q) = list(map(int, input().split()))
C = []
for i in range(Q):
	C.append(list(map(int, input().split())))
import itertools
res = 0
ls = list(range(1, M + 1))
for a in itertools.combinations_with_replacement(ls, N):
	p = 0
	for c in C:
		if a[c[1] - 1] - a[c[0] - 1] == c[2]:
			p += c[3]
		res = max(res, p)
print(res)


import itertools
(n, m, q) = map(int, input().split())
ch = list(itertools.combinations_with_replacement(range(1, m + 1), n))
lch = len(ch)
rel = [0] * lch
l = [list(map(int, input().split())) for _ in range(q)]
for k in range(lch):
	for i in range(q):
		if ch[k][l[i][1] - 1] - ch[k][l[i][0] - 1] == l[i][2]:
			rel[k] += l[i][3]
print(max(rel))


import itertools
(N, M, Q) = map(int, input().split())
X = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for t in itertools.combinations_with_replacement(range(1, M + 1), N):
	tmp = 0
	for (a, b, c, d) in X:
		if t[b - 1] - t[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in itertools.combinations_with_replacement(range(1, M + 1), N):
	tmp = sum([x[3] for x in X if A[x[1] - 1] - A[x[0] - 1] == x[2]])
	if tmp > ans:
		ans = tmp
print(ans)


def dfs(lst):
	nonlocal ans
	if len(lst) > n:
		return
	lowest = 1
	if lst != []:
		lowest = lst[-1]
	for i in range(lowest, m + 1):
		lst.append(i)
		dfs(lst)
		lst.pop()
	if len(lst) == n:
		cnt = 0
		for i in range(q):
			(a, b, c, d) = ab[i]
			if lst[b - 1] - lst[a - 1] == c:
				cnt += d
		ans = max(cnt, ans)
(n, m, q) = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
dfs([])
print(ans)


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[i], b[i], c[i], d[i]) = map(int, input().split())
cc = [i for i in range(1, M + 1)]
ans = 0
for A in itertools.combinations_with_replacement(cc, N):
	A = list(A)
	t = 0
	for i in range(Q):
		if A[b[i] - 1] - A[a[i] - 1] == c[i]:
			t += d[i]
	ans = max(ans, t)
print(ans)


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)


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()))
	qs = []
	for _ in range(q):
		abcd = tuple(map(int, input().split()))
		qs.append(abcd)
	A = tuple(combinations_with_replacement(list(range(1, m + 1)), n))
	r = 0
	for Ae in A:
		t0 = 0
		for qe in qs:
			if Ae[qe[1] - 1] - Ae[qe[0] - 1] == qe[2]:
				t0 += qe[3]
		r = max(r, t0)
	print(r)

def __starting_point():
	main()
__starting_point()


import itertools
s = list(map(int, input().split()))
t = [list(map(int, input().split())) for i in range(s[2])]
li = [x for x in range(1, s[1] + 1)]
lity = list(itertools.combinations_with_replacement(li, s[0]))
ans = 0
for k in lity:
	vmax = 0
	for l in t:
		if k[l[1] - 1] - k[l[0] - 1] == l[2]:
			vmax += l[3]
	ans = max(ans, vmax)
print(ans)


from itertools import combinations_with_replacement as combinations
(n, m, q) = map(int, input().split())
r = tuple(range(1, m + 1))
l = [tuple(map(int, input().split())) for i in range(q)]
ans = 0
for a in combinations(r, n):
	x = 0
	for t in l:
		if a[t[1] - 1] - a[t[0] - 1] == t[2]:
			x += t[3]
	ans = max(ans, x)
print(ans)


import copy

def main():
	(N, M, Q) = [int(n) for n in input().split(' ')]
	q = [[int(a) for a in input().split(' ')] for i in range(Q)]
	all_series = get_series(N, M)
	points = [0]
	for l in all_series:
		points.append(get_score(l, q))
	print(max(points))

def get_score(l, q):
	return sum([q[i][3] if l[q[i][1] - 1] - l[q[i][0] - 1] == q[i][2] else 0 for i in range(len(q))])

def get_series(N, M):
	all_series = []
	checked = [[0] * M for i in range(N)]
	to_check = [[0, j + 1] for j in range(M)]
	series = [0 for k in range(N)]
	while len(to_check) > 0:
		checking = to_check.pop(-1)
		series[checking[0]] = checking[1]
		if checking[0] == N - 1:
			l = copy.deepcopy(series)
			all_series.append(l)
		else:
			to_check.extend([[checking[0] + 1, k] for k in range(checking[1], M + 1)])
	return all_series
main()


(N, M, Q) = list(map(int, input().split()))
from itertools import combinations_with_replacement as comb
l = comb(range(1, M + 1), N)
q = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for i in l:
	tmp = 0
	for (a, b, c, d) in q:
		if i[b - 1] - i[a - 1] == c:
			tmp += d
	ans = max(tmp, ans)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
A = list(itertools.combinations_with_replacement(range(1, m + 1), n))
qs = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for t in A:
	temp = 0
	for (a, b, c, d) in qs:
		if t[b - 1] - t[a - 1] == c:
			temp += d
	ans = max(ans, temp)
print(ans)


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

def check(x):
	z = [0] * n
	temp = 0
	y = 0
	for p in x:
		if p == 0:
			temp += 1
		else:
			z[y] = temp
			y += 1
	ans = 0
	for (a, b, c, d) in abcd:
		if z[b - 1] - z[a - 1] == c:
			ans += d
	return ans

def func(x, s, t):
	if s == m:
		temp = [] + x
		while t < n:
			temp.append(1)
			t += 1
		return check(temp)
	ans = func(x + [0], s + 1, t)
	if t == n:
		return ans
	else:
		return max(ans, func(x + [1], s, t + 1))
print(func([0], 1, 0))


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)


import itertools
(n, m, q) = map(int, input().split())
l = list(itertools.combinations_with_replacement(range(1, m + 1), n))
res = [0] * len(l)
for i in range(q):
	(a, b, c, d) = map(int, input().split())
	for (j, x) in enumerate(l):
		if x[b - 1] - x[a - 1] == c:
			res[j] += d
print(max(res))


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(q)]
N = [i + 1 for i in range(m)]
cnt = 0
for A in combinations_with_replacement(N, n):
	point = 0
	for (a, b, c, d) in ABCD:
		if A[b - 1] - A[a - 1] == c:
			point += d
	cnt = max(cnt, point)
print(cnt)


from itertools import combinations_with_replacement as c
(n, m, q) = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(q)]
a = 0
for i in c(range(1, m + 1), n):
	s = 0
	for j in Q:
		if i[j[1] - 1] - i[j[0] - 1] == j[2]:
			s += j[3]
	a = max(a, s)
print(a)


import itertools
k = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(k[2])]
m = [i + 1 for i in range(k[1])]
n = list(itertools.combinations_with_replacement(m, k[0]))
now = 0
re = 0
for i in n:
	for x in l:
		if i[x[1] - 1] - i[x[0] - 1] == x[2]:
			re += x[3]
	now = max([now, re])
	re = 0
print(now)


import itertools
(N, M, Q) = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(Q)]
data = list(range(1, M + 1))
A = list(itertools.combinations_with_replacement(data, N))
max_score = 0
for Ai in A:
	x = 0
	for (a, b, c, d) in L:
		if Ai[b - 1] - Ai[a - 1] == c:
			x += d
	max_score = max(max_score, x)
print(max_score)


import itertools
(N, M, Q) = list(map(int, input().split()))
ABCD = [list(map(int, input().split())) for _ in range(Q)]
ans = []
for i in itertools.combinations_with_replacement(range(1, M + 1), N):
	num = 0
	for (a, b, c, d) in ABCD:
		if i[b - 1] - i[a - 1] == c:
			num += d
	ans.append(num)
print(max(ans))


import itertools
(n, m, q) = map(int, input().split())
a = [[int(i) for i in input().split()] for l in range(q)]
b = [int(i) for i in range(1, m + 1)]
M = 0
for x in itertools.combinations_with_replacement(b, n):
	r = 0
	for y in a:
		if x[y[1] - 1] - x[y[0] - 1] == y[2]:
			r += y[3]
	M = max(M, r)
print(M)


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
qx = [list(map(int, input().split())) for i in range(q)]
ans = 0
for i in combinations_with_replacement(range(1, m + 1), n):
	s = 0
	for (a, b, c, d) in qx:
		s += d if i[b - 1] - i[a - 1] == c else 0
	ans = max(ans, s)
print(ans)


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))


import itertools
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
v = list(itertools.combinations_with_replacement(range(1, m + 1), n))
ans = 0
for i in v:
	s = 0
	for (a, b, c, d) in abcd:
		if i[b - 1] - i[a - 1] == c:
			s += d
	ans = max(ans, s)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
AA = itertools.combinations_with_replacement(range(1, m + 1), n)
res = 0
for A in AA:
	cnt = 0
	for (a, b, c, d) in abcd:
		if A[b - 1] - A[a - 1] == c:
			cnt += d
	res = max(res, cnt)
print(res)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
l = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	ans = max(ans, sum((d for (a, b, c, d) in l if A[b - 1] - A[a - 1] == c)))
print(ans)


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(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)


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

def dfs(A):
	nonlocal max_score
	if len(A) == N:
		score = 0
		for (ai, bi, ci, di) in abcd:
			if A[bi - 1] - A[ai - 1] == ci:
				score += di
		return score
	if len(A) >= 1:
		prev_last = A[-1]
	else:
		prev_last = 1
	for i in range(prev_last, M + 1):
		A.append(i)
		max_score = max(max_score, dfs(A))
		A.pop()
	return max_score
dfs([])
print(max_score)


from itertools import combinations_with_replacement
(N, M, Q, *ABCD) = map(int, open(0).read().split())
L = list(zip(*[iter(ABCD)] * 4))
print(max((sum((d for (a, b, c, d) in L if A[b - 1] - A[a - 1] == c)) for A in combinations_with_replacement(range(1, M + 1), N))))


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


import itertools
(N, M, Q) = list(map(int, input().split()))
ABCD = [[int(x) for x in input().split()] for i in range(Q)]
ans = 0
for A in itertools.combinations_with_replacement([m for m in range(1, M + 1)], N):
	ans2 = 0
	for (a, b, c, d) in ABCD:
		if A[b - 1] - A[a - 1] == c:
			ans2 += d
	ans = max(ans, ans2)
print(ans)


from itertools import combinations_with_replacement as cwr
(n, m, q) = map(int, input().split())
lis = range(1, m + 1)
cdt = list(cwr(lis, n))
res = [0] * len(cdt)
for i in range(q):
	(a, b, c, d) = map(int, input().split())
	for (j, k) in enumerate(cdt):
		if k[b - 1] - k[a - 1] == c:
			res[j] += d
print(max(res))


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


f = lambda : 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(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)


from sys import stdin
import itertools
(N, M, Q) = [int(x) for x in stdin.readline().rstrip().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]) = [int(x) for x in stdin.readline().rstrip().split()]
	a[i] -= 1
	b[i] -= 1
ans = 0
for A in itertools.combinations_with_replacement(list(range(1, M + 1)), N):
	cur = 0
	for i in range(Q):
		if A[b[i]] - A[a[i]] == c[i]:
			cur += d[i]
	if ans < cur:
		ans = cur
print(ans)


import itertools
(n, m, q) = map(int, input().split())
ql = []
for _ in range(q):
	ql.append(tuple(map(int, input().split())))

def check(t):
	ret = 0
	for q in ql:
		if t[q[1] - 1] - t[q[0] - 1] == q[2]:
			ret += q[3]
	return ret
print(max(map(check, itertools.combinations_with_replacement(range(1, m + 1), n))))


def f(A, N, M, Q, conditions):
	L = len(A) - 1
	if L > N:
		return
	elif L < N:
		fans = 0
		for j in range(A[L], M + 1):
			fans = max(fans, f(A + [j], N, M, Q, conditions))
	else:
		fans = 0
		for i in range(Q):
			(a, b, c, d) = conditions[i]
			if A[b] - A[a] == c:
				fans += d
	return fans
(N, M, Q) = [int(x) for x in input().split()]
conditions = []
for i in range(Q):
	c = [int(x) for x in input().split()]
	conditions.append(c)
ans = f([1], N, M, Q, conditions)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
l = []
for i in range(q):
	tl = list(map(int, input().split()))
	l.append(tl)
lis = [i + 1 for i in range(m)]
lis = list(itertools.combinations_with_replacement(lis, n))
ans = 0
for i in lis:
	tmp = 0
	for j in range(q):
		a = l[j][1] - 1
		b = l[j][0] - 1
		if l[j][2] == i[a] - i[b]:
			tmp += l[j][3]
	ans = max(tmp, ans)
print(ans)


from itertools import combinations_with_replacement as cwr
[N, M, Q] = [int(i) for i in input().split()]
req = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for seq in cwr(range(1, M + 1), N):
	sco = 0
	for i in range(Q):
		if seq[req[i][1] - 1] - seq[req[i][0] - 1] == req[i][2]:
			sco += req[i][3]
	s = max(ans, sco)
	ans = s
print(ans)


from collections import deque

def calc(seq):
	score = 0
	for (a, b, c, d) in array:
		if seq[b - 1] - seq[a - 1] == c:
			score += d
	return score
(N, M, Q) = map(int, input().split())
array = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
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)


import itertools

def abc165c_many_requirements():
	(n, m, q) = map(int, input().split())
	all_list = []
	for _ in range(q):
		all_list.append(list(map(int, input().split())))
	pattern = itertools.combinations_with_replacement(range(1, m + 1), n)
	best = 0
	for p in pattern:
		cost = 0
		for l in all_list:
			if p[l[1] - 1] - p[l[0] - 1] == l[2]:
				cost += l[3]
		if cost > best:
			best = cost
	print(best)
abc165c_many_requirements()


import itertools
(N, M, Q) = list(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]) = list(map(int, input().split()))
l = list(range(1, M + 1))
i = 0
D = []
for v in itertools.combinations_with_replacement(l, N):
	D.append(0)
	for j in range(Q):
		if v[b[j] - 1] - v[a[j] - 1] == c[j]:
			D[i] += d[j]
	i += 1
print(max(D))


(N, M, Q, *ABCD) = map(int, open(0).read().split())

def solve(A):
	if len(A) == N:
		return sum((d for (a, b, c, d) in zip(*[iter(ABCD)] * 4) if A[b - 1] - A[a - 1] == c))
	return max((solve(A + [i]) for i in range(A[-1], M + 1)))
print(solve([1]))


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

def rec():
	if len(A) == N:
		return sum((d for (a, b, c, d) in abcd if A[b - 1] == A[a - 1] + c))
	t = 0
	for x in range(A[-1], M + 1):
		A.append(x)
		t = max(t, rec())
		A.pop()
	return t
print(rec())


import itertools
(N, M, Q) = map(int, input().split())
Qs = [list(map(int, input().split())) for _ in range(Q)]
_max = 0
for A in itertools.combinations_with_replacement(range(1, M + 1), N):
	score = sum([q[3] for q in Qs if A[q[1] - 1] - A[q[0] - 1] == q[2]])
	_max = score if _max < score else _max
print(_max)


(N, M, Q) = map(int, input().split())
Query = [list(map(int, input().split())) for i in range(Q)]
import itertools
ans = 0
for x in itertools.combinations_with_replacement(range(1, M + 1), N):
	l = 0
	for i in range(Q):
		(a, b, c, d) = map(int, Query[i])
		if x[b - 1] - x[a - 1] == c:
			l += d
	ans = max(ans, l)
print(ans)


(N, M, Q) = map(int, input().split())
ABCD = [list(map(int, input().split())) for i in range(Q)]
from itertools import combinations
ans = 0
for p in combinations(range(N + M - 1), N):
	r = 0
	for (a, b, c, d) in ABCD:
		if p[b - 1] - b + 1 - (p[a - 1] - (a - 1)) == c:
			r += d
	ans = max(ans, r)
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(Q)]
m = [x for x in range(1, M + 1)]
A = list(itertools.combinations_with_replacement(m, N))
ans = 0
for a in A:
	d = 0
	for i in range(Q):
		if a[L[i][1] - 1] - a[L[i][0] - 1] == L[i][2]:
			d += L[i][3]
	ans = max(ans, d)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
lst = []
ans = 0
for i in range(q):
	(a, b, c, d) = map(int, input().split())
	lst.append([a, b, c, d])
for x in list(itertools.combinations_with_replacement(range(1, m + 1), n)):
	cnt = 0
	for i in lst:
		if x[i[1] - 1] - x[i[0] - 1] == i[2]:
			cnt += i[3]
	ans = max(ans, cnt)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
X = [tuple(map(int, input().split())) for i in range(q)]
ans = 0
for A in itertools.combinations_with_replacement(range(m), n):
	tmp = 0
	for (a, b, c, d) in X:
		if A[b - 1] - A[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


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


(N, M, Q) = list(map(int, input().split()))
G = []
for i in range(Q):
	(a, b, c, d) = list(map(int, input().split()))
	G.append([a, b, c, d])
ans = 0

def dfs(s):
	nonlocal ans
	if len(s) == N:
		now = 0
		for (a, b, c, d) in G:
			if s[b - 1] - s[a - 1] == c:
				now += d
		ans = max(ans, now)
		return
	for i in range(s[-1], M):
		dfs(s + [i])
s = [0]
dfs(s.copy())
print(ans)


import itertools
(n, m, q) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(q)]
l = range(1, m + 1)
h_list = list(itertools.combinations_with_replacement(l, n))
ans = 0
for i in h_list:
	b = 0
	for j in a:
		if i[j[1] - 1] - i[j[0] - 1] == j[2]:
			b += j[3]
	ans = max(ans, b)
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
query = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for p in itertools.combinations_with_replacement(range(1, M + 1), N):
	A = list(p)
	s = 0
	for (a, b, c, d) in query:
		s += d if A[b - 1] - A[a - 1] == c else 0
	ans = max(ans, s)
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
abcd = []
ans = 0
for i in range(Q):
	abcd.append(input())
for X in itertools.combinations_with_replacement(range(1, M + 1), N):
	tmp = 0
	for z in abcd:
		(a, b, c, d) = map(int, z.split())
		if X[b - 1] - X[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


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


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
s = 0
for p in combinations_with_replacement(range(1, m + 1), n):
	t = 0
	for (a, b, c, d) in abcd:
		t += d * (p[b - 1] - p[a - 1] == c)
	s = max(s, t)
print(s)


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

def dfs(A, max_a):
	if len(A) == N:
		num = 0
		for (a, b, c, d) in conditions:
			if A[b - 1] - A[a - 1] == c:
				num += d
		return num
	ans = 0
	for i in range(max_a, M + 1):
		ans = max(ans, dfs(A + [i], i))
	return ans
print(dfs([1], 1))


(n, m, q) = map(int, input().split())
abcd = [[*map(int, input().split())] for _ in range(q)]
from itertools import *
ans = 0
for A in combinations_with_replacement(range(1, m + 1), n):
	now = sum((d if A[b - 1] - A[a - 1] == c else 0 for (a, b, c, d) in abcd))
	ans = max(ans, now)
print(ans)


(N, M, Q) = map(int, input().split())
ABCD = [tuple(map(int, input().split())) for i in range(Q)]
import itertools
ans = 0
for ptn in itertools.combinations_with_replacement(range(1, M + 1), N):
	tmp = 0
	for (a, b, c, d) in ABCD:
		if ptn[b - 1] - ptn[a - 1] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
AA = itertools.combinations_with_replacement(range(1, M + 1), N)
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = -10 ** 18
for A in AA:
	score = 0
	for (a, b, c, d) in abcd:
		if A[b - 1] - A[a - 1] == c:
			score += d
	ans = max(ans, score)
print(ans)


import itertools

def check(lis, a):
	return sum((i[3] for i in a if lis[i[1] - 1] - lis[i[0] - 1] == i[2]))
(n, m, q) = map(int, input().split())
a = [[int(i) for i in input().split(' ')] for j in range(q)]
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
	ans = max(ans, check(i, a))
print(ans)


f = lambda : 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(range(m), n))))


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


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

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

def dfs(A, i):
	nonlocal ans
	if len(A) == n:
		ans = max(ans, calc(A))
		return
	for j in range(i, m + 1):
		A.append(j)
		dfs(A, j)
		A.pop()
dfs([], 1)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
l = range(1, m + 1)
point = 0
for v in itertools.combinations_with_replacement(l, n):
	p = 0
	for (a, b, c, d) in abcd:
		if v[b - 1] - v[a - 1] == c:
			p += d
	if p > point:
		point = p
print(point)


import itertools
(n, m, q) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(q)]
ms = [i for i in range(1, m + 1)]
ans = 0
for k in itertools.combinations_with_replacement(ms, n):
	x = 0
	for j in range(q):
		if a[j][2] == k[a[j][1] - 1] - k[a[j][0] - 1]:
			x += a[j][3]
	if ans < x:
		ans = x
print(ans)


import itertools
(n, m, q) = map(int, input().strip().split())
L = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for possibleA in itertools.combinations_with_replacement(range(1, m + 1), n):
	di_total = 0
	for (a, b, c, d) in L:
		if possibleA[b - 1] - possibleA[a - 1] == c:
			di_total += d
	ans = max(ans, di_total)
print(ans)


(n, m, q) = map(int, input().split())
l = []
for i in range(q):
	l.append(list(map(int, input().split())))
li = []
for i in range(m):
	li.append(i + 1)
import itertools
ans = 0
for lis in itertools.combinations_with_replacement(li, n):
	lis = list(lis)
	an = 0
	for z in l:
		(a, b, c, d) = z
		if lis[b - 1] - lis[a - 1] == c:
			an += d
	ans = max(an, ans)
print(ans)


(n, m, q) = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
import itertools
cr = itertools.combinations_with_replacement(range(1, m + 1), n)
M = 0
for seq in cr:
	score = sum([d for (a, b, c, d) in abcd if seq[b - 1] - seq[a - 1] == c])
	M = max(M, score)
print(M)


import itertools
(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
total = []
for A in itertools.combinations_with_replacement(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))


import sys
import itertools
(N, M, Q) = map(int, input().split())
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)
num = list(range(1, M + 1))
ans = 0
for tpl in itertools.combinations_with_replacement(num, N):
	point = 0
	for i in range(Q):
		if tpl[B[i] - 1] - tpl[A[i] - 1] == C[i]:
			point += D[i]
	ans = max(ans, point)
print(ans)


def rec(L):
	return sum([d for (a, b, c, d) in X if L[b] - L[a] == c]) if len(L) > N else max([rec(L + [i]) for i in range(L[-1], M)])
(N, M, Q) = map(int, input().split())
X = []
for _ in range(Q):
	(a, b, c, d) = map(int, input().split())
	X.append((a, b, c, d))
print(rec([0]))


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


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


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)


from itertools import combinations_with_replacement as cwr
(N, M, Q, *Z) = map(int, open(0).read().split())
(*Z,) = zip(*[iter(Z)] * 4)
ans = 0
for A in cwr(range(M), N):
	score = 0
	for (a, b, c, d) in Z:
		if A[b - 1] - A[a - 1] == c:
			score += d
	if score > ans:
		ans = score
print(ans)


def backtrack(nums, N, cur_state, result):
	if len(cur_state) == N:
		result.append(cur_state[:])
	else:
		for i in range(len(nums)):
			cur_state.append(nums[i])
			backtrack(nums[i:], N, cur_state, result)
			cur_state.pop()

def solve():
	(N, M, Q) = [int(i) for i in input().split()]
	quads = []
	for i in range(Q):
		quads.append([int(i) for i in input().split()])
	candidates = []
	backtrack(list(range(1, M + 1)), N, [], candidates)
	ans = 0
	for candidate in candidates:
		score = 0
		for (a, b, c, d) in quads:
			if candidate[b - 1] - candidate[a - 1] == c:
				score += d
		ans = max(score, ans)
	print(ans)

def __starting_point():
	solve()
__starting_point()


import itertools
(N, M, Q) = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
a = [i for i in range(1, M + 1)]
for A in itertools.combinations_with_replacement(a, N):
	tmp = 0
	for l in li:
		if A[l[1] - 1] - A[l[0] - 1] == l[2]:
			tmp += l[3]
	else:
		ans = max(ans, tmp)
print(ans)


import itertools
(n, m, q, *l) = map(int, open(0).read().split())
print(max((sum((d for (a, b, c, d) in [l[j * 4:j * 4 + 4] for j in range(q)] if i[b - 1] - i[a - 1] == c)) for i in itertools.combinations_with_replacement(range(m), n))))


import itertools
(n, m, q) = map(int, input().split())
_q = [list(map(int, input().split())) for _ in range(q)]
_A = 0
for v in itertools.combinations_with_replacement(range(1, m + 1), n):
	A = list(v)
	_d = 0
	for q in _q:
		(a, b, c, d) = q
		if A[b - 1] - A[a - 1] == c:
			_d += d
	if _d > _A:
		_A = _d
print(_A)


from itertools import combinations_with_replacement as comb_r
(n, m, q) = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(q)]
mx = 0
for x in comb_r(range(1, m + 1), n):
	sm = 0
	for y in lst:
		if x[y[1] - 1] - x[y[0] - 1] == y[2]:
			sm += y[3]
	if mx < sm:
		mx = sm
print(mx)


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

def gen(cur):
	if len(cur) == n:
		candidate.append(cur)
	else:
		t = cur[-1]
		for tv in range(t, m + 1):
			nex = cur[:]
			nex.append(tv)
			gen(nex)
for i in range(1, 10):
	arr = [i]
	gen(arr)
ask = []
for _ in range(q):
	ask.append(list(map(int, input().split())))
ans = 0
for cv in candidate:
	tmp = 0
	for av in ask:
		if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
			tmp += av[3]
	ans = max(ans, tmp)
print(ans)


from math import ceil, floor, factorial, gcd, sqrt, log2, cos, sin, tan, acos, asin, atan, degrees, radians, pi, inf
from itertools import accumulate, groupby, permutations, combinations, product, combinations_with_replacement
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
from operator import itemgetter
from heapq import heapify, heappop, heappush
from queue import Queue, LifoQueue, PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)

def input():
	return sys.stdin.readline().strip()

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

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

def MAP1():
	return map(lambda x: int(x) - 1, input().split())

def LIST():
	return list(MAP())

def solve():
	(N, M, Q) = MAP()
	A = [LIST() for _ in range(Q)]
	ans = 0
	for x in combinations_with_replacement(range(1, M + 1), N):
		score = 0
		for (a, b, c, d) in A:
			if x[b - 1] - x[a - 1] == c:
				score += d
		ans = max(ans, score)
	print(ans)

def __starting_point():
	solve()
__starting_point()


(n, m, q) = map(int, input().split())
abcd = []
for _ in range(q):
	(a, b, c, d) = map(int, input().split())
	abcd.append((a, b, c, d))
ans = 0
import itertools
p = itertools.combinations_with_replacement(range(1, m + 1), n)
for x in p:
	r = 0
	for i in range(q):
		if x[abcd[i][1] - 1] - x[abcd[i][0] - 1] == abcd[i][2]:
			r += abcd[i][3]
	ans = max(ans, r)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
A = itertools.combinations_with_replacement(range(1, m + 1), n)
cond = [list(map(int, input().split())) for i in range(q)]
ma = 0
for a in A:
	p = 0
	for c in cond:
		if a[c[1] - 1] - a[c[0] - 1] == c[2]:
			p += c[3]
	if ma < p:
		ma = p
print(ma)


(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)


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


import itertools
(N, M, Q) = (int(x) for x in input().split())
h = [[int(i) for i in input().split()] for _ in range(Q)]
ans = 0
for A in itertools.combinations_with_replacement(range(1, M + 1), N):
	temp = 0
	for (a, b, c, d) in h:
		if A[b - 1] - A[a - 1] == c:
			temp += d
	ans = max(ans, temp)
print(ans)


import itertools

def main():
	(n, m, q) = map(int, input().split())
	matrix = []
	for _ in range(q):
		row = list(map(int, input().split()))
		matrix.append(row)
	A = list(range(1, m + 1))
	ans = 0
	for tmp_A in itertools.combinations_with_replacement(A, n):
		tmp_A = sorted(list(tmp_A))
		tmp_ans = 0
		for row in matrix:
			tmp = tmp_A[row[1] - 1] - tmp_A[row[0] - 1]
			if row[2] == tmp:
				tmp_ans += row[3]
		if tmp_ans >= ans:
			ans = tmp_ans
	print(ans)
main()


(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
from itertools import combinations_with_replacement
m = [i for i in range(1, M + 1)]
A = [i for i in combinations_with_replacement(m, N) if i[0] == 1]
ans = 0
for i in A:
	an = 0
	for (a, b, c, d) in abcd:
		if i[b - 1] - i[a - 1] == c:
			an += d
	ans = max(ans, an)
print(ans)


import sys
import math
import itertools
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

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

def MF():
	return map(float, input().split())

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

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	(N, M, Q) = MI()
	L = [LI() for i in range(Q)]
	A = [i for i in range(1, M + 1)]
	ans = 0
	for i in itertools.combinations_with_replacement(A, N):
		temp = 0
		for (a, b, c, d) in L:
			if i[b - 1] - i[a - 1] == c:
				temp += d
		if temp > ans:
			ans = temp
	print(ans)

def __starting_point():
	main()
__starting_point()


def dfs(s):
	nonlocal ans
	if len(s) == n:
		cnt = 0
		for i in range(q):
			if s[abcd[i][1] - 1] - s[abcd[i][0] - 1] == abcd[i][2]:
				cnt += abcd[i][3]
		ans = max(ans, cnt)
		return
	last = 1
	if len(s) > 0:
		last = s[-1]
	for i in range(last, m + 1):
		dfs(s + [i])
	return
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(q)]
ans = 0
dfs([])
print(ans)


import itertools
(N, M, Q) = map(int, input().split())
seisu_list = []
for i in range(Q):
	seisu = list(map(int, input().split()))
	seisu_list.append(seisu)
M_list = [i for i in range(1, M + 1)]
A_list = list(itertools.combinations_with_replacement(M_list, N))
ans = 0
for i in range(len(A_list)):
	ans_temp = 0
	for j in range(Q):
		if A_list[i][seisu_list[j][1] - 1] - A_list[i][seisu_list[j][0] - 1] == seisu_list[j][2]:
			ans_temp += seisu_list[j][3]
			ans = max(ans, ans_temp)
print(ans)


(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())

def dfs(n_list, o, l, L):
	if o + l == 0:
		num = 1
		pos = 0
		L_child = [0] * N
		for i in range(N + M - 1):
			if n_list[i] == 1:
				num += 1
			else:
				L_child[pos] = num
				pos += 1
		L.append(L_child)
	if o >= 1:
		n_list[N + M - o - l - 1] = 0
		dfs(n_list, o - 1, l, L)
	if l >= 1:
		n_list[N + M - o - l - 1] = 1
		dfs(n_list, o, l - 1, L)
A = [0] * (N + M - 1)
L = []
dfs(A, N, M - 1, L)
ans = 0
for i in L:
	score = 0
	for j in range(Q):
		if i[b[j] - 1] - i[a[j] - 1] == c[j]:
			score += d[j]
	if score > ans:
		ans = score
print(ans)


import itertools
(n, m, q) = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(q)]
r = 0
for A in itertools.combinations_with_replacement(range(m), n):
	s = 0
	for i in range(q):
		if A[x[i][1] - 1] - A[x[i][0] - 1] == x[i][2]:
			s += x[i][3]
	r = max(r, s)
print(r)


import itertools
import sys

def score(abcd, A):
	ans = 0
	for (a, b, c, d) in abcd:
		if A[b - 1] - A[a - 1] == c:
			ans += d
	return ans
(N, M, Q) = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for i in range(Q)]
a = itertools.combinations_with_replacement(range(1, M + 1), N)
a = list(a)
Ans = 0
for i in range(len(a)):
	Ans = max(Ans, score(abcd, list(a[i])))
print(Ans)


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)


def dfs(seq):
	ans = 0
	if len(seq) == n:
		kou = 0
		for u in data:
			if seq[u[1] - 1] - seq[u[0] - 1] == u[2]:
				kou += u[3]
		return kou
	else:
		for i in range(seq[-1], m + 1):
			seq_next = seq + [i]
			ans = max(ans, dfs(seq_next))
		return ans
(n, m, q) = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(q)]
score = dfs([1])
print(score)


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


import itertools
(n, m, q) = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for v in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
	sub = 0
	for a in l:
		if v[a[1] - 1] - v[a[0] - 1] == a[2]:
			sub += a[3]
	ans = max(ans, sub)
print(ans)


import itertools
(n, m, q) = [int(i) for i in input().split()]
l = [[int(i) for i in input().split()] for i in range(q)]
a = itertools.combinations_with_replacement(range(1, m + 1), n)
print(max([sum([i[3] for i in l if j[i[0] - 1] + i[2] == j[i[1] - 1]]) for j in a]))


import sys
input = sys.stdin.buffer.readline
from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(q)]
ans = 0
for com in combinations_with_replacement(range(m), n):
	t = 0
	for (a, b, c, d) in L:
		if com[b - 1] - com[a - 1] == c:
			t += d
	if t > ans:
		ans = t
print(ans)


(N, M, Q) = map(int, input().split())
L = list(range(1, M + 1))
X = list()
for i in range(Q):
	X.append(list(map(int, input().split())))
ans = 0
import itertools
for l in itertools.combinations_with_replacement(L, N):
	l = sorted(list(l))
	s = 0
	for (a, b, c, d) in X:
		if l[b - 1] - l[a - 1] == c:
			s += d
	if ans < s:
		ans = s
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
(N, M, Q) = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
A = []

def dfs():
	nonlocal A
	if len(A) == N:
		return
	nonlocal ans
	if len(A) == 0:
		start = 1
	else:
		start = A[-1]
	for i in range(start, M + 1):
		A.append(i)
		dfs()
		if len(A) != N:
			A.pop()
			continue
		tmp_ans = 0
		for (a, b, c, d) in Q:
			if A[b - 1] - A[a - 1] == c:
				tmp_ans += d
		ans = max(ans, tmp_ans)
		A.pop()
dfs()
print(ans)


import itertools
(n, m, q) = map(int, input().split())
x = [list(map(int, input().split())) for y in range(q)]
ans = 0
for A in itertools.combinations_with_replacement(range(1, m + 1), n):
	new_ans = 0
	for i in range(q):
		if A[x[i][1] - 1] - A[x[i][0] - 1] == x[i][2]:
			new_ans += x[i][3]
	ans = max(ans, new_ans)
print(ans)


from collections import deque
(n, m, q) = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(q)]
(a, b, c, d) = [list(i) for i in zip(*l)]
queue = deque([[1]])
ans = 0
while queue:
	x = queue.popleft()
	if len(x) == n:
		s = 0
		for i in range(q):
			if x[b[i] - 1] - x[a[i] - 1] == c[i]:
				s += d[i]
		ans = max(ans, s)
	else:
		for j in range(x[-1], m + 1):
			y = x + [j]
			queue.append(y)
print(ans)


(n, m, q) = map(int, input().split())
s = [list(map(int, input().split())) for i in range(q)]
import itertools
A = [i for i in range(1, m + 1)]
k = list(itertools.combinations_with_replacement(A, n))
ma = 0
for i in k:
	su = 0
	for n in s:
		if i[n[1] - 1] - i[n[0] - 1] == n[2]:
			su += n[3]
	ma = max(ma, su)
print(ma)


import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
import numpy as np

def main():
	(n, m, q) = map(int, input().split())
	A = np.array(list(combinations_with_replacement(range(1, m + 1), n)))
	numA = len(A)
	score = np.zeros(numA, np.int32)
	ma = map(int, read().split())
	for (a, b, c, d) in zip(ma, ma, ma, ma):
		a -= 1
		b -= 1
		eachA_is_equalOrNot = A[:, b] - A[:, a] == c
		score += d * eachA_is_equalOrNot
	print(score.max())

def __starting_point():
	main()
__starting_point()


import itertools
(n, m, q) = map(int, input().split())
S = []
for i in range(q):
	(a, b, c, d) = map(int, input().split())
	S.append([a, b, c, d])
tl = itertools.combinations_with_replacement(range(1, m + 1), n)
ans = 0
for l in tl:
	t = 0
	for (a, b, c, d) in S:
		if l[b - 1] - l[a - 1] == c:
			t += d
	ans = max(ans, t)
print(ans)


(n, m, q) = map(int, input().split())
A = []
B = []
C = []
D = []
for i in range(q):
	(a, b, c, d) = map(int, input().split())
	A.append(a - 1)
	B.append(b - 1)
	C.append(c)
	D.append(d)
from collections import deque
que = deque([[1]])
M = 0
while que:
	lst = que.popleft()
	if len(lst) == n:
		s = 0
		for i in range(q):
			if lst[B[i]] - lst[A[i]] == C[i]:
				s += D[i]
		if M < s:
			M = s
	else:
		for j in range(lst[-1], m + 1):
			lst2 = lst + [j]
			que.append(lst2)
print(M)


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)


from itertools import *
(n, m, q, *L) = map(int, open(0).read().split())
print(max((sum((d * (x[b - 1] - x[a - 1] == c) for (a, b, c, d) in zip(*[iter(L)] * 4))) for x in combinations_with_replacement(range(m), n))))


import itertools
(N, M, Q) = map(int, input().split())
A = [int(i + 1) for i in range(M)]
abcd = [[int(j) for j in input().split()] for i in range(Q)]
ans = 0
for i in itertools.combinations_with_replacement(A, N):
	a = 0
	for j in abcd:
		if i[j[1] - 1] - i[j[0] - 1] == j[2]:
			a += j[3]
	ans = max(a, ans)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
P = [list(map(int, input().split())) for i in range(q)]
C = list(itertools.combinations_with_replacement([i + 1 for i in range(m)], n))
ans = 0
for c in C:
	tmp = 0
	for p in P:
		if c[p[1] - 1] - c[p[0] - 1] == p[2]:
			tmp += p[3]
	ans = max(ans, tmp)
print(ans)


(N, M, Q) = map(int, input().split())
ABCD = [[] for i in range(Q)]
for i in range(Q):
	ABCD[i] = list(map(int, input().split()))
ans = 0
import itertools
for p in itertools.combinations_with_replacement([x for x in range(1, M + 1)], N):
	point = 0
	p = list(p)
	for (a, b, c, d) in ABCD:
		if p[b - 1] - p[a - 1] == c:
			point += d
	ans = max(ans, point)
print(ans)


from itertools import combinations_with_replacement as c_w_r
(N, M, Q) = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for i in c_w_r([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
	ans = max(ans, e)
print(ans)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
Req = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	point = sum([d for (a, b, c, d) in Req if A[b - 1] - A[a - 1] == c])
	ans = max(ans, point)
print(ans)


import itertools
(n, m, q) = map(int, input().split())
s = [list((int(i) for i in input().split())) for j in range(q)]
ans = []
for v in itertools.combinations_with_replacement(range(1, m + 1), n):
	tmp = 0
	for i in range(q):
		if v[s[i][1] - 1] - v[s[i][0] - 1] == s[i][2]:
			tmp += s[i][3]
	ans.append(tmp)
print(max(ans))


import itertools
(n, m, q) = map(int, input().split())
X = []
B = []
for i in range(q):
	X.append(list(map(int, input().split())))
for A in itertools.combinations_with_replacement(range(1, m + 1), n):
	ans = 0
	for x in X:
		if A[x[1] - 1] - A[x[0] - 1] == x[2]:
			ans += x[3]
	B.append(ans)
print(max(B))


(N, M, Q) = map(int, input().split())
A = []
for _ in range(Q):
	(a, b, c, d) = map(int, input().split())
	A.append((a - 1, b - 1, c, d))
import itertools
ans = 0
for x in itertools.combinations_with_replacement(range(1, M + 1), N):
	ans_temp = 0
	for (a, b, c, d) in A:
		if x[b] - x[a] == c:
			ans_temp += d
	ans = max(ans, ans_temp)
print(ans)


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 t in combinations_with_replacement(range(1, M + 1), N):
	an = 0
	for (a, b, c, d) in ABCD:
		if t[b - 1] - t[a - 1] == c:
			an += d
	if ans < an:
		ans = an
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
(N, M, Q) = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
A = []

def rec(itr, lst):
	if itr == N:
		res = 0
		for (a, b, c, d) in abcd:
			if A[b - 1] - A[a - 1] == c:
				res += d
		return res
	else:
		res = 0
		for i in range(lst, M):
			A.append(i)
			res = max(res, rec(itr + 1, i))
			A.pop()
		return res
print(rec(0, 0))


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
result = 0
for x in combinations_with_replacement(range(1, m + 1), n):
	t = 0
	for (a, b, c, d) in abcd:
		if x[b - 1] - x[a - 1] == c:
			t += d
	result = max(result, t)
print(result)


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)


import itertools
(N, M, Q) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(Q)]
com = list(itertools.combinations_with_replacement(range(1, M + 1), N))
answer = 0
for i in com:
	ans = 0
	for j in A:
		if i[j[1] - 1] - i[j[0] - 1] == j[2]:
			ans += j[3]
	answer = max(answer, ans)
print(answer)


import itertools
(N, M, Q) = map(int, input().split())
X = [list(map(int, input().split())) for i in range(Q)]
L = list(itertools.combinations_with_replacement(range(1, M + 1), N))
p_list = []
for l in L:
	point = 0
	for x in X:
		if l[x[1] - 1] - l[x[0] - 1] == x[2]:
			point += x[3]
	p_list.append(point)
print(max(p_list))


import itertools
(N, M, Q) = map(int, input().split())
ABCD_list = [[int(_) for _ in input().split()] for i in range(Q)]
combs = itertools.combinations_with_replacement(range(1, M + 1), N)
points = []
for comb in combs:
	A_list = list(comb)
	point = 0
	for inner in ABCD_list:
		if A_list[inner[1] - 1] - A_list[inner[0] - 1] == inner[2]:
			point += inner[3]
		points.append(point)
print(max(points))


import sys
from itertools import chain

def solve(N: int, M: int, Q: int, ABCD: 'List[(int,int,int,int)]'):

	def dfs(A, l, r, n):
		if n > 0:
			max_score = 0
			for Ai in range(l, r + 1):
				A.append(Ai)
				score = dfs(A, Ai, r, n - 1)
				if score > max_score:
					max_score = score
				A.pop()
			return max_score
		else:
			score = 0
			for (a, b, c, d) in ABCD:
				if A[b - 1] - A[a - 1] == c:
					score += d
			return score
	return dfs([], 1, M, N)

def main():
	tokens = chain(*(line.split() for line in sys.stdin))
	N = int(next(tokens))
	M = int(next(tokens))
	Q = int(next(tokens))
	ABCD = []
	for i in range(Q):
		a = int(next(tokens))
		b = int(next(tokens))
		c = int(next(tokens))
		d = int(next(tokens))
		ABCD.append((a, b, c, d))
	answer = solve(N, M, Q, ABCD)
	print(answer)

def __starting_point():
	main()
__starting_point()


from itertools import combinations_with_replacement as comb
(n, m, q) = map(int, input().split(' '))
lis = [list(map(int, input().split())) for _ in range(q)]
tes = comb(range(1, m + 1), n)
res = 0
for i in tes:
	te = 0
	for (a, b, c, d) in lis:
		if c == i[b - 1] - i[a - 1]:
			te += d
	res = max(te, res)
print(res)


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


def main(k, s):
	if k == n:
		return sum((d * (s[b] - s[a] == c) for (a, b, c, d) in abcd))
	return max((main(k + 1, s + [ai]) for ai in range(s[-1], m + 1)))
(n, m, q) = map(int, input().split())
abcd = [[int(i) for i in input().split()] for _ in range(q)]
print(main(0, [1]))


(N, M, Q) = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for a in range(M):
	for b in range(a, M):
		for c in range(b, M):
			for d in range(c, M):
				for e in range(d, M):
					for f in range(e, M):
						for g in range(f, M):
							for h in range(g, M):
								for i in range(h, M):
									for j in range(i, M):
										tmp = [a, b, c, d, e, f, g, h, i, j]
										tmp_ans = 0
										for q in range(Q):
											if tmp[L[q][1] - 1] - tmp[L[q][0] - 1] == L[q][2]:
												tmp_ans += L[q][3]
										ans = max(ans, tmp_ans)
print(ans)


from itertools import combinations_with_replacement
(n, m, q) = map(int, input().split())
conditions = []
for i in range(q):
	conditions.append(list(map(int, input().split())))
cands = []
for i in combinations_with_replacement(list(range(1, m + 1)), n):
	cands.append(i)
maxi = 0
for cand in cands:
	score = 0
	for condition in conditions:
		if cand[condition[1] - 1] - cand[condition[0] - 1] == condition[2]:
			score += condition[3]
	if score > maxi:
		maxi = score
print(maxi)


from itertools import combinations_with_replacement
(N, M, Q) = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
result = 0
for A in combinations_with_replacement(range(1, M + 1), N):
	t = 0
	for (a, b, c, d) in abcd:
		if A[b - 1] - A[a - 1] == c:
			t += d
	result = max(result, t)
print(result)


(n, m, q) = map(int, input().split())
ans = 0
x = [list(map(int, input().split())) for i in range(q)]
import itertools
arrs = list(itertools.combinations_with_replacement(range(m), n))
for arr in arrs:
	buf = 0
	for i in x:
		(a, b, c, d) = i
		if arr[b - 1] - arr[a - 1] == c:
			buf += d
	ans = max(ans, buf)
print(ans)


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