Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

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

lis = []
for i in range(1, M+1):
    for j in range(N):
        lis.append(i)
box = set(combinations(lis, N))

ans = []
for i in box:
    temp = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            temp += d
    ans.append(temp)

print(max(ans))

Fixed submission:
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 v in combinations_with_replacement(range(1, M+1), N):
    A = list(v)
    temp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            temp += d
    ans = max(ans, temp)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

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

lis = []
for i in range(1, M+1):
    for j in range(N):
        lis.append(i)
box = list(combinations(lis, N))

ans = []
for i in box:
    temp = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            temp += d
    ans.append(temp)

print(max(ans))


Fixed submission:
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 v in combinations_with_replacement(range(1, M+1), N):
    A = list(v)
    temp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            temp += d
    ans = max(ans, temp)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

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

lis = []
for i in range(1, M+1):
    for j in range(N):
        lis.append(i)
box = list(combinations(lis, N))

ans = []
for i in box:
    temp = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            temp += d
    ans.append(temp)

print(max(ans))


Fixed submission:
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 v in combinations_with_replacement(range(1, M+1), N):
    A = list(v)
    temp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            temp += d
    ans = max(ans, temp)

print(ans)

Buggy python submission (verdict: Runtime Error):
from itertools import combinations

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

lis = list(range(1, M+1))
box = list(combinations(lis, N))
print(abcd)

ans = []
for i in box:
    temp = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            temp += d
    ans.append(temp)

print(max(ans))

Fixed submission:
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 v in combinations_with_replacement(range(1, M+1), N):
    A = list(v)
    temp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            temp += d
    ans = max(ans, temp)

print(ans)