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 glob


REL_PATH = 'ABC\\165\\C'


TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class C(Common):

    def rec(self, A, now_index):

        point1 = 0
        point2 = 0
        if now_index == self.N-1:
            for i in self.abcd:
                if A[i[1]-1] - A[i[0]-1] == i[2]:
                    point1 += i[3]

        else:
            for i in range(A[now_index], self.M+1):
                A[now_index+1] = i
                tmp = self.rec(A, now_index+1)
                if tmp > point2:
                    point2 = tmp

        if point1 > point2:
            return point1
        return point2

    def resolve(self):

        self.N, self.M, self.Q = map(int, self.input_data().split())
        self.abcd = []

        for i in range(self.Q):
            self.abcd.append(list(map(int, self.input_data().split())))

        A = [0]*self.N
        A[0] = 1

        point = self.rec(A, 0)
        print(str(point))

solver = C(REL_PATH)
solver.exec_resolve()

Solution (Python):
from sys import stdin
import itertools
import numpy as np
from numba import njit

def main():

    readline=stdin.readline
    N,M,Q=map(int,readline().split())
    a=np.zeros(Q,dtype=np.int64)
    b=np.zeros(Q,dtype=np.int64)
    c=np.zeros(Q,dtype=np.int64)
    d=np.zeros(Q,dtype=np.int64)
    for i in range(Q):
        a[i],b[i],c[i],d[i]=map(int,readline().split())

    arrays=np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)),dtype=np.int64)
    l=len(arrays)

    print(f(l,Q,a,b,c,d,arrays))
    
@njit("i8(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:,:])",cache=True)
def f(l,Q,a,b,c,d,arrays):
    max_res=0
    for i in range(l):
        res=0
        for j in range(Q):
            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:
                res+=d[j]
        max_res=max(max_res,res)
    return max_res

if __name__=="__main__":
	main()

Solution (Python):
from sys import stdin
import itertools
import numpy as np
from numba import njit

@njit("i8(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:,:])",cache=True)
def f(l,Q,a,b,c,d,arrays):
    max_res=0
    for i in range(l):
        res=0
        for j in range(Q):
            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:
                res+=d[j]
        max_res=max(max_res,res)
    return max_res
  
def main():

    readline=stdin.readline
    N,M,Q=map(int,readline().split())
    a=np.zeros(Q,dtype=np.int64)
    b=np.zeros(Q,dtype=np.int64)
    c=np.zeros(Q,dtype=np.int64)
    d=np.zeros(Q,dtype=np.int64)
    for i in range(Q):
        a[i],b[i],c[i],d[i]=map(int,readline().split())

    arrays=np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)),dtype=np.int64)
    l=len(arrays)

    print(f(l,Q,a,b,c,d,arrays))
    
if __name__=="__main__":
	main()

Solution (Python):
from sys import stdin
import itertools
import numpy as np
from numba import njit

@njit(cache=True)
def f():
    max_res=0
    for i in range(l):
        res=0
        for j in range(Q):
            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:
                res+=d[j]
        max_res=max(max_res,res)
    return max_res


readline=stdin.readline
N,M,Q=map(int,readline().split())
a=np.zeros(Q,dtype=np.int64)
b=np.zeros(Q,dtype=np.int64)
c=np.zeros(Q,dtype=np.int64)
d=np.zeros(Q,dtype=np.int64)
for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int,readline().split())

arrays=np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)),dtype=np.int64)
l=len(arrays)

print(f())

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

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

ans = 0
for each in lis:
    tmp = 0
    for i in range(len(a)):
        if each[b[i]-1] - each[a[i]-1] == 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(list(map(int,input().split())))
  L[i][0]-=1
  L[i][1]-=1

import itertools
R=list(range(1,M+1))
A=list(itertools.combinations_with_replacement(R,N))
ans=0

for i in A:
  tmp=0  
  for a,b,c,d in L:
    if (i[b]-i[a])==c:
      tmp+=d
  ans=max(ans,tmp)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
N,M,Q=map(int,input().split())
L=[]
for i in range(Q):
  LI=list(map(int,input().split()))
  L.append(LI)
  L[i][0]-=1
  L[i][1]-=1
R=range(1,M+1)
V=list(combinations_with_replacement(R,N))
ans=0
for arr in V:
  tmp=0
  for a,b,c,d in L:
    if arr[b]-arr[a]==c:
      tmp+=d
  ans=max(ans,tmp)
print(ans)

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

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

import itertools
l =list(range(1,M+1))
v=list(itertools.combinations_with_replacement(l,N))
ans=0
for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:      
      score+=d
  ans=max(ans,score)
print(ans)

Solution (Python):
import math
from itertools import combinations_with_replacement

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

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

for i in range(q):
    aa, bb, cc, dd = map(int, input().split())
    a.append(aa)
    b.append(bb)
    c.append(cc)
    d.append(dd)

zzz = list(combinations_with_replacement(ls, n))
ans = 0

for i in zzz:
    com = list(i)
    p = 0
    for j in range(q):
        if (com[b[j] - 1] - com[a[j] - 1]) == c[j]:
            p += d[j]
    ans = max(ans, p)

print(ans)

Solution (Python):
import itertools
n,m,q= map(int, input().split())
g = []
for _ in range(q):
    z = list(map(int, input().split()))
    g.append(z)
seq = []
for i in range(1,m+1):
  seq.append(i)
f = [0]
for i in list(itertools.combinations_with_replacement(seq, n)):
  ans = 0
  for j in g:
    if i[j[1]-1]-i[j[0]-1] == j[2]:
      ans += j[3]
  f.append(ans)
print(max(f))

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

import itertools
l =list(range(1,M+1))
v=list(itertools.combinations_with_replacement(l,N))
ans=0
for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:      
      score+=d
  ans=max(ans,score)
print(ans)

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

As = []

def make(A):
  global As, N, M
  
  if len(A) == N+1:
    As.append(A)
  else:
    for i in range(A[len(A)-1], M+1):
      make(A+[i])

make([1])

maxs = 0
for A in As:
  score = 0
  for req in reqs:
    if A[req[1]] - A[req[0]] == req[2]:
      score += req[3]
  maxs = max(score, maxs)

print(maxs)

Solution (Python):
n, m , q = list(map(int, input().split()))
lists = []
sequence = [1] * n
output = 0
for i in range(q):
  lists.append(list(map(int, input().split())))
  
while (True):
  score = 0
  limit_digit = -1
  for i in range(q):
    if sequence[lists[i][1] - 1] - sequence[lists[i][0] - 1] == lists[i][2]:
      score += lists[i][3]
  if output < score:
    output = score
  for i in range(n):
    if sequence[n - i - 1] == m:
      limit_digit = n - i - 1
  if limit_digit == 0:
    break
  elif limit_digit == -1:
    sequence[n - 1] += 1
  else:
    if limit_digit == 1:
      sequence = [sequence[0] + 1] * n
    else:
      sequence[limit_digit - 1] += 1
      for k in range(limit_digit, n):
        sequence[k] = sequence[limit_digit - 1]

print(output)

Solution (Python):


import sys
from collections import deque
from itertools import combinations_with_replacement
input=sys.stdin.readline

def main():
    N,M,Q=map(int,input().split())
    g=[i for i in range(1,M+1)]
    abcds=[tuple(map(int,input().split())) for i in range(Q)]
    maxp=0
    for A in combinations_with_replacement(g, N):
        p=0
        for abcd in abcds:
            if A[abcd[1]-1]-A[abcd[0]-1]==abcd[2]:
                p+=abcd[3]
        maxp=max(maxp,p)
    print(maxp)
    
    
if __name__=="__main__":
    main()

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

A = list(i for i in range(1, M+1))
X = list(itertools.combinations_with_replacement(A, N))
R = 0
T = 0
for x in X:
    for i in range(Q):
        if x[P[i][1]-1]-x[P[i][0]-1]==P[i][2]:
            R += P[i][3]
    T = max(T, R)
    R = 0
print(T)

Solution (Python):

import sys
sys.setrecursionlimit(10000)

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

def make(a):
    global A_list,N,M
    if len(a)==N:
        A_list.append(a)
    else:
        for i in range(a[len(a)-1],M+1):
            make(a+[i])
make([1])

ans = 0
for A in A_list:
    tmp = 0
    for a,b,c,d in q:
        if A[b-1]-A[a-1]==c:
            tmp += d
    ans = max(ans,tmp)

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

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

ans = 0
for i in num:
  t = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      t += j[3]
  ans = max(ans, t)
  
print(ans)

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

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

point_max=0
for i in itertools.combinations_with_replacement(range(1,M+1),N):
    point = 0
    for cond in cond_list:

        if (i[cond[1]-1]-i[cond[0]-1]) == cond[2]:

            point = point + cond[3]
    if point>point_max:
        point_max = point

print(point_max)

Solution (Python):
import sys
import itertools
import numpy as np

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

N,M,Q = map(int, readline().split())
A = np.array(list(itertools.combinations_with_replacement(range(1,M+1), N)))

score = np.zeros(len(A), int)
m = map(int, read().split())
for a,b,c,d in zip(m,m,m,m):
    score += (A[:,b-1] - A[:,a-1] == c) * d
print(score.max())

Solution (Python):
n,m,q = map(int,input().split())
li2=[input() for i in range(q)]
abcd = []
for i in range(q):
    abcd.append([int(x.strip()) for x in li2[i].split()])

ans = 0

import itertools
l = list(range(1,m+1))

for v in itertools.combinations_with_replacement(l, n):
    memo = 0
    for i in range(q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = v[b-1] - v[a-1]
        if c == abcd[i][2]:
            memo += abcd[i][3]

    ans = max(ans,memo)

print(ans)

Solution (Python):
import itertools

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

ans = -10**18
for A in AA:
    score = 0
    for a,b,c,d in abcd:
        if A[b-1] - A[a-1] == c:
            score += d
    ans = max(ans, score)
print(ans)

Solution (Python):
N,M,Q = (int(x) for x in input().split())
abcd  = [[]]*Q
for T in range(0,Q):
    abcd[T] = [int(x) for x in input().split()]
    
def PointCal(A,Q,abcd,MaxP):
    Point = 0
    for TT in range(0,Q):
        if A[abcd[TT][1]-1]-A[abcd[TT][0]-1]==abcd[TT][2]:
            Point += abcd[TT][3]
    return max(Point,MaxP)

MaxP  = 0
for A1 in range(1,M+1):
    for A2 in range(A1,M+1):
        if N==2:
            A    = [A1,A2]
            MaxP = PointCal(A,Q,abcd,MaxP)
        else:
            for A3 in range(A2,M+1):
                if N==3:
                    A    = [A1,A2,A3]
                    MaxP = PointCal(A,Q,abcd,MaxP)
                else:
                    for A4 in range(A3,M+1):
                        if N==4:
                            A    = [A1,A2,A3,A4]
                            MaxP = PointCal(A,Q,abcd,MaxP)
                        else:
                            for A5 in range(A4,M+1):
                                if N==5:
                                    A    = [A1,A2,A3,A4,A5]
                                    MaxP = PointCal(A,Q,abcd,MaxP)
                                else:
                                    for A6 in range(A5,M+1):
                                        if N==6:
                                            A    = [A1,A2,A3,A4,A5,A6]
                                            MaxP = PointCal(A,Q,abcd,MaxP)                                    
                                        else:
                                            for A7 in range(A6,M+1):
                                                if N==7:
                                                    A    = [A1,A2,A3,A4,A5,A6,A7]
                                                    MaxP = PointCal(A,Q,abcd,MaxP)
                                                else:
                                                    for A8 in range(A7,M+1):
                                                        if N==8:
                                                            A    = [A1,A2,A3,A4,A5,A6,A7,A8]
                                                            MaxP = PointCal(A,Q,abcd,MaxP)   
                                                        else:
                                                            for A9 in range(A8,M+1):
                                                                if N==9:
                                                                    A    = [A1,A2,A3,A4,A5,A6,A7,A8,A9]
                                                                    MaxP = PointCal(A,Q,abcd,MaxP)
                                                                else:
                                                                    for A10 in range(A9,M+1):
                                                                        A    = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10]
                                                                        MaxP = PointCal(A,Q,abcd,MaxP)
print(MaxP)

Solution (Python):
N,M,Q = (int(x) for x in input().split())
ABCD  = [0]*Q
for T in range(0,Q):
    ABCD[T] = [int(x) for x in input().split()]
    
MaxP  = 0
for A1 in range(1,M+1):
    for A2 in range(A1,M+1):
        if N<=2:
            Point = 0
            A = [A1,A2]
            for TT in range(0,Q):
                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                    Point = Point + ABCD[TT][3]
            if Point > MaxP:
                MaxP = Point
        else:
            for A3 in range(A2,M+1):
                if N<=3:
                    Point = 0
                    A = [A1,A2,A3]
                    for TT in range(0,Q):
                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                            Point = Point + ABCD[TT][3]
                    if Point > MaxP:
                        MaxP = Point                    
                else:
                    for A4 in range(A3,M+1):
                        if N<=4:
                            Point = 0
                            A = [A1,A2,A3,A4]
                            for TT in range(0,Q):
                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                    Point = Point + ABCD[TT][3]
                            if Point > MaxP:
                                MaxP = Point
                        else:
                            for A5 in range(A4,M+1):
                                if N<=5:
                                    Point = 0
                                    A = [A1,A2,A3,A4,A5]
                                    for TT in range(0,Q):
                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                            Point = Point + ABCD[TT][3]
                                    if Point > MaxP:
                                        MaxP = Point                                    
                                else:
                                    for A6 in range(A5,M+1):
                                        if N<=6:
                                            Point = 0
                                            A = [A1,A2,A3,A4,A5,A6]
                                            for TT in range(0,Q):
                                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                    Point = Point + ABCD[TT][3]
                                            if Point > MaxP:
                                                MaxP = Point                                        
                                        else:
                                            for A7 in range(A6,M+1):
                                                if N<=7:
                                                    Point = 0
                                                    A = [A1,A2,A3,A4,A5,A6,A7]
                                                    for TT in range(0,Q):
                                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                            Point = Point + ABCD[TT][3]
                                                    if Point > MaxP:
                                                        MaxP = Point                                                
                                                else:
                                                    for A8 in range(A7,M+1):
                                                        if N<=8:
                                                            Point = 0
                                                            A = [A1,A2,A3,A4,A5,A6,A7,A8]
                                                            for TT in range(0,Q):
                                                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                    Point = Point + ABCD[TT][3]
                                                            if Point > MaxP:
                                                                MaxP = Point                                                            
                                                        else:
                                                            for A9 in range(A8,M+1):
                                                                if N<=9:
                                                                    Point = 0
                                                                    A = [A1,A2,A3,A4,A5,A6,A7,A8,A9]
                                                                    for TT in range(0,Q):
                                                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                            Point = Point + ABCD[TT][3]
                                                                    if Point > MaxP:
                                                                        MaxP = Point                                                                
                                                                else:
                                                                    for A10 in range(A9,M+1):
                                                                        Point = 0
                                                                        A = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10]
                                                                        for TT in range(0,Q):
                                                                            if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                                Point = Point + ABCD[TT][3]
                                                                        if Point > MaxP:
                                                                            MaxP = Point
print(MaxP)

Solution (Python):
import itertools
n, m, q = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(q)]
r = 0
for A in itertools.combinations_with_replacement(range(m), n):
    s = 0
    for i in range(q):
        if A[x[i][1] - 1] - A[x[i][0] - 1] == x[i][2]:
            s += x[i][3]
    r = max(r, s)
print(r)

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

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

base = 0
for i in range(len(num_list)):
    sum = 0
    for j in range(q):
        if num_list[i][P[j][1]-1] - num_list[i][P[j][0]-1] == P[j][2]:
            sum += P[j][3]
    
    if base  < sum:
        base = sum

print(base)

Solution (Python):
def dfs(A):
    if len(A) > n:
        total = 0
        for i in range(q):
            if A[b[i]]-A[a[i]] == c[i]:
                total += d[i]
        global ans
        if total > ans:
            ans = total
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A[:])
        A[-1] += 1

if __name__ == "__main__":
    ans = 0
    n, m, q = map(int, input().split())
    a, b, c, d = [None]*q, [None]*q, [None]*q, [None]*q
    for i in range(q):
        a[i], b[i], c[i], d[i] = map(int, input().split())
    dfs([1])
    print(ans)

Solution (Python):
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
 
def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score
 
def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        f(p + [x])
 
f([])
 
print(ans)

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

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

def f(p):
    if len(p) == n:
        return calc_score(p)
    ans = 0
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        p.append(x)
        ans = max(ans, f(p))
        p.pop()
    return ans

print(f([]))

Solution (Python):
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

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

def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        p.append(x)
        f(p)
        p.pop()

f([])

print(ans)

Solution (Python):
from itertools import combinations_with_replacement as comb

n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

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

for p in comb(range(1, m+1), n):
    ans = max(ans, calc_score(p))

print(ans)

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

mx = 0
nums = list(range(1,m+1))
for h in itertools.combinations_with_replacement(nums,n):
    mxtemp = 0
    for j in range(q):
        a = sets[j][0]
        b = sets[j][1]
        if list(h)[b-1] - list(h)[a-1] == sets[j][2]:
            mxtemp += sets[j][3]
    if mxtemp > mx:
        mx = mxtemp
print(mx)

Solution (Python):
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

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

def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        f(p + [x])

f([])

print(ans)

Solution (Python):
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

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

def f(p):
    if len(p) == n+1:
        global ans
        ans = max(ans, calc_score(p))
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

print(ans)

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

def f(p):
    if len(p) == n+1:
        arr.append(p[:-1])
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

ans = 0
for p in arr:
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)

Solution (Python):
from collections import deque

n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
arr = []

def f(p):
    if len(p) == n+1:
        arr.append(p[:-1])
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

ans = 0
for p in arr:
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)

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

ans = 0


def dfs(A):
    global ans
    if len(A) == n:
        score = 0
        for a, b, c, d in qry:
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max(ans, score)
        return
    elif len(A) == 0:
        for nx in range(1, m + 1):
            dfs(A + [nx])
    else:
        for nx in range(A[-1], m + 1):
            dfs(A + [nx])


dfs([])
print(ans)

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


def main():
    n, m, q = map(int, input().split())
    al = [None]*q
    bl = [None]*q
    cl = [None]*q
    dl = [None]*q
    for i in range(q):
        a, b, c, d = map(int, input().split())
        al[i] = a
        bl[i] = b
        cl[i] = c
        dl[i] = d
    
    def dfs(A):
        if len(A) > n:
            total = 0
            for i in range(q):
                if A[bl[i]]-A[al[i]] == cl[i]:
                    total += dl[i]
            global ans
            if total > ans:
                ans = total
            return
        A.append(A[-1])
        while A[-1] <= m:
            dfs(A[:])
            A[-1] += 1
    
    dfs([1])
    print(ans)
                
                

if __name__ == "__main__":
    ans = 0
    main()

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


max_cost = 0

def dfs(A):
  global max_cost
  
  if (len(A)==N):
    cost = 0
    
    for Q in Q_list:
      a = Q[0]
      b = Q[1]
      c = Q[2]
      d = Q[3]
      if (A[b-1]-A[a-1]==c):
        cost += d
    if (cost>max_cost):
      max_cost = cost
    return 1
  
  low = 0
  if (A == []):
    low = 1
  else:
  	low = A[-1]
  for i in range(low, M+1):
    dfs(A + [i])
    
dfs([])
print(max_cost)

Solution (Python):
from sys import stdin
import itertools
import numpy as np
from numba import njit


readline=stdin.readline
N,M,Q=map(int,readline().split())
a=np.zeros(Q,dtype=np.int64)
b=np.zeros(Q,dtype=np.int64)
c=np.zeros(Q,dtype=np.int64)
d=np.zeros(Q,dtype=np.int64)
for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int,readline().split())

arrays=np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)),dtype=np.int64)
l=len(arrays)

@njit
def f():
    max_res=0
    for i in range(l):
        res=0
        for j in range(Q):
            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:
                res+=d[j]
        max_res=max(max_res,res)
    return max_res

print(f())

Solution (Python):


import itertools
import numpy as np

def resolve():
    N,M,Q = map(int,input().split())
    A = np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)))
    s = np.zeros(len(A), np.int32)
    for i in range(Q):
        a,b,c,d = map(int,input().split()) 
        s += d * (A[:,b-1] - A[:,a-1] == c)
    print(s.max())
resolve()

Solution (Python):
N, M, Q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
ans = float("-inf")
def calcu(arr):
    res = 0
    for a, b, c, d in abcd:
        if arr[b-1] - arr[a-1] == c:
            res += d
    return res

def dfs(A):
    global ans
    if len(A) == N:
        ans = max(ans, calcu(A))
        return
    else:
        start = A[-1] if len(A) > 0 else 1
        for num in range(start, M+1):
            A.append(num)
            dfs(A)
            A.pop()

dfs([])
print(ans)