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):
import copy

N, M, Q = map(int, input().split())
A = [[] for i in range(20)]
for i in range(N):
    if i == 0:
        for j in range(1, M+1):
            A[i].append([j])
    else:
        for j in range(len(A[i-1])):
            for k in range(A[i-1][j][i-1], M+1):
                temp = copy.copy(A[i-1][j])
                temp.append(k)
                A[i].append(temp)

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

ans = 0
for i in range(len(A[N-1])):
    s = copy.copy(A[N-1][i])
    p = 0
    for q in range(Q):
        if s[Y[q]] - s[X[q]] == C[q]:
            p += D[q]
    ans = max([p, ans])
print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, q = LI()
L = LIR(q)
ans = 0
def f(arr):
    global ans
    if len(arr) == n:
        ret = 0
        for a, b, c, d in L:
            if arr[b - 1] - arr[a - 1] == c:
                ret += d
        ans = max(ans, ret)
    else:
        if arr:
            for k in range(arr[-1], m + 1):
                f(arr + [k])
        else:
            for k in range(1, m + 1):
                f(arr + [k])

f([])
print(ans)

Solution (Python):
import itertools
k = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(k[2])]
m = [i+1 for i in range(k[1])]
n= list(itertools.combinations_with_replacement(m, k[0]))
now = 0
re = 0
for i in n:
  for x in l:
    if i[x[1] -1] - i[x[0] -1] == x[2]:
      re += x[3]
  now = max([now, re])
  re = 0

print(now)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7

        



N, M, Q  = map(int,input().split())
query = []
for q in range(Q):
    a,b,c,d = map(int,input().split())
    a-=1
    b-=1
    query.append((a,b,c,d))




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

for j in range(N-1):
    new = []
    for e in old:
        for i in range(e[-1],M+1):
            new.append(e+[i])
    old = new



mx = 0
for n in new:
    score = 0
    for a,b,c,d in query:
        if (n[b] - n[a] == c):
            score += d
    mx = max(score,mx)
print (mx)

Solution (Python):

import itertools
N,M,Q=map(int,input().split())
X=[[int(x) for x in input().split()] for i in range(Q)]
IN=[int(i) for i in range(1,M+1)]
Max=0
for com in itertools.combinations_with_replacement(IN, N):
    Sum=0
    for i in range(Q):
        a=int(X[i][0])-1
        b=int(X[i][1])-1
        c=int(X[i][2])
        d=int(X[i][3])
        if com[b]-com[a]==c:
            Sum+=d
    Max=max(Max,Sum)
print(Max)

Solution (Python):
N,M,Q=list(map(int,input().split()))
M=[i for i in range(1,M+1)]
import itertools
A=list(itertools.combinations_with_replacement(M, N))
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]=list(map(int,input().split()))
s=[]
ans=0
for j in range(len(A)):
  for k in range(Q):
    if A[j][b[k]-1]-A[j][a[k]-1]==c[k]:
      ans=ans+d[k]
  s.append(ans)
  ans=0
print(max(s))

Solution (Python):
N,M,Q=map(int, input().split())
ABCD=[tuple(map(int, input().split()))for _ in range(Q)]
def rec(X):
    ans=0
    if len(X)==N:
        res=0
        for a,b,c,d in ABCD:
            if X[b-1]-X[a-1]==c:
                res+=d
        return res
    s = 1 if len(X)==0 else X[-1]
    for i in range(s,M+1):
        X.append(i)
        ans=max(ans,rec(X))
        X.pop()
    return ans
print(rec([]))

Solution (Python):
import itertools
N , M ,Q= map(int,input().split())
seq = list(range(N+M-1))
pattern = list(itertools.combinations(seq,N))
prob_array = []
for i in range(Q):
    a ,b ,c , d = map(int,input().split())
    prob_array.append([a,b,c,d])
    
def convert_number(lst):
    num_ = 1
    lst = list(lst)
    lst.insert(0,-1)
    return_list = []
    for i in range(len(lst) - 1):
        num_ += lst[i+1] - lst[i] - 1
        return_list.append(num_)
    return return_list
    
opt_point = 0
for input_list in pattern:
    judge_list = convert_number(input_list)
    temp_point = 0
    for p_ in prob_array:
        if judge_list[p_[1] - 1] - judge_list[p_[0] - 1] == p_[2]:
            temp_point += p_[3]
    if temp_point >= opt_point:
        opt_point = temp_point
print(opt_point)

Solution (Python):
from collections import deque
n, m, q = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(q)]
result = [0]
p = deque([[i] for i in range(1, m+1)])
while p:
    i = p.popleft()
    if len(i) == n:
        r = 0
        for a, b, c, d in x:
            if i[b-1]-i[a-1]==c:
                r += d
        result.append(r)
    else:
        for j in range(i[-1], m+1):
            p.append(i+[j])
print(max(result))

Solution (Python):
import itertools

N,M,Q = map(int,input().split())
q = []
ans = 0
c = 0
array = [int(i) for i in range(1,M+1)]

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

def check(A):
    score = 0
    i = 0
    for a,b,c,d in q:
        i += 1
        if A[b-1]-A[a-1] == c:

            score += d

    return score

h = itertools.combinations_with_replacement(array, N)
for v in h:
    ans = max(ans,check(v))


"""
for i in range(2**M):
    len = 0
    A = []
    for j in range(M):
        if (i>>j) & 1:
            len += 1
            A.append(j+1)
    if len == N:
        print(A)
        ans = max(ans,check(A))
"""
print(ans)

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

import itertools

List = []
for v in itertools.combinations_with_replacement(range(1, m+1), n):
    List.append(list(v))
    
answer = []
for l in List:
    ans = 0
    for j in a:
        if l[j[1]-1] - l[j[0]-1] == j[2]:
            ans += j[3]
    answer.append(ans)
    
print(max(answer))

Solution (Python):
from collections import deque
N, M, Q = [int(a) for a in input().split()]
q = [None] * Q
for i in range(Q):
    q[i] = [int(x) for x in input().split()]

stk = deque([[i] for i in range(1, M + 1)])
ans = 0
while len(stk) > 0:
    A = stk.pop()
    if len(A) == N:
        temp = 0
        for i in range(Q):
            if A[q[i][1] - 1] - A[q[i][0] - 1] == q[i][2]:
                temp += q[i][3]
        ans = max(ans, temp)
        continue
    for i in range(A[-1], M + 1):
        stk.append(A + [i])

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
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

pmax = 0
A = [i for i in range(1, m + 1)]
for L in combinations_with_replacement(A, n):
    l = sorted(list(L))
    p = 0
    for i in range(q):
        if l[b[i] - 1] - l[a[i] - 1] == c[i]:
            p += d[i]
    pmax = max(pmax, p)
print(pmax)

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

def make_list(len_, max_, list_):
    min_ = list_[-1]
    lists = [list_ + [i] for i in range(min_, max_+1)]
    if len(list_)+1 == len_:
        return lists
    else:
        return_lists = []
        for l in lists:
            return_lists += make_list(len_, max_, l)
        return return_lists

all_lists = []
for j in range(1, M+1):
    all_lists += make_list(N, M, [j])

points = []
for A in all_lists:
    point = 0
    for a,b,c,d in conditions:
        if A[b-1]-A[a-1]==c:
            point += d
    points.append(point)
    
print(max(points))

Solution (Python):
from itertools import combinations_with_replacement


n, m, q = map(int, input().split())
cl = []
dl = []
abl = []

for _ in range(q):
    a, b, c, d = map(int, input().split())
    cl.append(c)
    dl.append(d)
    abl.append([a, b])

ans = 0
for comb in list(combinations_with_replacement(range(1, m+1), n)):
    point = 0
    for i, ab in enumerate(abl):
        a, b = ab
        if comb[b-1] - comb[a-1] == cl[i]:
            point += dl[i]
    ans = max(ans, point)
print(ans)

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

Solution (Python):
def main():
  from sys import stdin
  import itertools
  N, M, Q = map(int, input().split())
  scores = [list(map(int, stdin.readline().split())) for _ in range(Q)]
  best = 0
  x = list(range(1, M+1))
  for y in itertools.combinations_with_replacement(x, N):
    target = sorted(list(y))
    sco = 0
    for score in scores:
      if target[score[1]-1] - target[score[0]-1] == score[2]:
      	sco += score[3]
    if sco > best:
      best = sco
      
  print(best)
main()

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

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

max_point = 0
point_cont = 0
for pt in comb:
    point_cont = 0
    for list_pt in List:
        if pt[list_pt[1]-1] - pt[list_pt[0]-1] == list_pt[2]:
            point_cont += list_pt[3]
    if max_point <= point_cont:
        max_point = point_cont

print(max_point)

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

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


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

ans = 0
for comb in combs:
  point = 0
 
  for abcd in abcds:
    if (int(comb[abcd[1]-1])-int(comb[abcd[0]-1]) == abcd[2]):
      point += abcd[3]
  ans = max(ans, point)
 
print(ans)

Solution (Python):
n,m,q = map(int ,input().split())
lst = [list(map(int, input().split())) for _ in range(q)]
import itertools
l = [i+1 for i in range(m)]
lst2 = [com for com in itertools.combinations_with_replacement(l, n)]
res = 0
for com in lst2:
    score = 0
    for l in lst:
        if com[l[1]-1] - com[l[0]-1] == l[2]:
            score += l[3]
    if res < score:
        res = score 
    
print(res)

Solution (Python):
import itertools
n,m,q=map(int,input().split())
p=[list(map(int,input().split())) for i in range(q)]
ans=0
def init(n):
  c=[i for i in range(1,m+1)]
  cand=[]
  for i in c:
    cand.append([i])
  for _ in range(n-1):
    cand2=[]
    for i in range(len(cand)):
      for j in range(cand[i][-1],m+1):
        cand2.append(cand[i]+[j])
    cand=cand2
  return cand
cand=init(n)
for i in range(len(cand)):
  m=0
  for j in range(q):
    if cand[i][p[j][1]-1]-cand[i][p[j][0]-1]==p[j][2]:
      m+=p[j][3]
  if m>ans:
    ans=m
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

for A in combinations_with_replacement(range(1,M+1),N):
    tensu=0
    for abcd_i in abcd:
        ai=abcd_i[0]
        bi=abcd_i[1]
        ci=abcd_i[2]
        di=abcd_i[3]

        if A[bi-1]-A[ai-1]==ci:
            tensu+=di
        
    ans=max(ans,tensu)

print(ans)

Solution (Python):
import itertools

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

def A_score(A):
    dsum = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if A[b-1] - A[a-1] == c:
            dsum += d
    return dsum

seq = [i for i in range(1,m+1)]
A_comb = list(itertools.combinations_with_replacement(seq, n))
ans = 0

for t in A_comb:
    score = A_score(t)
    if  score > ans:
        ans = score

print(ans)

Solution (Python):
import sys
from math import factorial
from collections import Counter
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

N, M, Q = MAP()
req = []
for i in range(Q):
    a, b, c, d = MAP()
    req.append([a-1, b-1, c, d])

A = [[1]*N, ]
def plus(a):
    n = len(a)
    if a[n-1] !=M:
        a[n-1] = a[n-1]+1
        return a
    if n==1: return [M,]
    res = plus(a[:n-1])
    return res + [res[n-2],]

while True:
    a = A[-1]
    if a == [M]*N:
        break
    A.append(plus(a))

res = 0
for i in range(len(A)):
    temp = 0
    for j in range(Q):
        if A[i][req[j][1]] - A[i][req[j][0]] == req[j][2]:
            temp += req[j][3]
    if temp > res:
        res = temp

print(res)

Solution (Python):
import itertools

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

ans = 0

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

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

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M, Q = lr()
ABCD = [lr() for _ in range(Q)]
A = [1] * N
ABCD.sort(key=lambda x: x[3], reverse=True)
ABCD.sort(key=lambda x: x[0])
answer = 0
for a in [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):
                                        temp = 0
                                        x = [a,b,c,d,e,f,g,h,i,j]
                                        for k, l, m, n in ABCD:
                                            if x[l-1] - x[k-1] == m:
                                                temp += n
                                        if temp > answer:
                                            answer = temp

print(answer)

Solution (Python):
import itertools

s1 = input()

N = int(s1.split()[0])
M = int(s1.split()[1])
Q = int(s1.split()[2])

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

s = [input() for i in range(Q)]

for k in range(Q):
  a.append(int(s[k].split()[0]))
  b.append(int(s[k].split()[1]))
  c.append(int(s[k].split()[2]))
  d.append(int(s[k].split()[3]))
  
result = 0
  
suretu = []
for i in range(M):
  suretu.append(i+1)
  
for A in list(itertools.combinations_with_replacement(suretu, N)):
  A = list(A)
  subresult = 0
  for i in range(Q):
    if A[b[i]-1] - A[a[i]-1] == c[i]:
      subresult += d[i]
  if result < subresult:
    result = subresult
    
print(result)

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache

import heapq






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





N,M,Q = inputIL()
A = inputILL(Q)

l = []
count = 0
for i_1 in range(1,M+1):
  for i_2 in range(i_1,M+1):
    for i_3 in range(i_2,M+1):
      for i_4 in range(i_3,M+1):
        for i_5 in range(i_4,M+1):
          for i_6 in range(i_5,M+1):
            for i_7 in range(i_6,M+1):
              for i_8 in range(i_7,M+1):
                for i_9 in range(i_8,M+1):
                  for i_10 in range(i_9,M+1):
                    l.append([i_1,i_2,i_3,i_4,i_5,i_6,i_7,i_8,i_9,i_10])
                    

best = 0 
for item in l:
  score = 0
  for q in A:
    a,b,c,d = q
    if item[b-1] - item[a-1] == c:
      score += d
  best = max(score,best)
  
print(best)

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

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

Solution (Python):

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

all = list(itertools.combinations_with_replacement([i + 1 for i in range(M)], N - 1))
ans = []
m = 0
for i in all:
    now = [1]
    now += list(i)
    p = 0

    for j in point:

        a, b, c, d = j[0], j[1], j[2], j[3]
        if now[b - 1] - now[a - 1] == c:
            p += d
    if p > m:

        m = p
        ans = now
print(m)

Solution (Python):
import itertools

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

kazu = []

for i in range(1,m+1):
    kazu.append(i)

ans = 0

for v in itertools.combinations_with_replacement(kazu, n):
    memo = 0
    for i , j ,k ,l in ab:
        if v[j-1]-v[i-1] == k:
            memo += l
    ans = max(memo,ans)
print(ans)

Solution (Python):
import itertools

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

points = []
for able in itertools.combinations_with_replacement(range(1, m+1), n):
    point = 0
    for a, b, c, d in abcd:
        if able[b-1] - able[a-1] == c:
            point += d
    points.append(point)

print(max(points))

Solution (Python):

def check(l,P):
    f = 0
    for i in P:
        if l[i[1]-1] - l[i[0]-1] == i[2]:
            f += i[3]
    return f
def main():
    A,B,C = map(int,input().split())
    P = [list(map(int,input().split())) for i in range(C)]
    L = [[1]]
    for i in range(2,A+1):
        L2 = []
        for j in L:
            for k in range(j[-1],B+1):
                L2.append(j+[k])
        L = L2[:]
    res = 0
    for i in L:
        res = max(res,check(i,P))
    print(res)


if __name__ == "__main__":
    main()

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

ans =0
    
if n == 2:
    for a in range(1, m+1):
        for b in range(a, m+1):
            A = [a, b]
            ans_ = 0
            for res in abcd:
                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
            ans = max(ans, ans_)   
            
elif n == 3:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                A = [a, b, c]
                ans_ = 0
                for res in abcd:
                    if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                ans = max(ans, ans_)      
                
elif n == 4:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    A = [a, b, c, d]
                    ans_ = 0
                    for res in abcd:
                        if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                    ans = max(ans, ans_)
                    
elif n == 5:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        A = [a, b, c, d, e]
                        ans_ = 0
                        for res in abcd:
                            if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                        ans = max(ans, ans_)
                        
elif n == 6:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            A = [a, b, c, d, e, f]
                            ans_ = 0
                            for res in abcd:
                                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                            ans = max(ans, ans_)
                            
elif n == 7:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                A = [a, b, c, d, e, f, g]
                                ans_ = 0
                                for res in abcd:
                                    if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                ans = max(ans, ans_)
                                
elif n == 8:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                for h in range(g, m+1):
                                    A = [a, b, c, d, e, f, g, h]
                                    ans_ = 0
                                    for res in abcd:
                                        if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                    ans = max(ans, ans_)
                                    
elif n == 9:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                for h in range(g, m+1):
                                    for i in range(h, m+1):
                                        A = [a, b, c, d, e, f, g, h, i]
                                        ans_ = 0
                                        for res in abcd:
                                            if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                        ans = max(ans, ans_)
                
elif n == 10:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                for h in range(g, m+1):
                                    for i in range(h, m+1):
                                        for j in range(i, m+1):
                                            A = [a, b, c, d, e, f, g, h, i, j]
                                            ans_ = 0
                                            for res in abcd:
                                                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                            ans = max(ans, ans_)
                                                    
print(ans)

Solution (Python):
import math

N,M,Q=map(int,input().split())
S=[[]for i in range(Q)]
for i in range(Q):
    a=list(map(int,input().split()))
    S[i]=a
ans=0
T=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):
                                        D=[a,b,c,d,e,f,g,h,i,j]
                                        for k in range(Q):
                                            if D[S[k][1]-1]-D[S[k][0]-1]==S[k][2]:
                                                ans+=S[k][3]
                                        if ans>=T:
                                            T=ans
                                        ans = 0
print(T)

Solution (Python):
n,m,q = [int(x) for x in input().split()]
li = []
for i in range(q):
    li.append([int(x) for x in input().split()])
import itertools
num = range(1,m+1)
v = list(itertools.combinations_with_replacement(num,n))
ans = 0
for i in v:
    su = 0
    for j in li:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            su+=j[3]
    ans = max(ans,su)
print(ans)

Solution (Python):
n, M, Q = map(int, input().split())
a, b, c, d = [], [], [], []
for _ in [0]*Q:
    aa, bb, cc, dd = map(int, input().split())
    a += [aa-1]; b += [bb-1]; c += [cc]; d += [dd]
if n==2:
    AA = [[i, j] for i in range(1, M+1) for j in range(i, M+1)]
elif n==3:
    AA = [[i, j, k] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1)]
elif n==4:
    AA = [[i, j, k, l] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1)]
elif n==5:
    AA = [[i, j, k, l, m] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1) for m in range(l, M+1)]
elif n==6:
    AA = [[i, j, k, l, m, n] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1) for m in range(l, M+1) for n in range(m, M+1)]
elif n==7:
    AA = [[i, j, k, l, m, n, o] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1) for m in range(l, M+1) for n in range(m, M+1) for o in range(n, M+1)]
elif n==8:
    AA = [[i, j, k, l, m, n, o, p] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1) for m in range(l, M+1) for n in range(m, M+1) for o in range(n, M+1) for p in range(o, M+1)]
elif n==9:
    AA = [[i, j, k, l, m, n, o, p, q] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1) for m in range(l, M+1) for n in range(m, M+1) for o in range(n, M+1) for p in range(o, M+1) for q in range(p, M+1)]
elif n==10:
    AA = [[i, j, k, l, m, n, o, p, q, r] for i in range(1, M+1) for j in range(i, M+1) for k in range(j, M+1) for l in range(k, M+1) for m in range(l, M+1) for n in range(m, M+1) for o in range(n, M+1) for p in range(o, M+1) for q in range(p, M+1) for r in range(q, M+1)]
ans = 0
for A in AA:
    tmp = 0
    for i in range(Q):
        if A[b[i]] - A[a[i]] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)

Solution (Python):


import sys
import itertools


N, M, Q = map(int, input().split())
nums_list = []
A = [] 
for m in range(M):
    A.append(m+1)
run_list = []
for v in itertools.combinations_with_replacement(A, N):
    count = 0 
    for i in range(N-1):
        if v[i] <= v[i+1]:
            count += 1
    if count == N-1:
        run_list.append(v)

for q in range(Q):
    nums = [int(x.strip()) for x in input().split(' ') ]
    nums_list.append(nums)
ans = 0

for run in run_list:
    tmp = 0
    for num in nums_list:
        if run[num[1]-1] - run[num[0]-1] == num[2]:
            tmp = tmp + num[3]
            
    if ans < tmp:
        ans = tmp
    
print(ans)