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

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

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

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

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

ans = 0
for item in A:
    tmp = 0
    for req in abcd:
        if item[req[1]-1] - item[req[0]-1] == req[2]:
            tmp+= req[3]
    ans = max(ans,tmp)
print(ans)

Solution (Python):

import collections
import copy
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())

dq = collections.deque([])
list_val_a = []

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

while(len(dq) > 0):
    tmp_la = dq.popleft()
    
    last_ai = tmp_la[-1]
    
    for j in range(last_ai, m+1):
        t_tmp_la = copy.copy(tmp_la)
        
        t_tmp_la.append(j)
        if len(t_tmp_la) >= n:
            list_val_a.append(t_tmp_la)
        else:
            dq.append(t_tmp_la)

max_score = 0
for lai in list_val_a:
    tmp_score = 0
    for j in range(q):
        if lai[b[j]-1]-lai[a[j]-1] == c[j]:
            tmp_score += d[j]
    if tmp_score > max_score:
        max_score = tmp_score

print(max_score)

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

    res = []
    dfs(list(), res,M,N)

    mx = 0
    for p in res:
        point = 0
        for a,b,c,d in req:
            if p[b] - p[a] == c:
                point += d
        mx = max(point, mx)
    
    print(mx)


def dfs(now,res,M,N):
    if len(now) == N:
        res.append(now)
        return
    start = 1
    if len(now) != 0:
        start = now[-1]

    if start > M:
        return

    for i in range(start,M+1):
        nxt = now[:]
        nxt.append(i)
        dfs(nxt, res,M,N)
    
if __name__ == '__main__':
    solve()

Solution (Python):
import itertools
N,M,Q=map(int,input().split())
a=[]
for _ in range(Q):
    b=list(map(int,input().split()))
    a.append(b)
s=[]
for i in range(1,M+1):
    s.append(i)
alist=list(itertools.combinations_with_replacement(s, r=N+1))


tmp=0
for j in alist:
    s=0
    for i in range(Q):
        j=list(j)
        if j[a[i][1]]-j[a[i][0]]==a[i][2]:
            s+=a[i][3]
    tmp=max(tmp,s)
print(tmp)

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

A = [1]*N
ans = 0
while A[0] == 1:
    tmp = 0
    for a,b,c,d in ABCD:
        if A[b-1]-A[a-1]==c: tmp += d
    ans = max(ans,tmp)
    A[N-1] += 1
    for i in range(N-1,-1,-1):
        if A[i]>M:
            A[i-1] += 1
            for j in range(i,N):
                A[j] = A[i-1]
print(ans)

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

import itertools
a = []
for i in range(q):
    a.append(input().split())

suma = sum([int(aa[3]) for aa in a])

ans = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n-1):
    ans_ = 0
    v = [0]+list(v)
    for j in range(q):
        if v[int(a[j][1])-1] - v[int(a[j][0])-1] == int(a[j][2]):
            ans_ += int(a[j][3])
    if ans_ > ans:
        ans = ans_
        if ans == suma:
            break
print(ans)

Solution (Python):
n,m,q = map(int,input().split())
a = [0 for i in range(q)]
ans = 0
for i in range(q):
    a[i] = list(map(int,input().split()))
for i in range(1,m+1):
    for j in range(i,m+1):
        for k in range(j,m+1):
            for s in range(k,m+1):
                for p in range(s,m+1):
                    for l in range(p,m+1):
                        for e in range(l,m+1):
                            for u in range(e,m+1):
                                for v in range(u,m+1):
                                    b = [1,i,j,k,s,p,l,e,u,v]
                                    nn = 0
                                    for h in range(q):
                                        if b[a[h][1]-1] - b[a[h][0]-1] == a[h][2]:
                                            nn += a[h][3]
                                    ans = max(ans,nn)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

def main():
    N,M,Q=map(int,input().split())
    abcd_lst=[list(map(int,input().split())) for _ in range(Q)]
    num=list(range(1,M+1))
    ans=0
    for A in list(combinations_with_replacement(num, N)):
        tmp=0
        for ai,bi,ci,di in abcd_lst:
            if A[bi-1]-A[ai-1]==ci:
                tmp+=di
                ans=max(ans,tmp)
    print(ans)

main()

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
 
import itertools
n,m,q=n1()
s=n3(q)
 
Alist=list(itertools.combinations_with_replacement(range(1,m+1), n))
ans=0
for A in Alist:
    tmp=0
    for r in s:
        if A[r[1]-1]-A[r[0]-1]==r[2]:
            tmp+=r[3]
    ans=max(ans,tmp)
print(ans)

Solution (Python):






import sys,bisect,math,itertools
sys.setrecursionlimit(15000)

N,M,Q = map(int,sys.stdin.readline().split())
qs = [list(map(int,sys.stdin.readline().split())) for _ in range(Q)]
com = list(itertools.combinations_with_replacement(range(1,M+1), N))
m = 0


for A in com:
    s = 0
    for a,b,c,d in qs:
        if A[b-1] - A[a-1] == c:
            s += d
    m = max(m,s)
print(m)

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
 
import itertools
n,m,q=n1()
s=n3(q)

Alist=list(itertools.combinations_with_replacement(range(1,m+1), n))
ans=0
for A in Alist:
    tmp=0
    for r in s:
        if A[r[1]-1]-A[r[0]-1]==r[2]:
            tmp+=r[3]
    ans=max(ans,tmp)
print(ans)

Solution (Python):
import itertools

n,m,Q = map(int,input().split())
q = []
number = [i for i in range(1,m+1)]
kouho = itertools.combinations_with_replacement(number, n)
for i in range(Q):
    q.append(list(map(int,input().split())))

ans = []

for i in kouho:
    tmpans = 0
    for j in q:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            tmpans +=j[3]
    ans.append(tmpans)

print(max(ans))

Solution (Python):
N, M, Q = map(int,input().split())
L = []
def dfs(List):
    if len(List) == N - 1:
        L.append([0] + List)
    else:
        k = List[-1]
        for i in range(M-k):
            n_L = List[:]
            n_L.append(k+i)
            dfs(n_L)
for i in range(M):
    dfs([i])

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

MAX = 0
for l in L:
    cnt = 0
    for a, b, c, d in Qs:
        if l[b-1] - l[a-1] == c:
            cnt += d
    MAX = max(cnt, MAX)
print(MAX)

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

A = itertools.combinations_with_replacement([q for q in range(1,(M+1))], N)
count=[]
for a in A:
    tmp=0
    for l in L:
        
        if a[l[1]-1] - a[l[0]-1] == l[2]:
            tmp+=l[3]
    count.append(tmp)

print(max(count))

Solution (Python):
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)]
v = [0 for i in range(n)]
for i in range(q):
	a[i],b[i],c[i],d[i]=map(int,input().split())
import itertools
maxcnt = 0
for pair in itertools.combinations_with_replacement(range(1,m+1), n):
	cnt = 0
	for i in range(q):
		if pair[b[i]-1]-pair[a[i]-1] == c[i]:
			cnt += d[i]
	if maxcnt < cnt:
		maxcnt = cnt
print(maxcnt)

Solution (Python):
import itertools
F=list(map(int, input().split()))
s = [list(map(int,input().split())) for i in range(int(F[2]))]


flist=list(range(1,F[1]+1))
comb=list(itertools.combinations_with_replacement(flist,F[0]))
result=[0]*len(comb)


for i,a in enumerate(comb):
    for row in s:
        if a[row[1]-1]-a[row[0]-1]==row[2]:
            result[i]+=row[3]

print(max(result))

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

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




numbers = []
count = 0
def dfs(num_list):
    last = num_list[-1]
    if len(num_list) == N:
        numbers.append(num_list)
    else:
        for i in range(last, M+1):
            tmp = num_list[:]
            tmp.append(i)
            dfs(tmp)

for i in range(1, M+1):
    dfs([i])

_max = 0
for number in numbers:
    _sum = 0
    for i in range(Q):
        if number[A[i][1] - 1] - number[A[i][0] - 1] == A[i][2]:
            _sum += A[i][3]
    if _max < _sum:
        _max = _sum
print(_max)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,m,q = map(int,readline().split())
lst1 = []
for i in range(q):
    a,b,c,d = map(int,readline().split())
    lst1.append([a-1,b-1,c,d])

def func(lst):
    res = 0
    for a,b,c,d in lst1:
        if lst[b]-lst[a] == c:
            res += d
    return res
lst2 = []
def func2(lst,now,num):
    if now == -1:
        lst2.append(lst)
        return
    else:
        for i in range(1,num+1):
            res = lst.copy()
            res[now] = i
            func2(res,now-1,i)

func2([1]*n,n-1,m)

def dfs(lst):
    ans = 0
    for i in lst2:
        ans = max(ans,func(i))
    return ans

print(dfs([1]*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())

s = [1] * n

num = n-1

ans = 0
pre_ans = 0
t = 0

while(s[0] != m ):

    for i in range(q):
        if s[b[i]-1] - s[a[i]-1]  == c[i]:
            ans += d[i]
    
    
    ans = max(pre_ans,ans)
    pre_ans = ans
    

    ans = 0

    if s[num] < m:
        s[num] += 1
    elif num > 0 :
        num -= 1
        temp = s[num]+1
        if temp < m:
            for i in range(num,n):
                s[i] = temp
        else:
            while(s[num] == m):
                num -= 1
            temp = s[num]+1
            for i in range(num,n):
                s[i] = temp

        num = n-1

for i in range(q):
    if s[b[i]-1] - s[a[i]-1]  == c[i]:
        ans += d[i]

ans = max(pre_ans,ans)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
inf = 10**17
mod = 10**9 + 7


n,m,q=MI()
strs=[[[] for i in range(m)] for i in range(n)]
for i in range(m):
    strs[0][i].append(str(i))

for i in range(n-1):
    for j in range(m):
        for k in range(j,m):
            for s in strs[i][k]:
                strs[i+1][j].append(str(j)+s)


L=strs[n-1]

lis=[]
for i in range(m):
    for s in L[i]:
        lis.append(s)

mx=0
zyou=[LI() for i in range(q)]
for u in lis:
    po=0
    for k in range(q):
        a,b,c,d=zyou[k]
        if int(u[b-1])-int(u[a-1])==c:
            po+=d
    if po>mx:
        mx=po
print(mx)

Solution (Python):

N, M, Q = [eval(v) for v in input().strip().split(" ")]

a_list, b_list, c_list, d_list = [], [], [], []

for k in range(Q):
    a, b, c, d = [eval(v) for v in input().strip().split(" ")]
    a_list.append(a)
    b_list.append(b)
    c_list.append(c)
    d_list.append(d)

max_value = 0

def calc_score(complete_A):
    score = 0
    for i in range(Q):
        if complete_A[b_list[i]] - complete_A[a_list[i]] == c_list[i]:
            score += d_list[i]
    return score

def search(A, M, depth, a_list, b_list, c_list, d_list, max_value, count):
    
    
    
    
    if depth > 0:
        last_A = A[-1]
        for k in range(last_A, M + 1):
            
            
            next_A = A + [k]
            max_value = search(next_A, M, depth - 1, a_list, b_list, c_list, d_list, max_value, count)
    else:
        value = calc_score(A[1:])
        if value > max_value:
            max_value = value
    return max_value

count = 0
A = [1]
print(search(A, M, N + 1, a_list, b_list, c_list, d_list, max_value, count))

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

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
combs = list(cwr(range(1,M+1),N))
ans = [0]*(len(combs))
for i,comb in enumerate(combs):
    for a,b,c,d in zip(A,B,C,D):
        if comb[b-1] - comb[a-1] ==c:
            ans[i] += d
print(max(ans))

Solution (Python):
from itertools import combinations_with_replacement

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

ps = list(combinations_with_replacement(range(1, m+1), n-1))
n_p = len(ps)

for i in range(n_p):
    ns = [1] + list(ps[i])
    ss = 0
    for j in range(q):
        k = l[j]
        if ns[k[1]-1] - ns[k[0]-1] == k[2]:
            ss += k[3]
    s.append(ss)

print(max(s))

Solution (Python):
from itertools import*
n,m,q,*L=map(int,open(0).read().split())
print(max(sum(d*(x[b-1]-x[a-1]==c)for a,b,c,d in zip(*[iter(L)]*4))for x in combinations_with_replacement(range(m),n)))

Solution (Python):
import itertools

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

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

M_list = [_ for _ in range(1, M+1)]
A_list = []
for pair in itertools.combinations_with_replacement(M_list, N):
    A_list.append(pair)

sum_list = []
for i in range(len(A_list)):
    sum_list_ = []
    for j in range(Q):
        if A_list[i][b[j]-1] - A_list[i][a[j]-1] == c[j]:
            sum_list_.append(d[j])
    sum_list.append(sum(sum_list_))

print(max(sum_list))

Solution (Python):


n, m, q = map(int, input().split())
dict_list = {}
key_list = []
for i in range(0, q):
    a, b, c, d =  map(int, input().split())
    key = tuple([a, b, c])
    key_list.append(key)
    dict_list[key] = d

def func(i, k, a_list):
    if k == 0:
        temp_score = 0 
        for key in key_list:
            if key[2] == a_list[key[1]-1] - a_list[key[0]-1]:
                temp_score += dict_list[key]
        return temp_score
    k = k - 1
    max_score = 0
    for j in range(i, m + 1):
        max_score = max(func(j, k, a_list + [j]), max_score)
    return max_score
score = 0
for i in range(1, m + 1):
    score = max(func(i, n - 1, [i]), score)
print(score)

Solution (Python):
from collections import defaultdict, deque
from itertools import combinations

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

D = defaultdict(dict)
for a, b, c, d in ABCD:
  D[b-1][(a-1, c)] = d

for j in range(N-1, 0, -1):
  for x, y in combinations(D[j], 2):
    if x[0] == y[0]:
      continue
    if x[0] > y[0]:
      x, y = y, x
    if x[1] > y[1]:
      if (x[0], x[1] - y[1]) not in D[y[0]]:
        D[y[0]][(x[0], x[1] - y[1])] = 0

for j in range(1, N):
  if (j-1, 0) not in D[j]:
    D[j][(j-1, 0)] = 0

que = deque([([1], 0)])
memo = set([(1,)])
ans = 0
while que:
  L, score = que.popleft()
  if len(L) == N:
    if score > ans:
      ans = score
  else:
    for a, c in D[len(L)]:
      l = L[a] + c
      if L[-1] <= l <= M:
        x = tuple(L + [l])
        if x not in memo:
          memo.add(x)
          que.append((L + [l], score + sum(d for v, d in D[len(L)].items() if l - L[v[0]] == v[1])))
print(ans)

Solution (Python):
import math
import itertools

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

ans_list=list()
ans=0
xans=0

lis =  list(range(1,m+1))
for team in itertools.combinations_with_replacement(lis,n):
  
    for i in range(q):
      
      lix=li[i].split()
      
      if team[int(lix[1])-1]-team[int(lix[0])-1]==int(lix[2]):
        ans=int(lix[3])
        xans+=ans
        
       
        
    ans_list+=[xans]
    xans=0
      
    
    
print(max(ans_list))

Solution (Python):
import itertools

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

seleted = [x for x in range(1, m+1)]
A_list = list(itertools.combinations_with_replacement(seleted, n))
for A in A_list:
    tmp = 0
    for q in q_list:
        if A[q[1]-1] - A[q[0]-1] == q[2]:
            tmp += q[3]
    ans = max(ans, tmp)
print(ans)

Solution (Python):
def count_up(x,n):
  x[n]+=1
  if x[n]>m:
    count_up(x,n-1)
    x[n]=x[n-1]
  return
n,m,q=map(int,input().split())
abcd=[map(int,input().split()) for _ in range(q)]
a,b,c,d=[list(i) for i in zip(*abcd)]
x=[1,1,1,1,1,1,1,1,1,1,1]
ans=0
if m==1:
  temp=0
  for i in range(q):
    if x[b[i]]-x[a[i]]==c[i]:
      temp+=d[i]
  ans=max(ans,temp)
else:
  while x[1]==1:
    temp=0
    for i in range(q):
      if x[b[i]]-x[a[i]]==c[i]:
       temp+=d[i]
    ans=max(ans,temp)
    count_up(x,n)
print(ans)

Solution (Python):
import itertools

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


hoge = []
kouho = list(range(1,M+1))
for i in itertools.combinations_with_replacement(kouho, N):
    hoge.append(i)

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

Solution (Python):
import itertools as itr
N,M,Q = map(int,input().split())
sheat = list()
for i in range(Q):
    sheat.append(list(map(int,input().split())))

comb = tuple(itr.combinations_with_replacement(range(1,M+1),N))
Maxsum = 0


sumd = 0
for i in range(len(comb)):
    ansum = 0
    for j in range(Q):
        if(comb[i][sheat[j][1]-1]-comb[i][sheat[j][0]-1] == sheat[j][2]):
            ansum += sheat[j][3]
           
    if(Maxsum < ansum):
        Maxsum = ansum
    
print(Maxsum)

Solution (Python):
N, M, Q = map(int, input().split())
a = []
for i in range(Q):
	a.append(list(map(int, input().split())))
P = 0
for i in range(1,M+1):
	for p in range(i,M+1):
		for j in range(p,M+1):
			for k in range(j,M+1):
				for m in range(k,M+1):
					for n in range(m,M+1):
						for o in range(n,M+1):
							for q in range(o,M+1):
								for r in range(q,M+1):
									for s in range(r,M+1):
										l=[i,p,j,k,m,n,o,q,r,s]
										ans=0
										for b in range(Q):
											if l[a[b][1]-1]-l[a[b][0]-1]==a[b][2]:
												ans+=a[b][3]
										P=max(P,ans)
print(P)

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

def dfs( s , n ):
    i = 0
    if len(s) >= n:
        ans.append(s)
        return
    else:
        a = int(s[len(s)-1])
        for j in range(m-a):
            dfs( s + str(a+j) , n )
                
ans = []
for j in range(m):
    ak = str(j)
    dfs( ak , n )
    
tans = 0
for i in range(len(ans)):
    an = 0
    s = [0]*(n+1)
    for j in range(n):
        s[j+1] = int(ans[i][j])
    for j in range(q):
        if s[b[j]] - s[a[j]] == c[j]:
            an += d[j]
                  
    tans = max(tans,an)
    
print(tans)

Solution (Python):
import itertools
from collections import deque


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

    m = list(range(1, M + 1))
    As = []
    A = [0 for _ in range(N)]
    for i in m:
        
        A[0] = i
        sub_m = m[i - 1:]
        for j in sub_m:
            
            A[1] = j
            sub_m = m[j - 1:]

            if N == 2:
                As.append(A)
                A = A.copy()
                continue

            for k in sub_m:
                
                A[2] = k
                sub_m = m[k - 1:]

                if N == 3:
                    As.append(A)
                    A = A.copy()
                    continue

                for n in sub_m:
                    
                    A[3] = n
                    sub_m = m[n - 1:]

                    if N == 4:
                        As.append(A)
                        A = A.copy()
                        continue

                    for o in sub_m:
                        
                        A[4] = o
                        sub_m = m[o - 1:]

                        if N == 5:
                            As.append(A)
                            A = A.copy()
                            continue

                        for p in sub_m:
                            
                            A[5] = p
                            sub_m = m[p - 1:]

                            if N == 6:
                                As.append(A)
                                A = A.copy()
                                continue

                            for q in sub_m:
                                
                                A[6] = q
                                sub_m = m[q - 1:]

                                if N == 7:
                                    As.append(A)
                                    A = A.copy()
                                    continue

                                for r in sub_m:
                                    
                                    A[7] = r
                                    sub_m = m[r - 1:]

                                    if N == 8:
                                        As.append(A)
                                        A = A.copy()
                                        continue

                                    for s in sub_m:
                                        
                                        A[8] = s
                                        sub_m = m[s - 1:]

                                        if N == 9:
                                            As.append(A)
                                            A = A.copy()
                                            continue

                                        for t in sub_m:
                                            
                                            A[9] = t
                                            sub_m = m[t - 1:]

                                            As.append(A)
                                            A = A.copy()

    ans = 0
    for A in As:
        
        score = 0
        for i, l in enumerate(ls):
            
            if A[l[1] - 1] - A[l[0] - 1] == l[2]:
                score += l[3]
        if score >= ans:
            ans = score

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import copy

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)

seq = [[] for i in range(n)]

for i in range(1, m + 1):
  seq[0].append([i])

for i in range(1, n):
  for j in range(len(seq[i - 1])):
    s = copy.copy(seq[i - 1][j])
    s.append(0)
    for k in range(s[-2], m + 1):
      s[-1] = k
      s2 = copy.copy(s)
      seq[i].append(s2)

score = [0 for i in range(len(seq[n - 1]))]
for i in range(len(seq[n - 1])):
  for j in range(q):
    if seq[n - 1][i][b[j] - 1] - seq[n - 1][i][a[j] - 1] == c[j]:
      score[i] += d[j]

print(max(score))

Solution (Python):
n, m, q = input().split()
n = int( n )
m = int( m )
q = int( q )
a = []
b = []
c = []
d = []
for i in range( int( q ) ):
    a_i, b_i, c_i, d_i = input().split()
    a.append( int( a_i ) )
    b.append( int( b_i ) )
    c.append( int( c_i ) )
    d.append( int( d_i ) )

array = [ 1 ] * n
array_list = [ array ]

i = 0
while True:
    for k in range( array_list[i][-1], m + 1 ):
        array_list.append( array_list[i][1:] + [ k ] )
        
    i = i + 1
    if len( array_list ) > 100000:
        break

max_score = 0
for array in array_list:
    score = 0
    for i in range( q ):
        
        if array[b[i]-1] - array[a[i]-1] == c[i]:
            score = score + d[i]
    if max_score < score:
        max_score = score

print( max_score )

Solution (Python):
import itertools as it
 
n,m,q = map(int, input().split())
l = [list(map(int, input().split())) for i in range(q)]
ans = 0
 
for v in list(it.combinations_with_replacement(range(1,m+1),n)):
    num = 0
    for w in l:
        x,y,z = w[0]-1,w[1]-1,w[2]
        if v[y] - v[x] == z:
            num += w[3]
    if num > ans:
        ans = num
print(ans)

Solution (Python):
import itertools
F=list(map(int, input().split()))
s = [list(map(int,input().split())) for i in range(int(F[2]))]


flist=list(range(1,F[1]+1))
comb=list(itertools.combinations_with_replacement(flist,F[0]))
result=[0]*len(comb)


for i,a in enumerate(comb):
    for row in s:
        if a[row[1]-1]-a[row[0]-1]==row[2]:
            result[i]+=row[3]

print(max(result))

Solution (Python):
def rec(L):
    return sum([d for a, b, c, d in X if L[b] - L[a] == c]) if len(L) > N else max([rec(L + [i]) for i in range(L[-1], M)])

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

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

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

A_score = 0
for A in A_possible:
    A_sum = 0
    for i in range(Q):
        A_test = A[a_b_c_d[i][1]-1] - A[a_b_c_d[i][0]-1]
        if A_test == a_b_c_d[i][2]:
            A_sum += a_b_c_d[i][3]
    if A_score < A_sum:
        A_score = A_sum
print(A_score)

Solution (Python):
import itertools

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

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

    seq = [i for i in range(1, min(11, M+1))]

    max_point = 0

    for e in list(itertools.combinations_with_replacement(seq, N)):
        point = 0
        for i in range(Q):
            if e[b[i]-1] - e[a[i]-1] == c[i]:
                point += d[i]
        if point >= max_point:
            max_point = point

    print(max_point)


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
n,m,q = map(int,input().split())
group = []
for i in range(q):
  w,x,y,z = map(int,input().split())
  v = (w,x,y,z)
  group.append(v)

max_value = max(group[i][3] for i in range(len(group)))



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

for v in h:
    array.append(v)



for j in range(len(array)):
  total = 0
  for i in range(q):
    if array[j][group[i][1]-1] - array[j][group[i][0]-1] == group[i][2]:
      total += group[i][3]
  if total >= max_value:
    max_value = total  

print(max_value)

Solution (Python):
from itertools import combinations_with_replacement
import sys
input = sys.stdin.readline
N,M,Q=(int(x) for x in input().rstrip('\n').split())
ress=[x for x in combinations_with_replacement(range(1,M+1),N)]
res_dict = {}
for i in range(Q):
  res_dict[i] = [int(x) for x in input().rstrip('\n').split()]
res_s = []
for i in ress:
  match=0
  for x in range(Q):
    if i[res_dict[x][1]-1]-i[res_dict[x][0]-1]==res_dict[x][2]:
      match += res_dict[x][3]
  res_s.append(match)
print(max(res_s))

Solution (Python):
def rec(L):
    return sum([d for a, b, c, d in X if L[b] - L[a] == c]) if len(L) > N else max([rec(L + [i]) for i in range(L[-1], M)])

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

Solution (Python):
import itertools

n, m, q = map(int, input().split(' '))
a = [list(map(int, input().split(' '))) for i in range(q)]
b = list(itertools.combinations_with_replacement(range(1, m+1), n))
c = 0
d = 0
for i in range(len(b)):
	for j in range(len(a)):
		if b[i][(a[j][1]-1)] - b[i][(a[j][0]-1)] == a[j][2]:
			c += a[j][3]
	if d <= c:
		d = c
	c = 0
print(d)

Solution (Python):
from itertools import *

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

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

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

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

Solution (Python):
rd = dict()
rd[0] = 0
def check(x):
    r = 0
    for a, b, c, d in X:
        if x[b-1]-x[a-1] == c:
            r += d
    return r

def dfs(x, l):
    if len(x) == N:
        rd[0] = max(rd[0], check(x))
        return
    for i in range(l, M+1):
        dfs(x+[i], i)
N, M, Q = map(int, input().split())
X = []
for _ in range(Q):
    a, b, c, d = map(int, input().split())
    X.append((a, b, c, d))
dfs([], 1)
print(rd[0])