Problem:
 Score : 300 points    Problem Statement Given are positive integers N , M , Q , and Q quadruples of integers ( a_i , b_i , c_i , d_i ).  Consider a sequence A satisfying the following conditions:   A is a sequence of N positive integers.  1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M .   Let us define a score of this sequence as follows:   The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i . (If there is no such i , the score is 0 .)   Find the maximum possible score of A .      Constraints  All values in input are integers.  2 ≤ N ≤ 10  1 \leq M \leq 10  1 \leq Q \leq 50  1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )  0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )  (a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j )  1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )         Input Input is given from Standard Input in the following format:  N  M  Q  a_1  b_1  c_1  d_1  :  a_Q  b_Q  c_Q  d_Q       Output Print the maximum possible score of A .        Sample Input 1 3 4 3
1 3 3 100
1 2 2 10
2 3 2 10      Sample Output 1 110  When A = \{1, 3, 4\} , its score is 110 . Under these conditions, no sequence has a score greater than 110 , so the answer is 110 .       Sample Input 2 4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328      Sample Output 2 357500       Sample Input 3 10 10 1
1 10 9 1      Sample Output 3 1

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

ans = 0


def dfs(s):
    global ans
    if len(s) == N:
        now = 0
        for a, b, c, d in G:
            if s[b - 1] - s[a - 1] == c:
                now += d
        ans = max(ans, now)
        return

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


s = [0]
dfs(s.copy())

print(ans)

Solution (Python):
from itertools import combinations_with_replacement
N, M, Q = map(int,input().split())
ls = []
for _ in range(Q):
    a,b,c,d = map(int,input().split())
    ls.append((a-1,b-1,c,d))
ans = -1
for A in combinations_with_replacement(range(1,M+1),r=N):
    sm = 0
    for a,b,c,d in ls:
        if A[b]-A[a] == c:
            sm += d
    ans = max(ans, sm)
print(ans)

Solution (Python):
def f(n, m, arrs=[], arr=[]):
    if len(arr) == n:
        arrs.append(arr)
    else:
        if len(arr) == 0:
            for i in range(1, m + 1):
                f(n, m, arrs, arr + [i])
        else:
            for i in range(arr[-1], m + 1):
                f(n, m, arrs, arr + [i])
    return arrs


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

ans = 0
for ball in f(n, m):
    a = 0
    
    for q in Q:
        if ball[q[1] - 1] - ball[q[0] - 1] == q[2]:
            a += q[3]
    ans = max(ans, a)
print(ans)

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

candidate = []


def gen(cur):
    if len(cur) == n:
        candidate.append(cur)
    else:
        t = cur[-1]
        for tv in range(t, m + 1):
            nex = cur[:]
            nex.append(tv)
            gen(nex)


for i in range(1, m + 1):
    arr = [i]
    gen(arr)

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

ans = 0
for cv in candidate:
    tmp = 0
    for av in ask:
        if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
            tmp += av[3]
    ans = max(ans, tmp)
print(ans)

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

candidate = []


def gen(cur):
    if len(cur) == n:
        candidate.append(cur)
    else:
        t = cur[-1]
        for tv in range(t, m + 1):
            nex = cur[:]
            nex.append(tv)
            gen(nex)


for i in range(1, 10):
    arr = [i]
    gen(arr)

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

ans = 0
for cv in candidate:
    tmp = 0
    for av in ask:
        if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
            tmp += av[3]
    ans = max(ans, tmp)
print(ans)

Solution (Python):
import itertools
N, M, Q = map(int,input().split())
ABCD_list = [[int(_) for _ in input().split()] for i in range(Q)]
combs = itertools.combinations_with_replacement(range(1,M+1),N)
points = []
for comb in combs:
    A_list = list(comb)
    point = 0
    for inner in ABCD_list:
        if A_list[inner[1]-1]-A_list[inner[0]-1] == inner[2]:
            point += inner[3]
        points.append(point)
print(max(points))

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

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

def calc(B, n):
  ans = 0
  
  if len(B) < N:
    for i in range(n, M + 1):
      ans = max(ans, calc(B + [i], i))
  
  else:
    
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    
  return ans



print(calc([], 1))

Solution (Python):
def dfs(i, p, seq):
    global N, M
    ret = 0
    if i == N:
        ret = check(seq)
    else:
        for x in range(p, M + 1):
            ret = max(ret, dfs(i + 1, x, tuple(list(seq) + [x])))
    return ret


def check(seq):
    cnt = 0
    for a, b, c, d in query:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    return cnt


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

print(dfs(0, 1, tuple()))

Solution (Python):
def resolve():
    N, M, Q = list(map(int, input().split()))
    Q = [list(map(int, input().split())) for _ in range(Q)]
    import itertools
    maxpoint = 0
    for seq in itertools.combinations_with_replacement(range(1, M+1), N):
        point = 0
        for a, b, c, d in Q:
            if seq[b-1] - seq[a-1] == c:
                point += d
        maxpoint = max(maxpoint, point)
    print(maxpoint)

if '__main__' == __name__:
    resolve()

Solution (Python):
def return_d(A,abcds):
    sum_d=0
    for abcd in abcds:
        a,b,c,d=abcd
        if A[b]-A[a]==c:
            sum_d+=d
    return sum_d

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


As=[]
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):
                                        As.append([0,i_1,i_2,i_3,i_4,i_5,i_6,i_7,i_8,i_9,i_10])


ans=0
for A in As:
    ans=max(ans,return_d(A,abcds))

print(ans)

Solution (Python):
N, M, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(list(map(int, input().split())))
def f(B, n):
  ans = 0
  if len(B) < N:
    for i in range(n, M+1):
      ans = max(ans, f(B+[i], i))
  else:
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    return ans
  return ans
print(f([], 1))

Solution (Python):

def f(A, N, M, Q, conditions):
    L = len(A) - 1

    if L > N:
        return

    elif L < N:
        fans = 0
        for j in range(A[L], M + 1):
            fans = max(fans ,f(A + [j], N, M, Q, conditions))

    else: 
        fans = 0
        for i in range(Q):
            a, b, c, d = conditions[i]
            if A[b] - A[a] == c:
                fans += d
        
    return fans

N, M, Q = [int(x) for x in input().split()]

conditions = []
for i in range(Q):
    c = [int(x) for x in input().split()]
    conditions.append(c)

ans = f([1], N, M, Q, conditions)

print(ans)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))


def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if lst else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))


def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        len_lst = len(lst)
        if len_lst == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len_lst > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))


def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len(lst) > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))


def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1]
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([1]))


if __name__ == '__main__':
    main()

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

ans = 0

for C in itertools.combinations_with_replacement(range(1,m+1),n):
    x = 0
    for i in range(q):
        if C[b[i]-1] - C[a[i]-1] == c[i]:
            x += d[i]

    ans = max(ans,x)

print(ans)

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

Solution (Python):
import itertools

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


arrs = []
for arr in itertools.combinations_with_replacement(range(1, M + 1), N):
    arrs.append(list(arr))



answer = - float("inf")
for arr in arrs:
    tmp = 0
    for a, b, c, d in ABCD:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    answer = max(answer, tmp)

print(answer)

Solution (Python):
import itertools

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


arrs = []
for arr in itertools.combinations_with_replacement(range(1, M + 1), N):
    arrs.append(list(arr))

answer = - float("inf")
for arr in arrs:
    tmp = 0
    for a, b, c, d in ABCD:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    answer = max(answer, tmp)

print(answer)

Solution (Python):
def backtrack(nums, N, cur_state, result):
    if len(cur_state) == N:
        result.append(cur_state[:])
    else:
        for i in range(len(nums)):
            cur_state.append(nums[i])
            backtrack(nums[i:], N, cur_state, result)
            cur_state.pop()

def solve():
    N,M,Q = [int(i) for i in input().split()]
    quads = []
    for i in range(Q):
        quads.append([int(i) for i in input().split()])
    candidates = []
    backtrack(list(range(1, M+1)), N, [], candidates)

    ans = 0
    for candidate in candidates:
        score = 0
        for a,b,c,d in quads:
            if candidate[b-1] - candidate[a-1] == c:
                score += d
        ans = max(score, ans)
    print(ans)

if __name__ == "__main__":
    solve()

Solution (Python):
from itertools import combinations_with_replacement
ans = 0
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
for A in combinations_with_replacement(range(1, M+1), N):
    tmp = 0
    for i in range(Q):
        a,b,c,d = abcd[i]
        a-=1
        b-=1
        if A[b]-A[a] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

Solution (Python):
import itertools as itr
N, M, Q = map(int, input().split())
req = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    a -= 1; b -= 1;
    req.append((a, b, c, d))


ans = -1
for pattern in itr.combinations_with_replacement(range(1, M+1), N):
    A = pattern
    tmp = 0
    for reqirements in req:
        a, b, c, d = reqirements
        if A[b] - A[a] == c:
            tmp += d

        ans = max(ans, tmp)

print(ans)

Solution (Python):
import itertools

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

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

    ans = 0
    for A in list(itertools.combinations_with_replacement(range(1, M+1), N)):

        score = 0
        for i in range(Q):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                score += d[i]
        if ans < score:
            ans = score

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

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

    ans = 0
    for A in list(itertools.combinations_with_replacement(range(1, M+1), N)):

        score = 0
        for i in range(Q):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                score += d[i]
        if ans < score:
            ans = score

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import itertools

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

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

ans = 0
for A in list(itertools.combinations_with_replacement(range(1, M+1), N)):

    score = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            score += d[i]
    if ans < score:
        ans = score


print(ans)

Solution (Python):
from itertools import combinations_with_replacement

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


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

print(ans)

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

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))
max_score = 0
def dfs(A):
    global max_score
    if len(A) == N:
        
        score = 0
        for ai,bi,ci,di in abcd:
            if A[bi-1] -A[ai-1] == ci:
                score += di
        return score
    
    if len(A) >= 1:
        prev_last = A[-1]
    else:
        prev_last = 1
    
    for i in range(prev_last,M+1):
        A.append(i)
        max_score = max(max_score,dfs(A))
        A.pop()
    return max_score
dfs([])
print(max_score)

Solution (Python):

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


def score(A):
    tmp = 0
    for ai, bi, ci, di in zip(a, b, c, d):
        if A[bi] - A[ai] == ci:
            tmp += di
    return tmp


def dfs(A):
    if len(A) == N:
        return score(A) 
    res = 0
    prev_last = A[-1] if len(A) > 0 else 0
    for v in range(prev_last, M):
        A.append(v)
        res = max(res, dfs(A)) 
        A.pop()
    return res


print(dfs([]))

Solution (Python):
import itertools

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

L = list(itertools.combinations_with_replacement(range(1,M+1), N))
p_list =  []

for l in L :
    point = 0
    for x in X :
        if l[x[1]-1] - l[x[0]-1] == x[2] :
            point += x[3]
            
    p_list.append(point)




print(max(p_list))

Solution (Python):
import copy

def main():
	N, M, Q = [int(n) for n in input().split(" ")]
	q = [[int(a) for a in input().split(" ")] for i in range(Q)]
	all_series = get_series(N, M)
	points = [0]
	for l in all_series:
		points.append(get_score(l, q))
	print(max(points))

def get_score(l, q):
	return sum([q[i][3] if l[q[i][1] - 1] - l[q[i][0] - 1] == q[i][2] else 0 for i in range(len(q))])

def get_series(N, M):
	
	
	all_series = []
	checked = [[0] * M for i in range(N)]
	to_check = [[0, j + 1] for j in range(M)]
	series = [0 for k in range(N)]
	while len(to_check) > 0:
		checking = to_check.pop(-1)
		series[checking[0]] = checking[1]
		if checking[0] == N - 1:
			l = copy.deepcopy(series)
			all_series.append(l)
		else:
			to_check.extend([[checking[0] + 1, k] for k in range(checking[1], M + 1)])
	return all_series

main()

Solution (Python):
import itertools

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

    pattern = itertools.combinations_with_replacement(range(1, m+1), n)
    best = 0
    for p in pattern:
        cost = 0
        for l in all_list:
            if p[l[1]-1] - p[l[0]-1] == l[2]:
                cost += l[3]
        if cost > best:
            best = cost
    print(best)

abc165c_many_requirements()

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

Solution (Python):
n,m,q=map(int,input().split())
def dfs(seq):
    u=0
    if len(seq)==n:
        ans=0
        for a,b,c,d in data:
            if seq[b-1]-seq[a-1]==c:
                ans+=d
        return ans
    else:
        for i in range(seq[-1],m+1):
            seq_next=seq.copy()
            seq_next.append(i)
            u=max(u,dfs(seq_next))
        return u

data=[]
for _ in range(q):
    A=list(map(int,input().split()))
    data.append(A)
print(dfs([1]))

Solution (Python):
n,m,q=map(int,input().split())
def dfs(seq):
    u=0
    if len(seq)==n:
        ans=0
        for a,b,c,d in data:
            if seq[b-1]-seq[a-1]==c:
                ans+=d
        return ans
    else:
        for i in range(seq[-1],m+1):
            seq_next=seq+[i]
            u=max(u,dfs(seq_next))
        return u

data=[]
for _ in range(q):
    A=list(map(int,input().split()))
    data.append(A)
print(dfs([1]))

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M,Q=mi()
    X = []
    for i in range(Q):
        a,b,c,d = mi()
        X.append((a,b,c,d))

    ans = 0
    num_list = list(range(1,M+1))
    for _nums in itertools.combinations_with_replacement(num_list, N):
        nums = list(_nums)
        D = 0
        for j in range(Q):
            a,b,c,d = X[j]
            if nums[b-1] - nums[a-1] == c: 
                D += d
        
        ans = max(ans,D)

    print(ans)



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)]
l = list(itertools.combinations_with_replacement(range(1, m+1), n))
ans = 0
for i in l:
    w = 0
    for j in abcd:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            w += j[3]
    ans = max(ans, w)
print(ans)

Solution (Python):

from itertools import combinations_with_replacement
def main():
    n, m, q = map(int, input().split())
    xs = []
    for i in range(q):
        xs.append(list(map(int, input().split())))
    max = 0
    for suretu in combinations_with_replacement(range(1, m + 1), n):
        wa = 0
        for x in xs:
            a, b, c, d = x
            if suretu[b - 1] - suretu[a - 1] == c:
                wa += d
        if wa > max:
            max = wa
    print(max)
    

if __name__ == "__main__":
    main()

Solution (Python):
def dfs(s):
    global ans
    if len(s) == n:
        cnt = 0
        for i in range(q):
            if s[abcd[i][1]-1]-s[abcd[i][0]-1] == abcd[i][2]:
                cnt += abcd[i][3]
        ans = max(ans, cnt)
        return
    last = 1
    if len(s) > 0:
        last = s[-1]
    for i in range(last, m+1):
        dfs(s+[i])
    return

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

Solution (Python):
import sys
N, M, Q = map(int, input().split())
abcd = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
ans = 0
done = set()
todo = [(i,) for i in range(1, M + 1)]
while todo:
    A = todo.pop()
    done.add(A)
    if len(A) == N:
        total = 0
        for a, b, c, d in abcd:
            if A[b-1] - A[a-1] == c:
                total += d
        ans = max(ans, total)
        continue
    for i in range(A[-1], M + 1):
        nA = A + (i,)
        if not nA in done:
            todo.append(nA)
print(ans)

Solution (Python):
n,m,q=map(int, input().split())
abcd=[]
for i in range(q):
  a,b,c,d=map(int, input().split())
  abcd.append([a,b,c,d])
def dfs(i):
  if i==0:
    alist=[]
    for k in range(m):
      alist.append([k+1])
    return alist
  elif i>0:
    blist=[]
    for k in dfs(i-1):
      last=k[-1]
      for l in range(last,m+1):
        clist=k.copy()
        clist.append(l)
        blist.append(clist)
    return blist
ans=0
for alist in dfs(n-1):
  cnt=0
  for i in range(q):
    if alist[abcd[i][1]-1]-alist[abcd[i][0]-1]==abcd[i][2]:
      cnt+=abcd[i][3]
  ans=max(ans,cnt)
print(ans)

Solution (Python):
n,m,q=map(int, input().split())
abcd=[]
for i in range(q):
  a,b,c,d=map(int, input().split())
  abcd.append([a,b,c,d])
def dfs(i,j):
  if i==0:
    alist=[]
    for k in range(j):
      alist.append([k+1])
    return alist
  elif i>0 and i<=n:
    blist=[]
    for k in dfs(i-1,j):
      last=k[-1]
      for l in range(last,j+1):
        clist=k.copy()
        clist.append(l)
        blist.append(clist)
    return blist
ans=0
for alist in dfs(n-1,m):
  cnt=0
  for i in range(q):
    if alist[abcd[i][1]-1]-alist[abcd[i][0]-1]==abcd[i][2]:
      cnt+=abcd[i][3]
  ans=max(ans,cnt)
print(ans)

Solution (Python):



N, M, Q = list(map(int, input().split()))
 
conditions = [list(map(int, input().split())) for _ in range(Q)]
 
def dfs(A, max_a):
    if len(A) == N:
        num = 0
        for a, b, c, d in conditions:
            if A[b-1] - A[a-1] == c:
                num += d
        return num
    ans = 0
    for i in range(max_a, M+1):
        ans = max(ans, dfs(A + [i], i))
    return ans
 
print(dfs([1], 1))

Solution (Python):
from itertools import combinations_with_replacement

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

A = list(combinations_with_replacement(list(range(1, M+1)), N))
Alist = [list(a) for a in A]



Max = 0

for a in Alist:
    cost = 0
    for t in T:
        if a[t[1]-1] - a[t[0]-1] == t[2]:
            cost += t[3]
    if cost > Max:
        Max = cost
print(Max)

Solution (Python):
import itertools
import sys

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

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

a = itertools.combinations_with_replacement(range(1,M+1), N)
a = list(a)
Ans = 0
for i in range(len(a)):
  Ans = max(Ans,score(abcd,list(a[i])))
            
print(Ans)

Solution (Python):
import itertools

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

seisu_list = []

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

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

ans = 0

for i in range(len(A_list)):
    ans_temp = 0
    for j in range(Q):
        if A_list[i][seisu_list[j][1]-1] - A_list[i][seisu_list[j][0]-1] == seisu_list[j][2]:
            ans_temp += seisu_list[j][3]
            ans = max(ans, ans_temp)
print(ans)

Solution (Python):
import itertools
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

  
def main(n, m, abcd):
    p = itertools.combinations_with_replacement(range(1, m+1), n)
    L = list(p)
    ans = 0
    for A in L:
        ans = max(ans, score(list(A)))
    return ans


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

print(main(n, m, abcd))

Solution (Python):
target = []
def dfs(A):
    if len(A) == N:
        tmp = 0
        for a, b, c, d in arr:
            if A[b-1] - A[a-1] == c:
                tmp += d
        target.append(tmp)
        return
    start = 1
    if len(A) != 0:
        start = A[-1]
    for v in range(start,M+1):
        A.append(v)
        dfs(A)
        A.pop()
N, M, Q = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(Q)]
dfs([])
print(max(target))

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())

def solve():
    N, M, Q = MAP()
    A = [LIST() for _ in range(Q)]

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

if __name__ == '__main__':
    solve()

Solution (Python):
from itertools import combinations_with_replacement

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

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

Solution (Python):
import itertools

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

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

Solution (Python):
import itertools

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

ans=0
t=[m for m in range(1,M+1)]
for A in itertools.combinations_with_replacement(t, N):
  ans2=0
  for (a,b,c,d) in ABCD:
    if A[b-1]-A[a-1]==c:
      ans2+=d
  ans=max(ans, ans2)
print(ans)

Solution (Python):
import itertools

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

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

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

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

print(ans)

Solution (Python):
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for t in combinations_with_replacement(range(1, M+1), N):
    an = 0
    for a, b, c, d in ABCD:
        if t[b-1]-t[a-1] == c:
            an += d
    if ans < an:
        ans = an
print(ans)

Solution (Python):
def dfs(seq):
    ans = 0
    if len(seq) == n:
        kou = 0
        for u in data:
            if seq[u[1]-1] - seq[u[0]-1] == u[2]:
                kou+=u[3]
        return kou
    else:
        for i in range(seq[-1], m+1):
            seq_next=seq+[i]
            ans = max(ans, dfs(seq_next))
        return ans

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

score = dfs([1])
print(score)

Solution (Python):
N,M,Q = map(int, input().split())
a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    
def dfs(n_list, o, l, L):
    if o+l==0:
        num = 1
        pos = 0
        
        L_child = [0]*N
        for i in range(N+M-1):
            if n_list[i]==1:
                num += 1
            else:
                L_child[pos] = num
                pos += 1
        L.append(L_child)
    
    if o>=1:
        n_list[N+M-o-l-1] = 0
        dfs(n_list, o-1,l, L)
    if l>=1:
        n_list[N+M-o-l-1] = 1
        dfs(n_list, o, l-1, L)
    
A = [0]*(N+M-1)
L =[]
dfs(A,N,M-1,L)

ans = 0
for i in L:
    score = 0
    for j in range(Q):
        if i[b[j]-1]-i[a[j]-1]==c[j]:
            score += d[j]

    if score > ans:
        ans = score

print(ans)