Buggy python submission (verdict: Runtime Error):
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n,m,q = map(int, input().split())
lis = [1 for i in range(n)]
ords = []
cost = []
dp = [-1 for i in range(200)]
ans = 0
for i in range(q):
    o = list(map(int, input().split()))
    ords.append(o[:n])
    cost.append(o[-1])
def checker(p, arr, ans):
    if(dp[p] == ans):
        return dp[p]
    for i in range(n-p-1, -1, -1):
        for c in range(1, m+1):
            arr[p] = c
            temp = 0
            ans = checker(p+1, arr, ans)
            for i in range(q):
                if(arr[ords[i][1]-1]-arr[ords[i][0]-1]==ords[i][2]):
                    temp+=cost[i]
            if(temp > ans):
                ans = temp
            dp[p] = ans
    return ans
print(checker(0, lis, 0))


Fixed submission:
N,M,Q=map(int,input().split())
L = []
for i in range(Q):
  L.append(list(map(int, input().split())))
  L[i][0]-=1
  L[i][1]-=1
 
import itertools
l =list(range(1,M+1))
v=list(itertools.combinations_with_replacement(l,N))
ans=0
for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:      
      score+=d
  ans=max(ans,score)
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n,m,q = map(int, input().split())
lis = [1 for i in range(n)]
ords = []
cost = []
dp = [-1 for i in range(200)]
ans = 0
for i in range(q):
    o = list(map(int, input().split()))
    ords.append(o[:n])
    cost.append(o[-1])
def checker(p, arr, ans):
    if(dp[p] == ans):
        return dp[p]
    for i in range(n-p-1, -1, -1):
        for c in range(m):
            arr[p] = c
            temp = 0
            ans = checker(p+1, arr, ans)
            for i in range(q):
                if(arr[ords[i][1]-1]-arr[ords[i][0]-1]==ords[i][2]):
                    temp+=cost[i]
            if(temp > ans):
                ans = temp
            dp[p] = ans
    return ans
print(checker(0, lis, 0))


Fixed submission:
N,M,Q=map(int,input().split())
L = []
for i in range(Q):
  L.append(list(map(int, input().split())))
  L[i][0]-=1
  L[i][1]-=1
 
import itertools
l =list(range(1,M+1))
v=list(itertools.combinations_with_replacement(l,N))
ans=0
for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:      
      score+=d
  ans=max(ans,score)
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n,m,q = map(int, input().split())
lis = [1 for i in range(n)]
ords = []
cost = []
dp = [200 for i in range(200)]
ans = 0
for i in range(q):
    o = list(map(int, input().split()))
    ords.append(o[:n])
    cost.append(o[-1])
def checker(p, arr, ans):
    if(dp[p]!= 200):
        return dp[p]
    for i in range(n-p-1, -1, -1):
        for c in range(m):
            arr[p] = c
            temp = 0
            ans = checker(p+1, arr, ans)
            for i in range(q):
                if(arr[ords[i][1]-1]-arr[ords[i][0]-1]==ords[i][2]):
                    temp+=cost[i]
            if(temp > ans):
                ans = temp
            dp[p] = ans
    return ans
print(checker(0, lis, 0))


Fixed submission:
N,M,Q=map(int,input().split())
L = []
for i in range(Q):
  L.append(list(map(int, input().split())))
  L[i][0]-=1
  L[i][1]-=1
 
import itertools
l =list(range(1,M+1))
v=list(itertools.combinations_with_replacement(l,N))
ans=0
for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:      
      score+=d
  ans=max(ans,score)
print(ans)