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 os
import sys
from itertools import permutations, product, combinations_with_replacement

def main():
	n,m,q = map(int, input().split())
	target = [list(map(int,input().split())) for _ in range(q)]
	max_value = 0	
	
	for v in combinations_with_replacement(range(1,m+1),n):
		result = 0
		for i in target:
			if v[i[1]-1] - v[i[0]-1] == i[2]:
				result += i[3]
			else:
				pass
		max_value =  max(result,max_value)

	print(max_value)

if __name__ == "__main__":
        main()

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


lst = []
nn,mm,qq = (int(x) for x in input().split())
for i in range(1,mm+1):
    for j in range(i,mm+1):
        for k in range(j,mm+1):
            for l in range(k,mm+1):
                for m in range(l,mm+1):
                    for n in range(m,mm+1):
                        for o in range(n,mm+1):
                            for p in range(o,mm+1):
                                for q in range(p,mm+1):
                                    for r in range(q,mm+1):
                                        lst.append([i,j,k,l,m,n,o,p,q,r])
qlst = []

for i in range(qq):
    qlst.append([int(x) for x in input().split()])
score = 0
for combo in lst:
    cur_score = 0
    for a,b,c,d in qlst:
        if combo[b-1] - combo[a-1] == c:
            cur_score += d
    score = max(score, cur_score)
print(score)

Solution (Python):
import itertools
n, m, q = [int(i) for i in input().split()]
sm = 0
abcd = [[int(j) for j in input().split()] for i in range(q)]
a = [i[0] for i in abcd]
b = [i[1] for i in abcd]
c = [i[2] for i in abcd]
d = [i[3] for i in abcd]

it = itertools.combinations_with_replacement(range(1, m+1), n)
ans = 0
for i in it:
    sum = 0
    for j in range(len(a)):
        if(i[b[j]-1] - i[a[j]-1] == c[j]):
            sum += d[j]
    ans = max(sum,ans)

print(ans)

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

A_N = [[i] for i in range(1, M+1)]
for i in range(N-1):
  a = []
  for j in range(len(A_N)):
    last = A_N[j][-1]
    for k in range(last, M+1):
      a.append(A_N[j] + [k])
  A_N = a

high_score = 0
for i in range(len(A_N)):
  A = A_N[i]
  score = 0
  for j in range(len(l)):
    aj, bj, cj, dj = l[j]
    if A[bj-1]-A[aj-1] == cj:
      score += dj
  if high_score < score:
    high_score = score 

print(high_score)

Solution (Python):
from sys import stdin


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


def main():
    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())
        a[i] -= 1
        b[i] -= 1

    def dfs(seq, N, M):
        if len(seq) == N:
            score = 0
            for i in range(Q):
                if seq[b[i]] - seq[a[i]] == c[i]:
                    score += d[i]
            return score

        score = 0
        start = seq[-1] if seq else 1
        for n in range(start, M + 1):
            score = max(score, dfs(seq.copy() + [n], N, M))

        return score

    print(dfs([], N, M))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m, q = map(int, input().split())
pairs = [0] * q
for i in range(q):
    pairs[i] = list(map(int, input().split()))

from collections import deque

def dfs(min_val, length):
    if length == 0:
        return list([deque()])
    if min_val == m:
        return list([deque([m]) * length])
    ans = list()
    for i in range(min_val, m+1):
        remain = dfs(i, length-1)
        for j in remain:
            j.appendleft(i)
        ans.extend(remain)
    return ans

ans = 0
for i in dfs(1, n):
    tmp = 0
    for a, b, c, d in pairs:
        if (i[b-1] - i[a-1]) == c:
            tmp += d
    ans = max(ans, tmp)

print(ans)

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m, q = map(int, input().split())
pairs = [0] * q
for i in range(q):
    pairs[i] = list(map(int, input().split()))

from collections import deque

def dfs(min_val, length):
    if length == 0:
        return list([deque()])
    if min_val == m:
        return list([deque([m]) * length])
    ans = list()
    for i in range(min_val, m+1):
        remain = dfs(i, length-1)
        for j in remain:
            j.appendleft(i)
        ans.extend(remain)
    return ans

ans = 0
for i in dfs(1, n):
    tmp = 0
    for a, b, c, d in pairs:
        if (i[b-1] - i[a-1]) == c:
            tmp += d
    ans = max(ans, tmp)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline
from itertools import combinations_with_replacement
def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N,M,Q = MI()
    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] = MI()
    dsum = 0
    max = 0

    l =list(combinations_with_replacement(range(1,M+1), N))
    for j in l:
        j = list(j)
        dsum = 0
        for k in range(Q):
            if j[b[k]-1]-j[a[k]-1] == c[k]:
                dsum += d[k]
        if max < dsum:
            max = dsum 
    print(max)




if __name__ == "__main__":
	main()

Solution (Python):
from itertools import *
N,M,Q=map(int,input().split())
a=[0 for i in range(Q)]
b=[0 for i in range(Q)]
c=[0 for i in range(Q)]
d=[0 for i in range(Q)]
for i in range(Q):
  a[i],b[i],c[i],d[i] = map(int,input().split())
  b[i] -= 1
  a[i] -= 1
ans = 0
for pat in combinations_with_replacement(range(M),N):
  count = 0
  for i in range(Q):
    if pat[b[i]]-pat[a[i]] == c[i]:
      count += d[i]
  ans=max(ans, count)
print(ans)

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

Solution (Python):
from itertools import combinations_with_replacement

def main():
    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(M), N):
        temp = 0
        for abcd in ABCD:
            if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:
                temp += abcd[3]
        if temp > ans:
            ans = temp
    print(ans)

if __name__ == '__main__':
    main()

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

Solution (Python):
from itertools import combinations_with_replacement
from math import factorial
n,m,q=map(int,input().split())
lst=[list(map(int,input().split())) for i in range(q)]
total=[]
for a in combinations_with_replacement(range(1,m+1),n):
  ans=0
  for j in range(q):
    if int(a[lst[j][1]-1])-int(a[lst[j][0]-1])==lst[j][2]:
      ans+=lst[j][3]
  total.append(ans)

print(max(total))

Solution (Python):
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(M), N):
    temp = 0
    for abcd in ABCD:
        if A[abcd[1]-1]+1 == A[abcd[0]-1]+1 + abcd[2]:
            temp += abcd[3]
    if temp > ans:
        ans = temp
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
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())


sequences = []

def dfs(seq):
    if len(seq) == N+1:
        sequences.append(seq)
        return
    tail_num = seq[-1]
    for nxt_d in range(tail_num,M+1):
        dfs(seq+[nxt_d])
dfs([1])


ans = 0
for seq in sequences:
    score = 0
    for i in range(Q):
        if int(seq[b[i]]) - int(seq[a[i]]) == c[i]:
            score += d[i]
    ans = max(ans, score)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
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())


sequences = []

def dfs(seq):
    if len(seq) == N+1:
        sequences.append(seq)
        return
    tail_num = seq[-1]
    for nxt_d in range(tail_num,M+1):
        dfs(seq+[nxt_d])
dfs([1])


ans = 0
for seq in sequences:
    score = 0
    for i in range(Q):
        if int(seq[b[i]]) - int(seq[a[i]]) == c[i]:
            score += d[i]
    ans = max(ans, score)
print(ans)

Solution (Python):
def main():
    from itertools import combinations_with_replacement
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]
    ans = 0
    for C in combinations_with_replacement(range(1, M+1), N):
        A = sorted(C)
        cur = 0
        for a, b, c, d in T:
            if A[b-1] - A[a-1] == c:
                cur += d
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
N, M, Q = map(int, input().split(" "))
abcd = []
for i in range(Q):
    abcd.append(list(map(int, input().split(" "))))
q = deque([[i] for i in range(1, M + 1)])
A = []
while q:
    tmp = q.pop()
    if len(tmp) == N:
        A.append(tmp)
    else:
        for i in range(tmp[-1], M + 1):
            tmp_copy = tmp.copy()
            tmp_copy.append(i)
            q.append(tmp_copy)
ans = 0            
for a in A:
    score = 0
    for i in abcd:
        if a[i[1] - 1] == a[i[0] - 1] + i[2]:
            score += i[3]
    ans = max(ans, score)
print(ans)

Solution (Python):
n,m,q = map(int,input().split())
cd = 0
score_list = [list(map(int,input().split())) for i in range(q)]

def sum_d(l):
    s = 0
    
    for a,b,c,d in score_list:
        if (l[b-1] - l[a-1])==c:
            s += d
    return s

def f(l,ans):
    if(len(l)==n):
        return sum_d(l)

    if(len(l)==0):
        num=1
    else:
        num=l[-1]

    while(num <= m):
        ret = f(l+[num],ans)
        if(ans < ret):
            ans = ret
        num += 1

    return ans

ans = f([],ans=0)
print(ans)

Solution (Python):
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 x in combinations_with_replacement(range(M), N):
    temp = 0
    for i in range(Q):
        if x[ABCD[i][1]-1]+1 == x[ABCD[i][0]-1]+1 + ABCD[i][2]:
            temp += ABCD[i][3]
    if temp > ans:
        ans = temp
print(ans)

Solution (Python):
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 x in combinations_with_replacement(range(M), N):
    temp = 0
    for i in range(Q):
        if x[ABCD[i][1]-1]+1 == x[ABCD[i][0]-1]+1 + ABCD[i][2]:
            temp += ABCD[i][3]
    if temp > ans:
        ans = temp
print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
n,m,q=map(int,input().split())
abcd=[list(map(int,input().split())) for _ in range(q)]
ans=0
s,seq=[],[]
def dfs(s):
    if len(s)==n:
        seq.append(s)
        return
    for i in range(1,m+1):
        if s:
            if s[-1]<=i:
                dfs(s+[i])
        else:
            dfs(s+[i])
dfs(s)

for s in seq:
    cand=0
    for a,b,c,d in abcd:
        a-=1
        b-=1
        if s[b]-s[a]==c:
            cand+=d
    ans=max(ans,cand)
print(ans)

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

Solution (Python):
def dfs(nums, length, min_lim):

    ans = 0
    if length == n:
    
        score_ret = 0
        for a, b, c, d in req:
            
            if nums[b] - nums[a] == c:  
                score_ret += d
        return score_ret  
    else:
        
        for nu in range(min_lim, m + 1):
        
            new_nums = nums + [nu]  
            
            score = dfs(new_nums, length + 1, nu)
            ans = max(ans, score) 

    
    return ans


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


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




ans = dfs([-1], 0, 1)
print(ans)

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

mx = 0
a = [1]*10

for s1 in range(1, M+1):
    a[0] = s1
    for s2 in range(s1, M+1):
        a[1] = s2
        for s3 in range(s2, M+1):
            a[2] = s3
            for s4 in range(s3, M+1):
                a[3] = s4
                for s5 in range(s4, M+1):
                    a[4] = s5
                    for s6 in range(s5, M+1):
                        a[5] = s6
                        for s7 in range(s6, M+1):
                            a[6] = s7
                            for s8 in range(s7, M+1):
                                a[7] = s8
                                for s9 in range(s8, M+1):
                                    a[8] = s9
                                    for s10 in range(s9, M+1):
                                        a[9] = s10
                                        
                                        cnt = 0
                                        for i in range(Q):
                                            si = s[i]
                                            if a[si[1]-1] - a[si[0]-1] == si[2]:
                                                cnt += si[3]
                                            mx = max(mx, cnt)
                                            
print(mx)

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

L = []
def f(l):
    if len(l) == n + 1:
        L.append(l[1:])
        return
    for i in range(l[-1], m + 1):
        f(l + [i])
f([1])

print(max(sum(d for a, b, c, d in T if l[b] - l[a] == c) for l in L))

Solution (Python):
import numpy as np

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

A = np.sort(np.random.randint(1, m+1, n))


def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 20000
temperature = 100
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,3)
    A[idx] = np.random.randint(1, m+1,3)
    A = np.sort(A)
    energy = calc_energy(A)

    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature  = temperature*0.999

print(np.int(calc_energy(A)))

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(10**9)

ans = 0

def calc(LL):
    global n,m,q,D,ans
    ret = 0
    for dd in D:
        a,b,c,d = dd[0],dd[1],dd[2],dd[3]
        if LL[b-1]-LL[a-1] == c:
            ret += d
    ans = max(ans,ret)

def func(LL):
    global n,m
    if len(LL) >= n:
        return calc(LL)
    for i in range(LL[-1],m+1):
        func(LL+[i])

n,m,q = map(int,input().split())
D = [list(map(int,input().split())) for _ in range(q)]
for i in range(1,m+1):
    func([i])
print(ans)

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

listA = list(range(1,N+1))
ANS = 0

import itertools
for i in itertools.combinations_with_replacement(range(1,M+1), N):
    f = 0
    for j in range(len(i)-1):
        if not i[j] <= i[j+1]:
            f=1
    if f == 0:
        
        ans = 0
        for q in range(Q):
            if i[B[q]-1] - i[A[q]-1] == C[q]:
                ans += D[q]
        ANS = max([ANS, ans])
print(ANS)

Solution (Python):
import itertools
import copy
  
N,M,Q = map(int, input().split())
a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q

now = 0
ans = 0

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

  global ans
  if (len(A) == N):
    now = 0
    for i in range(Q):
      if A[b[i]-1] - A[a[i]-1] == c[i]:
        now += d[i]
    ans = max(now,ans)
    return
  if len(A) == 0:
    A=[1]
  else:
    A.append(A[-1])

  while A[-1] <= M:

    dfs(copy.deepcopy(A))
    A[-1] +=1
  
  
dfs(list([1]))  
print(ans)

Solution (Python):




import sys
from io import StringIO
import unittest
import itertools

sys.setrecursionlimit(500*500)

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,m,q=map(int, readline().rstrip().split())

    aa=[0]*q
    bb=[0]*q
    cc=[0]*q
    dd=[0]*q

    for i in range(q):
        aa[i],bb[i],cc[i],dd[i]=map(int, readline().rstrip().split())

    max_score=0

    for a in itertools.combinations_with_replacement(list(range(1,m+1)),n):
        score=0
        for i in range(q):
            if (a[bb[i]-1]-a[aa[i]-1])==cc[i]:
                score+=dd[i]
        max_score=max(max_score,score)

    print(max_score)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
def main():
    n, m, q = map(int, input().split())
    abcd = [list(map(int, input().split())) for _ in [0]*q]

    que = [[1]]
    for _ in range(n):
        que2 = []
        for qq in que:
            i = qq[-1]
            for j in range(i, m+1):
                que2.append(qq+[j])
        que = que2

    ans = 0
    for q in que:
        temp = 0
        for a, b, c, d in abcd:
            if q[b]-q[a] == c:
                temp += d
        ans = max(ans, temp)
    print(ans)


main()

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, M, Q = LI()
ABCD = LRI(Q)

def calc(A):
    ret = 0

    for a, b, c, d in ABCD:
        if A[b] - A[a] == c:
            ret += d
    
    return ret


def bfs(A):
    ans = 0
    stack = collections.deque(A)

    while stack:
        s = stack.popleft()
        
        if len(s) < N+1:
            back = s[-1]
            while back <= M:
                stack.append(s + [back])
                back += 1
        else:
            ans = max(ans, calc(s))

    return ans

print(bfs([[1]]))

Solution (Python):
import itertools
import copy
  
N,M,Q = map(int, input().split())
a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q

now = 0
ans = 0

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

  global ans
  if (len(A) == N):
    now = 0
    for i in range(Q):
      if A[b[i]-1] - A[a[i]-1] == c[i]:
        now += d[i]
    ans = max(now,ans)
    return
  if len(A) == 0:
    A=[1]
  else:
    A.append(A[-1])

  while A[-1] <= M:

    dfs(copy.deepcopy(A))
    A[-1] +=1
  
  
dfs(list([1]))  
print(ans)

Solution (Python):
import itertools

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




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

ans = 0
for suuretu in suuretus:
    tmp = 0

    if len(suuretu) == N:

        for number in numbers:
            if suuretu[number[1]-1] - suuretu[number[0]-1] == number[2]:
                tmp += number[3]
        
    ans = max(ans, tmp)


print(ans)

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, M, Q = LI()
ABCD = LRI(Q)

def calc(A):
    ret = 0

    for a, b, c, d in ABCD:
        if A[b] - A[a] == c:
            ret += d
    
    return ret


def dfs(A):
    ans = 0
    stack = A

    while stack:
        s = stack.pop()
            
        if len(s) < N+1:
            back = s[-1]
            while back <= M:
                stack.append(s + [back])
                back += 1
        else:
            ans = max(ans, calc(s))

    return ans

print(dfs([[1]]))

Solution (Python):

import numpy as np
from collections import deque
import itertools

n, m, q = list(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] = list(map(int, input().split()))
    a[i] -= 1
    b[i] -= 1

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

Solution (Python):
n,m,q=map(int,input().split())
l=[]
for i in range(q):
    l.append([int(x) for x in input().split()])
for i in range(q):
    l[i][0]-=1
    l[i][1]-=1
result=0
for i0 in range(1,m+1):
    for i1 in range(i0,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):
                                        a=[i0,i1,i2,i3,i4,i5,i6,i7,i8,i9]
                                        score=0
                                        for i in l:
                                            if a[i[1]]-a[i[0]]==i[2]:
                                                score+=i[3]
                                        result=max(score,result)
print(result)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
abcd = list()
for i in range(Q):
    a,b,c,d = map(int, input().split())
    abcd.append((a,b,c,d))
iters = itertools.combinations_with_replacement(range(1, M+1), N)
ans = 0
for com in iters:
    sum = 0
    for a,b,c,d in abcd:
        if(com[b-1]-com[a-1] == c):
            sum += d
    if(sum > ans):
        ans = sum
print(ans)

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

from itertools import combinations_with_replacement

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

lst = list(combinations_with_replacement(range(1, M + 1), N))

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

Solution (Python):



def main():
    from collections import deque
    import sys
    sys.setrecursionlimit(10 ** 7)

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

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

    A = deque([1])

    def rec():
        if len(A) == N:
            return sum(d for a, b, c, d in abcd if A[b] == A[a] + c)

        ret = 0
        for x in range(A[-1], M + 1):
            A.append(x)
            ret = max(ret, rec())
            A.pop()
        return ret

    print(rec())


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

ans = 0
def dfs(now,last,use):
    
    if now == n:
        res = 0
        for a,b,c,d in abcd:
            if use[b-1] - use[a-1] == c:
                res += d
        return res
    res = 0
    for to in range(last,m+1):
        res = max(res, dfs(now+1, to, use + [to]))
    return res

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

Solution (Python):
N,M,Q=map(int,input().split())
a=[]
b=[]
c=[]
d=[]
for i in range(Q):
  ta,tb,tc,td=map(int,input().split())
  a+=[ta]
  b+=[tb]
  c+=[tc]
  d+=[td]
maxs=0
A=[0 for i in range(10)]
for i in range(M):
  for j in range(i,M):
    for k in range(j,M):
      for l in range(k,M):
        for m in range(l,M):
          for n in range(m,M):
            for o in range(n,M):
              for p in range(o,M):
                for q in range(p,M):
                  for r in range(q,M):
                    A[0]=i
                    A[1]=j
                    A[2]=k
                    A[3]=l
                    A[4]=m
                    A[5]=n
                    A[6]=o
                    A[7]=p
                    A[8]=q
                    A[9]=r
                    su=0
                    for z in range(Q):
                      if A[b[z]-1]-A[a[z]-1]==c[z]:
                        su+=d[z]
                    if su>maxs:
                      maxs=su
print(maxs)

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

Solution (Python):
def combinationWithRepetitionListRecursive(data, r):
  if r <= 0:
    return []

  result = []
  _combinationWithRepetitionListRecursive(data, r, 0, [], result)
  return result

def _combinationWithRepetitionListRecursive(data, r, start, progress, result):
  if r == 0:
    result.append(progress)
    return

  for i in range(start, len(data)):
    _combinationWithRepetitionListRecursive(data, r - 1, i, progress + [data[i]], result)


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

L = [[0] * 4 for _ in range(Q)]

for i in range(Q):
    a, b, c, d = map(int, input().split())
    L[i][0] = a
    L[i][1] = b
    L[i][2] = c
    L[i][3] = d



allnum = []

for i in range(1, M+1):
    allnum.append(i)

allseq = combinationWithRepetitionListRecursive(allnum, N)


MAX = 0

for j in range(len(allseq)):
    point = 0
    for k in range(Q):
        if allseq[j][L[k][1]-1] - allseq[j][L[k][0]-1] == L[k][2]:
            point += L[k][3]
    MAX = max(MAX, point)

print(MAX)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

ans = 0

import itertools
for seq in itertools.combinations_with_replacement(range(1,m+1),n):
    tmp = 0
    for a,b,c,d in abcd:
        if seq[b-1] - seq[a-1] == c:
            tmp += d

    ans = max(tmp,ans)

print(ans)

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

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

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