CodeEff/ECCO huggingface dataset:
# Original solution
from functools import lru_cache

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

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

@lru_cache(maxsize=None)
def search(mi, A):
    if len(A) > N:
        return 0

    ret = 0
    for i in range(1, A[-1] + 1):
        ret = max(ret, search(i, A + (i,)))

    d = len(A)
    for b, c, d in BCD[N - d]:
        if A[-(b + 1)] - A[-1] == c:
            ret += d
    return ret

ans = 0
for i in range(1, M + 1):
    ans = max(ans, search(i, (i,)))
print(ans)


# Optimized solution
N, M, Q = list(map(int, input().split()))

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

def search(mi, A):
    if len(A) > N:
        return 0

    ret = 0
    for i in range(1, A[-1] + 1):
        ret = max(ret, search(i, A + (i,)))

    d = len(A)
    for b, c, d in BCD[N - d]:
        if A[-(b + 1)] - A[-1] == c:
            ret += d
    return ret

ans = 0
for i in range(1, M + 1):
    ans = max(ans, search(i, (i,)))
print(ans)
# Original solution
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())))

lun = [0]*N
l = []

def make(n):
    if len(n) > N:
        return 0
    if int(n) > 3234566667:
        return 0
    else:

        p = []
        for i in range(int(n[-1]),M):
            p.append(str(i))

        lun.append(n)
        for i in p:
            lun.append(n)
            if len(n) == N:
                l.append(n)
            make(n+i)
        return 0
for i in range(M):
    make(str(i))

l = list(set(l))

nax = 0
for x in l:
    score = 0
    for i in range(Q):
        if int(x[b[i]-1]) - int(x[a[i]-1]) == c[i]:
            score += d[i]
    nax = max(nax,score)

print(nax)

# Optimized solution
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(itertools.combinations_with_replacement(list(range(1,M+1)), N))

nax = 0
for x in l:
    score = 0
    for i in range(Q):
        if int(x[b[i]-1]) - int(x[a[i]-1]) == c[i]:
            score += d[i]
    nax = max(nax,score)

print(nax)
# Original solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools

# 標準入力の取得
N,M,Q = list(map(int, input().split()))
a,b,c,d = [],[],[],[]
for q in range(Q):
    a_q,b_q,c_q,d_q = list(map(int, input().split()))
    a.append(a_q)
    b.append(b_q)
    c.append(c_q)
    d.append(d_q)

# 求解処理
l = list(range(1, M+1))
result = 0
for A in itertools.combinations_with_replacement(l, N):
    score = 0
    for a_i,b_i,c_i,d_i in zip(a, b, c, d):
        if A[b_i-1] - A[a_i-1] == c_i:
            score += d_i

    result = max(result, score)

# 結果出力
print(result)

# Optimized solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools

# 標準入力の取得
N, M, Q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
for q in range(Q):
    a_q, b_q, c_q, d_q = list(map(int, input().split()))
    a.append(a_q)
    b.append(b_q)
    c.append(c_q)
    d.append(d_q)


def get_score(A: tuple) -> int:
    """得点を計算するメソッド

    Arguments:
        A {tuple} -- 数列

    Returns:
        int -- 得点
    """
    score = 0
    for a_i, b_i, c_i, d_i in zip(a, b, c, d):
        score += (A[b_i-1] - A[a_i-1] == c_i) * d_i
    return score


def main() -> None:
    """Entry point
    """
    # 求解処理
    # 条件を満たす数列を全探索
    l = list(range(1, M+1))
    result = 0
    for A in itertools.combinations_with_replacement(l, N):
        result = max(result, get_score(A))

    # 結果出力
    print(result)


if __name__ == "__main__":
    main()
# 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 iter in itertools.combinations_with_replacement(list(range(1,m+1)),n):
    result = 0
    for i in range(q):
        a,b,c,d = abcd[i][0],abcd[i][1],abcd[i][2],abcd[i][3]
        if iter[b-1] - iter[a-1] == c:
            result += d
    ans = max(ans, result)

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

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

print(ans)
# Original solution
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)

def search(seq, min_n, max_n, n, req):
	ans = 0
	if len(seq) == n:
		for a, b, c, d in req:
			if seq[b-1] - seq[a-1] == c:
				ans += d
		return ans
	else:
		for i in range(min_n, max_n+1):
			new_seq = seq + [i]
			ans = max(ans, search(new_seq, i, max_n, n, req))
	return ans

def resolve():
	n, m, q = list(map(int, input().split()))
	req = [list(map(int, input().split())) for _ in range(q)]
	print((search([], 1, m, n, req)))
resolve()

# Optimized solution
def search(v, n, m, seq, scs):
    if len(seq) == n:
        ans = 0
        for r in scs:
            if seq[r[1]-1] - seq[r[0]-1] == r[2]:
                ans += r[3]
        return ans
    ans = 0
    for i in range(v, m+1):
        tmp = search(i, n, m, seq + [i], scs)
        ans = max(ans, tmp)
    return ans

def resolve():
    n, m, q = list(map(int, input().split()))
    scs = [list(map(int, input().split())) for _ in range(q)]
    v = search(1, n, m, [], scs)
    print(v)
resolve()
# Original solution
N, M, Q = list(map(int, input().split()))
#N: 正整数列の長さ, M:正整数列の最大値, Q: 4つの整数の組みの数

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

def dfs(now, n, num): #nowはこれから生成するもの
    if n == 0:
        make_group.append(now)
    else:
        for i in range(num, M+1):
            dfs(now+[i], n-1, i)

make_group = []
dfs([], N, 1)
ans = 0
for group in make_group:
    tmp = 0
    for q in Q_li:
        a = q[0] - 1
        b = q[1] - 1
        if q[2] == (group[b] - group[a]):
            tmp += q[3]
    ans = max(ans, tmp)
print(ans)



# Optimized solution
N, M, Q = list(map(int, input().split()))
#N: 正整数列の長さ, M:正整数列の最大値, Q: 4つの整数の組みの数

a_l = [0]*Q
b_l = [0]*Q
c_l = [0]*Q
d_l = [0]*Q
for i in range(Q):
    a_l[i], b_l[i], c_l[i], d_l[i] = list(map(int, input().split()))

def calc_score(now):
    score = 0
    for a, b, c, d in zip(a_l, b_l, c_l, d_l):
        a -= 1
        b -= 1
        if c == (now[b]-now[a]):
            score += d

    return score

def dfs(now, n, num): #nowはこれから生成するもの
    if n == 0:
        score_list.append(calc_score(now))
    else:
        for i in range(num, M+1):
            dfs(now+[i], n-1, i)

score_list = []
dfs([], N, 1)
print((max(score_list)))
# Original solution
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()))
import itertools
ans=0
for A in itertools.combinations_with_replacement(list(range(1,M+1)),N):
  A_=list(A)
  s=0
  
  for i in range(Q):
    if A_[b[i]-1]-A_[a[i]-1]==c[i]:
      s+=d[i]
    ans=max(ans,s)
print(ans)  

# Optimized solution
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:#len(seq)==N-1からの遷都を考えると良い
        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)
# Original solution
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:#len(seq)==N-1からの遷都を考えると良い
        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)


# Optimized solution
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+[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])))
# Original solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, m, q = list(map(int,input().split()))
    ABCD = [None] * q
    for i in range(q):
        a, b, c, d = list(map(int,input().split()))
        ABCD[i] = (a, b, c, d)

    def dfs(A):
        # base case
        if len(A) == n + 1: # score を計算して返す
            # print(A)
            score = 0
            for a, b, c, d in ABCD:
                if A[b] - A[a] == c:
                    score += d
            return score
        # iterate
        res = -INF
        for i in range(A[-1], m + 1):
            res = max(res, dfs(A + [i]))
        return res

    print((dfs([1])))
resolve()

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

n,m,q=list(map(int,input().split()))
l=[]
fa=[m]*n

def check_list(la: list):
    for i in range(len(la)-1):
        if la[i+1]<la[i] or la[i]>m or la[i]<1:
            return False
    
    return True


def dfs(i,a):
    if not check_list(a):
        return -1
    elif i==len(l):
        ret=0
        for i in range(len(a)-1):
            if a[i+1]<a[i] or a[i]>m:
                ret=-1

        for le in l:
            if a[le[1]-1]-a[le[0]-1]==le[2]:
                ret+=le[3]
        return ret
    else:
        c0=a.copy()     # use this (change first)
        c1=a.copy()     # use this (change second)
        c2=a.copy()     # dojn't use this
        c0[l[i][0]-1]=c0[l[i][1]-1]-l[i][2]
        c1[l[i][1]-1]=c1[l[i][0]-1]+l[i][2]
        return max(dfs(i+1,c0),dfs(i+1,c1),dfs(i+1,c2))




for _ in range(q):
    l.append(tuple(map(int,input().split())))


print((dfs(0,fa)))

# Optimized solution
import itertools
n,m,q=list(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(list(map(check,itertools.combinations_with_replacement(list(range(1,m+1)),n))))))
# Original solution
from itertools import combinations_with_replacement

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

# n,m,q=3,4,3
# l=[[1,3,3,100], [1,2,2,10], [2,3,2,10]]

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

res=0

for i in range(len(A)):
    tmp=0
    for j in range(len(l)):
        bnum=l[j][1]-1
        anum=l[j][0]-1
        c=l[j][2]
        d=l[j][3]

        if A[i][bnum]-A[i][anum]==c:
            tmp+=d

    res=max(res, tmp)

print(res)

# Optimized solution
from itertools import combinations_with_replacement

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

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

res=0

for i in range(len(A)):
    tmp=0
    for j in range(len(l)):
        if A[i][l[j][1]-1]-A[i][l[j][0]-1]==l[j][2]:
            tmp+=l[j][3]

    res=max(res, tmp)

print(res)
# Original solution
from sys import stdin
import itertools
N,M,Q=list(map(int, (stdin.readline().strip().split())))
q=[]
num_list=[]
number=[]
score=[0]
for i in range(1,M+1):number.append(i)
for _ in range(Q):
    a,b,c,d=list(map(int, (stdin.readline().strip().split())))
    q.append([a,b,c,d])
def dfs(num_list,i):
    if i==M and len(num_list)<N:
        return
    if len(num_list)!=N:
        a=num_list.copy()
        num_list.append(number[i])
        dfs(num_list,i)
        dfs(num_list,i+1)
        dfs(a,i+1)
    else:
        ans=0
        for a,b,c,d in q:
            if num_list[b-1]-num_list[a-1]==c:
                ans+=d
        score[0]=max(ans,score[0])



dfs(num_list,0)
print((score[0]))


# Optimized solution
from sys import stdin
import itertools
N,M,Q=list(map(int, (stdin.readline().strip().split())))
q=[]
num_list=[]
number=[]
score=[0]
daburi=[]
for i in range(1,M+1):number.append(i)
for _ in range(Q):
    a,b,c,d=list(map(int, (stdin.readline().strip().split())))
    q.append([a,b,c,d])
def dfs(num_list,i):
    if i==M and len(num_list)<N:
        return
    if len(num_list)!=N:
        a=num_list.copy()
        num_list.append(number[i])
        dfs(num_list,i)
        dfs(a,i+1)
    else:
        ans=0
        for a,b,c,d in q:
            if num_list[b-1]-num_list[a-1]==c:
                ans+=d
        score[0]=max(ans,score[0])



dfs(num_list,0)
print((score[0]))
# Original solution
N, M, Q = list(map(int, input().split()))
S=[[0 for i in range(4)] for j in range(Q)]
for i in range(Q):
    inputS=list(map(int,input().split()))
    S[i][0]=inputS[0]
    S[i][1]=inputS[1]
    S[i][2]=inputS[2]
    S[i][3]=inputS[3]
S2=[]
maxNum=0
for a in range(11):
    for b in range(a,11):
        for c in range(b,11):
            for d in range(c,11):
                for e in range(d,11):
                    for f in range(e,11):
                        for g in range(f,11):
                            for h in range(g,11):
                                for i in range(h,11):
                                    for j in range(i,11):
                                        S2.append([a,b,c,d,e,f,g,h,i,j])
                                        
                                        count=0
                                        for k in range(Q):
                                            if S2[len(S2)-1][S[k][1]-1]-S2[len(S2)-1][S[k][0]-1]==S[k][2]:
                                                count+=S[k][3]
                                        maxNum=max(maxNum,count)

print(maxNum)


# Optimized solution
N, M, Q = list(map(int, input().split()))
S=[[0 for i in range(4)] for j in range(Q)]
for i in range(Q):
    inputS=list(map(int,input().split()))
    S[i][0]=inputS[0]
    S[i][1]=inputS[1]
    S[i][2]=inputS[2]
    S[i][3]=inputS[3]
S2=[]
maxNum=0
for a in range(1,M+1):
    for b in range(a,M+1):
        for c in range(b,M+1):
            for d in range(c,M+1):
                for e in range(d,M+1):
                    for f in range(e,M+1):
                        for g in range(f,M+1):
                            for h in range(g,M+1):
                                for i in range(h,M+1):
                                    for j in range(i,M+1):
                                        S2=[a,b,c,d,e,f,g,h,i,j]
                                        count=0
                                        for k in range(Q):
                                            if S2[S[k][1]-1]-S2[S[k][0]-1]==S[k][2]:
                                                count+=S[k][3]
                                        maxNum=max(maxNum,count)
                                       
    
print(maxNum)
# Original solution
#
# abc165 c
#
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(100000)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 4 3
1 3 3 100
1 2 2 10
2 3 2 10"""
        output = """110"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328"""
        output = """357500"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 10 1
1 10 9 1"""
        output = """1"""
        self.assertIO(input, output)


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

    T = []
    func([1], T)

    ans = 0
    for t in T:
        score = 0
        for abcd in ABCD:
            a, b, c, d = abcd
            if t[b-1] - t[a-1] == c:
                score += d
        ans = max(ans, score)
    print(ans)


def func(L, T):
    if len(L) == N:
        T.append(L)
        return

    for i in range(L[-1], M+1):
        NL = L + [i]
        func(NL, T)


if __name__ == "__main__":
    # unittest.main()
    resolve()


# Optimized solution
#
# abc165 c
#
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(100000)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 4 3
1 3 3 100
1 2 2 10
2 3 2 10"""
        output = """110"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328"""
        output = """357500"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 10 1
1 10 9 1"""
        output = """1"""
        self.assertIO(input, output)


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

    print((dfs([1])))


def dfs(L):
    if len(L) == N:
        score = 0
        for abcd in ABCD:
            a, b, c, d = abcd
            if L[b-1] - L[a-1] == c:
                score += d
        return score

    ans = 0
    for i in range(L[-1], M+1):
        NL = L + [i]
        ans = max(ans, dfs(NL))

    return ans


if __name__ == "__main__":
    # unittest.main()
    resolve()
# Original solution
n, m, q = 0, 0, 0
ABCD = []
ans = 0


def dfs(ns):
    global ans

    A = [int(c) for c in ns.split()]
    if len(A) == n:
        s = 0
        for qi in range(q):
            a, b, c, d = ABCD[qi]
            if A[b - 1] - A[a - 1] == c:
                s += d

        ans = max(s, ans)
        return

    for i in range(A[-1], m + 1):
        dfs('{} {}'.format(ns, str(i)))


def main():
    global n, m, q, ABCD
    n, m, q = [int(e) for e in input().split()]
    ABCD = [[int(e) for e in input().split()] for i in range(q)]

    for mi in range(1, m + 1):
        dfs(str(mi))

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
n, m, q = 0, 0, 0
ABCD = []
ans = 0


def dfs(A):
    global ans

    if len(A) == n:
        s = 0
        for qi in range(q):
            a, b, c, d = ABCD[qi]
            if A[b - 1] - A[a - 1] == c:
                s += d

        ans = max(s, ans)
        return

    for i in range(A[-1], m + 1):
        AC = A.copy()
        AC.append(i)
        dfs(AC)


def main():
    global n, m, q, ABCD
    n, m, q = [int(e) for e in input().split()]
    ABCD = [[int(e) for e in input().split()] for i in range(q)]

    for mi in range(1, m + 1):
        dfs([mi])

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
# https://atcoder.jp/contests/abc165/tasks/abc165_c
# C - Many Requirements
import sys

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


def resolve():
    n, m, q = list(map(int, input().split()))
    ABCD = [list(map(int, input().split())) for _ in range(q)]

    # 数列Aのスコアを返す
    def calc_score(L):
        score = 0
        for a, b, c, d in ABCD:
            if L[b - 1] - L[a - 1] == c:
                score += d
        return score

    # 数列Aを全パターン試す
    def dfs(A):
        if len(A) == n:
            return calc_score(A)

        res = 0
        prev = A[-1] if len(A) > 0 else 1
        for v in range(prev, m + 1):
            A.append(v)
            res = max(res, dfs(A))
            A.pop()

        return res

    print((dfs([])))


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys

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


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

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


if __name__ == '__main__':
    resolve()