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 itertools
N, M, Q = map(int, input().split())

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

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

max_num = 0

for s in seq:
    cnt = 0
    for l in lst:
        if s[l[1]-1] - s[l[0]-1] == l[2]:
            cnt += l[3]

    if max_num < cnt:
        max_num = cnt

print (max_num)

Solution (Python):
import itertools

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

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

for comb in list(itertools.combinations_with_replacement(range(1, m + 1),n)):
    sum = 0
    for i in range(q):
        if comb[abcd[i][1] - 1] - comb[abcd[i][0] - 1] == abcd[i][2]:
            sum += abcd[i][3]
    if ans < sum:
        ans = sum

print(ans)

Solution (Python):
import itertools

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=list(itertools.combinations_with_replacement([i for i in range(1,m+1)],n))


g=[]
for v in A:
    e=0
    for i in range(q):
        
        if v[b[i]-1]-v[a[i]-1] ==c[i]:
            e += d[i]
            
        
    g += [e]
print(max(g))

Solution (Python):
import math
import sys
import itertools

n,m,q=map(int,input().split())
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
e=[0]*m
for i in range(q): 
  a[i],b[i],c[i],d[i]=map(int,input().split())
for j in range(m):
  e[j]=j+1
total=0
maxnum=0
num=1<<len(e)
for current in itertools.combinations_with_replacement(e, n):
  if(len(current)==n):
    total=0
    
    for i in range(q):
      if current[b[i]-1]-current[a[i]-1]==c[i]:
        total+=d[i]
    
    maxnum=max(maxnum,total)
print(maxnum)

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

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

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

Solution (Python):
from itertools import combinations_with_replacement

n,m,q = map(int,input().split())
a = [list(map(int,input().split())) for i in range(q)]
com = list(combinations_with_replacement(range(1,m + 1), n))
max_ = 0

for v in com:
    
    tot = 0
    for u in a:
        if v[u[1] - 1] - v[u[0] - 1] == u[2]:
            tot += u[3]
    if tot > max_:
        max_ = tot
        
print(max_)

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
n = [i+1 for i in range(M)]
A_list = list(itertools.combinations_with_replacement(n,N))
ans = 0
for A in A_list:
    ans_ = 0
    for a, b, c, d in l:
        if A[b-1] - A[a-1] == c:
            ans_ += d
        ans = max(ans, ans_)

print(ans)

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

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

ans = []

for v in combinations_with_replacement(l, N):
  count = 0
  for i in range(Q):
    a, b, c, d = lists[i]
    if v[b-1] - v[a-1] == c:
      count += d
    ans.append(count)
print(max(ans))

Solution (Python):
import itertools
n,m,q=(int(i) for i in input().split())
l=[]
r=[i+1 for i in range(m)]
point=0
def checker(x,l):
  p=0
  for i in range(len(l)):
    if x[l[i][1]]-x[l[i][0]]==l[i][2]:
      p+=l[i][3]
  return p
for i in range(q):
  a,b,c,d=(int(i) for i in input().split())
  a-=1
  b-=1
  l.append([a,b,c,d])
for i in itertools.combinations_with_replacement(r,n):
  p=checker(i,l)
  if p>point:
    point=p
print(point)

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

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

import itertools

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

print(ans)

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

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

def check(t):
    ret=0
    for q in ql:
        if t[q[1]-1]-t[q[0]-1]==q[2]:
            ret+=q[3]

    return ret

print(max(map(check,itertools.combinations_with_replacement(range(1,m+1),n))))

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

ans = 0
for case in combinations([i for i in range(1,n+m)],n):
    now = 0
    A = [x-i for i,x in enumerate(case)]
    for i in range(q):
        a,b,c,d = abcd[i]
        if A[b-1]-A[a-1] == c:
            now += d
    ans = max(now,ans)

print(ans)

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

ans = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
    A = sorted(list(A))
    point = 0
    for abcd in abcd_list:
        if A[abcd[1]-1] - A[abcd[0]-1] == abcd[2]:
            point += abcd[3]
    if point > ans:
        ans = point

print(ans)

Solution (Python):
import sys
import copy
sys.setrecursionlimit(10**9)
N,M,Q = list(map(int,input().split()))
A = []
for n in range(Q):
    a,b,c,d = map(int,input().split())
    A.append((a-1,b-1,c,d))

def dfs(arr, cur):
    if cur == N-1:
        score = 0
        for aa in A:
            a,b,c,d = aa 
            if arr[b]-arr[a] == c:
                score+=d
        return score
    else:
        mx = 0
        for s in range(arr[cur], M+1):
            arr = copy.copy(arr)
            
            for i in range(cur+1, N):
                arr[i] = s
            mx = max(mx, dfs(arr, cur+1))
        return mx


t = dfs([1 for i in range(N)], 0)
print(t)

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

reqs = sorted(reqs, key=lambda x: x[1])

def search(A, req_head):
    if req_head >= len(reqs):
        return 0

    max_score = 0
    for e in range(A[-1], M+1):
        score = 0
        i = req_head
        while i < len(reqs) and reqs[i][1] < len(A):
            i += 1
        while i < len(reqs) and reqs[i][1] == len(A):
            a, b, c, d = reqs[i]
            if e - A[a] == c:
                score += d
            i += 1
        score += search(A + [e], i)
        max_score = max(max_score, score)

    return max_score

print(search([None, 1], 0))

Solution (Python):
import itertools

n, m, q = map(int, input().split())
value = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    value.append([a - 1, b - 1, c, d])
ans = 0
for comb in itertools.combinations_with_replacement(range(1, m + 1), n):
    temp = 0
    for v in value:
        if comb[v[1]] == comb[v[0]] + v[2]:
            temp += v[3]
    ans = max(ans, temp)
print(ans)

Solution (Python):
n,m,s=map(int,input().split())
a=[]
q=[[0]]
while q:
  x=q.pop()
  if len(x)<=n:
    if x[-1]==0:
      for j in range(1,m+1):
        c=x+[j]
        q.append(c)
    else:
      for j in range(x[-1],m+1):
        c=x+[j]
        q.append(c)
  else:
    a.append(x)
li=[tuple(map(int,input().split())) for _ in  range(s)]

ans=0
for A in a:
  s=0
  for i,j,k,l in li:
    if A[j]-A[i]==k:
      s+=l
  ans=max(ans,s)
print(ans)

Solution (Python):
import itertools

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

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

M=0
for l in map(list,itertools.combinations(range(1,n+m),n)):
    s=0
    for j in range(q):
        aj=lis[j][0]
        bj=lis[j][1]
        cj=lis[j][2]
        dj=lis[j][3]
        if l[bj-1]-bj-l[aj-1]+aj==cj:
            s+=dj
    M=max(M,s)
print(M)

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

from itertools import permutations,  combinations, accumulate, product, combinations_with_replacement
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop


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

for _ in range(Q):
    abcd_ls.append(list(map(int, input().split())))

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

max_total = 0
for A in a_candidate:
    total = 0
    for abcd in abcd_ls:
        a, b, c, d = abcd
        a -= 1
        b -= 1
        if c == A[b] - A[a]:
            total += d
    max_total = max(total, max_total)

print(max_total)

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


for i in combinations_with_replacement(range(1,M+1), N):
    X = list(i)
    
    ans_1 = 0
    for k in S:
      if X[k[1]-1] - X[k[0]-1] == k[2]:
        ans_1 += k[3]
    if ans_1 > ans:
        ans = ans_1
      
print(ans)

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

def check(abcd, pattern):
    result = 0
    for a,b,c,d in abcd:
        if (pattern[b-1] - pattern[a-1]) == c:
            result += d
    return result

def main():
    N, M, Q = map(int, input().split())
    abcd = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    for pattern in combinations_with_replacement(range(1, M+1), N):
        ans = max(ans, check(abcd, pattern))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np

from itertools import combinations_with_replacement as CwR



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

Tables = list(CwR(range(1,m+1),n))
Sum_cor = []
for Table in Tables:
    d_sum = 0
    for l in Lis:
        if Table[l[1]-1]-Table[l[0]-1] == l[2]:
            d_sum+=l[3]
    Sum_cor.append(d_sum)

print(max(Sum_cor))

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

Solution (Python):
from itertools import combinations_with_replacement, accumulate

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

ans = 0
for adds in combinations_with_replacement(range(n + 1), m - 1):
    arr = [0] * (n + 1)
    for i in adds:
        arr[i] += 1
    arr = list(accumulate(arr))
    score = 0
    for a, b, c, d in queries:
        if arr[b] - arr[a] == c:
            score += d
    ans = max(ans, score)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement as comb
n, m, q = map(int, input().split())
query = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    query.append((a-1, b-1, c, d))
ans = 0
for p in comb(list(range(1, m+1)), n):
    res = 0
    for a, b, c, d in query:
        if p[b]-p[a]==c:
            res+=d
    ans = max(ans, res)
print(ans)

Solution (Python):
from collections import deque
N, M, Q = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
q = deque()
q.append([1])
while q:
  A = q.pop()
  if len(A) == N+1:
    ans = max(ans, sum([d if A[b]-A[a] == c else 0 for a, b, c, d in ABCD]))
  else:
    for i in range(A[-1], M+1):
      q.append(A+[i])
print(ans)

Solution (Python):
import itertools
N, M, Q = map(int,input().split())
ABCD = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for s in itertools.combinations_with_replacement(range(1,M+1),N):
    temp = 0
    for i in range(Q):
        if s[ABCD[i][1]-1] - s[ABCD[i][0]-1] == ABCD[i][2]:
            temp += ABCD[i][3]
    ans = max(ans,temp)
print(ans)

Solution (Python):
from sys import stdin

[N, M, Q] = [int(x) for x in input().rstrip().split()]
A = []
B = []
C = []
D = []
score_dict = {}
for i in range(Q):
    [a, b, c, d] = [int(x) for x in input().rstrip().split()]
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)


def check_Q(temp_arr):
    tot_val = 0
    for i in range(Q):
        a = A[i]-1
        b = B[i]-1
        c = C[i]
        d = D[i]
        if temp_arr[b] - temp_arr[a] == c:
            tot_val += d

    return tot_val

temp_arr = [1 for _ in range(N)]
max_val = 0
while(True):
    
    temp_val = check_Q(temp_arr)
    
    if temp_val>max_val:
        max_val=temp_val

    for i in range(N):
        temp_arr[N-1-i]+=1
        if temp_arr[N-1-i] == M+1:
            temp_arr[N-1-i] = 0
            continue
        else:
            break

    if temp_arr[0] ==  0:
        break

    min_val=0
    for i in range(1,N):
        if temp_arr[i]<temp_arr[i-1]:
            temp_arr[i] = temp_arr[i-1]
    

print(max_val)

Solution (Python):
import sys

input = sys.stdin.buffer.readline

from itertools import combinations_with_replacement

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

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

kumiawase = list(combinations_with_replacement(range(1, m + 1), n))

ans = 0
for kumi in kumiawase:
    A = [0] + list(kumi)
    temp = 0
    for abcd in ABCD:
        a, b, c, d = abcd
        if A[b] - A[a] == c:
            temp += d
    ans = max(ans, temp)

print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import itertools
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
N, M, Q = LI()

abcd = []

all = 0
for i in range(Q):
    a, b, c, d = LI()
    a -= 1
    b -= 1
    abcd.append([a, b, c, d])
    all += d

abcd = sorted(abcd, key= lambda x:x[3], reverse= True)
ans = 0
if Q <= 2:
    for q in range(Q):
        ans += abcd[q][-1]
    print(ans)
    sys.exit()

def cal_ans(A):
    ans = 0
    for i in range(Q):
        a, b, c, d = abcd[i]
        if A[b] - A[a] == c:
            ans += d
    return ans

def n2kdigit(n, k):
    bi=''
    before = 1
    while n!=0:
        if before <= (n % k):
            
            bi+=str(n%abs(k))
            before = n % k
            if k<0:n=-(-n//k)
            else:n=n//k
        else:
            return None
    if len(bi) < N:
        return None
    return bi
ans = 0

for i in itertools.combinations_with_replacement(range(1, M+1), N):
    
    ans = max(ans, cal_ans(i))
print(ans)

Solution (Python):



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


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


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


def main():
    ans = solve([1])
    print(ans)


if __name__ == "__main__":
    main()

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

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

for i in range(N):
    a = deque([])
    while numbers:
        l = numbers.pop()
        b = l[-1]
        for j in range(b,M+1):
            a.append(l+[j])
    numbers = a

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

ans = 0
while numbers:
    cnt = 0
    L = numbers.pop()
    for x in X:
        a,b,c,d = x
        if L[b-1]-L[a-1]==c:
            cnt+=d
    ans = max(ans,cnt)
print(ans)

Solution (Python):


import itertools
def LI():return list(map(int,input().split()))

n,m,q=LI()
abcd=[]
for _ in range(q):
    a,b,c,d=LI()
    a-=1
    b-=1
    abcd.append((a,b,c,d))


Alist=itertools.combinations_with_replacement(range(m),n)
ans=0
for A in Alist:
    cnt=0
    for a,b,c,d in abcd:
        if A[b]-A[a]==c:
            cnt+=d
    ans=max(ans,cnt)
print(ans)

Solution (Python):



import itertools

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

As = itertools.combinations_with_replacement(range(m), n)


def calc(A):
    ans = 0
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1] == c[i]):
            ans += d[i]
    return ans


print(max([calc(A) for A in As]))

Solution (Python):
import itertools

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

maxx = 0
for p in itertools.combinations_with_replacement(range(M), N):
    sum = 0
    for j in range(Q):
        if p[b[j] - 1] - p[a[j] - 1] == c[j]:
            sum += d[j]
    maxx = max(sum, maxx)
print(maxx)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M, Q = MAP()
L = [LIST() for i in range(Q)]

ans = 0
A_list = list(combinations_with_replacement(range(1, M+1), N))
for A in A_list:
    tmp = 0
    for a, b, c, d in L:
        if A[b-1] - A[a-1] == c:
            tmp += d
    ans = max(tmp,ans)
print(ans)

Solution (Python):
import itertools
def LI():return list(map(int,input().split()))

n,m,q=LI()
abcd=[]
for _ in range(q):
    a,b,c,d=LI()
    a-=1
    b-=1
    abcd.append((a,b,c,d))


Alist=itertools.combinations_with_replacement(range(m),n)
ans=0
for A in Alist:
    cnt=0
    for a,b,c,d in abcd:
        if A[b]-A[a]==c:
            cnt+=d
    ans=max(ans,cnt)
print(ans)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6) 
import math
from copy import copy, deepcopy
from operator import itemgetter

from bisect import bisect_left, bisect, bisect_right

from collections import deque 



from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush


def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds





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

l=[1]*N

def next(i):
    if i==N-1:
        if l[i]==M:
            return True
        else:
            l[i]+=1
            return False
    x=next(i+1)
    if x:
        if l[i]==M:
            return True
        else:
            lp=l[i]+1
            for j in range(i,N):
                l[j]=lp
            return False
    else: return False

def score(l):
    sc=0
    for z in L:
        a,b,c,d=z[0],z[1],z[2],z[3]
        if l[b-1]-l[a-1]==c:
            sc+=d
    return sc

b=False
ans=0
while b==False:
    ans=max(ans,score(l))
    b=next(0)
print(ans)

Solution (Python):
import math
import pprint
import fractions
import collections
import itertools
N,M,Q=map(int,input().split())
req=[list(map(int,input().split())) for _ in range(Q)]
seq=list(range(1,M+1))
l=list(itertools.combinations_with_replacement(seq,N))

score=0
maxscore=0
buf=[]
for i in range(len(l)):
    score=0
    buf=list(l[i])
    for j in range(Q):
        left,right,dist,num=map(int,req[j])
        left=left-1
        right=right-1
        if buf[right]-buf[left]==dist:
            score=score+num
    maxscore=max(score,maxscore)
print(maxscore)

Solution (Python):

lis = []
res = 0


def solve():

    N, M, Q = map(int, input().split())
    e = [list(map(int, input().split())) for i in range(Q)]
    global lis
    lis = [0]*N

    def dfs(x, y):
        global lis, res
        if x == N:
            tmp = 0
            for a, b, c, d in e:
                if lis[b-1]-lis[a-1] == c:
                    tmp += d
            res = max(res, tmp)
            return

        for i in range(y, M+1):
            lis[x] = i
            dfs(x+1, i)

    dfs(0, 1)
    print(res)


if __name__ == "__main__":
    solve()

Solution (Python):

import sys
import numpy as np
from itertools import *


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    l = [i for i in range(1,M+1)]
    ans = 0
    for v in combinations_with_replacement(l, N):
        score = 0
        for j in range(Q):
            
            
            if (v[b[j]-1]-v[a[j]-1])==c[j]:
                score += d[j]
        
        ans = max(ans, score)
    
    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    Q = int(next(tokens))  
    a = [int()] * (Q)  
    b = [int()] * (Q)  
    c = [int()] * (Q)  
    d = [int()] * (Q)  
    for i in range(Q):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations_with_replacement

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

ans = 0

for i in combinations_with_replacement(range(1,M+1),N):
    tmp_ans = 0
    for j in A:
        if i[j[1] - 1] - i[j[0] - 1] != j[2]:
            continue
        tmp_ans += j[3]

    if ans < tmp_ans:
        ans = tmp_ans

print(ans)

Solution (Python):
def c_many_requirements():
    from itertools import combinations_with_replacement
    N, M, Q = [int(i) for i in input().split()]
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    ans = 0
    for sequence in combinations_with_replacement(range(1, M + 1), N):
        score = 0
        for a, b, c, d in Queries:
            a, b = a - 1, b - 1
            if sequence[b] - sequence[a] == c:
                score += d
        ans = max(ans, score)
    return ans

print(c_many_requirements())

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

Solution (Python):
from itertools import combinations_with_replacement

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


def score(A, ABCD):
  ans = 0
  for i in range(Q):
    x = ABCD[i]
    if A[x[1] - 1] - A[x[0] - 1] == x[2]:
      ans += x[3]
  return ans


b = []
for i in range(M):
  b.append(i + 1)
  
A = list(combinations_with_replacement(b, N))

ans = 0
for i in A:
  B = list(i)
  ans = max(ans, score(B, ABCD))
print(ans)

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

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

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

ans=0
for a in itertools.combinations_with_replacement(num,N):
    tmp_sum=0
    for i in range(Q):
        if a[B[i]]-a[A[i]]==C[i]:
            tmp_sum+=D[i]
    ans=max(ans,tmp_sum)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement as CwR

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

A = list(CwR(range(1,M+1),N))
lis = list()
for i in range(Q):
    a,b,c,d = map(int,input().split())
    a,b = a-1,b-1
    lis.append([a,b,c,d])

ans = 0
for A_ in A:
    tmp = 0
    for a,b,c,d in lis:
        if A_[b] - A_[a] == c:
            tmp += d
    ans = max(ans,tmp)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement as CwR

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

A = list(CwR(range(1,M+1),N))
lis = list()
for i in range(Q):
    a,b,c,d = map(int,input().split())
    a,b = a-1,b-1
    lis.append([a,b,c,d])

ans = 0
for A_ in A:
    tmp = 0
    for a,b,c,d in lis:
        if A_[b] - A_[a] == c:
            tmp += d
    ans = max(ans,tmp)

print(ans)

Solution (Python):

"""
Created on Sat May  2 22:58:08 2020

@author: Kei
"""

import numpy as np
import itertools

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

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



a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q

for i in range(0,Q):

    a[i],b[i],c[i],d[i] = map(int,input().split())
    

max_point = 0
    
for i in range(0,len(A)):
    point = 0
    for j in range (0,Q):
        if A[i][b[j]-1]-A[i][a[j]-1] == c[j]:
            point += d[j]
    max_point = max(point,max_point)
    













print(max_point)

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

Solution (Python):



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

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

ai=set([])

def zentan(ss,x,i):
    
    global n,m,ai
    if i==n-1:
        for j in range(x,m):
            ai.add(ss+str(j))
    else:
        for j in range(x,m):
            zentan(ss+str(j),j,i+1)

result=0
zentan("",0,0)


for item in ai:
    point=0
    checked=[0]*n
    for item2 in abcd:
        
        a,b,c,d=item2[0]-1,item2[1]-1,item2[2],item2[3]
        if int(item[a])+c==int(item[b]):
            point+=d
    result=max(result,point)

print(result)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product
import sys
import bisect
import string
import math
import time



def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False


def rec(li, i, M, N, a, b, c, d, cans):
    
    if len(li) >= N:
        
        ans = 0
        for j in range(len(a)):
            if li[b[j]-1] - li[a[j]-1] == c[j]:
                ans += d[j]
        
        return ans

    
    

    ans = 0
    if len(cans[i]) == 0:
        ans = max(ans, rec(li + [1], i+1, M, N, a, b, c, d, cans))
    else:
        for v in cans[i]:
            ans = max(ans, rec(li + [v], i+1, M, N, a, b, c, d, cans))

    return ans


def main():
    N, M, Q = MI()
    a = [0] * Q
    b = [0] * Q
    c = [0] * Q
    d = [0] * Q
    didx = [None] * Q
    lis = [[[] for i in range(N+1)] for i in range(N+1)]
    cans = [set() for i in range(N+1)]

    for i in range(Q):
        a[i], b[i], c[i], d[i] = MI()
        

    ans = 0
    A = [i for i in range(1, M + 1)]
    for v in itertools.combinations_with_replacement(A, N):
        tmp = 0
        for j in range(len(a)):
            if v[b[j]-1] - v[a[j]-1] == c[j]:
                tmp += d[j]
        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input():
    return sys.stdin.readline().rstrip('\n')

def make_a(n, M, L):
    if n == 0:
        return L
    W = len(L[0])
    newL = []
    for l in L:
        for i in range(l[W - 1], M + 1):
            newL += [list(l + [i])]
    return make_a(n - 1, M, newL)

def calc(N, M, Q, R):
    result = 0
    for A in make_a(N - 1, M, [[1]]):
        score = 0
        for a, b, c, d in R:
            score += (d if A[b - 1] - A[a - 1] == c else 0)
        result = max(score, result)
    return result


(N, M, Q) = tuple([int(s) for s in input().split()])
R = [tuple([int(s) for s in input().split()]) for _ in range(Q)]
print(calc(N, M, Q, R))