Problem:
 Score : 300 points    Problem Statement Given are positive integers N , M , Q , and Q quadruples of integers ( a_i , b_i , c_i , d_i ).  Consider a sequence A satisfying the following conditions:   A is a sequence of N positive integers.  1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M .   Let us define a score of this sequence as follows:   The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i . (If there is no such i , the score is 0 .)   Find the maximum possible score of A .      Constraints  All values in input are integers.  2 ≤ N ≤ 10  1 \leq M \leq 10  1 \leq Q \leq 50  1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )  0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )  (a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j )  1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )         Input Input is given from Standard Input in the following format:  N  M  Q  a_1  b_1  c_1  d_1  :  a_Q  b_Q  c_Q  d_Q       Output Print the maximum possible score of A .        Sample Input 1 3 4 3
1 3 3 100
1 2 2 10
2 3 2 10      Sample Output 1 110  When A = \{1, 3, 4\} , its score is 110 . Under these conditions, no sequence has a score greater than 110 , so the answer is 110 .       Sample Input 2 4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328      Sample Output 2 357500       Sample Input 3 10 10 1
1 10 9 1      Sample Output 3 1

Solution (Python):
n,m,q=map(int,input().split())
abc=[]
def f(L):
    v=0
    for a,b,c,d in abc:
        
        if L[b-1]-L[a-1] !=c:
            pass
        else:
            v+=d
    else:
        return v
def gen(ls):
    res=[]
    for l in ls:
        for j in range(l[-1],m+1):
            ll = l.copy()
            ll.append(j)
            res.append(ll)
    return res

for _ in range(q):
    a,b,c,d=map(int,input().split())
    abc.append((a,b,c,d))
K = [[1]]
for _ in range(n-1):
    K = gen(K)
max_=0
for k in K:
    max_=max(max_,f(k))
print(max_)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

    data = [[1]]
    for _ in range(N):
        tmp = []
        for d in data:
            v = d[-1]
            for m in range(M):
                if v + m > M:
                    break
                tmp.append(d + [v + m])
        data = tmp

    ans = 0
    for val in data:
        tmp_ans = 0
        for a, b, c, d in abcd:
            if val[b - 1] - val[a - 1] == c:
                tmp_ans += d
        if tmp_ans > ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import itertools

def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    l = list(range(N+M-1))
    max_score = 0
    for comb in itertools.combinations(l, M-1):
        A = []
        n = 1
        for i in range(N+M-1):
            if i in comb:
                n+=1
            else:
                A.append(n)
        
        score = sum([di if A[bi-1] - A[ai-1] == ci else 0 for ai, bi, ci, di in zip(a, b, c, d)])
        max_score = max(score, max_score)

    print(max_score)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    Q = int(next(tokens))  
    a = [int()] * (Q)  
    b = [int()] * (Q)  
    c = [int()] * (Q)  
    d = [int()] * (Q)  
    for i in range(Q):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations_with_replacement
N,M,Q=map(int,input().split())
abcd = [[int(j) for j in input().split()] for i in range(Q)]


ans =list(combinations_with_replacement(range(1,M+1),N))
x=[]
for i in range(len(ans)):
  kz=0
  a = list(ans[i])
  for j in range(Q):
    if a[(abcd[j][1])-1] - a[(abcd[j][0])-1] == abcd[j][2]:
      kz += abcd[j][3]
    else:
      continue
  x.append(kz)
  
x.sort()

print(x[-1])

Solution (Python):
import itertools
import numpy as np

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

l = np.arange(1, M + 1)
max = 0
temp = 0

for c in itertools.combinations_with_replacement(l, N):
    for d in grid:
        if c[d[1]-1] - c[d[0]-1] == d[2]:
            temp += d[3]
    if max < temp:
        max = temp
    temp = 0

print(max)

Solution (Python):
import itertools

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

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

tmp = []
for v in hoge:
    sum = 0
    for i in range(Q):
        if v[b[i] - 1] - v[a[i] - 1] == c[i]:
            sum += d[i]

    tmp.append(sum)

print(max(tmp))

Solution (Python):
import itertools

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

li=[i for i in range(1,m+1)]
li=list(itertools.combinations_with_replacement(li,n))

result=[]

for j in li:
    ans=0
    for k in abcd:
        t1=k[1]-1
        t2=k[0]-1
        if j[t1]-j[t2]==k[2]:
            ans+=k[3]
    else:
        result.append(ans)

print(max(result))

Solution (Python):
import itertools
n, m, q = map(int, input().split())
l = [list(map(int, input().split())) for i in range(q)]
c = [i for i in range(1, m+1)]

i = list(itertools.combinations_with_replacement(c, n))

ans = 0
for j in i:
    temp = 0
    for k in l:
        if j[k[1]-1]-j[k[0]-1] == k[2]:
            temp += k[3]
    ans = max(ans, temp)

print(ans)

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

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

c = itertools.combinations_with_replacement(A, N)

data=[None]*Q

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

ans=0
for p in c:
    tmp=0
    for d in data:
        if p[d[1]-1] - p[d[0]-1] == d[2]:
            tmp+=d[3]
    ans=max(ans,tmp)
print(ans)

Solution (Python):
import itertools





















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


def calculate(n, m, q, arr):
    result = []
    for comb in itertools.combinations_with_replacement(range(1, m + 1), n):
        result.append(mmm(list(comb), arr))

    print(max(result))


def mmm(comb, arr):
    result = 0

    for ar in arr:
        if (comb[ar[1] - 1] - comb[ar[0] - 1]) == ar[2]:
            result = result + ar[3]
    return result


calculate(N, M, Q, ARR)

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

import itertools

tmp = []
for i in range(1,M+1):
    tmp.append(i)
A = list(itertools.combinations_with_replacement(tmp,N))
tmp2 = []
for i in range(len(A)):
    B = list(A[i])
    B.sort()
    tmp2.append(B)
answer = 0


for i in range(len(A)):
    ans = 0
    for j in range(Q):
        if tmp2[i][ abcd[j][1]-1 ] - tmp2[i][ abcd[j][0]-1 ] == abcd[j][2]:
            ans += abcd[j][3]  
    answer = max(answer,ans)
    
print(answer)

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

from collections import deque

N, M, Q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for i in range(Q)]
abcd = [(a-1, b-1, c, d) for a,b,c,d in abcd]

ans = -1e18
q = deque()
def rec():
    global ans
    if len(q) == N:
        s = 0
        l = list(q)
        for a,b,c,d in abcd:
            if l[b] - l[a] == c:
                s += d
        ans = max(ans, s)
    else:
        for j in range(q[-1], M + 1):
            q.append(j)
            rec()
            q.pop()

q.append(1)

rec()
print(ans)

Solution (Python):
def mapint_inp():
    return map(int, input().split())

def intinp():
    return int(input())

N, M, Q = mapint_inp()
abcds = [list(mapint_inp()) for _ in range(Q)]

import itertools
l = []
for v in itertools.combinations_with_replacement(list(range(1,M+1)), N):
    l.append(v)
ans = 0
for x in l:
    t = 0
    for abcd in abcds:
        a, b, c, d = abcd
        if (x[b-1]-x[a-1])==c:
            t += d
    ans = max(ans, t)
print(ans)

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

ans = 0

import itertools
l = [i for i in range(1, M+1)]

for p in (itertools.combinations_with_replacement(l, N)):
    tmp = 0
    for a, b, c, d in Q:
        if p[b-1] - p[a-1] == c:
            tmp += d
    ans = max(tmp, ans)
print(ans)

Solution (Python):



from itertools import combinations_with_replacement


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

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

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

    max_total = 0
    for A in As:
        total = 0

        for i, q in enumerate(queries):
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                total += q[3]

        max_total = max(max_total, total)

    print(max_total)


if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

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

print(ans)

Solution (Python):
import itertools

def main():
    
    N, M, Q_num= map(int, input().split())
    Q = [list(map(int, input().split(" "))) for _ in range(Q_num)]
    
    
    
    l = range(1, M+1)
    max_score = 0
    for v in itertools.combinations_with_replacement(l, N):
        score = 0
        for q in Q:
            if (v[q[1]-1] - v[q[0]-1]) == q[2]:
                score += q[3]
            max_score = max(max_score, score)
    print(max_score)
    
if __name__ == "__main__":
    main()

Solution (Python):


import itertools

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

ans = 0
cnt = 0
X = [i for i in range(1, M+1)]

for i in itertools.combinations_with_replacement(X, r=N):
    cnt = 0
    j = list(i)
    for k in lis:
        if j[k[1]-1] - j[k[0]-1] == k[2]:
            cnt += k[3]
    ans = max(ans, cnt)

print(ans)

Solution (Python):


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

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

    def pattern(curr):
        if len(curr) == n:
            t = 0
            for a, b, c, d in points:
                if curr[b] - curr[a] == c:
                    t += d
            return t
        else:
            curr_max = 0
            if curr:
                st = curr[-1]
                ed = m
            else:
                st = 1
                ed = m

            for nxt in range(st, ed + 1):
                p = pattern(curr + [nxt])
                if p > curr_max:
                    curr_max = p
            return curr_max

    print(pattern([]))


submit()

Solution (Python):
N,M,Q=map(int,input().split())
que=[tuple(map(int,input().split())) for _ in range(Q)]
task=[]
sta=[[i] for i in range(1,M+1)]
while sta:
    s=sta.pop()
    if len(s)==N: task.append(s)
    else:
        for d in range(int(s[-1]),M+1):
            sta.append(s+[d])
ans=0
for s in task:
    tmp=0
    for a,b,c,d in que:
        if s[b-1]-s[a-1]==c: tmp+=d
    ans=max(ans,tmp)
print(ans)

Solution (Python):
import itertools

n, m, q = map(int, input().split())
l = []
ans = 0
ref = 0

for i in range(q):
    abcd = list(map(int, input().split()))
    l.append(abcd)

for i in itertools.combinations_with_replacement(range(1, m + 1), n):
    for j in range(q):
        if i[l[j][1] - 1] - i[l[j][0] - 1] == l[j][2]:
            ref += l[j][3]
    ans = max(ans, ref)    
    ref = 0

print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



from itertools import combinations

N,M,Q = LI()
a,b,c,d = LIR(Q,4)

X = list(combinations(range(N+M-1),M-1))

ans = 0
for x in X:
    s = 0
    L = []
    for x0 in x:
        L.append(x0-s)
        s = x0+1
    L.append(N+M-1-s)
    now = []
    for i in range(len(L)):
        now.extend([i+1]*L[i])
    val = 0
    for i in range(Q):
        if now[b[i]-1]-now[a[i]-1] == c[i]:
            val += d[i]
    if val > ans:
        ans = val

print(ans)

Solution (Python):
import itertools

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

a, b, c, d = [], [], [], []

for _ in range(q):
    ai, bi, ci, di = input().split()
    a.append(int(ai) - 1)
    b.append(int(bi) - 1)
    c.append(int(ci))
    d.append(int(di))

seqs = itertools.combinations_with_replacement(range(m), n)

ans = 0
for seq in seqs:
    res = 0
    for i in range(q):
        if seq[b[i]] - seq[a[i]] == c[i]:
            res += d[i]
    ans = max(ans, res)
print(ans)

Solution (Python):
from itertools import permutations, combinations,combinations_with_replacement,product
import sys
input = sys.stdin.readline

n, m, q = map(int, input().replace("\n","").split())
a = [[int(j) for j in input().replace("\n","").split()] for i in range(q)]


tmp = [i for i in range(1,m+1)]
chk = list(combinations_with_replacement(tmp, n))


tmpans = 0
ans = 0

for i in range(len(chk)):
    tmpans = 0
    for j in range(q):
        
        """
        print(a[j])
        print(chk[i])
        print(listchk)
        print(a[j][1])
        print(a[j][0])
        print(listchk[a[j][1]-1])
        print(listchk[a[j][0]-1])
        print(a[j][2])
        """
        if (chk[i][a[j][1]-1] - chk[i][a[j][0]-1]) == a[j][2]:
            tmpans += a[j][3]
    if tmpans >= ans:
        ans = tmpans

print(ans)

Solution (Python):
import itertools



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

abcd = []

for i in range(q):
    s = input()
    aa, bb, cc, dd = map(int, s.split(' '))
    abcd.append((aa, bb, cc, dd))




max_point = 0

for A in itertools.combinations_with_replacement(range(1, m+1), n):
    
    point = 0
    for t in abcd:
        
        if A[t[1]-1] - A[t[0]-1] == t[2]:
            point += t[3]

    
    if max_point < point:
        max_point = point

print(max_point)

Solution (Python):
from itertools import combinations_with_replacement
N, M, Q = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(Q)]
max = 0
for A in combinations_with_replacement(range(1, M + 1), N):
    A = (1, ) + A + (M, )
    score = 0
    for i in range(Q):
        if A[a[i][1]] - A[a[i][0]] == a[i][2]:
            score += a[i][3]
    if score > max:
        max = score
print(max)

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

def point(x):
    temp = 0
    for i in range(Q):
        a, b, c, d = abcd[i]
        if x[b-1] - x[a-1] == c:
            temp += d
    return temp

def maker(x):
    dig = len(x)
    if  dig == N:
        ans.add(point(x))
    else:
        for i in range(x[-1], M+1):
            maker(x + [i])

for i in range(1, M+1):
    maker([i])
print(max(ans))

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
from collections import defaultdict

def f(x, i):
    if i == 10:
        return check(x)
    ret = 0
    for y in range(x[-1], M+1):
        ret = max(ret, f(x+[y], i+1))
    return ret

def check(x):
    ret = 0
    for a, b, c, d in abcd:
        if x[b-1] - x[a-1] == c:
            ret += d
    return ret

N, M, Q = map(int, input().split())
abcd = [None]*Q
for i in range(Q):
    abcd[i] = list(map(int, input().split()))
print(f([1], 1))

Solution (Python):
import itertools

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

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

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



res = 0

for c in comb:
    tmp_score = 0
    for a in alist:
        if c[a[1]-1] - c[a[0]-1] == a[2]:
            tmp_score += a[3]
    res = max(res, tmp_score)

print(res)

Solution (Python):
def next_arr(a,n,m):
    a[-1] += 1
    for i in range(n)[::-1]:
        if(a[i] > m):
            if(i == 0):
                return(False)
            a[i-1] += 1
            a[i] = a[i-1]
    for i in range(1,n):
        if(a[i] > m):
            a[i] = a[i-1]
    return(True)


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

a = [0 for _ in range(q)]
b = [0 for _ in range(q)]
c = [0 for _ in range(q)]
d = [0 for _ in range(q)]

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

arr = [1 for _ in range(n)]
arr[-1] = 0
ma = 0
while(next_arr(arr,n,m)):
    sc = 0
    for i in range(q):
        if(arr[b[i]-1] - arr[a[i]-1] == c[i]):
            sc += d[i]
    ma = max(sc,ma)
print(ma)

Solution (Python):
import itertools
n, m, q = map(int , input().split(" "))
l = []
for i in range(q):
    a, b, c, d = map(int, input().split(" "))
    l.append((a, b, c, d))


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

print(max([ score(l, t)  for t in itertools.combinations_with_replacement(list(range(1, m+1)), n)] ))

Solution (Python):
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)

Solution (Python):
import itertools

N,M,Q=map(int,input().split())
a_list,b_list,c_list,d_list=[],[],[],[]
for _ in range(Q):
    a,b,c,d=map(int,input().split())
    a_list.append(a);b_list.append(b);c_list.append(c);d_list.append(d)

score_max=0
for A in itertools.combinations_with_replacement(list(range(1,M+1)), N):
    score=0
    for i in range(Q):
        if A[b_list[i]-1]-A[a_list[i]-1]==c_list[i]:
            score+=d_list[i]
    score_max=max(score,score_max)
print(score_max)

Solution (Python):
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)

Solution (Python):
import itertools

n, m, q = map(int, input().split())
data = []
m_arange = list(range(1, m+1))
m = 0
ans = 0

for i in range(q):
    data.append(list(map(int, input().split())))

pattern = list(itertools.combinations_with_replacement(m_arange, n))


for row in pattern:
    for x in data:
        if row[x[1]-1] - row[x[0]-1]== x[2]:
            m += x[3]
    ans = max(ans, m)
    m = 0 

print(ans)

Solution (Python):
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)

def max_point(ary):
    if len(ary) == n:
        pt = 0
        for i in range(q):
            if ary[b[i] - 1] - ary[a[i] - 1] == c[i]:
                pt += d[i]
        return pt
    if len(ary) == 0:
        s = 1
    else:
        s = ary[-1]
    return max([max_point(ary + [i]) for i in range(s, m + 1)])

print(max_point([]))

Solution (Python):



import itertools as it

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

    li = []
    for q in range(Q):
        li.append(list(map(int, input().split())))
    
    maxp = 0

    comb = it.combinations_with_replacement(list(range(1, M+1)), N+2)
    
    for v in comb:
        if v[0] != 1 or v[N+1] != M:
            continue
        point = 0
        for q in li:
            if v[q[1]] - v[q[0]] == q[2]:
                point += q[3]
        
        if point > maxp:
            maxp = point
    
    print(maxp)



if __name__ == '__main__':
    main()

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

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

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

for n in range(N-1):
    arr=[]
    for i in range(len(Arr)):
        for j in range(Arr[i][-1],M+1):
            arr.append(Arr[i]+[j])
    
    Arr=arr

ans=0
for i in range(len(Arr)):
    l=[0]+Arr[i]
    cnt=0
    for a,b,c,d in L:
        if l[b]-l[a]==c:
            cnt+=d
    if ans<cnt:
        ans=cnt
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n,m,q = map(int,input().split())
abcd = [list(map(int,input().split())) for _ in range(q)]
A = [[i] for i in range(1,m+1)]
def listup(A,m):
  b = []
  for i in A:
    for j in range(i[-1],m+1):
      b.append(i+[j])
  return b
      
for i in range(n-1):
  A = listup(A,m)
ans = 0
for i in A:
  temp = 0
  for j in abcd:
    if i[j[1]-1]-i[j[0]-1] == j[2]:
      temp += j[3]
  ans = max(ans,temp)
print(ans)

Solution (Python):
import itertools

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

Solution (Python):
import itertools
n, m, q = map(int, input().split())
params = []
for _ in range(q):
    a1, a2, a3, a4 = map(int, input().split())
    params.append([a1, a2, a3, a4])

high_score = 0
for x in itertools.combinations_with_replacement(range(1, m + 1), n):
    score = 0
    for a, b, c, d in params:
        if x[b - 1] - x[a - 1] == c:
            score += d
    high_score = max(high_score, score)

print(high_score)

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

ans = 0
for i in range(2**(N+M-1)):
    cnt = 0
    mm = bin(i)
    for j in list(str(mm)):
        if j=="1":
            cnt += 1
    if cnt != M-1:
        continue
    
    li = [0]
    mm2 = 1
    for j in range(N+M-1):
        if (i>>j)&1:
            mm2 += 1
        else:
            li.append(mm2)
    
    ttl = 0
    for j in range(Q):
        a,b,c,d = ab[j]
        if li[b]-li[a]==c:
            ttl += d
    ans = max(ans,ttl)
    
    

print(ans)

Solution (Python):
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)

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


ans = 0
for A in combinations_with_replacement([j for j in range(1, m+1)], n):
    temp = 0
    for a, b, c, d in pair:
        if A[b-1] - A[a-1] == c:
            temp += d
    ans = max(temp, ans)

print(ans)

Solution (Python):
import copy
from pprint import pprint

N, M, Q = [int(x) for x in input().strip().split(" ")]
ABCD = [[int(x) for x in input().strip().split(" ")] for _ in range(Q)] 


def make_a_set(len_, min_, max_):
    if len_ == 1:
        return [[v] for v in range(min_, max_ + 1)]
    A = []
    for i in range(min_, max_ + 1):
        sub_a = make_a_set(len_ -1 , i, max_)
        A += [[i] + sub_a_ for sub_a_ in sub_a]
    return A

q_sum_set = []
for A in make_a_set(N, 1, M):
    q = 0
    for a, b, c, d in ABCD:
        a -= 1
        b -= 1
        if A[b] - A[a] == c:
            q += d
    q_sum_set.append(q)
print(max(q_sum_set))

Solution (Python):
import sys
sys.setrecursionlimit(100000000)


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

    all_qlist = []
    for i in range(q):
        _qlist = list(map(int, input().split()))
        all_qlist.append(_qlist)
    max = sum([x[3] for x in all_qlist])

    all_list = []
    
    _list = []
    for i in range(1, m+1):
        tmp_list = []
        all_list.append(tmp_list)
        tmp_list.append(i)

    for _ in range(n-1):
        tmp_all_list = []
        for _list_sub in all_list:
            x = _list_sub[-1]
            for i in range(x, m+1):
                tmp_list = _list_sub.copy()
                tmp_all_list.append(tmp_list)
                tmp_list.append(i)
        all_list = tmp_all_list

    total_max = 0
    for _list in all_list:
        total = 0
        for _qlist in all_qlist:
            if _list[_qlist[1]-1] - _list[_qlist[0]-1] == _qlist[2]:
                total += _qlist[3]
        if total > total_max:
            total_max = total
        if total_max == max:
            break

    print(total_max)


main()

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

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

if N == 2:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            A.append(i)
            A.append(j)
            arr.append(A)
            A = []
    ans = check(arr)
elif N == 3:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                A.append(i)
                A.append(j)
                A.append(k)
                arr.append(A)
                A = []
    ans = check(arr)
elif N == 4:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    A.append(i)
                    A.append(j)
                    A.append(k)
                    A.append(l)
                    arr.append(A)
                    A = []
    ans = check(arr)
elif N == 5:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        A.append(i)
                        A.append(j)
                        A.append(k)
                        A.append(l)
                        A.append(m)
                        arr.append(A)
                        A = []
    ans = check(arr)
elif N == 6:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            A.append(i)
                            A.append(j)
                            A.append(k)
                            A.append(l)
                            A.append(m)
                            A.append(n)
                            arr.append(A)
                            A = []
    ans = check(arr)
elif N == 7:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                A.append(i)
                                A.append(j)
                                A.append(k)
                                A.append(l)
                                A.append(m)
                                A.append(n)
                                A.append(o)
                                arr.append(A)
                                A = []
    ans = check(arr)
elif N == 8:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                for p in range(o, M+1):
                                    A.append(i)
                                    A.append(j)
                                    A.append(k)
                                    A.append(l)
                                    A.append(m)
                                    A.append(n)
                                    A.append(o)
                                    A.append(p)
                                    arr.append(A)
                                    A = []
    ans = check(arr)
elif N == 9:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                for p in range(o, M+1):
                                    for q in range(p, M+1):
                                        A.append(i)
                                        A.append(j)
                                        A.append(k)
                                        A.append(l)
                                        A.append(m)
                                        A.append(n)
                                        A.append(o)
                                        A.append(p)
                                        A.append(q)
                                        arr.append(A)
                                        A = []
    ans = check(arr)
elif N == 10:
    arr = []
    A = []
    for i in range(1, M+1):
        for j in range(i, M+1):
            for k in range(j, M+1):
                for l in range(k, M+1):
                    for m in range(l, M+1):
                        for n in range(m, M+1):
                            for o in range(n, M+1):
                                for p in range(o, M+1):
                                    for q in range(p, M+1):
                                        for r in range(q, M+1):
                                            A.append(i)
                                            A.append(j)
                                            A.append(k)
                                            A.append(l)
                                            A.append(m)
                                            A.append(n)
                                            A.append(o)
                                            A.append(p)
                                            A.append(q)
                                            A.append(r)
                                            arr.append(A)
                                            A = []
    ans = check(arr)

print(ans)