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())
a = [0 for _ in range(q)]
b = [0 for _ in range(q)]
c = [0 for _ in range(q)]
d = [0 for _ in range(q)]
for i in range(q):
    a[i],b[i],c[i],d[i] = map(int,input().split())
e = list(map(list,itertools.combinations_with_replacement([i+1 for i in range(m)],n)))
ans = 0
for i in range(len(e)):
    current = 0
    for j in range(q):
        if e[i][b[j]-1] - e[i][a[j]-1] == c[j]:
            current += d[j]
    ans = max(ans,current)
print(ans)

Solution (Python):
from collections import deque

N, M, Q = map(int, input().split(" "))
que = deque()
for i in range(1, M + 1):
    que.append([i])

while True:
    now = que.popleft()
    if len(now) == N:
        que.append(now)
        break

    last = now[-1] if len(now) > 0 else 1
    for i in range(last, M + 1):
        que.append(now + [i])

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

ans = 0
for seq in que:
    score = 0
    for i in range(Q):
        a, b, c, d = cond[i]
        if seq[b - 1] - seq[a - 1] == c:
            score += d
    ans = max(ans, score)
print(ans)

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

vs = []

def dfs(u, A):
    if len(A) == N:
        v = 0
        for j in range(Q):
            a, b, c, d = q[j]
            if A[b-1]-A[a-1] == c:
                v += d
        vs.append(v)
    else:
        for i in range(u, M+1):
            dfs(i, A+[i])
    
dfs(1, [1])
print(max(vs))

Solution (Python):
from itertools import permutations, accumulate, combinations, combinations_with_replacement
import sys
input = sys.stdin.readline


def main():
    n, m, q = map(int, input().split())
    
    ans = 0
    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[i] -= 1
        b[i] -= 1
    for A in combinations_with_replacement(range(1, m + 1), n):
        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)


if __name__ == '__main__':
    main()

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

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

ans = 0
N = [i+1 for i in range(m)]

for v in combinations_with_replacement(N,n):
    num = 0
    
    for i in range(q):
        if v[b[i]] - v[a[i]] == c[i]:
            num += d[i]
    ans = max(ans,num)
print(ans)

Solution (Python):
import sys
from itertools import combinations_with_replacement

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

def main():
    n, m, q = map(int, read().split())
    abcd = [[int(i) for i in read().split()] for _ in range(q)]
    s = 0
    for p in combinations_with_replacement(range(1, m + 1), n):
        t = 0
        for a, b, c, d in abcd:
            t += d * (p[b - 1] - p[a - 1] == c)
        s = max(s, t)
    print(s)

if __name__ == '__main__':
    main()

Solution (Python):
n,m,q=map(int,input().split())
t=[list(map(int,input().split())) for _ in range(q)]
ans=0
import itertools
for p in itertools.combinations_with_replacement(range(1,m+1),n):
    d=0
    for i in t:
        if p[i[1]-1]-p[i[0]-1]==i[2]:
            d+=i[3]
    ans=max(d,ans)        
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

def main():
    n, m, q = map(int, input().split())
    abcd = [[int(i) for i in input().split()] for _ in range(q)]
    s = 0
    for p in combinations_with_replacement(range(1, m + 1), n):
        t = 0
        for a, b, c, d in abcd:
            t += d * (p[b - 1] - p[a - 1] == c)
        s = max(s, t)
    print(s)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
abcd = [[int(i) for i in input().split()] for _ in range(q)]
s = 0
for p in combinations_with_replacement(range(1, m + 1), n):
    t = 0
    for a, b, c, d in abcd:
        t += d * (p[b - 1] - p[a - 1] == c)
    s = max(s, t)
print(s)

Solution (Python):
comb = []
def f(n,cnt,ls):
    if cnt == n:
        comb.append(ls)
        return
    for i in range(ls[cnt-1],m+1):
        tmp = ls.copy()
        tmp[cnt] = i
        f(n,cnt+1,tmp)
    return
ma = lambda :map(int,input().split())
ni = lambda:int(input())

n,m,q = ma()
abcd = []
for i in range(q):
    abcd.append(list(ma()))
ls = [1]*(n+1)
f(n,0,ls)
ans = 0
for A in  comb:
    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)

Solution (Python):
import itertools

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



def calculate(A, abcd):
    total = 0
    for abcdi in abcd:
        a = abcdi[0] - 1
        b = abcdi[1] - 1
        c = abcdi[2]
        d = abcdi[3]
        if A[b] - A[a] == c:
            total += d
    

    return total

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


maximum = max([calculate(Ai, abcd) for Ai in A])

print(maximum)

Solution (Python):
n,m,q=map(int,input().split())
abcd=[]
for _ in range(q):
	a,b,c,d=map(int,input().split())
	abcd.append((a,b,c,d))
ans=0
import itertools
p=itertools.combinations_with_replacement(range(1,m+1),n)
for x in p:
	r=0
	for i in range(q):
		if x[abcd[i][1]-1]-x[abcd[i][0]-1]==abcd[i][2]:
			r+=abcd[i][3]
	ans=max(ans,r)
print(ans)

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



def calculate(A, abcd):
    total = 0
    for abcdi in abcd:
        a = abcdi[0] - 1
        b = abcdi[1] - 1
        c = abcdi[2]
        d = abcdi[3]
        if A[b] - A[a] == c:
            total += d
    

    return total

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


maximum = 0
for Ai in A:
    maximum = max(calculate(Ai, abcd), maximum)

print(maximum)

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

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

def dfs(A,LEN):
  if LEN == n:
    global ans
    tmp = score(A)
    ans = max(ans, tmp)
  else:
    for i in range(A[-1],m+1):
      dfs(A+[i],LEN+1)

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

Solution (Python):
import itertools

n, m, q = map(int, input().split())
kumi = []
for i in range(q):
    kumii = list(map(int, input().split()))
    kumi.append(kumii)

ans = 0
for a in itertools.combinations_with_replacement(range(1, m+1), n):
    temp = 0
    for i in range(q):
        ai, bi, ci, di = kumi[i]
        ai, bi = ai-1, bi-1
        if a[bi] - a[ai] == ci:
            temp += di
    ans = max(ans, temp)

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))
As=itertools.combinations_with_replacement(range(1,m+1),n)

ans=0

for A in As:
  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 = list(itertools.combinations_with_replacement(range(1, M + 1), N))
B = []

for a in A:
    for j in range(N - 1):
        flag = True
        if a[j] > a[j + 1]:
            flag = False
            break
    if flag:
        B.append(a)

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

for i in range(Q):
    o, p, q, r = map(int, input().split())
    o -= 1
    p -= 1
    a.append(o)
    b.append(p)
    c.append(q)
    d.append(r)

ans = 0
for li in B:
    tmp = 0
    for i in range(Q):
        if li[b[i]] - li[a[i]] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

ans = 0
for x in combinations_with_replacement(range(1, M + 1), N):
    tmp = 0
    for i in range(Q):
        if x[X[i][1] - 1] - x[X[i][0] - 1] == X[i][2]:
            tmp += X[i][3]
    ans = max(ans, tmp)

print(ans)

Solution (Python):
def dfs2(i):
    global ans
    for j in range(num[i-1], m):
        num[i] = j
        if i == n:
            now = 0
            for k in range(q):
                if num[b[k]] - num[a[k]] == c[k]:
                    now += d[k]
            ans = max(ans, now)
        else:
            dfs2(i+1)


n, m, q = map(int, input().split())
a, b, c, d = [], [], [], []
ans = 0

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)

num = [0] * (n+1)


dfs2(1)
print(ans)

Solution (Python):

import itertools

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

ans = 0
for ite in itertools.combinations_with_replacement(range(1,M+1), N):
    val = 0
    for q in data:
        if ite[q[1]-1] - ite[q[0]-1] == q[2]:
            val += q[3]
    ans = max(ans, val)

print(ans)

Solution (Python):
import itertools

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

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

print(ans)

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


def dfs(x):
    if len(x) == N + 1:
        tmp = 0
        for i in range(Q):
            if x[X[i][1]] - x[X[i][0]] == X[i][2]:
                tmp += X[i][3]
        return tmp

    x.append(x[-1])
    res = 0
    while x[-1] <= M:
        res = max(res, dfs(x))
        x[-1] += 1
    x.pop()

    return res

print(dfs([1]))

Solution (Python):
from itertools import *

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

numbers = []
for i in range(1, m + 1):
    numbers.append(i)
    
cwr = list(combinations_with_replacement(numbers, n))

ans = 0
for c in cwr:
    cnt = 0
    for i in range(q):
        if c[abcd[i][1]] - c[abcd[i][0]] == abcd[i][2]:
            cnt += abcd[i][3]
    
    ans = max(ans, cnt)

print(ans)

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

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

ans=0
for status in co:
    tmp_score=0
    for a,b,c,d in lis:
        if status[b-1]-status[a-1]==c:
            tmp_score+=d
    ans=max(ans,tmp_score)
print(ans)

Solution (Python):
import math
import itertools

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

A = [i for i in range(1,m+1)]
sum = 0

for k in itertools.combinations_with_replacement(A,n):
  sum_tmp = 0
  for i in range(q):
    if k[a[i][1] - 1] - k[a[i][0] - 1] == a[i][2]:
        sum_tmp += a[i][3]
  
  if sum_tmp > sum:
    sum = sum_tmp

print(sum)

Solution (Python):


import itertools


N, M, Q = list(map(int, input().split()))
a, b, c, d = [], [], [], []
for q in range(Q):
    a_q, b_q, c_q, d_q = list(map(int, input().split()))
    a.append(a_q)
    b.append(b_q)
    c.append(c_q)
    d.append(d_q)


def get_score(A: tuple) -> int:
    """得点を計算するメソッド

    Arguments:
        A {tuple} -- 数列

    Returns:
        int -- 得点
    """
    score = 0
    for a_i, b_i, c_i, d_i in zip(a, b, c, d):
        score += (A[b_i-1] - A[a_i-1] == c_i) * d_i
    return score


def main() -> None:
    """Entry point
    """
    
    
    l = list(range(1, M+1))
    result = 0
    for A in itertools.combinations_with_replacement(l, N):
        result = max(result, get_score(A))

    
    print(result)


if __name__ == "__main__":
    main()

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

list = []
for i in range(1, m+1):
    list.append(i)
 
 
wa = 0
ans = 0
 
for i in itertools.combinations_with_replacement(list, n):
    for a,b,c,d in abcd:
        
        
        if i[b-1]-i[a-1]==c:
            wa += d
            ans = max(ans, wa)
    wa = 0
 
print(ans)

Solution (Python):
import queue

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

q = queue.Queue()
q.put([1])
while not q.empty():
    p = q.get()
    if len(p)==n+1:
        q.put(p)
        break
    for i in range(p[-1],m+1):
        q.put(p+[i])

ans = 0
while not q.empty():
    p = q.get()
    score = 0
    for a,b,c,d in g:
        if p[b]-p[a] == c:
            score += d
    ans = max(score,ans)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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

op = []

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

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

ans = 0
for arr in combinations_with_replacement(array, n):
    score = 0
    for a, b, c, d in op:
        if arr[b - 1] - arr[a - 1] == c:
            score += d
    ans = max(ans, score)

print(ans)

Solution (Python):
def resolve():
    import itertools
    n, m, q = map(int, input().split())
    cond = [list(map(int, input().split())) for _ in range(q)]
    ans = 0
    for i in itertools.combinations_with_replacement(range(1, m+1), n):
        point = 0
        for j in range(q):
            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)
resolve()

Solution (Python):
def resolve():
    import itertools
    n, m, q = map(int, input().split())
    cond = [list(map(int, input().split())) for _ in range(q)]
    ans = 0
    for i in list(itertools.combinations_with_replacement(range(1, m+1), n)):
        point = 0
        for j in range(q):
            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)
resolve()

Solution (Python):
def resolve():
    import itertools
    n, m, q = map(int, input().split())
    cond = [list(map(int, input().split())) for _ in range(q)]
    
    ans = 0
    for i in list(itertools.combinations_with_replacement(range(1, m+1), n)):
        
        point = 0
        for j in range(q):
            if i[cond[j][1]-1] - i[cond[j][0]-1] == cond[j][2]:
                point += cond[j][3]
        ans = max(ans, point)
    print(ans)
resolve()

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

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

ans = 0


def solve(A):
    if len(A) >= n + 1:
        global ans
        now = 0
        for a, b, c, d in abcd:
            if A[b] - A[a] == c:
                now += d
        ans = max(ans, now)
        return
    for i in range(A[-1], m + 1):
        solve(A + [i])


solve([1])
print(ans)

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

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

Solution (Python):


from itertools import combinations_with_replacement

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

x = max(sum(d for a, b, c, d in lst if seq[b-1] - seq[a-1] == c)
        for seq in combinations_with_replacement(range(1, m+1), n))
print(x)

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

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

def main():
    mod=10**9+7
    N,M,Q=MI()
    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]=MI()
    
    
    st=[]
    st2=[]
    for i in range(1,M+1):
        st.append([i])
    
    for dig in range(N):
        while len(st)!=0:
            L=st.pop()
            v=L[-1]
            for i in range(v,M+1):
                st2.append(L+[i])
                
        while len(st2)!=0:
            L=st2.pop()
            st.append(L)
            
    ans=0
    for i in range(len(st)):
        L=st[i]
        temp=0
        for j in range(Q):
            if L[b[j]-1]-L[a[j]-1]==c[j]:
                temp+=d[j]
        ans=max(ans,temp)
    print(ans)
    
    
    
main()

Solution (Python):
import sys
from collections import namedtuple
from typing import List

sys.setrecursionlimit(2 ** 20)


R = namedtuple('R', 'a b c d')

N, M, Q = map(int, input().split())
requirements: List[R] = []

for _ in range(Q):
  a, b, c, d = map(int, input().split())
  requirements.append(R(a, b ,c, d))

max_score = 0

def dfs(A):
  global max_score
  if len(A) > N + 1:
    score = 0
    for a, b, c, d in requirements:
      if A[b] - A[a] == c:
        score += d
    max_score = max(max_score, score)
    return
  for i in range(A[-1], M + 1):
    dfs(A + [i])

dfs([1])
print(max_score)

Solution (Python):
import itertools

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

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

l = itertools.combinations_with_replacement(range(1,m+1),n)
ans = 0
for li in l:
  ans = max(check(li),ans)
print(ans)

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

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

ans = 0
def dfs(l):
  global ans
  if len(l) == n:
    ans = max(check(l),ans)
  else:
    if len(l) == 0:
      mx = 1
    else:
      mx = l[-1]
    for i in range(mx,m+1):
      tmpl = l + [i]
      dfs(tmpl)

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

dfs([])
print(ans)

Solution (Python):
import itertools
 
n,m,q=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(q)]
 
ms=[i for i in range(1,m+1)]
ans=0
for k in itertools.combinations_with_replacement(ms, n):
  x=0
  for j in range(q):
    if a[j][2]==k[a[j][1]-1]-k[a[j][0]-1]:
      x+=a[j][3]
  if ans<x:
    ans=x
print(ans)

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

import itertools
cr = itertools.combinations_with_replacement(range(1, m+1), n)
M = 0
for seq in cr:
  score = sum([d for a, b, c, d in abcd if seq[b-1] - seq[a-1] == c])
  M = max(M, score)
print(M)

Solution (Python):
def judge():
    global num, a, b, c, d, q
    point = 0
    for i in range(q):
        if (num[b[i]] - num[a[i]] == c[i]):
            point += d[i]
    return point


def mk_num(i):
    global mx_p
    global m
    for j in range(num[i-1], m):
        num[i] = j
        if (i == n):
            tmp = judge()
            if (mx_p < tmp):
                mx_p = tmp
        else:
            mk_num(i+1)


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

num = [0] * (n+1)
mx_p = 0

mk_num(1)
print(mx_p)

Solution (Python):
import itertools
n, m, q = map(int, input().split())
a = []
b = []
c = []
d = []
for i in range(q):
	aa, bb, cc, dd = map(int, input().split())
	a.append(aa)
	b.append(bb)
	c.append(cc)
	d.append(dd)
ary = [i+1 for i in range(m)]
ans = 0
for i in itertools.combinations_with_replacement(ary,n-1):
	x = list(i)
	x.append(1)
	x.sort()
	cnt = 0
	for j in range(q):
		if x[b[j]-1] - x[a[j]-1] == c[j]:
			cnt += d[j]
	ans = max(ans,cnt)
print (ans)

Solution (Python):
from functools import lru_cache

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

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

@lru_cache(maxsize=None)
def search(mi, A):
    if len(A) > N:
        return 0

    ret = 0
    for i in range(1, A[-1] + 1):
        ret = max(ret, search(i, A + (i,)))

    d = len(A)
    for b, c, d in BCD[N - d]:
        if A[-(b + 1)] - A[-1] == c:
            ret += d
    return ret

ans = 0
for i in range(1, M + 1):
    ans = max(ans, search(i, (i,)))
print(ans)

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

def dfs(pos,a,m,cs):
    if pos==len(a):
        s=0
        for i in cs:
            if a[i[1]-1]-a[i[0]-1]==i[2]:s+=i[3]
        return s

    ans=0
    if pos==0:low=1
    else:low=a[pos-1]
    for i in range(low,m+1):
        a[pos]=i
        ans=max(ans,dfs(pos+1,a,m,cs))  
    return ans





print(dfs(0,[0]*n,m,cs))

Solution (Python):
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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



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

n = len(A) 
score = np.zeros(n, np.int32) 

m = map(int, read().split())
for a, b, c, d in zip(m, m, m, m):
    cond = A[:, b - 1] - A[:, a - 1] == c
    score += d * cond

print(score.max())

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

A=[1]*(n+1)
mx=0
while A[0]==1:
    s=0
    for i in range(0,q):
        if A[cs[i][1]]-A[cs[i][0]]==cs[i][2]:s+=cs[i][3]
    mx=max(mx,s)
    k=n
    while A[k]==m and k>=1:
        k-=1
    A[k]+=1
    for i in range(k+1,n+1):
        A[i]=A[i-1]
print(mx)

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

ans = 0
for an in itertools.combinations_with_replacement(range(1,M+1),N):
    score = 0
    for a,b,c,d in query:
        if an[b-1] - an[a-1] == c:
            score += d
    ans = max(ans,score)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
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())

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

A = combinations_with_replacement(B, n)
ans = []
for item in A:
    res = 0
    for i in range(q):
        if item[b[i]-1] - item[a[i]-1] == c[i]:
            res += d[i]
            ans.append(res)
print(max(ans))

Solution (Python):

N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
for i in range(len(abcd)):
    abcd[i][0] -= 1
    abcd[i][1] -= 1
    
options = ["A","B"]   
all_patterns = options

for i in range(N+M-2):
    all_patterns = [i+j for j in options for i in all_patterns if str(i+j).count("A") <=N and str(i+j).count("B") <= M-1]

for i in range(len(all_patterns)):
    num = []
    count = 1
    for j in range(N+M-1):
        if all_patterns[i][j] == "A":
            num.append(count)
        else:
            count+=1
    all_patterns[i] = num

all_scores = [0 for _ in range(len(all_patterns))]
for i, char in enumerate(all_patterns):
    for a,b,c,d in abcd:
        if int(char[b]) - int(char[a]) == c:
            all_scores[i]+= d

print(max(all_scores))

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

R=[range(i,m) for i in range(m)]
def dfs(d,s,l):
    global n,m,Q,ans
    if d==n:
        ans_=0
        for k in Q:
            a,b,c,e=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=e
        ans=max(ans,ans_)
    else:
        for i in R[l]:
            dfs(d+1,s+str(i),i)
dfs(0,"",0)

print(ans)

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

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

s = [1] * (n)


ans = 0

def dfs(k, c,old):
    global ans
    
    
    
    if k == n:
        tmp = 0
        for i in range(q):
            if s[abcd[i][1]-1] - s[abcd[i][0]-1] == abcd[i][2]:
                tmp += abcd[i][3]
        ans = max(ans,tmp)
        
        return
    for i in range(old,c+1):
        
        s[k] = i
        dfs(k+1, m,i)
        
    
    
    


dfs(0, m,1)
print(ans)

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

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

s = [1] * (n)


ans = 0

def dfs(k, c,old):
    global ans
    
    
    
    if k == n:
        tmp = 0
        for i in range(q):
            if s[abcd[i][1]-1] - s[abcd[i][0]-1] == abcd[i][2]:
                tmp += abcd[i][3]
        ans = max(ans,tmp)
        
        return
    for i in range(old,c+1):
        
        s[k] = i
        dfs(k+1, m,i)
        
    
    
    


dfs(0, m,1)
print(ans)

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

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

Solution (Python):
import sys
import itertools
import collections

N, M, Q = map(int, input().split())
As =[0]
Bs =[0]
Cs =[0]
Ds =[0]
 
for i in range(Q):
	a, b, c, d = map(int, input().split())
	As.append(a)
	Bs.append(b)
	Cs.append(c)
	Ds.append(d)
 
lst = range(1, M+1)
AAss = list(itertools.combinations_with_replacement(lst, N))

rCs = collections.Counter()

	
	
max_score = 0


for AAs in AAss:
	score = 0
	
	
	AAs = [0] + list(AAs)
	for i in range(1,Q+1):
		
		
		if Cs[i] == AAs[Bs[i]] - AAs[As[i]]:
			score+=Ds[i]
		
 
	if score > max_score:
		max_score = score
	
	
print(max_score)

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[i] -= 1
    b[i] -= 1

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

print(ans)

Solution (Python):
import itertools
n,m,q = map(int,input().split())
ch = list(itertools.combinations_with_replacement(range(1,m+1),n))
lch = len(ch)
rel = [0]*lch
l = [list(map(int,input().split())) for _ in range(q)]
for k in range(lch):
    for i in range(q):
        if ch[k][l[i][1]-1]-ch[k][l[i][0]-1]==l[i][2]:
            rel[k]+=l[i][3]
print(max(rel))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from itertools import combinations_with_replacement
def main():
    n,m,q=map(int,input().split())
    lis=list(combinations_with_replacement(range(1,m+1),n))
    ans=0
    Q=[[int(_) for _ in input().split()] for i in range(q)]
    for l in lis:
        cunt=0
        for q in Q:
            if int(l[q[1]-1])-int(l[q[0]-1])==q[2]:
                cunt+=q[3]
        ans=max(ans,cunt)
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from itertools import combinations_with_replacement
def main():
    n,m,q=map(int,input().split())
    lis=list(combinations_with_replacement(range(1,m+1),n))
    ans=0
    Q=[[int(_) for _ in input().split()] for i in range(q)]
    for l in lis:
        cunt=0
        for q in Q:
            if int(l[q[1]-1])-int(l[q[0]-1])==q[2]:
                cunt+=q[3]
        ans=max(ans,cunt)
    print(ans)
if __name__=='__main__':
    main()

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

ans = 0
for pat in combinations_with_replacement(range(1, m + 1), n):
    sm = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if pat[b] - pat[a] == c:
            sm += d

    ans = max(ans, sm)

print(ans)