Buggy python submission (verdict: Runtime Error):
# O(n^M) -> TLE
from itertools import product
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
arr = list(range(1, M+1))
ans = 0
for x in product(arr, repeat=N):
    cur = 0
    for y in ls:
        if x[y[1]-1] - x[y[0]-1] == y[2]:
            cur += y[3]
    ans = max(ans, cur)
print(ans)

# DFS
from collections import deque
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
arr = list(range(1, M+1))
ans = 0
for i in range(1, M+1):
    st = deque([])
    st.append((i, str(i)))
    while st:
        p, l = st.pop()
        if len(l) == N:
            cur = 0
            for y in ls:
                if int(l[y[1]-1]) - int(l[y[0]-1]) == y[2]:
                    cur += y[3]
            ans = max(ans, cur)
        else:
            for j in range(p, M+1):
                st.append((j, l + str(j)))
print(ans)

# 数列生成
from itertools import combinations
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
V = [[] for i in range(N+1)]
for i in range(1, M+1):
    V[1].append([i])

for i in range(1, N):
    for v in V[i]:
        b = v[-1]
        for x in range(b, M+1):
            V[i+1].append(v + [x])
ans = 0
for x in V[-1]:
    cur = 0
    for z in ls:
        if x[z[1]-1] - x[z[0]-1] == z[2]:
            cur += z[3]
    ans = max(ans, cur)
print(ans)

Fixed submission:
# 数列生成
from itertools import combinations
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
V = [[] for i in range(N+1)]
for i in range(1, M+1):
    V[1].append([i])

for i in range(1, N):
    for v in V[i]:
        b = v[-1]
        for x in range(b, M+1):
            V[i+1].append(v + [x])
ans = 0
for x in V[-1]:
    cur = 0
    for z in ls:
        if x[z[1]-1] - x[z[0]-1] == z[2]:
            cur += z[3]
    ans = max(ans, cur)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# DFS
from collections import deque
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
arr = list(range(1, M+1))
ans = 0
for i in range(1, M+1):
    st = deque([])
    st.append((i, str(i)))
    while st:
        p, l = st.pop()
        if len(l) == N:
            cur = 0
            for y in ls:
                if int(l[y[1]-1]) - int(l[y[0]-1]) == y[2]:
                    cur += y[3]
            ans = max(ans, cur)
        else:
            for j in range(p, M+1):
                st.append((j, l + str(j)))
print(ans)

Fixed submission:
# 数列生成
from itertools import combinations
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
V = [[] for i in range(N+1)]
for i in range(1, M+1):
    V[1].append([i])

for i in range(1, N):
    for v in V[i]:
        b = v[-1]
        for x in range(b, M+1):
            V[i+1].append(v + [x])
ans = 0
for x in V[-1]:
    cur = 0
    for z in ls:
        if x[z[1]-1] - x[z[0]-1] == z[2]:
            cur += z[3]
    ans = max(ans, cur)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# DFS
from collections import deque
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
arr = list(range(1, M+1))
ans = 0
for i in range(1, M+1):
    st = deque([])
    st.append((i, str(i)))
    while st:
        p, l = st.pop()
        if len(l) == N:
            cur = 0
            for y in ls:
                if int(l[y[1]-1]) - int(l[y[0]-1]) == y[2]:
                    cur += y[3]
            ans = max(ans, cur)
        else:
            for j in range(p, M+1):
                st.append((j, l + str(j)))
print(ans)

Fixed submission:
# 数列生成
from itertools import combinations
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
V = [[] for i in range(N+1)]
for i in range(1, M+1):
    V[1].append([i])

for i in range(1, N):
    for v in V[i]:
        b = v[-1]
        for x in range(b, M+1):
            V[i+1].append(v + [x])
ans = 0
for x in V[-1]:
    cur = 0
    for z in ls:
        if x[z[1]-1] - x[z[0]-1] == z[2]:
            cur += z[3]
    ans = max(ans, cur)
print(ans)