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())
arr=[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):
                 for i10 in range(i9,m+1):
                   tmp=0
                   arrs=[0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                   for a,b,c,d in arr: 
                     if arrs[b]-arrs[a]==c:
                       tmp+=d
                   ans=max(ans,tmp)
print(ans)

Solution (Python):



def solve(n, m, q, arrays):
    from itertools import combinations_with_replacement
    pattern = list(combinations_with_replacement(range(1, m+1), n))

    maxp = 0
    for x in pattern:
        get = 0
        for array in arrays:
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]

        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

arrays = []
loop = 0
array = []
for x in aa:
    array.append(x)
    loop += 1
    if loop % 4 == 0:
        arrays.append(array)
        array = []

solve(n, m, q, arrays)

Solution (Python):
import sys
input = sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log
from heapq import heapify, heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from decimal import Decimal
inf = float('inf')
MOD = 1000000007
def INT_(n): return int(n) - 1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')

n, m, q = MI()
abcd = LLIN(q)

arr = []
def dfs(A: list):
    if len(A) == n:
        arr.append(A)
        return
    
    if len(A) == 0:
        s = 1
    else:
        s = A[-1]
    
    while s <= m:
        dfs(A + [s])
        s += 1
       

def main():
    dfs([])
    ans = 0
    for A in arr:
        tmp = 0
        for a,b,c,d in abcd:
            if A[b-1] - A[a-1] == c:
                tmp += d
        ans = max(ans, tmp)
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import itertools
input = sys.stdin.readline

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

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

ans = 0

for v in itertools.combinations(range(N+M-1), N):
    l = []
    for i in range(N):
        l.append(v[i]-i+1)

    total = 0
    for i in range(Q):
        if l[S[i][1]-1] - l[S[i][0]-1] == S[i][2]:
            total += S[i][3]
    
    ans = max(ans, total)

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
buf=[None]*n

def dfs(nowdigit,cannum):
    global ans
    if nowdigit==n:
        tmp=0
        for j in range(q):
            if buf[b[j]] - buf[a[j]] == c[j]:
                tmp+=d[j]
        ans=max(ans,tmp)
    else:
        for i in range(cannum,m+1):
            buf[nowdigit]=i
            dfs(nowdigit+1,i)
dfs(0,1)
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
buf=[None]*n

def dfs(nowdigit,cannum):
    global ans
    if nowdigit==n:
        tmp=0
        for j in range(q):
            if buf[b[j]] - buf[a[j]] == c[j]:
                tmp+=d[j]
        ans=max(ans,tmp)
    else:
        for i in range(cannum,m+1):
            buf[nowdigit]=i
            dfs(nowdigit+1,i)
dfs(0,1)
print(ans)

Solution (Python):
import itertools

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

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


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

cnt = 0
ans = 0
for i in comb:
    for k in range(Q):
        a = A[k][0]
        b = A[k][1]
        c = A[k][2]
        d = A[k][3]
        if i[b-1] - i[a-1] == c:
            cnt += d
    ans = max(ans,cnt)
    cnt = 0
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
N,M,Q=map(int,input().split())
a=[0]*Q
b=[0]*Q
c=[0]*Q
d=[0]*Q
ans=0
for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int,input().split())
    b[i]-=1
    a[i]-=1
for i in combinations_with_replacement(range(M),N):
    kari=0
    for j in range(Q):
        if i[b[j]]-i[a[j]]==c[j]:
            kari+=d[j]
    ans=max(kari,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)]
ans = 0
for A in itertools.combinations_with_replacement([i for i in range(1,M+1)],N):
    sorted(A)
    pre_ans = 0
    for i in range(Q):
        a,b,c,d = L[i]
        if A[b-1] - A[a-1] == c:
            pre_ans += d
        ans = max(ans,pre_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)]
ans = 0
for A in itertools.combinations_with_replacement([i for i in range(1,M+1)],N):
    pre_ans = 0
    for i in range(Q):
        a,b,c,d = L[i]
        if A[b-1] - A[a-1] == c:
            pre_ans += d
        ans = max(ans,pre_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)]
tmp = [k+1 for k in range(M)]
ans = 0
for A in itertools.combinations_with_replacement(tmp,N):
    pre_ans = 0
    for i in range(Q):
        a,b,c,d = L[i]
        if A[b-1] - A[a-1] == c:
            pre_ans += d
        ans = max(ans,pre_ans)
print(ans)

Solution (Python):
N, M, Q = list(map(int, input().strip().split()))
A = []
for i in range(Q):
  a, b, c, d = list(map(int, input().strip().split()))
  A.append((a, b, c, d))

A_list = [1] * N
ans = 0

def calc_score():
  score = 0
  for i in range(Q):
    a, b, c, d = A[i][0], A[i][1], A[i][2], A[i][3]
    if A_list[b-1] - A_list[a-1] == c:
      score += d
  return score
      
def dfs(position, minimum):
  global ans
  if position == N:
    score = calc_score()
    ans = max(ans, score)
    return
  
  for i in range(minimum, M+1):
    A_list[position] = i
    dfs(position+1, i)
    
dfs(1, 1)
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n, m, q = MAP()
'''
arr = [[]]
for i in range(n):
    tmp = deepcopy(arr)
    arr = []
    for j in range(1, m + 1):
        for k in range(len(tmp)):
            if len(tmp[k]) == 0 or tmp[k][-1] <= j:
                arr.append(deepcopy(tmp[k]))
                arr[-1].append(j)
'''
arr = list(combinations_with_replacement(range(1, m + 1), n))
point = [0 for _ in range(len(arr))]
for i in range(q):
    a, b, c, d = MAP()
    for j in range(len(arr)):
        if arr[j][b - 1] - arr[j][a - 1] == c:
            point[j] += d

print(max(point))

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

def culc(List):
    global Q
    global commands

    score=0
    for i in range(Q):
        if List[commands[i][1]-1]-List[commands[i][0]-1]==commands[i][2]:
            score+=commands[i][3]
    return score



def max_score(List,last_number):
    global M
    global N
    global Q

    Max_score=0
    temp_score=0
    for i in range(last_number,M+1):
        List.append(i)
        if len(List)==N:
            temp_score=culc(List)
            Max_score=max(Max_score,temp_score)
        else:
            temp_score=max_score(List,i)
            Max_score=max(Max_score,temp_score)
        List.pop(-1)    
    
    return Max_score


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


ans=max_score([],1)
print(ans)

Solution (Python):
import itertools
def main():
    N, M, Q = map(int, input().split())
    abcd = []
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        abcd.append((a-1, b-1, c, d))
    score = 0
    for ones in list(itertools.combinations([i for i in range(N+M-1)], M-1)):
        temp = [0] * (N + M -1)
        for idx in ones:
            temp[idx] = 1
        A = [0] * N
        idx = 0
        sum_ = 0
        for i in temp:
            if i == 0:
                A[idx] = sum_ + 1
                idx += 1
            else:
                sum_ += 1
        score_temp = get_score(A, abcd)
        if score_temp > score:
            score = score_temp
    print(score)
            
def get_score(A, abcd):
    score = 0
    for a, b, c, d in abcd:
        if A[b] - A[a] == c:
            score += d
    return score

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
def main():
    N, M, Q = (int(_) for _ in input().split())
    query = [[int(_) for _ in input().split()] for __ in range(Q)]
    ret = 0
    for ll in itertools.combinations_with_replacement(range(1, M+1), N):
        cc = 0
        for a, b, c, d in query:
            a, b = a-1, b-1
            if ll[b] - ll[a] == c: cc += d
        ret = max(ret, cc)
    print(ret)
    return

if __name__ == '__main__':
    main()

Solution (Python):
ans = 0

def dfs():
    global ans
    if len(A) == n:
        count = 0
        for i in range(q):
            if A[b[i] - 1] - A[a[i] - 1] == c[i]:
                count += d[i]
        if count > ans: ans = count
        if A[-1] != m:
            A[-1] += 1
            dfs()
        else:
            A.pop(-1)
        return
    else:
        A.append(A[-1])
        dfs()
        if A[-1] != m:
            A[-1] += 1
            dfs()
        else:
            A.pop(-1)
        return

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 = []

for i in range(1,m + 1):
    A.append(i) 
    dfs()
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)

a_list = []
  
for i in range(m):
  a_list.append(i)

suuretsu_list = itertools.combinations_with_replacement(a_list, n)

point_list = [0]

for suuretsu in suuretsu_list:
  point = 0
  for _a, _b, _c, _d in zip(a, b, c, d):
    if suuretsu[_b - 1] - suuretsu[_a - 1] == _c:
      point += _d
  point_list.append(point)
print(max(point_list))

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
ANS = 0
SUM = 0
def dfs(arr, l):
    global SUM, ANS
    if l == N:
        for i in range(Q):
            if arr[A[i][1]-1]-arr[A[i][0]-1] == A[i][2]:
                SUM += A[i][3]
            ANS = max(SUM, ANS)
        SUM = 0
        return
    else:
        for i in range(arr[l-1], M+1):
            arr.append(i)
            l = len(arr)
            dfs(arr,l)
            arr.pop()

    return ANS

if __name__ == '__main__':
    N, M ,Q = map(int, input().split())
    A = [list(map(int, input().split())) for i in range(Q)]
    for i in range(M):
        pANS = ANS
        ARR = deque([i+1])
        dfs(ARR,1)
        ANS = max(pANS, ANS)
    print(ANS)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
ANS = 0
SUM = 0
def dfs(arr, l):
    global SUM, ANS
    if l == N:
        for i in range(Q):
            if arr[A[i][1]-1]-arr[A[i][0]-1] == A[i][2]:
                SUM += A[i][3]
            ANS = max(SUM, ANS)
        SUM = 0
        return
    else:
        for i in range(arr[l-1], M+1):
            arr.append(i)
            l = len(arr)
            dfs(arr,l)
            arr.pop()

    return ANS

if __name__ == '__main__':
    N, M ,Q = map(int, input().split())
    A = [list(map(int, input().split())) for i in range(Q)]
    for i in range(M):
        pANS = ANS
        ARR = deque([i+1])
        dfs(ARR,1)
        ANS = max(pANS, ANS)
    print(ANS)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
ANS = 0
SUM = 0
def dfs(arr, l):
    global SUM, ANS
    
    if l == N:
        for i in range(Q):
            

            if arr[A[i][1]-1]-arr[A[i][0]-1] == A[i][2]:
                SUM += A[i][3]
            ANS = max(SUM, ANS)
        SUM = 0
        return
    else:
        for i in range(arr[l-1], M+1):
            arr.append(i)
            l = len(arr)
            
            dfs(arr,l)
            arr.pop()

    return ANS

if __name__ == '__main__':
    N, M ,Q = map(int, input().split())
    A = [list(map(int, input().split())) for i in range(Q)]
    ARR = deque([1])
    dfs(ARR,1)
    print(ANS)

Solution (Python):
import sys
n, m, q = map(int, input().split())
alis, blis, clis, dlis = [], [], [], []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    alis.append(a)
    blis.append(b)
    clis.append(c)
    dlis.append(d)
sys.setrecursionlimit(10**9)
Alist = [] 
def dfs(A):
    if len(A)==(n+1):
        Alist.append(A)
        return
    for i in range(A[-1], m+1):
        dfs(A+[i])

dfs([1])

ans = 0
for A in Alist:
    score = 0
    for i in range(q):
        if A[blis[i]] - A[alis[i]] == clis[i]:
            score += dlis[i]
    ans = max(ans, score)

print(ans)

Solution (Python):
import itertools

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

def saitenn(suuretu):
    global numbers_list
    score = 0
    for numbers in numbers_list:
        a = numbers[0] - 1
        b = numbers[1] - 1
        c = numbers[2]
        d = numbers[3]

        if suuretu[b] - suuretu[a] == c:
            score += d
    return score

for A in itertools.combinations_with_replacement(range(1, m+1), n):
    score = saitenn(A)
    ans = max(ans, score)

print(ans)

Solution (Python):
import itertools

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

max_num = 0
for num_lst in itertools.combinations_with_replacement(range(1, m + 1), n):
    count = 0
    for a, b, c, d in lst:
        if num_lst[b - 1] - num_lst[a - 1] == c:
            count += d
    max_num = max(max_num, count)
print(max_num)

Solution (Python):
import itertools

n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ 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 abcd:
        if A[b-1] - A[a-1] == c:
            sum += d
    ans = max(ans, sum)

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):
                    lst = [0,a,b,c,d,e,f,g,h,i,j]
                    s = 0
                    for l in range(q):
                      s += abcd[l][3]*(lst[abcd[l][1]]-lst[abcd[l][0]]==abcd[l][2])
                    ans = max(ans, s)
print(ans)

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

import sys
sys.setrecursionlimit(10**6)
ans = [0]
def dfs(idx, now):
    if idx == n:
        
        sumd = 0
        for j in abcd:
            if now[j[1]-1] - now[j[0]-1] == j[2]:
                sumd += j[3]
        ans[0] = max(ans[0], sumd)
        return
    if len(now) > 0:
        start = now[-1]
    else:
        start = 1
    for i in range(start, m+1):
        now.append(i)
        dfs(idx + 1, now)
        now.pop()

dfs(0, [])
print(ans[0])

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
a, b, c, d = [], [], [], []

for i in range(Q):
    t = [int(x) for x in input().split()]
    a.append(t[0] - 1)
    b.append(t[1] - 1)
    c.append(t[2])
    d.append(t[3])

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

    ans = max(ans, score)

print(ans)

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

a_cand = list(itertools.combinations_with_replacement(num, n))
count_list = []
for a in a_cand:
    count = 0
    for q in q_list:
      if a[q[1]- 1] - a[q[0] - 1] == q[2]:
        count += q[3]
        
    count_list.append(count)
  
print(max(count_list))

Solution (Python):
def dfs(A, M, i):
  if i < 0:
    return
  A[i] += 1
  if A[i] > M:
    dfs(A, M, i-1)
    if i-1 >= 0:
      A[i] = A[i-1]

N,M,Q=map(int, input().split())
a,b,c,d = [], [], [], []
for i in range(Q):
  ai,bi,ci,di = map(int, input().split())
  a.append(ai)
  b.append(bi)
  c.append(ci)
  d.append(di)

A = [1]*N
ans = 0

for _ in range(100000):
  dsum = 0
  for i in range(Q):
    ai,bi,ci,di = a[i], b[i], c[i], d[i]
    if A[bi-1]-A[ai-1] == ci:
      dsum += di
  ans = max(ans, dsum)

  
  if A[0]==M:
    break

  
  dfs(A, M, N-1)

print(ans)

Solution (Python):
import itertools
n, m, q = map(int, input().split())
lst = [0] * q
for i in range(q):
    a, b, c, d = map(int, input().split())
    lst[i] = (a, b, c, d)
L = list(itertools.combinations_with_replacement(range(1,m+1), n))
ans = 0 
for l in L:
    point_l = 0
    for rule in lst:
        if l[rule[1]-1]-l[rule[0]-1] == rule[2]:
            point_l += rule[3]
    ans = max(ans,point_l)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
def main():
    n, m, q = map(int, input().split())
    qlist = []
    for _ in range(q):
        qlist.append(tuple(map(int, input().split())))

    nums = [i for i in range(1, m + 1)]
    alist = tuple(combinations_with_replacement(nums, n))

    r = 0
    for al in alist:
        tr = 0
        for eq in qlist:
            eq0 = eq[0] - 1
            eq1 = eq[1] - 1
            eq2 = eq[2]
            eq3 = eq[3]
            if al[eq1] - al[eq0] == eq2:
                tr += eq3
        r = max(r, tr)

    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log
from heapq import heapify, heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from decimal import Decimal
inf = float('inf')
MOD = 1000000007
def INT_(n): return int(n) - 1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')

n, m, q = MI()
abcd = LLIN(q)

ans = 0
num = [0 for _ in range(10)]
for i0 in range(1, m+1):
    num[0] = i0
    for i1 in range(i0, m+1):
        num[1] = i1
        for i2 in range(i1, m+1):
            num[2] = i2
            for i3 in range(i2, m+1):
                num[3] = i3
                for i4 in range(i3, m+1):
                    num[4] = i4
                    for i5 in range(i4, m+1):
                        num[5] = i5
                        for i6 in range(i5, m+1):
                            num[6] = i6
                            for i7 in range(i6, m+1):
                                num[7] = i7
                                for i8 in range(i7, m+1):
                                    num[8] = i8
                                    for i9 in range(i8, m+1):
                                        num[9] = i9
                                        tmp = 0
                                        for a,b,c,d in abcd:
                                            if num[b-1] - num[a-1] == c:
                                                tmp += d
                                        ans = max(ans, tmp)

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

def dfs(part_A,i):
    if i == N:
        A.append(part_A)
    else:
        for a in range(part_A[-1],M+1):
            dfs(part_A+[a],i+1)

A=[]
dfs([1],1)
max_score=0
for An in A:
    score = 0
    for q in range(Q):
        if An[b[q]-1]-An[a[q]-1] == c[q]:
            score += d[q]
    max_score = max(score, max_score)

print(max_score)

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(part_A,i):
    if i == N:
        A.append(part_A)
    else:
        for a in range(part_A[-1],M+1):
            dfs(part_A+[a],i+1)

A=[]
dfs([1],1)
max_score=0
for An in A:
    score = 0
    for q in range(Q):
        if An[b[q]-1]-An[a[q]-1] == c[q]:
            score += d[q]
    max_score = max(score, max_score)

print(max_score)

Solution (Python):
n, m, q = map(int, input().split())
abcd = []
A = 0
B = 1
C = 2
D = 3
for i in range(q):
    abcd.append(list(map(int, input().split())))

for i in range(q):
    abcd[i][A] -= 1
    abcd[i][B] -= 1

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):
                                    for i10 in range(i9,m+1):
                                        a = [i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
                                        now = 0
                                        for j in range(q):
                                            if a[abcd[j][B]] - a[abcd[j][A]] == abcd[j][C]:
                                                now += abcd[j][D]
                                        ans = max(ans, now)

print(ans)

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

A = []
score_list = []


for i in range(N):
  A.append(0)


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



def search(x, y):
  if x < 0  or x > N - 1 or y < 0 or y > M - 1:
    return
  A[x] = y + 1
  if x == N - 1:
    sum = 0
    for i in range(Q):
      if A[L[i][1] - 1] - A[L[i][0] - 1] == L[i][2]:
        sum += L[i][3]
    score_list.append(sum)
    
  search(x + 1, y)
  search(x, y + 1)

search(0, 0)
print(max(score_list))

Solution (Python):
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
from itertools import combinations_with_replacement
l = [i for i in range(1, M+1)]
lis = list(combinations_with_replacement(l, N))

for l in lis:
    tmp = 0
    for j in range(Q):
        a, b, c, d = abcd[j]
        if l[b-1]-l[a-1]==c:
            tmp+=d
    ans = max(tmp, ans)

print(ans)

Solution (Python):
import sys
from collections import deque
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
from itertools import combinations_with_replacement

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

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

Solution (Python):
n, m, q = 0, 0, 0
ABCD = []
ans = 0


def dfs(A):
    global ans

    if len(A) == n:
        s = 0
        for qi in range(q):
            a, b, c, d = ABCD[qi]
            if A[b - 1] - A[a - 1] == c:
                s += d

        ans = max(s, ans)
        return

    for i in range(A[-1], m + 1):
        AC = A.copy()
        AC.append(i)
        dfs(AC)


def main():
    global n, m, q, ABCD
    n, m, q = [int(e) for e in input().split()]
    ABCD = [[int(e) for e in input().split()] for i in range(q)]

    for mi in range(1, m + 1):
        dfs([mi])

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
n, m, q = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(q)]

def point(A):
    point = 0
    for i in lst:
        if A[i[1]] - A[i[0]] == i[2]:
            point += i[3]
    return point

def solve(A):
    length = len(A) - 1
    if length == n:
        return point(A)
    o = A[length]
    ans = 0
    for i in range(o, m+1):
        ans = max(ans, solve(A + [i]))
    return ans
print(solve([1]))

Solution (Python):
from itertools import combinations_with_replacement


def main():
    N, M, Q = map(int, input().split())
    conds = [list(map(int, input().split())) for i in range(Q)]
    combs = combinations_with_replacement([i for i in range(1, M+1)], N)
    ans = 0
    for comb in combs:
        ans_ = 0
        for c in conds:
            if comb[c[1]-1] - comb[c[0]-1] == c[2]:
                ans_ += c[3]
        ans = max(ans, ans_)
    print(ans)


if __name__ == "__main__":
    main()

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

up_lsts = list(itertools.combinations_with_replacement(range(1,m + 1), n))
ans = 0
for up_lst in up_lsts:
    tmp = 0
    for req in lst:
        if up_lst[req[1] - 1] - up_lst[req[0] - 1] == req[2]:
            tmp += req[3]
    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)]
values = set(i for i in range(1,M+1))
ans = 0
for A in combinations_with_replacement(values, N):
    cnt = 0
    for a,b,c,d in abcd:
        if A[b-1] - A[a-1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)

Solution (Python):
N, M, Q = map(int, input().split())
num4 = [[(int(i)) for i in input().split()] for _ in range(Q)]
A = [[i]for i in range(M,0,-1)]
Aseq = []
while len(A)!=0: 
  tmp = A.pop(-1)
  if len(tmp)==N: 
    Aseq.append(tmp)
    continue
  for i in range(M, tmp[-1]-1,-1): A.append(tmp + [i])
pmax = 0
for A in Aseq:
  p = 0
  for a,b,c,d in num4:
    if int(A[b-1])-int(A[a-1])==c: p += d
  pmax = max(pmax, p)
print(pmax)

Solution (Python):
N,M,Q = map(int,input().split())
ar=[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):
                                    for i10 in range(i9,M+1):

                                        con = 0

                                        arr = [0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]

                                        for a,b,c,d in ar:
                                            if arr[b] - arr[a] == c:
                                                con += d

                                        ans=max(ans,con)

print(ans)

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

Solution (Python):
N,M,Q = map(int,input().split())
ABCD = [list(map(int,input().split())) for _ in range (Q)]
import sys
sys.setrecursionlimit(10**9)
Alist = []
def dfs(v):
    if len(v)==N:
        Alist.append(v)
        return 
    for t in range(v[-1],M+1):
        dfs(v+[t])
dfs([1])

ans = 0
for A in Alist:
    res = 0
    for a,b,c,d in ABCD:
        if A[b-1]-A[a-1]==c:
            res += d
    ans = max(ans,res)
print(ans)