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

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

Solution (Python):
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for x in combinations_with_replacement(range(1, M+1), N):
    point = 0
    for a, b, c, d in abcd:
        if x[b-1] - x[a-1] == c:
            point += d
    if ans < point:
        ans = point
print(ans)

Solution (Python):
import itertools
N,M,Q = map(int, input().split())
qs = []
for i in range(Q):
  qs.append(list(map(int, input().split())))
ans = 0
a = [i for i in range(1,M+1)]
cands = itertools.combinations_with_replacement(a,N)    
for cand in cands:
  _cand = [1]
  _cand.extend(cand)
  tmp = 0
  for q in qs:
    if _cand[q[1]-1]-_cand[q[0]-1] == q[2]:
      tmp += q[3]
  ans = max(ans, tmp)
print(ans)

Solution (Python):
import itertools
n, m, q = map(int, input().split())
list_score = [ list(map(int,input().split(" "))) for i in range(q)]
list_NUM = [i for i in range(1, m+1)]
ans = 0


for l in itertools.combinations_with_replacement(list_NUM, n):
    cnt = 0
    for h in list_score:
        if l[h[1]-1] - l[h[0]-1] == h[2]:
            cnt += h[3]

    ans = max(ans, cnt)

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

Solution (Python):
import itertools
n, m, q = [int(a) for a in input().split()]
a = []
b = []
c = []
d = []
for i in range(q):
    a_,b_,c_,d_ = [int(a) for a in input().split()]
    a.append(a_-1)
    b.append(b_-1)
    c.append(c_)
    d.append(d_)
ans = 0

for A in itertools.combinations_with_replacement(range(m), n):
    ans_ = 0
    for i in range(q):
        if A[b[i]] - A[a[i]] == c[i]:
            ans_ += d[i]
    if ans_ > ans:
        ans = ans_

print(ans)

Solution (Python):
import itertools

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

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

maxs = 0
for xi in x:
    xii = list(xi)
    
    score = 0
    for ai, bi, ci, di in a:
        if xii[bi-1] - xii[ai-1] == ci:
            score += di
    maxs = max(maxs, score)
print(maxs)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right


from functools import reduce
from pprint import pprint

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")


def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

 
n,m,q = myinput()
abcd = mylistinput(q)

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



ls_ans = []
for i in range(len(ls)):
    A = list(ls[i])
    
    score = 0
    for j in range(q):
        a = abcd[j][0] - 1
        b = abcd[j][1] - 1
        c = abcd[j][2]
        d = abcd[j][3]
        if A[b]-A[a]==c:
            score += d
        else:
            pass
    ls_ans.append(score)

ans = max(ls_ans)
print(ans)

Solution (Python):
def dfs(A, max_a):
    if len(A) == n:
        score = 0
        for a, b, c, d in conditions:
            if A[b - 1] - A[a - 1] == c:
                score += d
        return score
    ans = 0
    for i in range(max_a, m + 1):
        ans = max(ans, dfs(A + [i], i))
    return ans


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

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

Solution (Python):
import sys
import itertools


sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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


def calsScore(lst):
    s = 0
    for a, b, c, d in ABCD:
        if (lst[b - 1] - lst[a - 1]) == c:
            s += d
    return s


import copy


def dfs(i, lst, N):
    ans = 0
    lst.append(i)
    N -= 1

    if N == 0:
        return calsScore(lst)
    else:
        for j in range(i, M + 1):
            nlst = copy.copy(lst)
            ans = max(dfs(j, nlst, N), ans)

    return ans


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

Solution (Python):
def ite(N, M):
    import itertools
    return itertools.combinations_with_replacement(range(M),N)
def check(li):
    for i in range(len(li)-1):
        if li[i] > li[i+1]:
            return False
    return True
    
def score(l1, l2):
    s = 0
    for (a, b, c, d) in l2:
      if l1[b-1] - l1[a-1] == c:
            s += d
    return s
        

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

m_s = 0
for l2 in ite(N, M):
    s = score(l2, li)
    if m_s < s:
      m_s = s
            
print(m_s)

Solution (Python):

import math
import itertools

def solve(N:int,M:int,Q:int,ABCD:list) -> int:
    ans = 0
    elements = [i for i in range(1,M+1)] 
    
    for A in itertools.combinations_with_replacement(elements,N):
        total = 0 
        for a,b,c,d in ABCD:
            if(A[b-1] - A [a-1] == c): 
                total+=d
        ans = max(ans,total) 
    return ans



def main():
    N ,M,Q = map(int,input().split())
    ABCD = []
    for _ in range(Q):
        ABCD.append(list(map(int,input().split())))
    
    print(solve(N,M,Q,ABCD))
if __name__ == '__main__':
   main()

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

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

cand = [[1]]
for i in range(N-1):
    tmp = []
    while cand:
        x = cand.pop()
        for i in range(x[-1], M + 1):
            tmp.append(x + [i])
    cand = tmp

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

Solution (Python):
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
a = []
b = []
c = []
d = []
for _ in range(q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

num = list(range(1, m + 1))
max_score = 0
for A in list(combinations_with_replacement(num, n)):
    score = 0
    for i in range(q):
        if A[b[i] - 1] - A[a[i] - 1] == c[i]:
            score += d[i]
    max_score = max(max_score, score)
print(max_score)

Solution (Python):
import itertools

N, M , Q =  [int(i) for i in input().split()]

numbers = []
for i in range(Q):
    numbers.append([int(j) for j in input().split()])


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

answer = 0
for k in  A:
    ans = 0
    for a, b, c, d in numbers:
        if int(k[b-1]) - int(k[a-1]) == c:
            ans += d
    if answer < ans:
        answer = ans
print(answer)

Solution (Python):
from itertools import combinations_with_replacement

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

ans = 0
for v in combinations_with_replacement(range(1, M+1), N):
    A = list(v)
    temp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            temp += d
    ans = max(ans, temp)

print(ans)

Solution (Python):
import itertools

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

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

answer = []
for i in range(len(Alist)):
    score = 0
    for j in range(q):
        b = li[j][1]-1
        a = li[j][0]-1
        if (Alist[i][b] - Alist[i][a] == li[j][2]):
            score += li[j][3]
    answer.append(score)

print(max(answer))

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



def calculate(A):
    total = 0
    for abcdi in abcd:
        a = abcdi[0] - 1
        b = abcdi[1] - 1
        c = abcdi[2]
        d = abcdi[3]
        if A[b] - A[a] == c:
            total += d
    

    return total

maximum = 0
for i in range(1, M + 1):
    if N == 1:
        maximum = max(calculate([i]), maximum)
    for j in range(i, M + 1):
        if N == 2:
            maximum = max(calculate([i, j]), maximum)
        for k in range(j, M + 1):
            if N == 3:
                maximum = max(calculate([i, j, k]), maximum)
            for l in range(k, M + 1):
                if N == 4:
                    maximum = max(calculate([i, j, k, l]), maximum)
                for m in range(l, M + 1):
                    if N == 5:
                        maximum = max(calculate([i, j, k, l, m]), maximum)
                    for n in range(m, M + 1):
                        if N == 6:
                            maximum = max(calculate([i, j, k, l, m, n]), maximum)
                        for o in range(n, M + 1):
                            if N == 7:
                                maximum = max(calculate([i, j, k, l, m, n, o]), maximum)
                            for p in range(o, M + 1):
                                if N == 8:
                                    maximum = max(calculate([i, j, k, l, m, n, o, p]), maximum)
                                for q in range(p, M + 1):
                                    if N == 9:
                                        maximum = max(calculate([i, j, k, l, m, n, o, p, q]), maximum)
                                    for r in range(q, M + 1):
                                        if N == 10:
                                            maximum = max(calculate([i, j, k, l, m, n, o, p, q, r]), maximum)

print(maximum)

Solution (Python):
def main():
    n,m,q = map(int,input().split())
    a = [list(map(int, input().split())) for _ in range(q)]
    
    ans = 0
    from itertools import combinations_with_replacement
    for dp in combinations_with_replacement(range(1,m+1),n):
        res = 0
        for j in range(q):
            if dp[a[j][1]-1] - dp[a[j][0]-1] == a[j][2]:
                res += a[j][3]
        ans = max(res,ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
import itertools
n,m,q = [int(x) for x in 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] = [int(x) for x in input().split()]

ans = 0

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

Solution (Python):
import itertools

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

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

ma = 0
for c in comb:
    score = 0
    for a in A:
        if (c[a[1] - 1] - c[a[0] - 1] == a[2]):
            score += a[3]
    ma = max(ma, score)
print(ma)

Solution (Python):
def main():
    n,m,q = map(int,input().split())
    a = [list(map(int, input().split())) for _ in range(q)]
    
    ans = 0
    from itertools import combinations_with_replacement
    for i in combinations_with_replacement(range(1,m+1),n):
        dp = i
        res = 0
        for j in range(q):
            if dp[a[j][1]-1] - dp[a[j][0]-1] == a[j][2]:
                res += a[j][3]
        ans = max(res,ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
import itertools
N, M, Q = map(int,input().split())
P = [list(map(int,input().split())) for i in range(Q)]
 
comb = list(itertools.combinations_with_replacement(range(1, M+1), N))
 
max_score = 0
for c in comb:
    score = 0
    
    for p in P:
        if c[p[1]-1] - c[p[0]-1] == p[2]:
            score += p[3]
    if score > max_score:
        max_score = score
 
print(max_score)

Solution (Python):
def saiki(N,M,A,MAX,queri,ans):
	
	if len(A) == N:
		ret = 0
		for q in queri:
			if (A[q[1]] - A[q[0]]) == q[2]:
				ret += q[3]
		if ret > ans[0]:
			ans[0] = ret

	else:
		for i in range(1,MAX+1):
			a = [i] + A[:]
			saiki(N,M,a,i,queri,ans)




if __name__ == '__main__':
	N,M,Q = map(int, input().split())

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

	ans = [0]
	saiki(N,M,[],M,queri,ans)

	print(ans[0])

Solution (Python):
import itertools
n,m,q = list(map(int,input().split()))
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(q):
    a[i],b[i],c[i],d[i] = list(map(int,input().split()))
    b[i]-=1
    a[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):

import itertools

N, M, Q = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(Q)]
l = [i for i in range(1, M + 1)]
A = list(itertools.combinations_with_replacement(l, N))

maxans = 0

for j in A:
    ans = 0
    for i in lst:
        if j[i[1]-1] - j[i[0]-1] == i[2]:
            ans = ans + i[3]
    maxans = max(maxans, ans)

print(maxans)

Solution (Python):
n,m,q = map(int,input().split())
a = [[int(i) for i in input().split()] for _ in range(q)]
b = []
for i in range(m):
    b.append(i+1)
import itertools
l = list(itertools.combinations_with_replacement(b,n))
max=0



for ll in l:
    m=0

    for aa in a:

        if ll[aa[1]-1]-ll[aa[0]-1]==aa[2]:
 
            m+=aa[3]
    if max < m:
        max=m
  
print(max)

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

_A=0
for v in itertools.combinations_with_replacement(range(1,m+1), n):
    A=list(v)
    _d=0
    for q in _q:
        a,b,c,d=q
        if A[b-1]-A[a-1]==c:
            _d+=d

    if _d > _A:
        _A = _d

print(_A)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x, 'b') 
def to_oct(x: int) -> str: return format(x, 'o') 
def to_hex(x: int) -> str: return format(x, 'x') 
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]

from itertools import accumulate as acc
from itertools import combinations_with_replacement as combi
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left


def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]
    l = [i for i in range(1,m+1)]
    maxv = 0
    for v in combi(l,n):
        sumv = 0
        for a,b,c,d in query:
            if v[b-1]-v[a-1] == c:
                sumv += d
        maxv = max(maxv, sumv)
    print(maxv)

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
n,m,q = map(int, input().split())
qlist = []

for _ in range(q):
    qlist.append(list(map(int,input().split())))

tmplist = list(range(1,m+1))
point = 0

for A in list(itertools.combinations_with_replacement(tmplist, n)):
    pointtmp = 0
    for tmp in qlist:
        if A[tmp[1]-1] - A[tmp[0]-1] == tmp[2]:
            pointtmp += tmp[3]
    if pointtmp > point:
        point = pointtmp

print(point)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from itertools import combinations_with_replacement
def resolve():
    N, M, Q = lr()
    ans = 0
    rest = []
    tmp = 0
    for i in range(Q):
        rest.append(lr())
    for seq in combinations_with_replacement(range(1, M+1), N):
        tmp = 0
        for i in range(Q):
            a, b, c, d = rest[i]
            if seq[b-1] - seq[a-1] == c:
                tmp += d
        ans = max(tmp, ans)
    print(ans)
resolve()

Solution (Python):
import sys
import itertools

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    ans = 0

    A = [[0] * N for j in range(N)]

    for m in itertools.combinations_with_replacement(range(1, M + 1), r=N):
        m = list(m)
        m.sort()
        tmp = 0
        for a, b, c, d in ABCD:
            if m[b - 1] - m[a - 1] == c:
                tmp += d
        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import itertools

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

finish = [M] * N


def calc(A, tup):
    a,b,c,d = tup
    if A[b-1] - A[a-1] == c:
        return d
    else:
        return 0

RES = 0
    
seq = [i for i in range(1, M+1)]

all_col = itertools.combinations_with_replacement(seq, N)

for col in all_col:
    tmp = col[0]
    flg = True
    for c in col:
        if c < tmp:
            flg = False
            break
    if flg == False:
        continue
    res = 0
    for inp in inputs:
        res += calc(col, inp) 
    RES = max(RES, res)


print(RES)

Solution (Python):
from collections import deque
de = deque()
n,m,q = map(int,input().split())
queries = []
for i in range(q):
    a,b,c,d = map(int,input().split())
    queries.append((a,b,c,d))
de.append([1])
maxscore = 0
while de: 
    li = de.popleft()
    if len(li) < n:
        for i in range(li[-1],m+1):
            new_li = li + [i]
            de.append(new_li)
    else:
        score = 0
        for i in range(q):
            a,b,c,d = queries[i]
            if li[b-1]-li[a-1] == c:
                score += d
        maxscore = max(maxscore,score)
print(maxscore)

Solution (Python):
from itertools import combinations_with_replacement
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):
  p=0
  for i in range(q):
    a,b,c,d=abcd[i]
    if A[b-1]-A[a-1]==c:
      p+=d
  ans=max(ans,p)
print(ans)

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

Solution (Python):
N, M, Q = map(int, input().split())
li = []
for q in range(Q):
    a, b, c, d = map(int, input().split())
    li.append([[a,b], c, d])
ok_list = []
def dfs(num_li, depth):
    if len(num_li) == N:
        ok_list.append(num_li)
        return
    if depth == M:
        return
    dfs(num_li, depth+1)
    while len(num_li) <N:
        num_li += str(depth)
        dfs(num_li, depth+1)
dfs("", 0)
maxpoint = 0
for ok in ok_list:
    point = 0
    for q in range(Q):
        if int(ok[li[q][0][1]-1])- int(ok[li[q][0][0]-1]) == li[q][1]:
            point += li[q][2]
    if point > maxpoint:
        maxpoint = point
print(maxpoint)

Solution (Python):
import itertools
n,m,q = map(int,input().split())
array = []
A = [i for i in range(1,m+1)]
A_list = list(itertools.combinations_with_replacement(A,n))
max_sum = [0]*len(A_list)

for i in range(q):
    a,b,c,d = map(int,input().split())
    for j in range(len(A_list)):
        if A_list[j][b-1]-A_list[j][a-1] == c:
            max_sum[j] += d
print(max(max_sum))

Solution (Python):
import itertools
n,m,q=map(int,input().split())
abcd=[None]*q
for i in range(q):
    abcd[i]=list(map(int,input().split()))
num=0
for l in list(map(list, itertools.combinations_with_replacement(range(1,m+1), n))):
    num_ = 0
    for x in abcd:
        if l[x[1]-1]-l[x[0]-1] == x[2]:
            num_ += x[3]
    num = max(num ,num_)
print(num)

Solution (Python):
from collections import Counter, defaultdict
import sys

sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque



def main():
    list_num, max_num, num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    kari_list = [i + 1 for i in range(max_num)]

    ans = 0
    for key_list in itertools.combinations_with_replacement(kari_list, list_num):
        now_list = sorted(key_list)
        now_cost = 0
        for i in range(num):
            if now_list[data[i][1] - 1] - now_list[data[i][0] - 1] == data[i][2]:
                now_cost += data[i][3]

        ans = max(ans, now_cost)

    print(ans)






if __name__ == '__main__':
    main()

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
l = []
for _ in range(Q):
    l.append(tuple(map(int, input().split())))
    
c = list(itertools.combinations_with_replacement(list(range(1, M+1)), N))

m = 0
for i in c:
    flag = True
    sum = 0
    for j in l:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            sum += j[3]
    m = max(m, sum)
        
print(m)

Solution (Python):
import itertools

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

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

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

List = list(itertools.combinations_with_replacement(L, N))
ans = 0
for l in List:
    temp = 0
    for q in range(Q):
        if (l[B[q]-1]-l[A[q]-1])==C[q]:
            temp = temp + D[q]
    ans = max(ans, temp)
print(ans)

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

if N==10:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    for a9 in range(a8,M+1):
                                        for a10 in range(a9,M+1):
                                            A.append([a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])
                                            
if N==9:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    for a9 in range(a8,M+1):
                                        A.append([a1,a2,a3,a4,a5,a6,a7,a8,a9])
if N==8:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    A.append([a1,a2,a3,a4,a5,a6,a7,a8])
                                        
if N==7:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):

                                A.append([a1,a2,a3,a4,a5,a6,a7])
if N==6:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):

                            A.append([a1,a2,a3,a4,a5,a6])
if N==5:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):

                        A.append([a1,a2,a3,a4,a5])                                
if N==4:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    

                    A.append([a1,a2,a3,a4])
if N==3:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                    

                A.append([a1,a2,a3])
if N==2:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):                    


            A.append([a1,a2])
ans=0

abcd=[]
for i in range(Q):
    abc = list(map(int,(input().split())))
    abcd.append(abc)
dd=[]
for i in range(len(A)):
    d=0
    for j in range(Q):
        if A[i][abcd[j][1]-1]-A[i][abcd[j][0]-1] == abcd[j][2]:
            d+=abcd[j][3]
    dd.append(d)
print(str(max(dd)))

Solution (Python):
from itertools import combinations_with_replacement

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

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

number_pattern = list(combinations_with_replacement(numbers,N))
right_pattern = [[] for i in range(len(number_pattern))]

for i in range(Q):
    a,b,c,d = map(int,input().split())
    for j in range(len(number_pattern)):
        if number_pattern[j][b-1] - number_pattern[j][a-1] == c:
            right_pattern[j].append(d)

ans_list = []

for i in right_pattern:
    if not i:
        ans_list.append(0)
    else:
        ans_list.append(sum(i))

ans_list.sort(reverse=True)

print(ans_list[0])

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

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



tmp = [i for i in range(1, m+1)]

from itertools import combinations_with_replacement

ans = 0
for v in combinations_with_replacement(tmp, n):
   A = list(v)
   score = 0
   for i in range(q):
      a, b, c, d = t[i]
      if A[b] - A[a] == c:
         score += d
   ans = max(ans, score)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement
from collections import defaultdict


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

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

    ans = 0
    for l in ll:
        tmp = 0
        for data in abcd:
            if l[data[1]-1] - l[data[0]-1] == data[2]:
                tmp += data[3]
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()