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



Sample input:
3 4 3
1 3 3 100
1 2 2 10
2 3 2 10


Sample output:
110

Submission status: Wrong Answer

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

R=0
for a in range(m+1):
    for b in range(a,m+1):
        for c in range(b,m+1):
            for d in range(c,m+1):
                for e in range(d,m+1):
                    for f in range(e,m+1):
                        for g in range(f,m+1):
                            for h in range(g,m+1):
                                for i in range(h,m+1):
                                    for j in range(i,m+1):
                                        A=[a+1,b+1,c+1,d+1,e+1,f+1,g+1,h+1,i+1,j+1]
                                        r=0
                                        for k in range(q):
                                            if A[L[k][1]-1]-A[L[k][0]-1]==L[k][2]:
                                                r+=L[k][3]
                                        R=max(R,r)
print(R)

Submission status: Wrong Answer

Solution code:
nmq=list(map(int, input().split()))
n=nmq[0]
m=nmq[1]
q=nmq[2]
aa = []
ab = []
ac = []
ad = []
for j in range(q):
    a1,b1,c1,d1=[int(j) for j in input().split()]
    aa.append(a1)
    ab.append(b1)
    ac.append(c1)
    ad.append(d1)
matc=[-1]*n*n
matd=[0]*n*n
for i in range (n*n):
    x=i//n
    y=i%n
    for k in range (q):
        if aa[k]-1==x and ab[k]-1==y:
            matc[i]=ac[k]
            matd[i]=ad[k]
score=0
if n==10:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    for a5 in range(a4,m+1):
                        for a6 in range(a5,m+1):
                            for a7 in range(a6,m+1):
                                for a8 in range(a7,m+1):
                                    for a9 in range(a8,m+1):
                                        for a10 in range(a9,m+1):
                                            s=0
                                            a=[0]*(n+1)
                                            a[1]=a1
                                            a[2]=a2
                                            a[3]=a3
                                            a[4]=a4
                                            a[5]=a5
                                            a[6]=a6
                                            a[7]=a7
                                            a[8]=a8
                                            a[9]=a9
                                            a[10]=a10
                                            for v in range(n*n):
                                                xv=1+(v//n)
                                                yv=1+(v%n)
                                                if a[yv]-a[xv]==matc[v]:
                                                    s+=matd[v]
                                            if s>score:
                                                score=s

if n==9:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    for a5 in range(a4,m+1):
                        for a6 in range(a5,m+1):
                            for a7 in range(a6,m+1):
                                for a8 in range(a7,m+1):
                                    for a9 in range(a8,m+1):
                                        s=0
                                        a=[0]*(n+1)
                                        a[1]=a1
                                        a[2]=a2
                                        a[3]=a3
                                        a[4]=a4
                                        a[5]=a5
                                        a[6]=a6
                                        a[7]=a7
                                        a[8]=a8
                                        a[9]=a9
                                        for v in range(n*n):
                                            xv=1+(v//n)
                                            yv=1+(v%n)
                                            if a[yv]-a[xv]==matc[v]:
                                                s+=matd[v]
                                        if s>score:
                                            score=s
                                        
if n==8:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    for a5 in range(a4,m+1):
                        for a6 in range(a5,m+1):
                            for a7 in range(a6,m+1):
                                for a8 in range(a7,m+1):
                                    s=0
                                    a=[0]*(n+1)
                                    a[1]=a1
                                    a[2]=a2
                                    a[3]=a3
                                    a[4]=a4
                                    a[5]=a5
                                    a[6]=a6
                                    a[7]=a7
                                    a[8]=a8
                                    for v in range(n*n):
                                        xv=1+(v//n)
                                        yv=1+(v%n)
                                        if a[yv]-a[xv]==matc[v]:
                                            s+=matd[v]
                                    if s>score:
                                        score=s
                            
if n==7:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    for a5 in range(a4,m+1):
                        for a6 in range(a5,m+1):
                            for a7 in range(a6,m+1):
                                s=0
                                a=[0]*(n+1)
                                a[1]=a1
                                a[2]=a2
                                a[3]=a3
                                a[4]=a4
                                a[5]=a5
                                a[6]=a6
                                a[7]=a7
                                for v in range(n*n):
                                    xv=1+(v//n)
                                    yv=1+(v%n)
                                    if a[yv]-a[xv]==matc[v]:
                                        s+=matd[v]
                                if s>score:
                                    score=s
                                
if n==6:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    for a5 in range(a4,m+1):
                        for a6 in range(a5,m+1):
                            s=0
                            a=[0]*(n+1)
                            a[1]=a1
                            a[2]=a2
                            a[3]=a3
                            a[4]=a4
                            a[5]=a5
                            a[6]=a6
                            for v in range(n*n):
                                xv=1+(v//n)
                                yv=1+(v%n)
                                if a[yv]-a[xv]==matc[v]:
                                    s+=matd[v]
                            if s>score:
                                score=s
                        
if n==5:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    for a5 in range(a4,m+1):
                        s=0
                        a=[0]*(n+1)
                        a[1]=a1
                        a[2]=a2
                        a[3]=a3
                        a[4]=a4
                        a[5]=a5
                        for v in range(n*n):
                            xv=1+(v//n)
                            yv=1+(v%n)
                            if a[yv]-a[xv]==matc[v]:
                                s+=matd[v]
                        if s>score:
                            score=s   
                        
if n==4:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                for a4 in range(a3,m+1):
                    s=0
                    a=[0]*(n+1)
                    a[1]=a1
                    a[2]=a2
                    a[3]=a3
                    a[4]=a4
                    for v in range(n*n):
                        xv=1+(v//n)
                        yv=1+(v%n)
                        if a[yv]-a[xv]==matc[v]:
                            s+=matd[v]
                    if s>score:
                        score=s 

if n==3:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            for a3 in range(a2,m+1):
                s=0
                a=[0]*(n+1)
                a[1]=a1
                a[2]=a2
                a[3]=a3
                for v in range(n*n):
                    xv=1+(v//n)
                    yv=1+(v%n)
                    if a[yv]-a[xv]==matc[v]:
                        s+=matd[v]
                if s>score:
                    score=s
                
if n==2:
    for a1 in range(1,m+1):
        for a2 in range(a1,m+1):
            s=0
            a=[0]*(n+1)
            a[1]=a1
            a[2]=a2
            for v in range(n*n):
                xv=1+(v//n)
                yv=1+(v%n)
                if a[yv]-a[xv]==matc[v]:
                    s+=matd[v]
            if s>score:
                score=s
print(score)

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int, input().split())
tapple = []
d_sum_max = 0
for i in range(Q):
    tapple.append([int(n) for n in input().split()])

A = list(itertools.product(range(1,M+1), repeat=N))
for j in range(len(A)):
    d_sum = 0
    for i in range(Q):
        if A[j][tapple[i][1]-1] - A[j][tapple[i][0]-1] == tapple[i][2]:
            d_sum += tapple[i][3] 
    if d_sum_max < d_sum:
        d_sum_max = d_sum
        
print(d_sum_max)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]

    def dfs(A):
        cur = 0
        if len(A) == N:
            for a, b, c, d in T:
                if A[b-1] - A[a-1] == c:
                    cur += d
        elif len(A) == 0:
            for i in range(1, 11):
                v = dfs(A + [i])
                cur = max(cur, v)
        else:
            for i in range(A[-1], 11):
                v = dfs(A + [i])
                cur = max(cur, v)
        return cur

    print(dfs([]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
n, m, q = map(int, input().split())
list_score = [ list(map(int,input().split(" "))) for i in range(q)]
list_NUM = [i for i in range(1, m+1)]
ans = 0

if q < n-1:
    for l in list_score:
        ans += l[3]
else:
    for l in itertools.product(list_NUM, repeat=n):
        cnt = 0
        for h in list_score:
            if l[h[1]-1] - l[h[0]-1] == h[2]:
                cnt += h[3]

        ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q=map(int,input().split())
abcd=[None]*q
for i in range(q):
    abcd[i]=list(map(int,input().split()))
num=0
for l in list(map(list, itertools.combinations(range(1,m+1), n))):
    num_ = 0
    for x in abcd:
        if l[x[1]-1]-l[x[0]-1] == x[2]:
            num_ += x[3]
    num = max(num ,num_)
print(num)

Submission status: Wrong Answer

Solution code:
import copy
n, m, q = map(int, input().split())
point_table = []
tmp_ans = [1] * n


def check_point(tmp):
    ans_point = 0
    
    for k in point_table:
        if tmp[k[1] - 1] == k[2] + tmp[k[0] - 1]:
            ans_point += k[3]
    
    return ans_point


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

point_table.sort(key=lambda row: row[3])

for j in point_table:
    new_ans = copy.copy(tmp_ans)
    new_ans[j[1] - 1] = j[2] + new_ans[j[0] - 1]
    
    if check_point(tmp_ans) <= check_point(new_ans):
        tmp_ans = new_ans
        
print(check_point(tmp_ans))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]

    def dfs(A):
        cur = 0
        if len(A) == N:
            for a, b, c, d in T:
                if A[b-1] - A[a-1] == c:
                    cur += d
        elif len(A) == 0:
            for i in range(1, 11):
                v = dfs(A + [i])
                cur = max(cur, v)
        else:
            if A[-1] == 10:
                return 0
            for i in range(A[-1], 11):
                v = dfs(A + [i])
                cur = max(cur, v)
        return cur

    print(dfs([]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from itertools import combinations

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

D = defaultdict(set)
for a, b, c, d in ABCD:
  D[b-1].add((a-1, c, d))

for j in range(N-1, 0, -1):
  for x, y in combinations(D[j], 2):
    if x[0] == y[0]:
      continue
    if x[0] > y[0]:
      x, y = y, x
    if x[1] > y[1]:
      for a, c, d in D[y[0]]:
        if a == x[0] and c == x[1] - y[1]:
          break
      else:
        D[y[0]].add((x[0], x[1] - y[1], 0))

for j in range(1, N):
  for a, c, d in D[j]:
    if a == j-1 and c == 0:
      break
  else:
    D[j].add((j-1, 0, 0))

que = deque([([1], 0)])
ans = 0
while que:
  L, score = que.popleft()
  if len(L) == N:
    if score > ans:
      ans = score
  else:
    for a, c, d in D[len(L)]:
      l = L[a] + c
      if l <= M:
        que.append((L + [l], score + sum(d for a, c, d in D[len(L)] if l - L[a] == c)))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q = map(int,input().split())
qset = [list(map(int, input().split())) for i in range(q)]

aa=list(itertools.combinations(range(1,m+1), r=n))

ans=0

for i in range(len(aa)):
  score = 0
  for n in range(len(qset)):
    a=aa[i]
    abcd=qset[n]
    if a[abcd[1]-1]-a[abcd[0]-1]==abcd[2]:
      score=score+abcd[3]
  if ans<score:
    ans=score
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

ans = 0
R = [i for i in range(1, M+1)]
# print(R)
for comb in itertools.combinations(R, N):
    # print(comb)
    temp = 0
    for abcd in ABCD:
        a, b, c, d = abcd
        if comb[b-1] - comb[a-1] == c:
            temp += d
    if temp >= ans:
        ans = temp

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

a = list(map(int,input().split()))
N = a[0]
M = a[1]
Q = a[2]

l = [[int(i) for i in input().split()] for j in range(Q)]

nums = list(range(1,M+1))
hoge = []
for balls in itertools.combinations_with_replacement(nums, N):
    hoge.append(list(set(balls)))

hoge = list(map(list, set(map(tuple, hoge))))

maji_ans = 0

for kouho_list in hoge:# 全候補試す iはlist
    ans = 0
    for j in l:# 条件を確認する jはlの要素(list)
        a = j[0]
        b = j[1]
        c = j[2]
        d = j[3]
        
        if b > len(kouho_list):#そもそも長さ足りなければpass
            pass
        else:
            if (kouho_list[b-1] - kouho_list[a-1]) == c:
                ans += d
    maji_ans = max(maji_ans,ans)

print(maji_ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
d=0
for i in range(len(Q)):
    if Q[i][1]-Q[i][0]==Q[i][2]:
        d+=Q[i][3]
print(d)

Submission status: Wrong Answer

Solution code:
n,m,q = (int(num) for num in input().split())
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q

for i in range(q):
    at,bt,ct,dt = (int(num) for num in input().split())
    a[i] = at
    b[i] = bt
    c[i] = ct
    d[i] = dt
    
def calc(s,a,b,c,d):
    ret = 0
    for i in range(n-1):
        if int(s[i]) > int(s[i+1]) or int(s[i]) >= m:
            return -1
    if int(int(s[n-1]) >= m):
        return -1
    for i in range(q):
        if int(s[b[i]-1]) - int(s[a[i]-1]) == c[i]:
            ret += d[i]
    return ret

ans = 0
qu = [str(num) for num in range(m)]
while True:
    s = qu.pop(0)
    if len(s) >= n:
        break
    t = int(s[-1])
    for i in range(t,m):
        qu.append(s + str(i))

for si in qu:
    s = str(si)
    if len(s) < n:
        s = "0" * (n-len(s)) + s
    ans = max(ans, calc(s,a,b,c,d))
    
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,Q = map(int, input().split())
score=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=[1]*N
temp=0
for i in range(Q):
    if A[b[i]-1] - A[a[i]-1]==c[i]:
        temp=temp+d[i]
if temp>score:
    score=temp

for k in range(2,M+1):
    for l in reversed(range(N)):
        for m in range(2,k+1):
            A[l]=m
            temp=0
            for i in range(Q):
                if A[b[i]-1] - A[a[i]-1]==c[i]:
                    temp=temp+d[i]
            if temp>score:
                score=temp
    A=[1]*N
    A[N-1]=k
print(score)

Submission status: Wrong Answer

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

R=0
for a in range(10):
    for b in range(a,10):
        for c in range(b,10):
            for d in range(c,10):
                for e in range(d,10):
                    for f in range(e,10):
                        for g in range(f,10):
                            for h in range(g,10):
                                for i in range(h,10):
                                    for j in range(i,10):
                                        A=[a+1,b+1,c+1,d+1,e+1,f+1,g+1,h+1,i+1,j+1]
                                        r=0
                                        for k in range(q):
                                            if A[L[k][1]-1]-A[L[k][0]-1]==L[k][2]:
                                                r+=L[k][3]
                                        R=max(R,r)
print(R)

Submission status: Wrong Answer

Solution code:
import itertools

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

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

total = 0

l = [1,2,3,4,5,6,7,8,9,10]
for v in itertools.combinations_with_replacement(l, n):
    scores = []
    for a,b,c,d in nums:
        if v[b-1]-v[a-1] == c:
            scores.append(d)
    if total < sum(scores):
        total = sum(scores)

print(total)

Submission status: Wrong Answer

Solution code:
import math

a, b, n = map(int, input().split())

if n>=b:
	n1 = n//b*b-1
	ans = max((a*n//b)-a*(n//b), (a*n1//b)-a*(n1//b))
else:
	ans = (a*n//b)-a*(n//b)
print(ans)

Submission status: Wrong Answer

Solution code:
#c
n,m,q =map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
a,b,c,d = [list(i) for i in zip(*abcd)]

def check(number):
    score = 0
    for i in range(q):
        if int(number[b[i]-1]) - int(number[a[i]-1]) == c[i]:
            score += d[i]
    return score

score_m = 0
for i in range(10**(n-1),10**n):
    if "0"  not in str(i):
        f = 0
        for k in range(m+1,10):
            if str(k) in str(i):
                f = 1
                break
        if f == 0:
            score = check(str(i))
            if score > score_m:
                score_m = score
print(score_m)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline
n,m,q=map(int,input().split())
x=[list(map(int,input().split())) for i in range(q)]
for i in range(q):
    for j in range(2):
        x[i][j]-=1
a=[]
def dfs(v):
    if len(v)>n:
        return
    if len(v)==n:
        a.append(v)
    for i in range(int(v[-1]),11):
        dfs(v+[i])
for i in range(1,11):
    dfs([i])

ans=0
for i in a:
    now=0
    for k in range(q):
        if i[x[k][1]]-i[x[k][0]]==x[k][2]:
            now+=x[k][3]
    ans=max(now,ans)
print(ans)

Submission status: Wrong Answer

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

ans_point = 0
for i in range(q):
    # どれかを正しいとする
    c_a, c_b, c_c = values[i][0], values[i][1], values[i][2]
    ans = [1] * (n + 1)
    ans[c_b] = ans[c_a] + c_c
    for a, b, c, d in values:
        temp = ans[b]
        ans[b] = ans[a] + c
        if ans[c_b] - ans[c_a] != c_c:
            ans[b] = temp
    point = 0
    for a, b, c, d in values:
        if ans[b] - ans[a] == c:
            point += d
    ans_point = max(ans_point, point)

print(ans_point)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from itertools import combinations

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

D = defaultdict(set)
for a, b, c, d in ABCD:
  D[b-1].add((a-1, c, d))

for j in range(N-1, 0, -1):
  for x, y in combinations(D[j], 2):
    if x[0] == y[0]:
      continue
    if x[0] > y[0]:
      x, y = y, x
    if x[1] > y[1]:
      for a, c, d in D[y[0]]:
        if a == x[0] and c == x[1] - y[1]:
          break
      else:
        D[y[0]].add((x[0], x[1] - y[1], 0))

for j in range(1, N):
  D[j].add((j-1, 0, 0))

que = deque([([1], 0)])
ans = 0
while que:
  L, score = que.popleft()
  if len(L) == N:
    if score > ans:
      ans = score
  else:
    for a, c, d in D[len(L)]:
      l = L[a] + c
      if l <= M:
        que.append((L + [l], score + sum(d for a, c, d in D[len(L)] if l - L[a] == c)))
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
j = [1] * N
ans = 0
i = N - 1
k = 0

c = 0

for A in l:
    a, b, c, d = A
    if j[b-1] - j[a-1] == c:
        ans += d

while True:
    
    j[-1] += 1
    ans_ = 0
    for A in l:
        a, b, c, d = A
        if j[b-1] - j[a-1] == c:
            ans_ += d
    if ans_ > ans:
        ans = ans_

    if j[-1] == M + 1:
        for i in range(1, N):
            j[-(i+1)] += 1
            for k in range(N-(i+1), N):
                j[k] = j[-(i+1)]
            if j[-(i+1)] != M + 1:
                break
    if j[0] == M:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

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

total = 0

l = [1,2,3,4,5,6,7,8,9,10]
for v in itertools.combinations_with_replacement(l, n):
    scores = []
    for a,b,c,d in nums:
        if v[b-1]-v[a-1] == c:
            scores.append(d)
    if total <= sum(scores):
        total = sum(scores)

print(total)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
A=[]
i=1
while i<m-n+2:
    for a in range(i,m-n+3):
        if n==2:
            A.append([i,a])
        else:
            for b in range(a,m-n+4):
                if n==3:
                    A.append([i,a,b])
                else:
                    for c in range(b,m-n+5):
                        if n==4:
                            A.append([i,a,b,c])
                        else:
                            for d in range(c,m-n+6):
                                if n==5:
                                    A.append([i,a,b,c,d])
                                else:
                                    for e in range(d,m-n+7):
                                        if n==6:
                                            A.append([i,a,b,c,d,e])
                                        else:
                                            for f in range(e,m-n+8):
                                                if n==7:
                                                    A.append([i,a,b,c,d,e,f])
                                                else:
                                                    for g in range(f,m-n+9):
                                                        if n==8:
                                                            A.append([i,a,b,c,d,e,f,g])
                                                        else:
                                                            for h in range(g,n-m+10):
                                                                if n==9:
                                                                    A.append([i,a,b,c,d,e,f,g,h])
                                                                else:
                                                                    for j in range(h,n-m+11):
                                                                        A.append([i,a,b,c,d,e,f,g,h,j])
    i+=1
length=len(A)
if length==0:
    print(0)
else:
    l=[]
    ans=0
    for i in range(q):
        l.append(list(map(int,input().split())))
    for tmpA in A:
        tmp=0
        for tmpQ in l:
            if tmpA[tmpQ[1]-1]-tmpA[tmpQ[0]-1]==tmpQ[2]:
                tmp+=tmpQ[3]
        ans=max(tmp,ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M,Q = map(int, input().split())
score=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=[1]*N
temp=0
for i in range(Q):
    if A[b[i]-1] - A[a[i]-1]==c[i]:
        temp=temp+d[i]
if temp>score:
    score=temp

for k in range(2,M+1):
    for l in reversed(range(N)):
        for m in range(2,k+1):
            A[l]=m
            print(A)
            temp=0
            for i in range(Q):
                if A[b[i]-1] - A[a[i]-1]==c[i]:
                    temp=temp+d[i]
            if temp>score:
                score=temp
    A=[1]*N
    A[N-1]=k
print(score)