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=list(map(int,input().split()))
abcd=[]
for _ in range(q):
    abcd.append(list(map(int,input().split())))

max_score=0

num=[0]*n
while True:
    score=0
    for lis in abcd:
        if num[lis[1]-1]-num[lis[0]-1]==lis[2]:
            score+=lis[3]
    if score>=max_score:
        max_score=score
    num[-1]+=1
    
    if num[-1]==m:
        check=-1
        for j in range(n-1):
            if num[j]!=m-1:
                check=j
        if check==-1:
            break
        else:
            num[check]+=1
            for k in range(check+1,n):
                num[k]=num[check]
print(max_score)

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(Q)]
data = list(range(1, M + 1))
L = list(itertools.combinations_with_replacement(data, N))
ans = 0
for l in L:
    tmp = 0
    for a, b, c, d in X:
        if l[b - 1] - l[a - 1] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(Q)]
data = list(range(1, M + 1))
L = list(itertools.combinations_with_replacement(data, N))
ans = 0
for l in L:
    tmp = 0
    for a, b, c, d in X:
        if l[b - 1] - l[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)]

result = 0
for A in combinations_with_replacement(range(1, M + 1), N):

    t = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            t += d
    result = max(result, t)
print(result)

Solution (Python):

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

ans = 0

def check(A):
    global ans
    max_ = 0

    
    for (a, b, c, d) in abcd:
        if A[b-1] - A[a-1] == c:
            max_ += d

    
    ans = max(ans, max_)

def suretu(A=[], st=1):
    
    for i in range(st, m+1):
        if len(A) == n: 
            check(A)
            return
        else:
            suretu(A + [i], i)

    return ans

print(suretu())

Solution (Python):
f=lambda:map(int,input().split())
n,m,q=f()
lq=[tuple(f()) for _ in range(q)]
ans=0
from itertools import *
for t in combinations_with_replacement(range(m),n):
  s=0
  for a,b,c,d in lq:
    if t[b-1]-t[a-1]==c: s+=d
  ans=max(ans,s)
print(ans)

Solution (Python):
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)

def search(seq, min_n, max_n, n, req):
	ans = 0
	if len(seq) == n:
		for a, b, c, d in req:
			if seq[b-1] - seq[a-1] == c:
				ans += d
		return ans
	else:
		for i in range(min_n, max_n+1):
			new_seq = seq + [i]
			ans = max(ans, search(new_seq, i, max_n, n, req))
	return ans

def resolve():
	n, m, q = map(int, input().split())
	req = [list(map(int, input().split())) for _ in range(q)]
	print(search([], 1, m, n, req))
resolve()

Solution (Python):
n, m, q = map(int, input().split())
num = [list(map(int, input().split())) for _ in range(q)]
ans = 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):
                  L = [1, i1, i2, i3, i4, i5, i6, i7, i8, i9]
                  A = []
                  for i in range(n):
                    A.append(L[i])
                  tmp = 0
                  for i in range(q):
                    a = num[i][0] - 1
                    b = num[i][1] - 1
                    c = num[i][2]
                    d = num[i][3]
                    if A[b] - A[a] == c:
                      tmp += d
                  ans = max(ans, tmp)
print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt
from bisect import bisect_left, bisect_right
from copy import deepcopy
import sys
def I(): return int(input())  
def Is(): return map(int, input().split())  
def LI(): return list(map(int, input().split()))  
def TI(): return tuple(map(int, input().split()))  
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  
def LS(): return list(input())  
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')



N, M, Q = Is()
ABCD = TIR(Q)
ans = -INF

def dfs(A):
    global ans
    if len(A) == N:
        score = 0
        for a, b, c, d in ABCD:
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max(ans, score)
    else:
        ret = 0
        amax = max(A) if len(A) != 0 else 1
        for i in range(amax, M+1):
            dfs(A + [i])

dfs([])
print(ans)

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


a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by1,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by1)
    c.append(cy)
    d.append(dy)
s=0
smax=-100

sys.setrecursionlimit(3000)



def dfs(h):
    global smax
    if len(h)==n+1:
        s=0
        for j in range(0,q):
            if h[b[j]]-h[a[j]]==c[j]:
                s+=d[j]
        if s>smax:
            smax=s

        return
    
    for i in range(0,m+1-h[len(h)-1]):
        
        h2=h[:]
        h2.append(h[len(h)-1]+i)
        dfs(h2)

sta=[1]
dfs(sta)
print(smax)

Solution (Python):

n,m,q=map(int, input().split( ))
qs=tuple(tuple(map(int, input().split( ))) for _ in range(q))
ans=0
def dfs(ll):
    global ans
    if len(ll)==n+1:
        sm=0
        for ai,bi,ci,di in qs:
            if ll[bi]-ll[ai]==ci:
                sm+=di 
        ans=max(ans,sm)
    else:
        for i in range(ll[-1],m):
            ll2=ll+[i]
            dfs(ll2)
l=[0]
dfs(l)
print(ans)

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

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

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

abcd = [0]*q
for i in range(q):
  abcd[i] = [int(x) for x in input().split()]



def dp(k, key, a):
  if k == 0:
    sub = 0
    for i in range(q):
      if key[abcd[i][1]-1] - key[abcd[i][0]-1] == abcd[i][2]:
        sub += abcd[i][3]
    return max([a, sub])
  elif k == n:
    sub = a
    for i in range(1, m+1):
      sub = max([dp(k-1, key+[i], sub), sub])
    return sub
  else:
    sub = a
    for i in range(key[-1], m+1):
      sub = max([dp(k-1, key+[i], sub), sub])
    return sub

ans = dp(n, [], 0)
print(ans)

Solution (Python):
n,m,q = list(map(int,input().split()))
abcd =  [list(map(int,input().split())) for i in range(q)]
 
 
def dfs(x,y,z,que):
    ret = 0
    if x == 0:
        for a,b,c,d in abcd:
            if que[b-1] - que[a-1]==c:
                ret+=d
        return ret
    for i in range(y,z+1):
        ret = max(ret,dfs(x-1,i,z,que + [i]))
    return ret
 
print(dfs(n,1,m,[]))

Solution (Python):
from collections import deque

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

S=[]
for i in range(Q):
    S.append(list(map(int,input().split())))
sorted(S, key=lambda x: x[-1])

S0=sorted(S, key=lambda x: x[0],reverse=False)

tmp=deque([str(0)])
out=0
while tmp:
    T=tmp.pop()
    Ans=0
    if len(T)<N:
        for i in range(int(T[-1]),M):
            tmp.append(T+str(i))
    else:
        for i in range(Q):
            S0=S[i]
            if int(T[S0[1]-1])-int(T[S0[0]-1])==S0[2]:
                Ans+=S0[3]
        out=max(out,Ans)
print(out)

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

def calc_score():
    res = 0
    for i,j,c,d in qd:
        if a[j-1]-a[i-1] == c:
            res += d
    return res

a = []
score = [-1<<31]

def dfs(i,l,depth):  
    if i == n:
        score[0] = max(score[0],calc_score())
        return
    for j in range(l, m+1):
        a.append(j)
        dfs(i+1,j,depth+1)
        a.pop()

dfs(0,1,0)

print(score[0])

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


A_list = list(itertools.combinations_with_replacement(range(1, M+1), N))
ans = 0
for A in A_list:
    
    sum = 0
    for a, b, c, d in q_list:
        if A[b-1] - A[a-1] == c:
            sum += d
    ans = max(ans, sum)

print(ans)

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

ans = 0
for A in itertools.combinations_with_replacement(range(1,m+1),n):
    tmp = 0
    for a,b,c,d in li:
        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())
pairs = [list(map(int, input().split())) for i in range(q)]
ans = 0

for combo in combinations_with_replacement(range(1, m+1), n):
    tmp = 0
    for pair in pairs:
        if combo[pair[1]-1] - combo[pair[0]-1] == pair[2]:
            tmp += pair[3]
    ans = max(tmp, ans)

print(ans)

Solution (Python):
import sys
from pprint import pprint
import itertools

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

ans = 0
for comb in itertools.combinations_with_replacement(list(range(1, m + 1)), n):
    l = list(comb)
    l.sort()
    tmp = 0

    for a, b, c, d in abcd:
        a -= 1
        b -= 1

        if l[b] - l[a] == c:
            tmp += d

    ans = max(ans, tmp)

print(ans)

Solution (Python):
import itertools




def main():
    N, M, Q = map(int, input().split())
    Q_l = []
    final_score = 0
    for i in range(Q):
        Q_l.append(list(map(int, input().split())))
    for A in itertools.combinations_with_replacement(range(1, M+1), N):
        score = 0
        for q in Q_l:
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                score += q[3]
            final_score = max([final_score, score])
    print(final_score)
main()

Solution (Python):
def digitSum(n):
    
    s = str(n)
    
    array = list(map(int, s))
    
    return sum(array)

def main():
    n,m,q=map(int,input().split())
    data=[]
    ans=0
    for i in range(q):
        a,b,c,d=map(int,input().split())
        data.append([a,b,c,d])
    num=0
    diff=[0]*(n-1)
    res=0
    while num<10**(n-1):
        res=0
        for i in range(q):
            if data[i][2]==sum(diff[data[i][0]-1:data[i][1]-1]):
                res+=data[i][3]
        ans=max(ans,res)
        num+=1
        i=1
        while num<10**n and digitSum(num)>m-1:
            num=(num//10**i)*10**i+10**i
            i+=1
        strDiff=list(str(num).zfill(n-1))
        diff=[int(s) for s in strDiff]
    print(ans)

if __name__=="__main__":
    main()

Solution (Python):



"""
import itertools
s = []
for i in range(N):
    s.append(int(input()))


for i in range(n):
    x,y = [int(j) for j in input().split()]
    dataX.append(x)
    dataY.append(y)

二次元配列
for i in range(n):
    d.append([int(j) for j in input().split()])

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


c = [[j for j in input()] for i in range(h)]

sp.sort(key=lambda x:(x[1], -x[2]))

コレクションのvalue値を取る時
for v in a.values():
        if v%2!=0:
            even = False

二次元配列
road = [[0]*n]*n　こうやって書くと全部更新されるから
road = [[0]*n for i in range(n)]って書くといい

一番回数の多いもの取得
l = collections.Counter(s)
a = l.most_common()[0][0]

forの逆
for i in reversed(range(0, n)):

順列重複あり
ans = list(itertools.product(l, repeat=n))

l_sorted = sorted(l, key=lambda x: x[1], reverse=True)

a_count = collections.Counter(a)
a_list = list(a_count.items())

"""
from fractions import gcd
from functools import reduce



import math
import sys
sys.setrecursionlimit(2000000)




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


a = []
b = []
c = []
d = []
abcd = []
for i in range(q):
    x, y, z, k = [int(j) for j in input().split()]
    abcd.append([x, y, z, k])

s = [0]*n
ans = 0


def dfs(s, num, i):
    if i == n:
        global ans
        total = 0
        for j in range(q):
            if s[abcd[j][1]-1]-s[abcd[j][0]-1] == abcd[j][2]:
                total += abcd[j][3]
        ans = max(ans, total)
        return
    else:
        for j in range(num, m+1):
            s[i] = j
            dfs(s, j, i+1)


dfs(s, 1, 0)
print(ans)

Solution (Python):
import sys
from itertools import combinations_with_replacement

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


def main():
    N, M, Q = map(int, input().split())
    abcd = [None] * Q
    for i in range(Q):
        abcd[i] = tuple(map(int, input().split()))

    ans = 0
    for A in combinations_with_replacement(range(1, M+1), N):
        score = 0
        for q in abcd:
            a, b, c, d = q
            if A[b-1] - A[a-1] == c:
                score += d
        if score > ans:
            ans = score

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def maxim(S,N,M,A,j,ans):
    way=0
    for i in range(j,M+1):
        A.append(i)
        
        if(len(A)==N):
            
            for k in S:
                
                if(A[k[1]-1]-A[k[0]-1]==k[2]):
                    way+=k[3]
                else:
                    pass
                
            if(way>ans):
                ans=way
            else:
                pass
            way=0
            
        else:
            way=maxim(S,N,M,A,i,ans)
            
            
            if(way>ans):
                ans=way
            else:
                pass
            way=0
        A.pop(len(A)-1)
        
    return ans

N,M,Q=map(int,input().split())
S=[]
A=[]
for i in range(Q):
    S.append(list(map(int,input().split())))
ans=0
ans=maxim(S,N,M,A,1,ans)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
n,m,q=map(int,input().split())
a,b,c,d=[0]*q,[0]*q,[0]*q,[0]*q
for i in range(q):
    x,y,z,w=map(int,input().split())
    x-=1
    y-=1
    a[i],b[i],c[i],d[i]=x,y,z,w
ans=0
for v in combinations_with_replacement(range(1,m+1),n):
    res=0
    for i in range(q):
        if v[b[i]]-v[a[i]]==c[i]:
            res+=d[i]
    ans=max(ans,res)
print(ans)

Solution (Python):
import itertools
 
def check(lis, a):
    return sum((i[3] for i in a if lis[i[1]-1]-lis[i[0]-1]==i[2]))
   
n, m, q = map(int, input().split())
a = [[int(i) for i in input().split(" ")] for j in range(q)]
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
    ans = max(ans, check(i, a))
print(ans)

Solution (Python):
def main():
  import itertools
  n,m,q=map(int,input().split())
  l=[tuple(map(int,input().split())) for _ in range(q)]
  a=[i+1 for i in range(m)]
  ans=0
  for i in itertools.combinations_with_replacement(a,n):
    cnt=0
    for j in range(q):
      ll=l[j]
      if i[ll[1]-1]-i[ll[0]-1]==ll[2]:
        cnt+=ll[3]
    ans=ans if ans>=cnt else cnt
    cnt=0
  print(ans)   
if __name__=='__main__':
  main()

Solution (Python):
from itertools import combinations_with_replacement
n,m,q=map(int,input().split())
a,b,c,d=[0]*q,[0]*q,[0]*q,[0]*q
for i in range(q):
    x,y,z,w=map(int,input().split())
    x-=1
    y-=1
    a[i],b[i],c[i],d[i]=x,y,z,w
ans=0
for v in combinations_with_replacement(range(1,m+1),n):
    res=0
    for i in range(q):
        if v[b[i]]-v[a[i]]==c[i]:
            res+=d[i]
    ans=max(ans,res)
print(ans)

Solution (Python):
import itertools

def check(lis, a):
    return sum((i[3] for i in a if lis[i[1]-1]-lis[i[0]-1]==i[2]))
   
n, m, q = map(int, input().split())
a = tuple(tuple(int(i) for i in input().split(" ")) for j in range(q))
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
    ans = max(ans, check(i, a))
print(ans)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n,m,q = map(int,input().split())
item = []
for i in range(q):
    a,b,c,d = map(int,input().split())
    item.append([a,b,c,d])
a = [0 for i in range(11)]
ans = 0
for i1 in range(1,11):
    a[1] = i1
    for i2 in range(i1,m+1):
        a[2] = i2
        for i3 in range(i2,m+1):
            a[3] = i3
            for i4 in range(i3,m+1):
                a[4] = i4
                for i5 in range(i4,m+1):
                    a[5] = i5
                    for i6 in range(i5,m+1):
                        a[6] = i6
                        for i7 in range(i6,m+1):
                            a[7] = i7
                            for i8 in range(i7,m+1):
                                a[8] = i8
                                for i9 in range(i8,m+1):
                                    a[9] = i9
                                    for i10 in range(i9,m+1):
                                        a[10] = i10
                                        s = 0
                                        for e,f,g,h in item:
                                            if a[f] - a[e] == g:
                                                s+=h
                                        ans = max(ans,s)
print(ans)

Solution (Python):
import itertools;n,m,q,*l=map(int,open(0).read().split());print(max(sum(d for a,b,c,d in[l[j*4:j*4+4]for j in range(q)]if i[b-1]-i[a-1]==c)for i in itertools.combinations_with_replacement(range(m),n)))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

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

def dfs(k, x):
    global mx
    if k == n:
        v = 0
        for line in query:
            a, b, c, d = line
            if x[b] - x[a] == c: v += d
        if mx < v: mx = v
    else:
        for i in range(x[-1], m + 1):
            dfs(k + 1, x + [i])
    
mx = 0
dfs(0, [1])

print(mx)

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

    result = 0

    for C in combinations_with_replacement(range(1, M + 1), N):
        tmp = sum([d for a, b, c, d in ABCD if C[b - 1] - C[a - 1] == c])
        result = max(result, tmp)
    print(result)


if __name__ == "__main__":
    main()

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

Solution (Python):
"""
https://atcoder.jp/contests/abc165/tasks/abc165_c

C - Many Requirements /

制約
2 <= N <= 10
1 <= N <= 10
1 <= Q <= 50
"""
from collections import deque

deq = deque()
al = []

def solve(n,m,q,x):

    max = 0

    for a_list in al:
        cnt = 0
        for a in x:
            if a_list[ a[1]-1 ] - a_list[ a[0]-1 ] == a[2]:
                cnt += a[3]
        if max < cnt:
            max = cnt

    return max

if __name__ == '__main__':
    n, m, q = map(int, input().split())
    x = [list(map(int, input().split())) for i in range(q)]

    
    deq.append([1])

    
    while(deq):
        item = deq.popleft()
        if len(item) != n:
            for i in range(item[-1],m+1):
                deq.append(item + [i])
        else:
            al.append(item)

    
    ans = solve(n,m,q,x)

    print(ans)

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

x=list(combinations_with_replacement(ten,n))
for i in range(len(x)):
  y=list(x[i])
  nowans=0
  for j in range(q):
    if y[con[j][1]-1]-y[con[j][0]-1]==con[j][2]:
      nowans+=con[j][3]
  ans=max(ans,nowans)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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 x in combinations_with_replacement(l,n):
    ans1=0
    x=sorted(x)
    for Q in query:
        if x[Q[1]-1]-x[Q[0]-1] == Q[2]:
            ans1 += Q[3]
    ans = max(ans,ans1)
        
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

query = []
num_list = list(range(m))

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

max_score = 0

for i in combinations_with_replacement(num_list, n):
    score = 0
    for a,b,c,d in query:
        if i[b-1] - i[a-1] == c:
            score += d
    if max_score < score:
        max_score = score

print(max_score)

Solution (Python):
import itertools


def resolve():
    N, M, Q = map(int, input().split())
    a = [0 for i in range(Q)]
    b = [0 for i in range(Q)]
    c = [0 for i in range(Q)]
    d = [0 for i in range(Q)]

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

    for seq in itertools.combinations_with_replacement(range(M), N):
        tmp = 0
        for i in range(Q):
            if seq[b[i]] - seq[a[i]] == c[i]:
                tmp += d[i]
        ans = max(ans, tmp)

    print(ans)


resolve()

Solution (Python):
N,M,Q=map(int,input().split())
R=[list(map(int,input().split())) for i in range(Q)]
S=[1]*N
 
def dfs(d):
    if d==N-1:
        t=0
        for a,b,c,d in R:
            if S[b-1]-S[a-1]==c:
                t+=d
        return t
    else:
        m=0
        for i in range(S[d],M+1):
            S[d+1]=i
            m=max(m,dfs(d+1))
        return m

print(dfs(0))

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

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

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

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

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

for i in itertools.combinations_with_replacement(range(1,m+1),n):
    tmp=0
    for j in range(q):
        if i[lis[j][1]] - i[lis[j][0]] == lis[j][2]:
            tmp+=lis[j][3]
    ans=max(ans,tmp)
print(ans)

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

Solution (Python):



N, M, Q = map(int, input().split())
A = [0] * Q
B = [0] * Q
C = [0] * Q
D = [0] * Q
for i in range(Q):
    A[i], B[i], C[i], D[i] = map(int, input().split())

def dfs(L):
    global ans
    if len(L)==N+1:
        tmp = 0
        for i in range(Q):
            if L[B[i]] - L[A[i]] == C[i]:
                tmp += D[i]
        ans = max(tmp, ans)
        return
    else:
        for j in range(L[-1], M+1):
            dfs(L+[j])
    return ans

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

Solution (Python):


from functools import lru_cache

n, m, q = map(int, input().split())
abc2d = {}
a2bcd = {}
for i in range(q):
    a,b,c,d = map(int, input().split())
    a -= 1
    b -= 1
    abc2d[a,b,c] = d
    a2bcd.setdefault(a, [])
    a2bcd[a].append((b,c,d))

a = [0] * n 
best = 0

@lru_cache(2**20)
def value(ind, *tmp_a):
    
    val = 0
    if ind in a2bcd:
        val = value(ind+1, *(item-tmp_a[0] for item in tmp_a[1:]))
        for bb,cc,dd in a2bcd[ind]:            
            if tmp_a[bb-ind] - tmp_a[0] == cc:
                val += dd

    return val

def sub(a,ind):
    global best
    
    if ind==n:
        val = value(0,*a)
        best = max(best, val)
        return
    for v in range(a[ind-1], m):
        a[ind] = v
        sub(a, ind+1)
sub(a, 1)
print(best)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(i)
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        if A[abcd[i][1]-1]-A[abcd[i][0]-1] == abcd[i][2]:
            pointA = pointA + abcd[i][3]
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(i)
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        if A[abcd[i][1]-1]-A[abcd[i][0]-1] == abcd[i][2]:
            pointA = pointA + abcd[i][3]
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

Solution (Python):

from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
l = [list(map(int, input().split())) for i in range(q)]

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





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

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

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

for i in itertools.combinations_with_replacement(ele,n):
    a = list(i)
    d = 0
    for j in range(q):
        if a[x[j][1]-1] - a[x[j][0]-1] == x[j][2]:
            d += x[j][3]
    ans = max(ans,d)
print(ans)

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

def DFS(s):
    if len(s) == N:
        return [s]
    ans = []
    if s == '':
        r = 0
    else:
        r = int(s[-1])
    for i in range(r, M):
        ans.extend(DFS(s+str(i)))
    return ans


V = DFS('')
final_ans = 0
for v in V:
    answer = 0
    A = [int(c)+1 for c in v]
    for a, b, c, d in abcd:
        if int(v[b-1]) - int(v[a-1]) == c:
            answer += d
    final_ans = max(final_ans , answer)
print(final_ans)

Solution (Python):
from itertools import *

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

aaa=[int(i) for i in range(1,m+1)]
a=list(combinations_with_replacement(aaa,n))

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


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):
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

Solution (Python):
import itertools

def check(lis, a):
    return sum([i[3] for i in a if lis[i[1]-1]-lis[i[0]-1]==i[2]])
   
n, m, q = map(int, input().split())
a = [[int(i) for i in input().split(" ")] for j in range(q)]
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
    ans = max(ans, check(i, a))
print(ans)

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

li = [list(map(int,input().split())) for _ in range(Q)]
ans = 0
a = [i for i in range(1,M+1)]

for A in itertools.combinations_with_replacement(a,N):
    tmp = 0
    for l in li:
        if A[l[1]-1] - A[l[0]-1] == l[2]:
            tmp += l[3]
    else:
        ans = max(ans,tmp)
print(ans)

Solution (Python):
import numpy as np
import math
n, m, q = map(int,input().split())
max = 0
j = []
for i in range(q):
    aa,b,c,d = map(int,input().split())
    j.append([aa,b,c,d])

a = [0 for k in range(11)]
for i1 in range(1,11):
    a[1] = i1
    for i2 in range(i1,m+1):
        a[2] = i2
        for i3 in range(i2,m+1):
            a[3] = i3
            for i4 in range(i3,m+1):
                a[4] = i4
                for i5 in range(i4,m+1):
                    a[5] = i5
                    for i6 in range(i5,m+1):
                        a[6] = i6
                        for i7 in range(i6,m+1):
                            a[7] = i7
                            for i8 in range(i7,m+1):
                                a[8] = i8
                                for i9 in range(i8,m+1):
                                    a[9] = i9
                                    for i10 in range(i9,m+1):
                                        a[10] = i10
                                        s = 0
                                        for i in j:
                                            if a[i[1]]-a[i[0]] == i[2]:
                                                s += i[3]
                                        if max < s:
                                            max = s

print(max)