Problem p02695 (python)

Fixed submission:
from collections import deque
N, M, Q = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
q = deque()
q.append([1])
while q:
  A = q.pop()
  if len(A) == N+1:
    ans = max(ans, sum([d if A[b]-A[a] == c else 0 for a, b, c, d in ABCD]))
  else:
    for i in range(A[-1], M+1):
      q.append(A+[i])
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque

def solve(n, m, q, A):
  if len(A) == m+1:
    rtn = 0
    for b, v in q.items():
      if v:
        for a, c, d in v:
          rtn += d if A[b]-A[a] == c else 0
    return rtn
  else:
    rtn = 0
    xs = []
    for i in range(A[-1], m+1):
      b = len(A)
      for a, c, _ in q[b]:
        if i-A[a] == c:
          xs.append(i)
    return max([solve(n, m, q, A+[x]) for x in (set(xs) if xs else range(A[-1], m+1))])

N, M, Q = map(int, input().split())
dic = {i:[] for i in range(1, M+1)}
for _ in range(Q):
  a, b, c, d = map(int, input().split())
  dic[b].append((a, c, d))
ans = 0
q = deque()
q.append([1])
while q:
  A = q.pop()
  if len(A) == N+1:
    rtn = 0
    for b, v in dic.items():
      if v:
        for a, c, d in v:
          rtn += d if A[b]-A[a] == c else 0
    ans = max(rtn, ans)
  else:
    for i in range(A[-1], M+1):
      b = len(A)
      if dic[b]:
        for a, c, d in dic[b]:
          if i-A[a] == c:
            q.append(A+[i])
      else:
        q.append(A+[i])
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque

def solve(n, m, q, A):
  if len(A) == m+1:
    rtn = 0
    for b, v in q.items():
      if v:
        for a, c, d in v:
          rtn += d if A[b]-A[a] == c else 0
    return rtn
  else:
    rtn = 0
    xs = []
    for i in range(A[-1], m+1):
      b = len(A)
      for a, c, _ in q[b]:
        if i-A[a] == c:
          xs.append(i)
    return max([solve(n, m, q, A+[x]) for x in (set(xs) if xs else range(A[-1], m+1))])

N, M, Q = map(int, input().split())
dic = {i:[] for i in range(1, M+1)}
for _ in range(Q):
  a, b, c, d = map(int, input().split())
  dic[b].append((a, c, d))
ans = 0
q = deque()
q.append([1])
while q:
  A = q.pop()
  if len(A) == N+1:
    rtn = 0
    for b, v in dic.items():
      if v:
        for a, c, d in v:
          rtn += d if A[b]-A[a] == c else 0
    ans = max(rtn, ans)
  else:
    for i in range(A[-1], M+1):
      b = len(A)
      if dic[b]:
        for a, c, d in dic[b]:
          if i-A[a] == c:
            q.append(A+[i])
      else:
        q.append(A+[i])
print(ans)

Buggy submission (verdict: Runtime Error):
import copy
import sys
sys.setrecursionlimit(2*10**9)
def solve(n, m, q, A):
  if len(A) == m+1:
    rtn = 0
    for b, v in q.items():
      if v:
        for a, c, d in v:
          rtn += d if A[b]-A[a] == c else 0
    return rtn
  else:
    rtn = 0
    xs = []
    for i in range(A[-1], m+1):
      b = len(A)
      for a, c, _ in q[b]:
        if i-A[a] == c:
          xs.append(i)
    return max([solve(n, m, q, A+[x]) for x in (set(xs) if xs else range(A[-1], m+1))])

N, M, Q = map(int, input().split())
dic = {i:[] for i in range(1, M+1)}
for _ in range(Q):
  a, b, c, d = map(int, input().split())
  dic[b].append((a, c, d))
print(solve(N, M, dic, [1]))

Buggy submission (verdict: Runtime Error):
import copy
import sys
sys.setrecursionlimit(2*10**9)
def solve(n, m, q, A):
  if len(A) == m+1:
    rtn = 0
    for a, b, c, d in q:
      rtn += d if A[b]-A[a] == c else 0
    return rtn
  else:
    return max([solve(n, m, q, A+[i]) for i in range(A[-1], m+1)])

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

Buggy submission (verdict: Runtime Error):
import copy
import sys
sys.setrecursionlimit(10**9)
def solve(n, m, q, A):
  if len(A) == m+1:
    rtn = 0
    for a, b, c, d in q:
      rtn += d if A[b]-A[a] == c else 0
    return rtn
  else:
    return max([solve(n, m, q, A+[i]) for i in range(A[-1], m+1)])

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

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
def solve(n, m, q, A):
  if len(A) == m+1:
    print(A)
    rtn = 0
    for a, b, c, d in q:
      rtn += d if A[b]-A[a] == c else 0
    return rtn
  else:
    return max([solve(n, m, q, A+[i]) for i in range(A[-1], m+1)])

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