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):
import sys
input = sys.stdin.readline

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

if n == 2:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            l = [i1, i2]
            v = 0
            for j in x:
                if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                    v += j[3]
            ans = max(ans, v)
elif n == 3:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                l = [i1, i2, i3]
                v = 0
                for j in x:
                    if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                        v += j[3]
                ans = max(ans, v)
elif n == 4:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    l = [i1, i2, i3, i4]
                    v = 0
                    for j in x:
                        if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                            v += j[3]
                    ans = max(ans, v)
elif n == 5:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    for i5 in range(i4, m + 1):
                        l = [i1, i2, i3, i4, i5]
                        v = 0
                        for j in x:
                            if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                v += j[3]
                        ans = max(ans, v)
elif n == 6:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    for i5 in range(i4, m + 1):
                        for i6 in range(i5, m + 1):
                            l = [i1, i2, i3, i4, i5, i6]
                            v = 0
                            for j in x:
                                if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                    v += j[3]
                            ans = max(ans, v)
elif n == 7:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    for i5 in range(i4, m + 1):
                        for i6 in range(i5, m + 1):
                            for i7 in range(i6, m + 1):
                                l = [i1, i2, i3, i4, i5, i6, i7]
                                v = 0
                                for j in x:
                                    if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                        v += j[3]
                                ans = max(ans, v)
elif n == 8:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    for i5 in range(i4, m + 1):
                        for i6 in range(i5, m + 1):
                            for i7 in range(i6, m + 1):
                                for i8 in range(i7, m + 1):
                                    l = [i1, i2, i3, i4, i5, i6, i7, i8]
                                    v = 0
                                    for j in x:
                                        if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                            v += j[3]
                                    ans = max(ans, v)
elif n == 9:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    for i5 in range(i4, m + 1):
                        for i6 in range(i5, m + 1):
                            for i7 in range(i6, m + 1):
                                for i8 in range(i7, m + 1):
                                    for i9 in range(i8, m + 1):
                                        l = [i1, i2, i3, i4, i5, i6, i7, i8, i9]
                                        v = 0
                                        for j in x:
                                            if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                                v += j[3]
                                        ans = max(ans, v)
else:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                for i4 in range(i3, m + 1):
                    for i5 in range(i4, m + 1):
                        for i6 in range(i5, m + 1):
                            for i7 in range(i6, m + 1):
                                for i8 in range(i7, m + 1):
                                    for i9 in range(i8, m + 1):
                                        for i10 in range(i9, m + 1):
                                            l = [i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
                                            v = 0
                                            for j in x:
                                                if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                                    v += j[3]
                                            ans = max(ans, v)

print(ans)

Solution (Python):
import itertools

n_len, n_max, q = map(int, input().split())
qs = []
for i in range(q):
    qs.append(tuple(map(int, input().split())))

ans = 0
for seq in itertools.combinations_with_replacement(range(1, n_max+1), n_len):
    point_tmp = 0
    for a, b, c, d in qs:
        if(seq[b-1]-seq[a-1] == c):
            point_tmp += d
            
    ans = max(ans, point_tmp)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
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.append(_a-1)
    b.append(_b-1)
    c.append(_c)
    d.append(_d)
ans = 0

for A in combinations_with_replacement(range(1, M+1), r=N):
    dmax = 0
    for q in range(Q):
        if A[b[q]] - A[a[q]] == c[q]:
            dmax += d[q]
    ans = max(ans, dmax)
print(ans)

Solution (Python):
from sys import stdin
from itertools import combinations_with_replacement


def get_result(data):
    N, M, Q = data[0]
    cond = data[1:]
    seq = [i+1 for i in range(M)]
    all_seq = combinations_with_replacement(seq, N)
    scores = []
    for seq in all_seq:
        score = 0
        for c in cond:
            if (seq[c[1]-1] - seq[c[0]-1]) == c[2]:
                score += c[3]
        scores.append(score)
    return max(scores)


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Solution (Python):
import itertools
 
N,M,Q=map(int,input().split())
all=[]
for i in range(Q):
  all.append(list(map(int,input().split())))
 
x=[i for i in range(1,M+1)]
maxdsum=0
 
for A in itertools.combinations_with_replacement(x, N):
  dsum=0
  for num in range(Q):
    if A[all[num][1]-1]-A[all[num][0]-1]==all[num][2]:
      dsum+=all[num][3]
  if maxdsum<dsum:
    maxdsum=dsum
print(maxdsum)

Solution (Python):
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
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)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

Solution (Python):
import itertools

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

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)
  
ans = 0

for team in itertools.combinations_with_replacement(range(1,M+1), N):
  cnt = 0
  for i in range(Q):
    a = List[i][0]
    b = List[i][1]
    c = List[i][2]
    d = List[i][3]
    
    if team[b-1] - team[a-1] == c:
      cnt += d
  ans = max(ans ,cnt)  
  
print(ans)

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from itertools import combinations_with_replacement

n, m, q = na()
abcd = nan(q)

ans = 0
for p in combinations_with_replacement(range(1, m + 1), n):
    t = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        a -= 1
        b -= 1
        if p[b] - p[a] == c:
            t += d
    ans = max(ans, t)

print(ans)

Solution (Python):
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

ans = 0


def dfs(cur, l):
    global ans
    if len(l) == N:
        s = 0
        for cond in conds:
            a, b, c, d = cond
            if l[b] - l[a] == c:
                s += d
        ans = max(ans, s)
        return

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


dfs(1, [1])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import combinations_with_replacement
def main():
    N, M, Q = map(int, input().split())
    q_list = []
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        q_list.append((a, b, c, d))
    A = combinations_with_replacement(range(1, M+1), N)
    ans = 0
    for Ai in A:
        v = 0
        Ai = list(map(int, Ai))
        for a, b, c, d in q_list:
            if Ai[b-1]-Ai[a-1] == c:v += d
        ans = max(ans, v)
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
import math
import copy
import sys

rl = sys.stdin.readline

n,m,q=map(int,rl().split())
x=[list(map(int,rl().split())) for i in range(q)]


def calc(lst):
  sum=0
  
  for a,b,c,d in x:
    if(lst[b]-lst[a]==c):
      sum+=d
      
  return sum
      
def dfs(n,lst,i):
  ans=0
  lst.append(i)
  if(n==0):
    ans=calc(lst)
  else:
    for j in range(i,m+1):
      tmp=copy.copy(lst)
      ans=max(ans,dfs(n-1,tmp,j))
  return ans

ans=0
for i in range(1,m+1):
  ans=max(ans,dfs(n+1,[],i))
print(ans)

Solution (Python):



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


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


def add(l):
    ret = []
    for i in range(len(l)):
        last = l[i][-1]
        for j in range(last, M+1):
            ret.append(l[i] + [j])
    return ret

for i in range(N-1):
    dfs = add(dfs)



m = -1
for i in range(len(dfs)):
    A = dfs[i]
    tmp = 0
    for j in range(len(lq)):
        aj = lq[j][0]
        bj = lq[j][1]
        cj = lq[j][2]
        dj = lq[j][3]
        
        
        
        
        
            
        if(A[bj-1]-A[aj-1] == cj):
            tmp += dj
    
    if(tmp > m):
        m = tmp    
print(m)

Solution (Python):
import itertools

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

max_score = 0

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
    max_score = max(max_score, score)

print(max_score)

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

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

cnt = 0

for i in range(len(A)):
    tmp = 0
    for j in range(q):
        if(A[i][b[j]-1] - A[i][a[j]-1]) == c[j]:
            tmp = tmp + d[j]
    cnt = max(cnt, tmp)
print(cnt)

Solution (Python):
from itertools import combinations_with_replacement as cmb

N, M, Q = map(int, input().split())
a = []
b = []
c = []
d = []
s = 0

for _ in range(Q):
    _a, _b, _c, _d = map(int, input().split())
    a.append(_a)
    b.append(_b)
    c.append(_c)
    d.append(_d)

for A in cmb(range(1,M+1),N):
    _s = 0
    for q in range(Q):
        if A[b[q]-1] - A[a[q]-1] == c[q]:
            _s += d[q]
    
    s = max(s,_s)

print(s)

Solution (Python):
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
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)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

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

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

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


ans=0
for A in itertools.combinations_with_replacement(range(1,M+1),N):
    A=sorted(A)
    total=0
    for k in range(Q):
        if A[(S[k][1]-1)]-A[S[k][0]-1]==S[k][2]:
            total+=S[k][3]
    if total>ans:
        ans=total

print(ans)

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

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

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

ans = 0
def dfs(A):
  global ans
  if len(A) == N:
    ans = max(ans, calc_score(A))
  else:
    last_a = A[-1]
    for a in range(last_a, M+1):
      dfs(A + [a])

dfs([1])
print(ans)

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

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

A = []


def dfs(num):
    if len(num) == n:
        A.append(num)
    else:
        now = num[-1]
        for i in range(now, m + 1):
            dfs(num + [i])


for i in range(1, 10):
    dfs([i])

ans = 0
for now in A:
    point = 0
    for a, b, c, d in abcd:
        if now[b-1] - now[a-1] == c:
            point += d

    ans = max(ans, point)

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

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

s = [1] * (n)

ans = []

def dfs(k, c,old,ans):
    
    
    if k == n:
        tmp = 0
        for i in range(q):
            if s[abcd[i][1]-1] - s[abcd[i][0]-1] == abcd[i][2]:
                tmp += abcd[i][3]
        
        ans.append(tmp)
        return
    for i in range(old,c+1):
        
        s[k] = i
        dfs(k+1, m,i,ans)
        
    
    
    return ans

print(max(dfs(0, m,1,ans)))

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

A = []
M = []
for i in range(1, m+1):
    M.append(i)
for team in itertools.combinations_with_replacement(M, n):
    A.append(team)
    
B = [0]*len(A)
for i in range(q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    for j in range(len(A)):
        if A[j][b-1] - A[j][a-1] == c:
            B[j] += d

print(max(B))

Solution (Python):
import sys

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


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

    res = []
    for e in range(1, 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):
                            for k in range(j, m + 1):
                                for l in range(k, m + 1):
                                    for o in range(l, m + 1):
                                        for p in range(o, m + 1):
                                            res.append([e, f, g, h, i, j, k, l, o, p])

    ans = 0
    for p in res:
        score = 0
        for a, b, c, d in ABCD:
            if p[b - 1] - p[a - 1] == c:
                score += d

        ans = max(ans, score)

    print(ans)


if __name__ == '__main__':
    resolve()

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

A = list(itertools.combinations_with_replacement(range(1,M+1), N))
number = len(A)

amount = 0
max_amount = 0
for i in range(number): 
    
    for j in range(Q): 
        bi = P[j][1]
        ai = P[j][0]
        if(A[i][bi-1] - A[i][ai-1] == P[j][2]):
            amount += P[j][3]
        
   
    
   
    if(amount >= max_amount):
        max_amount = amount
    
    amount = 0
    
print(max_amount)

Solution (Python):
import itertools

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

l = (list(itertools.combinations_with_replacement(range(1, M + 1), N)))
r = 0
result = 0
for k in l:
    r = 0
    for x in X:
        if k[x[1] - 1] - k[x[0] - 1] == x[2]:
            r += x[3]
    if result < r:
        result = r
print(result)

Solution (Python):

import itertools

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

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









acomn = list(itertools.combinations_with_replacement(list(range(m)), n))

def tensu(ls, a):
    if a[ls[1]-1] - a[ls[0]-1] == ls[2]:
        return ls[3]
    else:
        return 0

def tensu2(lslist, a):
    return sum(list(map(lambda x: tensu(x, a), lslist)))

print(max(list(map(lambda a: tensu2(lslist, a), acomn))))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)  
n, m, q = list(map(int, input().split()))
data = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    data.append([a-1, b-1, c, d])


def dfs(t):
    if len(t) == n:
        score = 0
        for a, b, c, d in data:
            if t[b] - t[a] == c:
                score += d
        global ans
        ans = max(ans, score)
        return

    if len(t) == 0:
        s = 1
    else:
        s = t[-1]

    for j in range(s, m+1):
        dfs(t+[j])


ans = 0
dfs([])
print(ans)

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

Solution (Python):
import itertools
n,m,q = map(int,input().split())
ab = [0]*q
for i in range(q):
    ab[i] = list(map(int,input().split()))
data = list(range(1,m+1))
ans = 0
a = list(itertools.combinations_with_replacement(data, n))
for s in a:
    score = 0
    for i in range(q):
        if s[ab[i][1]-1] != s[ab[i][0]-1] + ab[i][2]:
            pass
        else:
            score += ab[i][3]
    ans = max(ans,score)
print(ans)

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



 
x=sorted([i for i in range(1,M+1)])
maxdsum=0
 
 
for A in itertools.combinations_with_replacement(x, N):
  dsum=0
  
  for num in range(Q):
    if A[all[num][1]-1]-A[all[num][0]-1]==all[num][2]:
      dsum+=all[num][3]
  if maxdsum<dsum:
    maxdsum=dsum
print(maxdsum)

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

al=list()

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


Sum=0
Summax=0
cnt=0
for i1 in range(1,M+1): 
    for i2 in range(i1,M+1): 
        for i3 in range(i2,M+1): 
            for i4 in range(i3,M+1): 
                for i5 in range(i4,M+1): 
                    for i6 in range(i5,M+1): 
                        for i7 in range(i6,M+1): 
                            for i8 in range(i7,M+1): 
                                for i9 in range(i8,M+1): 
                                    for i10 in range(i9,M+1): 
                                        cnt+=1
                                        Al=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        Sum=0
                                        for i in range(Q): 
                                            if(Al[al[i][1]-1]-Al[al[i][0]-1]==al[i][2]):
                                                Sum+=al[i][3]
                                        if(Summax<Sum):
                                            Summax=Sum

print(Summax)

Solution (Python):
import itertools
n,m,q = map(int,input().split())
ab = [0]*q
for i in range(q):
    ab[i] = list(map(int,input().split()))
data = list(range(1,m+1))
ans = 0
a = list(itertools.combinations_with_replacement(data, n))
for s in a:
    score = 0
    for i in range(q):
        if s[ab[i][1]-1] != s[ab[i][0]-1] + ab[i][2]:
            pass
        else:
            score += ab[i][3]
    ans = max(ans,score)
print(ans)

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

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

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

s = list(range(1, M + 1))
ans = 0


for s in combinations_with_replacement(s, N):
    num = 0
    for q in range(Q):
        a, b, c, d = abcd[q]
        if s[b - 1] - s[a - 1] == c:
            num += d
    ans = max(ans, num)

print(ans)

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

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

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

s = list(range(1, M + 1))
ans = 0


for s in combinations_with_replacement(s, N):
    num = 0
    for q in range(Q):
        a, b, c, d = abcd[q]
        if s[b - 1] - s[a - 1] == c:
            num += d
    ans = max(ans, num)

print(ans)

Solution (Python):
import sys
import itertools

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


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

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

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
from itertools import combinations_with_replacement


n,m,q = map(int,input().split())
num_lst = []
for i in range(q):
    num_lst.append(list(map(int,input().split())))
l = list(range(1,m+1))
A = list(combinations_with_replacement(l,n))

mx = 0
for now_A in A:
    score = 0
    for nums in num_lst:
        a,b,c,d = nums
        if now_A[b-1] - now_A[a-1] == c:
            score += d
    mx = max(mx,score)
print(mx)

Solution (Python):
import itertools

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

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

score = 0
for _t in t:
    _s = 0
    for a, b, c, d in tmp:
        if c == _t[b - 1] - _t[a - 1]:
            _s += d
    score = max(_s, score)
print(score)

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

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

print(ans)

Solution (Python):
N, M, Q = map(int, input().split())
check = list()
for i in range(Q):
  check.append(list(map(int, input().split())))
ans = 0

for i1 in range(1, M+1):
  for i2 in range(i1, M+1):
    for i3 in range(i2, M+1):
      for i4 in range(i3, M+1):
        for i5 in range(i4, M+1):
          for i6 in range(i5, M+1):
            for i7 in range(i6, M+1):
              for i8 in range(i7, M+1):
                for i9 in range(i8, M+1):
                  L = [1, i1, i2, i3, i4, i5, i6, i7, i8, i9]
                  A = list()
                  for i in range(N):
                    A.append(L[i])
                  tmp = 0
                  for i in range(Q):
                    a = check[i][0]
                    b = check[i][1]
                    c = check[i][2]
                    d = check[i][3]
                    if(A[b-1]-A[a-1] == c):
                      tmp += d
                  ans = max(ans, tmp)

print(ans)

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


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

ls=list(con)

ans =[0]*len(ls)
k=0

for i in ls:
  for j in range(Q):
    if(i[ b[j]-1 ] - i[ a[j]-1 ]   == c[j] ):
      ans[k] += d[j]
  k += 1

print(max(ans))

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

def main():
    n,m,q=map(int,input().split())
    
    a = [ [int(x) for x in input().split()] for _ in range(q) ]
    
    ans=0

    def solve(b):
        
        if len(b)-1==n:
            cnt=0
            for v in a:
                if b[v[1]]-b[v[0]]==v[2]:
                    cnt+=v[3]
            nonlocal ans
            ans=max(ans,cnt) 
            return

        for i in range(b[-1],m+1):
            b.append(i)
            solve(b)
            b.pop()

    b=[1]
    solve(b)
    print(ans)



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


if __name__ == "__main__":
    main()