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

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

a = []
def gen_a(n,ai,cur):
    if n==0:
        for k in range(1,M+1):
            if k==10:
                k = "a"
                gen_a(1,10,str(k))
            else:
                gen_a(1,k,str(k))
    elif n==N:
        ar = list(cur)
        if ar[-1] == "a": ar[-1] = "10"
        a.append(ar)
    else:
        for k in range(ai,M+1):
            if k == 10:
                k = "a"
                gen_a(n+1,10,cur+str(k))
            else: 
                gen_a(n+1,k,cur+str(k))

gen_a(0,1,0)

ans = 0
for a_item in a:
    cnt = 0
    for d_item in data:
        nb = int(a_item[d_item[1]-1]) if a_item[d_item[1]-1] != "a" else 10
        na = int(a_item[d_item[0]-1]) if a_item[d_item[0]-1] != "a" else 10
        if nb - na == d_item[2]:
            cnt += d_item[3]
    if ans < cnt:
        ans = cnt

print(ans)

Solution (Python):


def iter_A(N,M_,M):
    if N==1:
        yield from ([a] for a in range(M_,M+1))
    else:
        for a in range(M_,M+1):
            yield from ([a] + b for b in iter_A(N-1,a,M))

def solve():
    N, M, Q = map(int, input().split())
    E = {}
    X = []
    for q in range(Q):
        a,b,c,d = map(int, input().split())
        X.append((a-1,b-1,c,d))

    
    return str(max(sum(d for a,b,c,d in X if A[b]-A[a]==c) for A in iter_A(N,1,M)))


if __name__ == '__main__':
    print(solve())

Solution (Python):
s = list(map(int, input().split()))
n = s[0]
m = s[1]
q = s[2]

abcd = []

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

maximum = 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):
                                        iarray = [i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
                                        goukei = 0
                                        for j in range(q):
                                            if iarray[abcd[j][1] - 1] - iarray[abcd[j][0] - 1] == abcd[j][2]:
                                                goukei = goukei + abcd[j][3]

                                        if goukei > maximum:
                                            maximum = goukei

print(maximum)

Solution (Python):
from itertools import *

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

aaa=[]
for i in range(m):
    aaa.append(i+1)

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

a=list(combinations_with_replacement(aaa,n))
ans=0
for i in a:
    aa=0
    for j in l:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            aa+=j[3]
    if aa>ans:
        ans=aa
print(ans)

Solution (Python):


def solve():
    import copy

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

    cands = []
    def rec(start, lst):
        if len(lst) == N:
            return lst

        for i in range(start, M+1):
            tmp_lst = copy.deepcopy(lst)
            tmp_lst.append(i)
            ret = rec(i, tmp_lst)
            if ret is not None:
                cands.append(ret)

    rec(1, [])

    ans = 0
    for cand in cands:
        total = 0
        for abcd in query:
            a,b,c,d = abcd
            if cand[b-1]-cand[a-1] == c:
                total += d
        ans = max(ans, total)

    print(ans)




if __name__ == "__main__":
    solve()

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
query = [list(map(int, input().split())) for _ in range(Q)]
l = list(range(1, M+1))
ans = 0
for v in itertools.combinations_with_replacement(l, N):
    A = sorted(list(v))
    cost = 0
    for i in range(Q):
        if A[query[i][1]-1] - A[query[i][0]-1] == query[i][2]:
            cost += query[i][3]
    ans = max(ans, cost)
print(ans)

Solution (Python):


def solve():
    import copy

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

    cands = []
    def rec(start, lst):
        if len(lst) == N:
            return lst

        for i in range(start, M+1):
            tmp_lst = copy.deepcopy(lst)
            tmp_lst.append(i)
            ret = rec(i, tmp_lst)
            if ret is not None:
                cands.append(ret)

    rec(1, [])

    ans = 0
    for cand in cands:
        total = 0
        for abcd in query:
            a,b,c,d = abcd
            if cand[b-1]-cand[a-1] == c:
                total += d
        ans = max(ans, total)

    print(ans)




if __name__ == "__main__":
    solve()

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
query = [list(map(int, input().split())) for _ in range(Q)]
l = list(range(1, M+1))
ans = 0
for v in itertools.combinations_with_replacement(l, N):
    A = sorted(list(v))
    cost = 0
    for i in range(Q):
        if A[query[i][1]-1] - A[query[i][0]-1] == query[i][2]:
            cost += query[i][3]
    ans = max(ans, cost)
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)]

ANS = []

if n == 2:
    for a in range(1,m+1):
        for b in range(a,m+1):
            ANS.append([a,b])
            
if n == 3:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                ANS.append([a,b,c])
                
if n == 4:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    ANS.append([a,b,c,d])
                    
if n == 5:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    for e in range(d,m+1):
                        ANS.append([a,b,c,d,e])
                        
if n == 6:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    for e in range(d,m+1):
                        for f in range(e,m+1):
                            ANS.append([a,b,c,d,e,f])
                        
if n == 7:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    for e in range(d,m+1):
                        for f in range(e,m+1):
                            for g in range(f,m+1):
                                ANS.append([a,b,c,d,e,f,g])
                                
if n == 8:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    for e in range(d,m+1):
                        for f in range(e,m+1):
                            for g in range(f,m+1):
                                for h in range(g,m+1):
                                    ANS.append([a,b,c,d,e,f,g,h])
                                    
if n == 9:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    for e in range(d,m+1):
                        for f in range(e,m+1):
                            for g in range(f,m+1):
                                for h in range(g,m+1):
                                    for i in range(h,m+1):
                                        ANS.append([a,b,c,d,e,f,g,h,i])
                                        
if n == 10:
    for a in range(1,m+1):
        for b in range(a,m+1):
            for c in range(b,m+1):
                for d in range(c,m+1):
                    for e in range(d,m+1):
                        for f in range(e,m+1):
                            for g in range(f,m+1):
                                for h in range(g,m+1):
                                    for i in range(h,m+1):
                                        for j in range(i,m+1):
                                            ANS.append([a,b,c,d,e,f,g,h,i,j])
                                            
ans = 0

for i in ANS:
    point = 0
    for j in ABCD:
        a = j[0] - 1
        b = j[1] - 1
        c = j[2]
        d = j[3]
        
        if i[b] - i[a] == c:
            point += d
            
    ans = max(ans,point)
    
print(ans)

Solution (Python):
def read_int():
    return int(input())

def read_int_map():
    return map(int,input().split(' '))

def read_int_list():
    return list(map(int,input().split(' ')))

def read_s_list_loop(n):
    return [input() for _ in range(n)]

N,M,Q = read_int_map()
ans_map = {}

abcd_list = []
for i in range(Q):
    abcd_list.append(read_int_list())

abcd_list.sort(key=lambda x:(x[0],x[1]))

for abcd in abcd_list:
    a,b,c,d = map(int,abcd)

import copy
import sys
sys.setrecursionlimit(36288000)
all_list = []
def make_list(current=1,n=10,m=10,current_list=[],cnt=0):
    cnt += 1
    for i in range(current,m):
        tmp = copy.deepcopy(current_list)
        tmp.append(i)
        if cnt == n:
            global all_list
            all_list.append(tmp)
        else:
            make_list(i,n,m,tmp,cnt)
make_list(current=1,n=N,m=M+1,current_list=[],cnt=0)
ans = 0
for An in all_list:
    tmp = 0
    for abcd in abcd_list:
        a,b,c,d = map(int,abcd)
        if An[b-1] - An[a-1] == c:
            tmp += d
    ans = max(tmp,ans)
print(ans)

Solution (Python):

"""
@author: H_Hoshigi
"""
def main():
    N, M, Q = map(int, input().split())
    r_list = []
    for i in range(Q):
        r_list.append(list(map(int, input().split())))
    max_score = 0
    a1 = 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_list = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]
                                        score = 0
                                        for r in r_list:
                                            if a_list[r[1]-1] - a_list[r[0]-1] == r[2]:
                                                score += r[3]
                                        max_score = max(score, max_score)
    print(max_score)

if __name__ == "__main__":
    main()

Solution (Python):
import itertools
N,M,Q = map(int, input().split())
l = [[int(i) for i in input().split()] for j in range(Q)]
A = []
nums = list(range(1,M+1))
for B in itertools.combinations_with_replacement(nums, N):
	A.append(B)

A_s = []*len(A)
m = 0
for i in range(0,len(A)):
  for j in range(0,Q):
    if(A[i][l[j][1]-1] - A[i][l[j][0]-1] == l[j][2]):
      m = m + l[j][3]
  A_s.append(m)
  m = 0
ans = max(A_s)
print(ans)

Solution (Python):
import itertools
N,M,Q= list(map(int, input().split(' ')))
L=[]
for i in range(Q):
    L.append(list(map(int, input().split(' '))))
A=[i + 1 for i in range(M)]
As=list(itertools.combinations_with_replacement(A, N))
S=[]
s=0
for I in As:
    for J in L:
        if I[J[1]-1]-I[J[0]-1]==J[2]:
                s=s+J[3]
    S.append(s)
    s=0
print(max(S))

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

def calc(L):
    s = 0
    for a, b, c, d in X:
        if L[b] - L[a] == c: s += d
    return s

def rec(L):
    if len(L) == N + 1:
        return calc(L)
    ma = 0
    for i in range(L[-1], M+1):
        ma = max(ma, rec(L + [i]))
    return ma

print(rec([1]))

Solution (Python):
def my_f(l, lq, q):
  p=0
  for i in range(q):
    if l[lq[i][1]-1]-l[lq[i][0]-1]==lq[i][2]:
      p+=lq[i][3]
  return p
  
n,m,q = (int(x) for x in input().split())

pmax = 0

lq=[]
for i in range(q):
  lq.append(list(map(int, input().split())))
  
l=[0]*10
while l[0] < m:
  l[0]+=1
  l[1]=l[0]-1
  while l[1] < m:
    l[1]+=1
    l[2]=l[1]-1
    while l[2] < m:
      l[2]+=1
      l[3]=l[2]-1
      while l[3] < m:
        l[3]+=1
        l[4]=l[3]-1
        while l[4] < m:
          l[4]+=1
          l[5]=l[4]-1
          while l[5] < m:
            l[5]+=1
            l[6]=l[5]-1
            while l[6] < m:
              l[6]+=1
              l[7]=l[6]-1
              while l[7] < m:
                l[7]+=1
                l[8]=l[7]-1
                while l[8] < m:
                  l[8]+=1
                  l[9]=l[8]-1
                  while l[9] < m:
                    l[9]+=1
                    ptmp = my_f(l, lq, q)
                    if ptmp > pmax:
                      pmax = ptmp

print(pmax)

Solution (Python):
N, M, Q = map(int, input().split())
ans = 0
zenbu = []
def tukuru(cur, last):
    if len(cur) == N:
        zenbu.append(cur)
        return
    for j in range(last, M+1):
        tukuru(cur+[j], j)
tukuru([], 1)
D = [0]*len(zenbu)
for i in range(Q):
    a, b, c, d = map(int, input().split())
    for i, p in enumerate(zenbu):
        if p[b-1] - p[a-1] == c:
            D[i] += d
print(max(D))

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

Solution (Python):
mod = 10**9+7
import sys
from collections import Counter, defaultdict, deque
from itertools import product, permutations, combinations, combinations_with_replacement
from itertools import accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from math import ceil,floor
from copy import deepcopy
from heapq import heappop,heappush,heapify
import heapq







sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1


p2D = lambda x: print(*x, sep="\n")

def II(): return int(sys.stdin.readline())

def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())




def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI(): return list(map(lambda x:int(x), sys.stdin.readline().split()))
def HI(): return list(map(lambda x:int(x)*(-1), sys.stdin.readline().split()))



n,m,q = MI()
A =[]
B = []
C = []
D = []



for _ in range(q):
	a,b,c,d = MI()
	A.append(a)
	B.append(b)
	C.append(c)
	D.append(d)


ll = list(combinations_with_replacement(range(1,m+1), n))
	
ans = 0
for l in ll:
	tmp = 0
	for i in range(q):
		if l[(B[i]-1)]-l[(A[i]-1)]==C[i]:
			tmp+=D[i]

	ans = max(tmp,ans)
print(ans)

Solution (Python):

"""
@author: H_Hoshigi
"""
def main():
    N, M, Q = map(int, input().split())
    r_list = []
    for i in range(Q):
        r_list.append(list(map(int, input().split())))
    max_score = 0
    a1 = 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_list = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]
                                        score = 0
                                        for r in r_list:
                                            if a_list[r[1]-1] - a_list[r[0]-1] == r[2]:
                                                score += r[3]
                                        max_score = max(score, max_score)
    print(max_score)

if __name__ == "__main__":
    main()

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

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

l = [i for i in range(1, m+1)]
ans = 0
for Alis in combinations_with_replacement(l, n):
    cnt = 0
    for i in range(q):
        if Alis[abcd[i][1] - 1] - Alis[abcd[i][0] - 1] == abcd[i][2]:
            cnt += abcd[i][3]
    ans = max(ans, cnt)

print(ans)

Solution (Python):

import math


def check_Q(current_list,Qset):

    sum_d = 0
    for i in range(len(Qset)):
        current_Qset = Qset[i]
        
        
        
        if (current_list[current_Qset[1]-1]-current_list[current_Qset[0]-1])==current_Qset[2]:
            sum_d = sum_d+current_Qset[3]

    return sum_d

def check_N(preget_list,zan_pre,M,nokori_N,Qset):

    if nokori_N==1:
        sum_list = []
        for i in range(zan_pre,M+1):
            current_list = preget_list+[i]
            sum_list.append(check_Q(current_list,Qset))

        return sum_list
    else:
        sum_list = []
        for i in range(zan_pre,M+1):
            current_list = preget_list+[i]
            sum_list = sum_list + check_N(current_list,i,M,nokori_N-1,Qset)

        return sum_list

def main():

    
    first_input = list(map(lambda x:int(x), input().split(" ")))
    

    
    
    

    
    

    N = first_input[0]
    M = first_input[1]
    Q = first_input[2]

    Qset = []
    for i in range(Q):
        secondkara_input = list(map(lambda x:int(x), input().split(" ")))
        Qset.append(secondkara_input)

    
    print(max(check_N([],1,M,N,Qset)))

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n,m,q = map(int,input().split())
    Q = []
    for _ in range(q):
        a,b,c,d = map(int,input().split())
        Q.append([a,b,c,d])
    ans = 0
    for a in range(1,11):
        for b in range(a,11):
            for c in range(b,11):
                for d in range(c,11):
                    for e in range(d,11):
                        for f in range(e,11):
                            for g in range(f,11):
                                for h in range(g,11):
                                    for i in range(h,11):
                                        for j in range(i,11):
                                            l = [a,b,c,d,e,f,g,h,i,j]
                                            l = l[0:n]
                                            M = 0
                                            if max(l)>m:
                                                continue
                                            for q in Q:
                                                A,B,C,D = q[0],q[1],q[2],q[3]
                                                if l[B-1]-l[A-1] == C:
                                                    M += D
                                            if M>ans:
                                                ans = M
    print(ans)

    

if __name__ == "__main__":
    main()

Solution (Python):

maxscore = 0



def solve():
    ans = [-1]*n
    ans[-1] = m
    rek(ans, m, 1)
    return

def rek(ans, m, rekdepth):
    global maxscore
    curr_ans = ans.copy()
    for i in range(1, m+1):
        curr_ans[-rekdepth-1] = i
        if rekdepth == n-1:
            score = get_score(curr_ans)
            maxscore = max(maxscore, score)
        else:
            rek(curr_ans.copy(), i, rekdepth + 1)


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




n, m, q = [int(s) for s in input().split(" ")]
abcd = []
for i in range(q):
    abcd.append([int(s) for s in input().split(" ")])

solve()
print(maxscore)

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

ans=0
def dfs(list):
    global ans
    if len(list)==N:
        test=0
        for i in range(0,Q):
            a,b,c,d=query[i]
            if list[b-1]-list[a-1]==c:
                test+=d
        ans=max(test,ans)
        return 0
    if list:
        m=list[-1]
        for i in range(m,M+1):
            dfs(list+[i])
    else:
        for i in range(1,M+1):
            dfs([i])

dfs([])
print(ans)

Solution (Python):
import itertools

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

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

ans = 0
for v in comb:
    tmp = 0
    for d in q_list:
        if v[d[1]-1] - v[d[0]-1] == d[2]: tmp += d[3]
    ans = max(ans, tmp)
print(ans)

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

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

Comb = list(itertools.combinations_with_replacement(num_all,N))

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

  

d_sum_max = 0  
for A in Comb:
  d_sum = 0
  
  
  
  for x in num_l:
    a,b,c,d = x[0],x[1],x[2],x[3]
    
    
    if(A[b-1]-A[a-1] == c):
      d_sum += d
      
  
  d_sum_max = max(d_sum_max,d_sum)

print(d_sum_max)

Solution (Python):
import itertools

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

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

ans = 0
for l in pat:
    cnt = 0
    for i in range(q):
        if l[abcd[i][1]-1] - l[abcd[i][0]-1] == abcd[i][2]:
            cnt += abcd[i][3]
    ans = max(ans, cnt)
print(ans)

Solution (Python):
import itertools

N,M,Q = map(int, input().split())
x = [list(map(int, input().split(" "))) for i in range(Q)]
list = list(itertools.combinations_with_replacement(range(1,M+1),N))


tmp = 0
mx = 0
for k in range(len(list)):
    for i in range(Q):
        a = x[i][0]
        b = x[i][1]
        c = x[i][2]
        d = x[i][3]
        if(list[k][b-1] - list[k][a-1]) == c:
            tmp += d
    mx = max(mx,tmp)
    tmp = 0

print(mx)

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

  ans = max(ans, an)
print(ans)

Solution (Python):
from collections import deque

N,M,Q = map(int,input().split())
QUERY = [0]*Q
for i in range(Q):
    QUERY[i] = tuple(map(int,input().split()))

ans = 0
D = deque()
D.append((1,[1]))
dlen = 1
while dlen != 0:
    Alen,A = D.pop()
    dlen -= 1
    if Alen == N:
        e = 0
        for a,b,c,d in QUERY:
            a -= 1
            b -= 1
            if A[b] - A[a] == c:
                e += d
        ans = max(ans,e)
    else:
        for AA in range(A[-1],M+1):
            D.append((Alen+1,A+[AA]))
            dlen += 1

print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

seq=list(range(1,M+1))
ret=0

for A in combinations_with_replacement(seq,N):
    tmp=0
    for a,b,c,d in query:
        if A[b-1]-A[a-1]==c:
            tmp+=d
    
    ret=max(tmp,ret)

print(ret)

Solution (Python):
n,m,q = map(int,input().split())
ans = 0
t = []
for _ in range(q):
    a,b,c,d = map(int,input().split())
    t += [(a,b,c,d)]

def score(l):
    ret = 0
    if len(l) == 0:
        for i in range(1, m+1):
            ret = max(ret, score([i]))
        return ret
    elif len(l) == n:
        for a,b,c,d in t:
            if l[b-1] -  l[a-1] == c:
                ret += d
        return ret
    else:
        for i in range(l[-1], m+1):
            ret = max(ret, score(l + [i]))
        return ret

print(score([]))

Solution (Python):
def main():
  n,m,q = map(int,input().split())
  
  a = [1]*n
    
  a_list = []
  while True:
    a_list.append([0, a[:]])
    end = True
    for i in range(n):
      index = n-1-i
      if a[index] < m:
        a[index] = a[index] + 1
        for j in range(i):
          a[index+1+j] = a[index]
        end = False
        break
    
    if end:
      break
  
  for _ in range(q):
    tmp = list(map(int, input().split()))
    for a in a_list:
      if a[1][tmp[1]-1] - a[1][tmp[0]-1] == tmp[2]:
        a[0] = a[0] + tmp[3]
  
  max = 0
  for a in a_list:
    if max < a[0]:
      max = a[0]
  
  return max

print(main())

Solution (Python):


from pprint import pprint
from collections import deque, defaultdict
import itertools
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float("inf")

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

numbers = range(1, m + 1)

combs = itertools.combinations_with_replacement(numbers, n)


res_max = 0
for A in combs:
    res = 0
    for a, b, c, d in queries:
        if A[b-1] - A[a-1] == c:
            res += d
    res_max = max(res_max, res)

print(res_max)

Solution (Python):
import numpy as np
from itertools import combinations_with_replacement

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

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

result = 0
index = combinations_with_replacement(range(1, int(m) + 1), int(n))
for i in index:
    temp = 0
    for j in range(int(q)):
        if i[my_list[j][1] - 1] - i[my_list[j][0] - 1] == my_list[j][2]:
            temp += my_list[j][3]

    result = max(result, temp)

print(result)

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

def dsum(A):
    s = 0
    for r in rs:
        a, b, c, d = r
        if A[b-1] - A[a-1] == c:
            s += d
    return s

ma = 0
for a in combinations_with_replacement(range(1, m+1), n):
    ma = max(ma, dsum(a))
print(ma)

Solution (Python):
import copy


def repeat(pattern, before, n):
    for i in range(before, M + 1):
        new_pattern = copy.copy(pattern)
        new_pattern.append(i)
        if n != N - 1:
            repeat(new_pattern, i, n + 1)
        else:
            P.append(new_pattern)


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

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

P = []
for i in range(1, M + 1):
    repeat([i], i, 1)

a_max = 0
for p in P:
    a = 0
    for c in combination:
        if p[c[1] - 1] - p[c[0] - 1] == c[2]:
            a += c[3]
    if a_max <= a:
        a_max = a
print(a_max)

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

def f(nums):
  out=0
  for samp in abcd:
    if nums[samp[1]-1]-nums[samp[0]-1]==samp[2]:
      out+=samp[3]
  return out

def g(nums):
  if nums[-1]<M:
    nums[-1]+=1
    return nums, False
  
  i=N-1
  while i>=0 and nums[i]==M:
    i-=1
  if i<0:
    return None, True
  
  nums=nums[:i]+[nums[i]+1 for _ in range(N-i)]
  return nums, False

nums=[1]*N
fi=False
ans=0

while fi==False:
  p=f(nums)
  if ans<p:
    ans=p
  nums, fi=g(nums)
  
print(ans)

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

nums = [i for i in range(1, m + 1)]
ans = 0
for nums in itertools.combinations_with_replacement(nums, r=n):
	tmp = 0
	for query in queries:
		if nums[query[1] - 1] - nums[query[0] - 1] == query[2]:
			tmp += query[3]
	ans = max(ans, tmp)
print(ans)

Solution (Python):

import sys


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    A = [0]
    def rb(i=0):
        if i < N:
            ans = 0
            for j in range(1, M + 1):
                if j >= A[-1]:
                    A.append(j)
                    ans = max(ans, rb(i + 1))
                    A.pop()
            return ans
        else:
            return sum(d[j] for j in range(Q) if A[b[j]] - A[a[j]] == c[j])
    print(rb())



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

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations_with_replacement
def main():
    n,m,q=map(int,input().split())
    ABCD=[list(map(int,input().split())) for _ in range(q)]
    ans=0
    L=list(range(1,m+1))
    for Q in combinations_with_replacement(L,n):
        Q=list(sorted(Q))
        tmp=0
        for a,b,c,d in ABCD:
            if Q[b-1]-Q[a-1]==c:
                tmp+=d
        ans=max(ans,tmp)
    print(ans)
    
if __name__=='__main__':
    main()