Buggy python submission (verdict: Time Limit Exceeded):
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(dict)
for a, b, c, d in ABCD:
  D[b-1][(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]:
      if (x[0], x[1] - y[1]) not in D[y[0]]:
        D[y[0]][(x[0], x[1] - y[1])] = 0

for j in range(1, N):
  if (j-1, 0) not in D[j]:
    D[j][(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 in D[len(L)]:
      l = L[a] + c
      if L[-1] <= l <= M:
        que.append((L + [l], score + sum(d for v, d in D[len(L)].items() if l - L[v[0]] == v[1])))
print(ans)

Fixed submission:
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(dict)
for a, b, c, d in ABCD:
  D[b-1][(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]:
      if (x[0], x[1] - y[1]) not in D[y[0]]:
        D[y[0]][(x[0], x[1] - y[1])] = 0

for j in range(1, N):
  if (j-1, 0) not in D[j]:
    D[j][(j-1, 0)] = 0

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

Buggy python submission (verdict: Runtime Error):
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[y[0]]:
    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)

Fixed submission:
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(dict)
for a, b, c, d in ABCD:
  D[b-1][(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]:
      if (x[0], x[1] - y[1]) not in D[y[0]]:
        D[y[0]][(x[0], x[1] - y[1])] = 0

for j in range(1, N):
  if (j-1, 0) not in D[j]:
    D[j][(j-1, 0)] = 0

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

Buggy python submission (verdict: Time Limit Exceeded):
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]:
      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
ans_L = []
while que:
  L, score = que.popleft()
  if len(L) == N:
    if score > ans:
      ans = score
      ans_L = L
  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)

Fixed submission:
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(dict)
for a, b, c, d in ABCD:
  D[b-1][(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]:
      if (x[0], x[1] - y[1]) not in D[y[0]]:
        D[y[0]][(x[0], x[1] - y[1])] = 0

for j in range(1, N):
  if (j-1, 0) not in D[j]:
    D[j][(j-1, 0)] = 0

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

Buggy python submission (verdict: Time Limit Exceeded):
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]:
      D[y[0]].add((x[0], x[1] - y[1], 0))

for i, j in combinations(range(N), 2):
  D[j].add((i, 0, 0))

que = deque([([1], 0)])
ans = 0
ans_L = []
while que:
  L, score = que.popleft()
  if len(L) == N:
    if score > ans:
      ans = score
      ans_L = L
  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)

Fixed submission:
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(dict)
for a, b, c, d in ABCD:
  D[b-1][(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]:
      if (x[0], x[1] - y[1]) not in D[y[0]]:
        D[y[0]][(x[0], x[1] - y[1])] = 0

for j in range(1, N):
  if (j-1, 0) not in D[j]:
    D[j][(j-1, 0)] = 0

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