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

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

ans = 0
V = f('')
for s in V:
    A = [int(c) + 1 for c in s]
    t = 0
    for a, b, c, d in zz:
        if A[b - 1] - A[a - 1] == c:
            t += d
    ans = max(ans, t)
print(ans)

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




 
masterlist = []
minilist = [0] * n
i = 0
 
 
def saiki(before_a,i,minilist):
    if i > n-1:
        masterlist.append([minilist[j] for j in range(n)])
        return
    for Ai in range(before_a,m+1):
        minilist[i] = Ai
        saiki(Ai,i+1,minilist)
 


 
before_a = 1
saiki(before_a,i,minilist)
 


 
dmax = 0
 
for a_mini in masterlist:
    dtemp = 0
    for ql_mini in ql:
        if a_mini[ql_mini[1]-1] - a_mini[ql_mini[0]-1] == ql_mini[2]:
            dtemp += ql_mini[3]
        else:
            pass
    if dtemp >= dmax:
        dmax = dtemp
 
print(dmax)

Solution (Python):
from itertools import combinations_with_replacement
def score_check(A,L):
    point = 0
    for a, b, c, score in L:
        temp = A[b - 1] - A[a - 1]
        if temp == c:point += score

    return point

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

num = 0
nums = list(range(1, m + 1))
for i in combinations_with_replacement(nums, n):
    
    temp = score_check(i, l)
    if num < temp:num = temp
print(num)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]





N, M, Q = mi()
abcd = li2(Q)

'''
a = '0' * N
m = str(M-1)
l = [a]

while True:
    i = N-1
    while i >= 0 and a[i] == m:
        i -= 1
    if i == -1:
        break
    a = list(a)
    num = str(int(a[i]) + 1)
    a[i] = num
    if i != N-1:
        for j in range(i+1, N):
            a[j] = num
    tmp = ''.join(a)
    l.append(tmp)
'''

def dfs(num, length):
    if length == N:
        l.append(num)
        return
    last = int(num[-1])
    for i in range(last, M):
        dfs(num + str(i), length+1)




from collections import deque
keta_list = [str(i) for i in range(M)]
l = deque([str(i) for i in range(M)])

while len(l[0]) <= N:
    num = l.popleft()
    last = int(num[-1])
    for tail in keta_list[last:]:
        l.append(num + tail)

ans = 0
for num in l:
    cnt = 0
    num = list(map(int, list(num)))
    for i in range(Q):
        if num[abcd[i][1]-1] - num[abcd[i][0]-1] == abcd[i][2]:
            cnt += abcd[i][3]
    ans = max(ans, cnt)

print(ans)

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

ans = 0
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):
                                        s = [0, a, b, c, d, e, f, g, h, i, j]
                                        cnt = 0
                                        for w, x, y, z in abcd:
                                            if s[x] - s[w] == y:
                                                cnt += z
                                        ans = max(cnt, ans)
print(ans)

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

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

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,m,q = map(int,input().split())
E = [tuple(map(int,input().split())) for i in range(q)]
A = list(itertools.combinations_with_replacement(range(1,m+1),n))
for comb in A:
  tmp = 0
  for a,b,c,d in E:
    a-=1;b-=1
    if comb[b]-comb[a] == c:
      tmp += d
  ans = max(ans,tmp)
print(ans)

Solution (Python):
import itertools
import numpy as np


def judge(li, T):
    answer = 0
    for i in range(Q):
        a, b, c, d = T[i]
        if li[b - 1] - li[a - 1] == c:
            answer += d
            
    return answer


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


num = list(np.arange(M) + 1)


for tup in itertools.combinations_with_replacement(num, N):
    
    

    li = list(tup)
    f = judge(li, T)
    ans = max(ans, f)
print(ans)

Solution (Python):
import itertools

n, m, q = map(int, input().split())
l = []
ans = 0
ref = 0

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

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

print(ans)

Solution (Python):
def main():
    import itertools
    n,m,q = map(int,input().split(" "))
    ops = []
    for i in range(q):
        ops.append(list(map(int,input().split(" "))))
    
    ans = 0
    for A in itertools.combinations_with_replacement(range(1,m+1),n):
        temp = 0
        for op in ops:
            if A[op[1]-1] - A[op[0]-1] == op[2]:
                temp+=op[3]
        ans = max(ans,temp)
    
    print(ans)
main()

Solution (Python):

import sys
from collections import deque
import copy

N,M,Q=map(int, sys.stdin.readline().split())
X=[ map(int, sys.stdin.readline().split()) for _ in range(Q) ]

qq=deque()
for m in range(1,M+1):
    q=deque()
    q.append([m])
    while q:
        if N<len(q[0]):
            qq+=q
            break
        fro=q.popleft()
        last=fro[-1]
        for to in range(last,M+1):
            q.append(fro+[to])

ans=float("-inf")
for L in qq:
    temp_ans=0
    for a,b,c,d in X:
        if L[b-1]-L[a-1]==c:
            temp_ans+=d
    ans=max(ans,temp_ans)
print ans

Solution (Python):
from pprint import pprint

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

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






masterlist = []
minilist = [0] * n
i = 0


def saiki(before_a,i,minilist):
    if i > n-1:
        masterlist.append([minilist[j] for j in range(n)])
        return
    for Ai in range(before_a,m+1):
        minilist[i] = Ai
        saiki(Ai,i+1,minilist)




before_a = 1
saiki(before_a,i,minilist)




dmax = 0

for a_mini in masterlist:
    dtemp = 0
    for ql_mini in ql:
        if a_mini[ql_mini[1]-1] - a_mini[ql_mini[0]-1] == ql_mini[2]:
            dtemp += ql_mini[3]
        else:
            pass
    if dtemp >= dmax:
        dmax = dtemp

print(dmax)

Solution (Python):





















import itertools
def combination_with_replace_all(seq, choice):
    return list(itertools.combinations_with_replacement(seq, choice))

n,m,q = map(int,input().split())
query = []
ans = -1
for i in range(q):
    a,b,c,d = map(int,input().split())
    query.append((a,b,c,d))
cond = combination_with_replace_all([i for i in range(1,m+1)], n)
for i in range(len(cond)):
    cond_ar = cond[i]
    cnt = 0
    for j in range(q):
        if cond_ar[query[j][1]-1] - cond_ar[query[j][0]-1] == query[j][2]:
            cnt += query[j][3]
    ans = max(ans,cnt)
print(ans)

Solution (Python):
import sys
import itertools

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


def main():
    N, M, Q = map(int, readline().split())
    qlist = []
    for _ in range(Q):
        qlist.append(tuple(map(int, readline().split())))

    iters = itertools.combinations_with_replacement(range(1, M+1), N)
    score = []
    for i in iters:
        stmp = 0
        for q in qlist:
            a = q[0]-1; b = q[1]-1
            if i[b] - i[a] == q[2]:
                stmp += q[3]
        score.append(stmp)
    
    ans = max(score)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,m,q = map(int,input().split())
E = [tuple(map(int,input().split())) for i in range(q)]
A = list(itertools.combinations_with_replacement(range(1,m+1),n))
for comb in A:
  tmp = 0
  for a,b,c,d in E:
    a-=1;b-=1
    if comb[b]-comb[a] == c:
      tmp += d
  ans = max(ans,tmp)
print(ans)

Solution (Python):


import sys














from itertools import combinations, combinations_with_replacement








def main():
    mod = 1000000007                
    inf = float('inf')              
    inf = 2 ** 64 - 1               
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())


    def calc_score(arr):
        current_score = 0
        for i, j, dist, score in L:
            if arr[j] - arr[i] == dist:
                current_score += score
        return current_score
    
    
    n, m, q = mi()
    L = []
    for _ in range(q):
        a, b, c, d = mi()
        L.append([a-1, b-1, c, d])
    
    ans = 0
    for number_array in combinations_with_replacement(range(1, m + 1), n):
        ans = max(ans, calc_score(number_array))

    print(ans)
                         

if __name__ == "__main__":
    main()

Solution (Python):
n, m, p = map(int, input().split())
A = []
B = []
C = []
D = []
maximum = 0
for i in range(p):
    a, b, c, d = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)
for z in range(m):
    for y in range(z, m):
        if n > 2:
            for x in range(y, m):
                if n > 3:
                    for w in range(x, m):
                        if n > 4:
                            for v in range(w, m):
                                if n > 5:
                                    for u in range(v, m):
                                        if n > 6:
                                            for t in range(u, m):
                                                if n > 7:
                                                    for s in range(t, m):
                                                        if n > 8:
                                                            for r in range(s, m):
                                                                if n > 9:
                                                                    for q in range(r, m):
                                                                        L = [z, y, x, w, v, u, t, s, r, q]
                                                                        a = 0
                                                                        for l in range(p):
                                                                            if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                                                a += D[l]
                                                                        maximum = max(maximum, a)
                                                                else:
                                                                    L = [z, y, x, w, v, u, t, s, r]
                                                                    a = 0
                                                                    for l in range(p):
                                                                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                                            a += D[l]
                                                                    maximum = max(maximum, a)
                                                        else:
                                                            L = [z, y, x, w, v, u, t, s]
                                                            a = 0
                                                            for l in range(p):
                                                                if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                                    a += D[l]
                                                            maximum = max(maximum, a)
                                                else:
                                                    L = [z, y, x, w, v, u, t]
                                                    a = 0
                                                    for l in range(p):
                                                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                            a += D[l]
                                                    maximum = max(maximum, a)
                                        else:
                                            L = [z, y, x, w, v, u]
                                            a = 0
                                            for l in range(p):
                                                if L[B[l]-1]-L[A[l]-1] == C[l]:
                                                    a += D[l]
                                            maximum = max(maximum, a)
                                else:
                                    L = [z, y, x, w, v]
                                    a = 0
                                    for l in range(p):
                                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                                            a += D[l]
                                    maximum = max(maximum, a)
                        else:
                            L = [z, y, x, w]
                            a = 0
                            for l in range(p):
                                if L[B[l]-1]-L[A[l]-1] == C[l]:
                                    a += D[l]
                            maximum = max(maximum, a)
                else:
                    L = [z, y, x]
                    a = 0
                    for l in range(p):
                        if L[B[l]-1]-L[A[l]-1] == C[l]:
                            a += D[l]
                    maximum = max(maximum, a)
        else:
            L = [z, y]
            a = 0
            for l in range(p):
                if L[B[l]-1]-L[A[l]-1] == C[l]:
                    a += D[l]
            maximum = max(maximum, a)
print(maximum)

Solution (Python):
from itertools import combinations_with_replacement

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

comb = list(combinations_with_replacement(range(1,M+1),N))

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

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

print(ans)

Solution (Python):
n, m, q = map(int, input().split())   
a = []
for i in range(10):
  a.append(1)
max = 0
inp = []
for i in range(q):
  ta,tb,tc,td = map(int,input().split())
  inp.append((ta,tb,tc,td))
for i1 in range(1,m+1):
  a[0] = i1
  for i2 in range(i1,m+1):
    a[1] = i2
    for i3 in range(i2,m+1):
      a[2] = i3
      for i4 in range(i3,m+1):
        a[3] = i4
        for i5 in range(i4,m+1):
          a[4] = i5
          for i6 in range(i5,m+1):
            a[5] = i6
            for i7 in range(i6,m+1):
              a[6] = i7
              for i8 in range(i7,m+1):
                a[7] = i8
                for i9 in range(i8,m+1):
                  a[8] = i9
                  for i10 in range(i9,m+1):
                    a[9] = i10
                    sum = 0
                    if q != 1:
                      for i in range(q):
                        if a[inp[i][1]-1] - a[inp[i][0]-1] == inp[i][2]:
                          sum = sum + inp[i][3]
                        if sum > max:
                          max = sum
                    else:
                      if a[inp[0][1]-1] - a[inp[0][0]-1] == inp[0][2]:
                          sum = sum + inp[0][3]
                      if sum > max:
                          max = sum

print(max)

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






query = [None] * Q
for i in range(Q):
  query[i] = list(map(int,input().split()))
  
stack = []
for i in range(1,M + 1):
  stack.append([i])

def calc(arr,query):
  res = 0
  for q in query:
    a,b,c,d = q
    if arr[b-1] - arr[a-1] == c:
      res += d
  return res
  
ans = 0
while stack:
  arr = stack.pop()
  if len(arr) == N:
    val = calc(arr,query)
    if val > ans:
      ans = val
    continue
  for i in range(arr[-1],M+1):
    stack.append(arr + [i])

print(ans)

Solution (Python):

import sys
from collections import deque
import copy

N,M,Q=map(int, sys.stdin.readline().split())
X=[ map(int, sys.stdin.readline().split()) for _ in range(Q) ]

qq=deque()
for m in range(1,M+1):
    q=deque()
    q.append([m])
    while q:
        if N<len(q[0]):
            qq+=q
            break
        fro=q.popleft()
        if N<len(fro):break
        last=fro[-1]
        for to in range(last,M+1):
            q.append(fro+[to])

ans=float("-inf")
for L in qq:
    temp_ans=0
    for a,b,c,d in X:
        if L[b-1]-L[a-1]==c:
            temp_ans+=d
    ans=max(ans,temp_ans)
print ans

Solution (Python):
from itertools import combinations_with_replacement

N, M, Q, *ABCD = map(int, open(0).read().split())

L = list(zip(*[iter(ABCD)] * 4))

print(max(sum(d for a, b, c, d in L if A[b - 1] - A[a - 1] == c) for A in combinations_with_replacement(range(1, M + 1), N)))

Solution (Python):
import itertools

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

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

ans = 0
for A in As:
    sm = 0
    for abcd in abcds:
        a, b, c, d = abcd[0], abcd[1], abcd[2], abcd[3]
        if A[b-1] - A[a-1] == c:
            sm += d
    ans = max(ans, sm)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]





N, M, Q = mi()
abcd = li2(Q)

'''
a = '0' * N
m = str(M-1)
l = [a]

while True:
    i = N-1
    while i >= 0 and a[i] == m:
        i -= 1
    if i == -1:
        break
    a = list(a)
    num = str(int(a[i]) + 1)
    a[i] = num
    if i != N-1:
        for j in range(i+1, N):
            a[j] = num
    tmp = ''.join(a)
    l.append(tmp)
'''

def dfs(num, length):
    if length == N:
        l.append(num)
        return
    last = int(num[-1])
    for i in range(last, M):
        dfs(num + str(i), length+1)

l = []
dfs('0', 1)

ans = 0
for num in l:
    cnt = 0
    num = list(map(int, list(num)))
    for i in range(Q):
        if num[abcd[i][1]-1] - num[abcd[i][0]-1] == abcd[i][2]:
            cnt += abcd[i][3]
    ans = max(ans, cnt)

print(ans)

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

def calc(p):
    ret = 0
    
    for a, b, c, d in abcd:
        if p[b-1] - p[a-1] == c:
            ret += d
    return ret

ans = [0]
def dfs(s):
    if len(s) == N:
        ans[0] = max(ans[0], calc(s))
        return
    for i in range(s[-1],M+1):
        dfs(s+[i])

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

print(ans[0])

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

for i in range(q):
    arr[i][0],arr[i][1],arr[i][2],arr[i][3]=map(int, input().split())

ans=0
for i in itertools.combinations_with_replacement(range(1,m+1),n):
    score=0
    for j in range(q):
        tmp1=i[arr[j][1]-1]
        tmp0=i[arr[j][0]-1]
        if tmp1-tmp0==arr[j][2]:
            score+=arr[j][3]
    if ans<score:
        ans=score
print(ans)

Solution (Python):



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


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


def add(l):
    ret = []
    for i in range(len(l)):
        last = l[i][-1]
        for j in range(last, M+1):
            ret.append(l[i] + [j])
    return ret

for i in range(N-1):
    dfs = add(dfs)



m = -1
for i in range(len(dfs)):
    A = dfs[i]
    tmp = 0
    for j in range(len(lq)):
        aj = lq[j][0]
        bj = lq[j][1]
        cj = lq[j][2]
        dj = lq[j][3]
        
        
        
        
        
            
        if(A[bj-1]-A[aj-1] == cj):
            tmp += dj
    
    if(tmp > m):
        m = tmp    
print(m)

Solution (Python):
import numpy as np
import itertools
from itertools import combinations_with_replacement
if __name__ == "__main__":
    N, M, Q = map(int, input().split())
    
    data = [list(map(int, input().split())) for _ in range(Q)]
    
    
    
    
    
    

    
    arr = combinations_with_replacement(range(1, M+1), N)
    ans = 0
    for a in arr:
        
        s = 0
        
        
        
        for d in data:
            if a[d[1]-1] - a[d[0]-1] == d[2]:
                s += d[3] 

        if s > ans:
            ans = s
    print(ans)

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

a=[]
for i in range(n-1):
  a.append(1)
a.append(0)
current=0
b=[]
for i in range(n):
  b.append(m)

while True:
  i=n-1
  while a[i]==m:
    i=i-1
    if i==-1:
      break
  if i==-1:
    break
  a[i]+=1
  for j in range(i+1,n):
      a[j]=a[i]
  d=0
  for j in range(q):
    if (a[l[j][1]-1]-a[l[j][0]-1])==l[j][2]:
      d+=l[j][3]
  if d>current:
    current=d

print(current)

Solution (Python):
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())
A = [1] * 10
 
def check(A, ans):
  t = 0
  for i in range(q):
    if A[b[i]-1] - A[a[i]-1] == c[i]:
      t += d[i]
  if ans < t: return t
  else: return ans
 
ans = 0
for i1 in range(1, m+1):
  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
                  ans = check(A, ans)
print(ans)

Solution (Python):
import itertools
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(q)]
M=[i+1 for i in range(m)]
L=[]
for i in itertools.combinations_with_replacement(M,n):
  L.append(i)
ans=[]
for i in range(len(L)):
  ct=0
  for j in range(q):
    if L[i][l[j][1]-1]-L[i][l[j][0]-1]==l[j][2]:
      ct+=l[j][3]
  ans.append(ct)
print(max(ans))

Solution (Python):
import itertools

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

x = []

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

res = 0

for l in itertools.combinations_with_replacement([x for x in range(m)], n):
    temp = 0
    
    for temp_x in x:
        if l[temp_x[1] - 1] - l[temp_x[0] - 1] == temp_x[2]:
            temp += temp_x[3]

    if res <= temp:
        res = temp

print(res)

Solution (Python):
from collections import defaultdict,deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


from itertools import combinations_with_replacement
n, m, q = inpl()
ans=0
data=[inpl() for _ in range(q)]
for t in combinations_with_replacement(range(1,m+1), n):
    tmp = 0
    for a,b,c,d in data:
        if t[b - 1] - t[a - 1] == c:
            tmp += d
    ans = max(tmp, ans)
print(ans)

Solution (Python):
N,M,Q = map(int,input().split())
abcd = [list(map(int,input().split())) for _ in range(Q)]
ans = 0
def res(lis,place,now_number,M,N,result):
  if place==N-1:
    lis[place]=now_number
    result.append(lis.copy())
    return
  else:
    lis[place]=now_number
    for j in range(now_number,M+1):
      res(lis,place+1,j,M,N,result)

for i in range(1,M+1):
  result = []
  place = 0
  l = [0]*N
  res(l,place,i,M,N,result)
  for A in result:
    tmp = 0
    for q in abcd:
      if A[q[1]-1]-A[q[0]-1]==q[2]:
        tmp+=q[3]
    ans=max(ans,tmp)
    
print(ans)

Solution (Python):
import itertools

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)

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

Max_amount = 0
for v in itertools.combinations_with_replacement(l, N):
    L = list(v)
    amount = 0
    for i in range(Q):
        if L[B[i]-1] - L[A[i]-1] == C[i]:
            amount += D[i]
    if Max_amount < amount:
        Max_amount = amount

print(Max_amount)

Solution (Python):
import itertools
n, m, q = map(int, input().split())
Q = []
for i in range(q):
    ai, bi, ci, di = map(int, input().split())
    Q.append([ai,bi,ci,di])

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

ans = -1
for x in X:

    p = 0
    for q in Q:
        if x[q[1]-1] - x[q[0]-1] == q[2]:
            p += q[3]

    if ans < p:
        ans = p


print(ans)

Solution (Python):
from itertools import combinations_with_replacement as combr

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

ans = 0

for pair in combr(range(1,M+1), N):
    tmp = 0
    pair = list(pair)
    for i in range(Q):
        if (pair[b[i]-1]-pair[a[i]-1]) == c[i]:
            tmp += d[i]
            
    ans=max(ans, tmp)

print(ans)

Solution (Python):

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


V = [[] for _ in range(N+1)]


for i in range(1, M+1):
    V[1].append([i])


for i in range(1, N):
    for v in V[i]:
        
        b = v[-1]
        for a in range(b, M+1):
            v2 = v + [a]
            V[i+1].append(v2)

ans = 0
for v in V[N]:
    res = 0
    for a, b, c, d in ABCD:
        if v[b-1] - v[a-1] == c:
            res += d
    ans = max(ans, res)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

ans = 0

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

print(ans)

Solution (Python):
from itertools import combinations_with_replacement as cwr

N, M, Q, *Z = map(int, open(0).read().split())
*Z, = zip(*[iter(Z)]*4)

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

    if score > ans:
        ans = score

print(ans)

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())
A = [1] * 10
 
def check(A, ans):
  t = 0
  for i in range(q):
    if A[b[i]-1] - A[a[i]-1] == c[i]:
      t += d[i]
  if ans < t:
    return t
  else:
    return ans
 
ans = 0
for i1 in range(1, m+1):
  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
                  ans = check(A, ans)
print(ans)

Solution (Python):

import sys
from collections import deque
import copy

N,M,Q=map(int, sys.stdin.readline().split())
X=[ map(int, sys.stdin.readline().split()) for _ in range(Q) ]

qq=deque()
for m in range(1,M+1):
    q=deque()
    q.append([m])
    flag=True
    while flag:
        fro=q.popleft()
        if N<len(fro):break
        last=fro[-1]
        for to in range(last,M+1):
            if N<len(fro+[to]):
                q.append(fro)
                qq+=q
                flag=False
                break
            q.append(fro+[to])

ans=float("-inf")
for L in qq:
    temp_ans=0
    for a,b,c,d in X:
        if L[b-1]-L[a-1]==c:
            temp_ans+=d
    ans=max(ans,temp_ans)
print ans

Solution (Python):
from itertools import combinations_with_replacement as cwd

n,m,q = map(int,input().split())
A = list(cwd(range(1,m+1),n))
l = []
ans = 0

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

for j in A:
    tmp = 0
    for k in l:
        if j[k[1]] - j[k[0]] == k[2]:
            tmp += k[3]
    ans = max(ans,tmp)

print(ans)