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):
def main():
	n,m,q = map(int,input().split())
	qu = [list(map(int,input().split())) for x in range(q)]
	ans = 0
	import itertools

	ans_list = set()
	for i in itertools.combinations_with_replacement(list(range(1,m+1)),n):
		for qul in range(q):
			if i[ qu[qul][1]-1 ] - i[ qu[qul][0]-1 ] == qu[qul][2]:
				ans += qu[qul][3]
		ans_list.add(ans)
		ans = 0
	print(max(ans_list))

if __name__ == '__main__':
	main()

Solution (Python):
import itertools

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

for A_ in itertools.combinations_with_replacement([i for i in range(1, M+1)], N):
    A = list(A_)
    tmp_ans = 0
    for i in range(Q):
        if A[list_q[i][1]-1] - A[list_q[i][0]-1] == list_q[i][2]:
            tmp_ans += list_q[i][3]
    if ans < tmp_ans:
        ans = tmp_ans

print(ans)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
tapple = []
d_sum_max = 0
for i in range(Q):
    tapple.append([int(n) for n in input().split()])
A = list(itertools.combinations_with_replacement(range(1, M+1), N))
for j in range(len(A)):
    d_sum = 0
    for i in range(Q):
        if A[j][tapple[i][1]-1] - A[j][tapple[i][0]-1] == tapple[i][2]:
            d_sum += tapple[i][3] 
    if d_sum_max < d_sum:
        d_sum_max = d_sum
print(d_sum_max)

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

a_base = [i for i in range(1,m+1)]
a_ls = []
for A in itertools.combinations_with_replacement(a_base, n):
    istarget = True
    for i in range(n-1):
        if A[i] > A[i+1]:
            istarget = False
    if istarget:
        a_ls.append(list(A))
ans = 0
for A in a_ls:
    value_of_A = 0
    for a,b,c,d in num_ls:
        if A[b-1] - A[a-1] == c:
            value_of_A += d
    ans = max(ans, value_of_A)
print(ans)

Solution (Python):
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
import itertools
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N,M,Q = FLI()
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] = FLI()
H = []
for j in range(1, M+1):
    H.append(j)
L = list(itertools.combinations_with_replacement(H,N))
max = 0
for l in L:
  sum = 0
  for q in range(Q):
    if l[b[q]-1] - l[a[q]-1] == c[q]:
        sum += d[q]
  if max < sum:
      max = sum

print(max)

Solution (Python):
from itertools import combinations_with_replacement
N, M, Q = [int(n) for n in input().split()]
X = [[int(n) for n in input().split()] for _ in range(Q)]
ans = []
for A in combinations_with_replacement(range(1, M+1), N):
    t = 0
    for x in X:
        if A[x[1]-1]-A[x[0]-1] == x[2]:
            t += x[3]
    ans.append(t)
print(max(ans))

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

ans=0
for i in arr:
  p=0
  for j in range(q):
    a,b,c,d=abcd[j][0],abcd[j][1],abcd[j][2],abcd[j][3]
    if i[b-1]-i[a-1]==c:
      p+=d
  ans=max(ans,p)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement


def sol(A):
    tmp_ans = 0
    for row in B:
        a, b, c, d = row
        a -= 1
        b -= 1
        if A[b] == A[a] + c:
            tmp_ans += d
    return tmp_ans


if __name__ == '__main__':
    N, M, Q = map(int, input().split())
    A = [1 for _ in range(N)]
    used = [False for _ in range(N)]

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

    cnt = 0
    ans = 0
    for A in combinations_with_replacement([i for i in range(1, M + 1)], N):
        ans = max(ans, sol(A))

    print(ans)

Solution (Python):
import itertools

def main(N, M, Q, input):
    '''
    配列Aを求める→得点をreturn
    A: 長さN, 最大値Mの配列
    '''

    max_score = 0
    for i in [sorted(i) for i in list(itertools.combinations_with_replacement([i + 1 for i in range(M)], N))]:
        score = 0
        for j in range(Q):
            if i[input[j][1] - 1] - i[input[j][0] - 1] == input[j][2]: 
                score += input[j][3]
        max_score = max(max_score, score)
    print(max_score)



if __name__ == '__main__':
    N, M, Q = list(map(int, input().split()))
    input = [list(map(int, input().split())) for i in range(Q)]
    main(N, M, Q, input)

Solution (Python):
import itertools
N,M,Q=map(int,input().split())
l=list(itertools.combinations_with_replacement(range(1,M+1),N))
ll=[]
List=[]
for x in range(Q):
    a,b,c,d=map(int,input().split())
    List.append([a,b,c,d])
for i in l:
    ans=0
    for j in List:
        a=j[0]
        b=j[1]
        c=j[2]
        d=j[3]
        if i[b-1]-i[a-1]==c:
            ans+=d
    ll.append(ans)
print(max(ll))

Solution (Python):
import itertools

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

mx = 0
for case in itertools.combinations_with_replacement(range(1, m+1), n):
    total = 0
    for i in range(q):
        if case[l[i][1]-1] - case[l[i][0]-1] == l[i][2]:
            total += l[i][3]
    mx = max(mx, total)
print(mx)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
S = [list(map(int, input().split())) for i in range(Q)]
sum_list = []
A_list = list(itertools.combinations_with_replacement(range(1, M+1), N))
for k in range(len(A_list)):
    A = A_list[k]
    sum = 0
    for i in range(Q):
        
        if A[S[i][1] - 1] - A[S[i][0] - 1] == S[i][2]:
        
            sum += S[i][3]
    sum_list.append(sum)
print(max(sum_list))

Solution (Python):
import itertools
import math
n,m,q = map(int,input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
sum = 0
max = 0
for i in range(q):
	a[i],b[i],c[i],d[i] = map(int,input().split())
l = [0]*m
for i in range(m):
	l[i] = i+1

for v in itertools.combinations_with_replacement(l,n):
	for i in range(q):
		if v[b[i]-1] - v[a[i]-1] == c[i]:
			sum += d[i]
	if max < sum:
		max = sum
	sum = 0
print(max)

Solution (Python):
n,m,q=map(int,input().split())
a=[]
b=[]
c=[]
d=[]
for i in range(q):
  A,B,C,D=map(int,input().split())
  a.append(A)
  b.append(B)
  c.append(C)
  d.append(D)
ans=0
M=[0,0,0,0,0,0,0,0,0,0,0]
for i in range(n):
  M[i]=1
cursor=n
breakflag=0
while True:
  ev=0
  for i in range(q):
    if M[b[i]-1]-M[a[i]-1]==c[i]:
      ev+=d[i]
  if ev>ans:
    ans=ev
  while True:
    if M[cursor-1]<m:
      M[cursor-1]+=1
      for i in range(cursor,n):
        M[i]=M[cursor-1]
      cursor=n
      break
    cursor-=1
    if cursor==0:
      breakflag=1
      break
  if breakflag==1:
    break
print(ans)

Solution (Python):
import itertools

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

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

for i in num_li:
    temp = 0
    for j in li:
        a,b,c,d = j
        if i[b-1] - i[a-1] == c:
            temp += d
    ans = max(ans,temp)
print(ans)

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

vs = [j for j in range(1, M + 1)]

temp = []
for v in vs:
    temp.append([v])

use = []
while True:
    hoge = []
    for i in temp:
        index = vs.index(i[-1])
        for v in vs[index:]:
            hoge.append(i + [v])
    temp = hoge
    if len(hoge[0]) == N:
        use = hoge
        break

ans = 0
for u in use:
    t = 0
    for v in a:
        if u[v[1] -1] - u[v[0] -1] == v[2]:
            t += v[3]
    ans = max(ans, t)

print(ans)

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

ABCD.sort(key=lambda x:x[3],reverse=True)

from itertools import combinations_with_replacement,product
a = []
for i in range(M):
    a.append(i+1)
L = list(combinations_with_replacement(a,N))

out = 0
for X in L:
    cand = 0
    for i in range(Q):
        A = ABCD[i][0]
        B = ABCD[i][1]
        C = ABCD[i][2]
        D = ABCD[i][3]
        if X[B-1]-X[A-1]==C:
            cand += D
    if cand>out:
        out = cand
print(out)

Solution (Python):
from itertools import combinations_with_replacement

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


ans = 0
for comb in combinations_with_replacement(range(M), N):
    tmp = 0
    for a, b, c, d in abcd:
        if comb[b - 1] - comb[a - 1] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

Solution (Python):
[n, m, q] = [int(x) for x in input().split(' ')]

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

for _ in range(q):
  tmp = [int(x) for x in input().split(' ')]
  a.append(tmp[0]-1)
  b.append(tmp[1]-1)
  c.append(tmp[2])
  d.append(tmp[3])

x = [1] * n

def next(x):
  for i in range(n-1, -1, -1):
    if x[i] != m:
      x[i] += 1
      for j in range(i, n):
        x[j] = x[i]
      return True
  else:
    return False

def score(x):
  s = 0
  for i in range(q):
    if x[b[i]] - x[a[i]] == c[i]:
      s += d[i]
  return s

ans = score(x)
while next(x):
  ans = max(ans, score(x))

print(ans)

Solution (Python):
import itertools

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

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

ALIST = list(itertools.combinations_with_replacement([i for i in range(1, M+1)], N))

total = 0

for A in ALIST:
  tokuten = 0
  for a, b, c, d in ABCD:
    diff = A[b-1] - A[a-1]
    if diff == c:
      tokuten += d
  total = max(total, tokuten)
print(total)

Solution (Python):
from itertools import combinations_with_replacement as combi

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

com = list(combi(list(range(1, m+1)), n))

total = []

for lis in com:
    tot = 0
    for cond in c:
        idx1 = cond[0]-1
        idx2 = cond[1]-1
        diff = cond[2]
        if lis[idx2] - lis[idx1] == diff:
            tot += cond[3]
    total.append(tot)

print(max(total))

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

ls = deque([[1]])
while True:
  v = ls.popleft()
  if len(v) == N+1:
    ls.append(v)
    break
  matubi = int(v[-1])
  for i in range(matubi,M+1):
    ls.append(v+[i])

def check(s):
  karid = 0
  for i in range(Q):
    a,b,c,d = l[i]
    if int(s[b]) - int(s[a]) == c:
      karid += d
  return karid

ans = 0
for i in range(len(ls)):
  s = ls[i]
  ans = max(check(s),ans)
print(ans)

Solution (Python):
N, M, Q = (int(i) for i in input().split())  
ABCD = []
ans = -1

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

comb_list = []

import sys
sys.setrecursionlimit(100000000)
def recur(N, lis, c):
  if len(lis) == N:
    comb_list.append(lis)
    return

  if lis == []:
    for i in range(1, M+1):
      recur(N, [i], c+1)
  else:
    for i in range(lis[c], M+1):
      recur(N, lis + [i], c+1)     

recur(N, [], -1)
for x in comb_list:
    tmp = 0
    for a, b, c, d in ABCD:
        if x[b-1] - x[a-1] == c:
             tmp += d           
    ans = max(ans, tmp)   
print(ans)

Solution (Python):
import itertools

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


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


A = list(itertools.combinations_with_replacement(kouho,n))


point = 0
maxp = 0
for a in A:
  point = 0
  
  for my in mylist:
    
    
    
    
    if a[my[1]-1] - a[my[0]-1] == my[2]:
      point += my[3]
      
      
  
  if point > maxp:
    maxp = point
    
    
print(maxp)

Solution (Python):
from itertools import combinations_with_replacement

N,M,Q=map(int,input().split())
abcd = []
for i in range(Q):
    x=list(map(int,input().split()))
    abcd.append(x)
ans,cnt=0,0
aa = [i for i in range(1,M+1)]
aa = list(combinations_with_replacement(aa,N))
for x in aa:
    x = list(x)
    for i in range(Q):
        ai,bi,ci,di = abcd[i][0]-1,abcd[i][1]-1,abcd[i][2],abcd[i][3]
        if int(x[bi]) - int(x[ai]) == int(ci):
           cnt+=di
    if cnt>=ans:
        ans = cnt
        cnt=0
    else:
        cnt=0
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
inputs = open(0).readlines()
n, m, q = map(int, inputs[0].split())
ABCD = [list(map(int, x.split())) for x in inputs[1:]]

ans = 0
for x in combinations_with_replacement(range(1, m+1), n):
    e = 0
    for a, b, c, d in ABCD:
        if x[b-1] - x[a-1] == c:
            e += d
    ans = max(e, ans)
print(ans)

Solution (Python):

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

def get_new_arr():
	last_update = N
	for i in reversed(range(N+1)):
		arr[i] = arr[i] + 1
		if arr[i] <= M:
			last_update = i
			break
	for i in range(last_update+1, N+1):
		arr[i] = arr[last_update]

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

quads = []
for q in range(Q):
	quads.append(map(int, raw_input().split()))

arr = [1]*(N+1)
best_score = 0
while arr != [2]*(N+1):
	score = get_score()
	if score > best_score:
		best_score = score
	get_new_arr()

print best_score

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

Solution (Python):
N,M,Q=map(int,input().split())
g=0
lst=[[0 for e in range(4)] for f in range(Q)]
for k in range(Q):
 lst[k]=list(map(int,input().split()))
for i in range(2**(M+N)):
 E=0
 W=1
 Sui=[0]*(M+N)
 for j in range(M+N):
  if ((i >> j) & 1):
   Sui[E]=W
   E+=1
  else:
   W+=1
 if E==N and Sui[N-1]<=M:
  f=0
  for s in range(Q):
   if Sui[lst[s][1]-1]-Sui[lst[s][0]-1]==lst[s][2]:
    f+=lst[s][3]
  g=max(g,f)
print(g)

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

R=0
for a in range(m):
    for b in range(a,m):
        for c in range(b,m):
            for d in range(c,m):
                for e in range(d,m):
                    for f in range(e,m):
                        for g in range(f,m):
                            for h in range(g,m):
                                for i in range(h,m):
                                    for j in range(i,m):
                                        A=[a+1,b+1,c+1,d+1,e+1,f+1,g+1,h+1,i+1,j+1]
                                        r=0
                                        for k in range(q):
                                            if A[L[k][1]-1]-A[L[k][0]-1]==L[k][2]:
                                                r+=L[k][3]
                                        R=max(R,r)
print(R)

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

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7

ma = 0


def main():
    n, m, q = MI()
    li = [LI() for _ in range(q)]

    m = m + 1
    l = [0] * n

    def f(x, now):
        global ma

        if now == n:
            s = 0
            for abcd in li:
                if l[abcd[1] - 1] - l[abcd[0] - 1] == abcd[2]:
                    s += abcd[3]
            if ma < s:
                ma = s

            return

        for i in range(x, m):
            l[now] = i
            f(i, now + 1)

    f(1, 0)

    print(ma)


if __name__ == "__main__":
    main()

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

if n == 2:
  for a in range(1,m+1):
    for b in range(a, m+1):
      lst.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):
        lst.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):
          lst.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):
            lst.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):
              lst.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):      
                lst.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):
                  lst.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):
                    lst.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):
                      lst.append([a, b, c, d, e, f, g, h, i, j])
        
ans = 0        
for i in range(len(lst)):
  res = 0
  for j in range(q):
    if lst[i][req[j][1]-1] - lst[i][req[j][0]-1] == req[j][2]:
      res += req[j][3]
  ans = max(ans, res)

print(ans)

Solution (Python):
import itertools
N,M,Q=map(int,input().split())
ABCD=[list(map(int,input().split())) for _ in range(Q)]
tl=[]
for i in range(1,M+1):
    tl.append([i])
for i in range(N):
    t=[]
    for l in tl:
        for j in range(1,M+1):
            if l[-1]<=j:
                t.append(l+[j])
    tl=t[:]
ans=0
for l in tl:
    t=0
    for a,b,c,d in ABCD:
        if l[b-1]-l[a-1]==c:
            t+=d
    ans=max(ans,t)
print(ans)

Solution (Python):
import itertools

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

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

num_list = [int(x) for x in range(1, M+1)]
pairs = []

for pair in itertools.combinations_with_replacement(num_list, N):
    pairs.append(pair)

ans = 0


for pair in pairs:
    now_sum = 0
    
    for target_row in Xn:
        
        ai = target_row[0]
        bi = target_row[1]
        ci = target_row[2]
        di = target_row[3]
        
        if(pair[bi-1] - pair[ai-1] - ci == 0):
            now_sum = now_sum + di
    ans = max(ans, now_sum)
    

print(ans)

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

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

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

def score(box):
  ret = 0
  for i in range(Q):
    if box[B[i]-1] - box[A[i]-1] == C[i]:
      ret += D[i]
  return(ret)

box = [1]*N
n = N - 1
flag = True
ans = 0
while flag:

  ans = max(ans,score(box))
  for nn in range(1,N+1):
    n = N-nn
    if box[n] < M:
      box[n] += 1
      box[n:N] = [box[n]]*(N-n)
      break
    elif box[0]==M:
      flag = False

print(ans)

Solution (Python):
n,m,q=map(int,input().split())
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
x=[0]*10
s=0
ans=0
for i in range(q):
  a[i],b[i],c[i],d[i]=map(int,input().split())
for a0 in range(1,m+1):
  x[0]=a0
  for a1 in range(x[0],m+1):
    x[1]=a1
    for a2 in range(x[1],m+1):
      x[2]=a2
      for a3 in range(x[2],m+1):
        x[3]=a3
        for a4 in range(x[3],m+1):
          x[4]=a4
          for a5 in range(x[4],m+1):
            x[5]=a5
            for a6 in range(x[5],m+1):
              x[6]=a6
              for a7 in range(x[6],m+1):
                x[7]=a7
                for a8 in range(x[7],m+1):
                  x[8]=a8
                  for a9 in range(x[8],m+1):
                    x[9]=a9
                    for j in range(q):
                      if x[b[j]-1]-x[a[j]-1]==c[j]:
                        s=s+d[j]
                    ans=max(ans,s)
                    s=0
print(ans)

Solution (Python):
import itertools

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

ans = 0
l = range(M)
c = list(itertools.combinations_with_replacement(l,N))


for i in range(len(c)):
    A = c[i]
    
    ans_i = 0
    for j in range(Q):
        A_b = A[abcd[j][1]-1]
        A_a = A[abcd[j][0]-1]
        

        c_i = A_b-A_a
        
        if c_i == abcd[j][2]:
            
            ans_i += abcd[j][3]
    
    if ans_i > ans:
        ans = ans_i

print(ans)

Solution (Python):



from pprint import pprint as pp
from pprint import pformat as pf


class Data:

    def __init__(self, frm, to, diff, score):
        self.frm = frm - 1
        self.to = to - 1
        self.diff = diff
        self.score = score

    def __repr__(self):
        return "{} {} {} {}".format(
                self.frm,
                self.to,
                self.diff,
                self.score,
                )

    def calc_score(self, l):
        if l[self.to] - l[self.frm] == self.diff:
            return self.score
        else:
            return 0

def flat(l, key, value):
    for i in range(key, len(l)):
        l[i] = value

def shift(l, m, key):
    if key < 0:
        return False
    if l[key] < m:
        flat(l, key, l[key] + 1)
        return True
    else:
        return shift(l, m, key - 1)

def count(n, m):
    l = [1] * n
    key = 0
    flg = True
    while flg:
        yield l
        flg = shift(l, m, len(l) - 1)

def get_scorebook(q):
    book = [None] * q
    for i in range(q):
        a, b, c, d = list(map(int, input().split()))
        book[i] = Data(a, b, c, d)
    return book

def calc_score(l, book):
    score = 0
    for data in book:
        score += data.calc_score(l)
    return score

if __name__ == '__main__':
    n, m, q = map(int, input().split())
    scorebook = get_scorebook(q)
    score = 0
    for l in count(n, m):
        score = max(score, calc_score(l, scorebook))
    print(score)

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

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

ans=0

for i in range(len(aa)):
  score = 0
  for n in range(len(qset)):
    a=aa[i]
    abcd=qset[n]
    if a[abcd[1]-1]-a[abcd[0]-1]==abcd[2]:
      score=score+abcd[3]
  if ans<score:
    ans=score
print(ans)

Solution (Python):
import itertools

nmq = list(map(int, input().split()))
n = nmq[0]
m = nmq[1]
q = nmq[2]
i = [list(map(int, input().split())) for i in range(q)]
a = list(range(1, m + 1))
a_all = list(itertools.combinations_with_replacement(a, n))
ans = 0
for x in a_all:
  val = 0
  for y in i:
    if x[y[1] - 1] - x[y[0] - 1] == y[2]:
      val = val + y[3]
  if val > ans:
    ans = val
print(ans)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
List = [list(map(int, input().split())) for i in range(Q)]
Max = 0
 
for a in itertools.combinations_with_replacement(range(1, M+1), N):
  A = list(a)
  A.sort()
  point = 0
  for i in range(Q):
    ai = List[i][0]
    bi = List[i][1]
    if ai < 1 or N < ai or bi < 1 or N < bi:
      continue
    if A[bi-1]-A[ai-1] == List[i][2]:
      point += List[i][3]
  if Max < point:
    Max = point
 
print(Max)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline
n,m,q=map(int,input().split())
x=[list(map(int,input().split())) for i in range(q)]
for i in range(q):
    for j in range(2):
        x[i][j]-=1
a=[]
def dfs(v):
    if len(v)>n:
        return
    if len(v)==n:
        a.append(v)
    for i in range(int(v[-1]),m+1):
        dfs(v+[i])
for i in range(1,m+1):
    dfs([i])
ans=0
for i in a:
    now=0
    for k in range(q):
        if i[x[k][1]]-i[x[k][0]]==x[k][2]:
            now+=x[k][3]
    ans=max(now,ans)
print(ans)

Solution (Python):
import itertools

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

nums = []
for i in range(q):
    line = [int(s) for s in input().split()]
    line[0] -= 1
    line[1] -= 1
    nums.append(line)

candidate = []
for line in list(itertools.combinations_with_replacement(range(1, m + 1), n)):
    a = 0
    for num in nums:
        if line[num[1]] - line[num[0]] == num[2]:
            a += num[3]
    
    candidate.append(a)

print(max(candidate))

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]

    def dfs(A):
        cur = 0
        if len(A) == N:
            for a, b, c, d in T:
                if A[b-1] - A[a-1] == c:
                    cur += d
        elif len(A) == 0:
            for i in range(1, M+1):
                v = dfs(A + [i])
                cur = max(cur, v)
        else:
            for i in range(A[-1], M+1):
                v = dfs(A + [i])
                cur = max(cur, v)
        return cur

    print(dfs([]))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
from copy import copy

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

queue = deque()
for i in range(1, M+1):
    queue.append([i])
for _ in range(N-1):
    while 1:
        if queue[0] == [1] * N:
            break
        x = queue.popleft()
        for i in range(x[-1], M+1):
            y = copy(x)
            y.append(i)
            queue.append(y)

score_max = 0
for A in queue:
    score = 0
    for a, b, c, d in X:
        if int(A[b-1]) - int(A[a-1]) == c:
            score += d
    score_max = max(score, score_max)

print(score_max)

Solution (Python):
import itertools
n,m,q = map(int,input().split())
mx = 0
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(q):
  ai,bi,ci,di = map(int,input().split())
  a[i] = ai-1
  b[i] = bi-1
  c[i] = ci
  d[i] = di
mx = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n):
  s = 0
  for i in range(q):
    if v[b[i]] - v[a[i]] == c[i]:
      s += d[i]
  if s > mx:
    mx = s
print(mx)

Solution (Python):
import sys
input=sys.stdin.readline
import itertools
N,M,Q=map(int,input().split())
T=[]

H=0
for y in range(Q):
    a,b,c,d=map(int,input().split())
    t=(a,b,c,d)
    T.append(t)
A=[x+1 for x in range(M)]
B=list(itertools.combinations_with_replacement(A,N))
C=len(B)
for z in range(C):
    E=B[z]
    G=0
    for l in range(Q):
        F=T[l]
        g=F[0]-1
        h=F[1]-1
        i=F[2]
        if E[h]-E[g]==i:
            G+=F[3]
    if G>=H:
        H=G
print(H)

Solution (Python):
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import itertools
n,m,q = ma()
abcd = []
ls = [i+1 for i in range(m)]
for i in range(q):
    abcd.append(list(ma()))

ans = 0
for A in  itertools.combinations_with_replacement(ls,n):
    tmp = 0
    for a,b,c,d in abcd:
        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())
chk = []
for _ in range(Q):
    *abcd, = map(int, input().split())
    chk.append(abcd)

li = [list(x) for x in list(combinations_with_replacement(range(1, M+1), N))]
ans = 0
for item in li:
    sub = 0
    for c in chk:
        if item[c[1]-1] - item[c[0]-1] == c[2]:
            sub += c[3]
    ans = max(ans, sub)
print(ans)

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

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

ans = 0
def dfs(i,before,score):
    global ans
    if i == n - 1:
        ans = max(ans,score)
        return
    for num in range(before[i],m + 1):
        point = 0
        for a,c,d in cond[i + 1]:
            if num - before[a] == c:
                point += d
        if point > 0:
            before[i + 1] = num
            dfs(i + 1,before,score + point)
    before[i + 1] = before[i]
    dfs(i + 1,before,score) 

dfs(0,[1] + [-1]*(n - 1),0)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement


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






num = [x + 1 for x in range(M)]
ans = 0
for var in combinations_with_replacement(num, N):
    cnt = 0
    for a, b, c, d in con:
        a -= 1; b -= 1
        if var[b] - var[a] == c:
            cnt += d
    ans = max(cnt, ans)

print(ans)