Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools
import numpy as np

N, M, Q = map(int, readline().split())
A = np.array(list(itertools.combinations_with_replacement(range(1, M+1), N)))

n = len(A) 

score = np.zeros(n, np.int32)
m = np.array(read().split(), np.int32).reshape(Q, 4)

ans = 0
for com in A:
    tmp = 0
    for a, b, c, d in m: 
        if com[b-1]-com[a-1] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement


def getPoint(data, A, q):
    res = 0
    for i in range(q):
        a, b, c, d = data[i]
        if A[b]-A[a] == c:
            res += d
    return res

def main():
    n, m, q = map(int, readline().split())    
    data = []
    for _ in range(q):
        a, b, c, d = map(int, readline().split())
        a-=1
        b-=1
        data.append((a, b, c, d))
    p = [int(i) for i in range(1, m+1)]
    ans = 0
    for A in combinations_with_replacement(p, n):
        ans = max(getPoint(data, A, q), ans)
    print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque

def f(data, q, cnd):
    res = 0
    for i in range(q):
        a, b, c, d = data[i]
        if int(cnd[b])-int(cnd[a]) == c:     
            res += d
    return res

def main():
    n, m, q = map(int, readline().split())
    data = []
    for _ in range(q):
        a, b, c, d = map(int, readline().split())
        a-=1
        b-=1
        data.append((a, b, c, d))  
    
    cnd = []
    dq = deque() 
    for i in range(1, m+1):
        dq.append(i)
    while len(dq)>0:
        v = dq.popleft() 
        u = v%10 
        for i in range(u, m+1):
            nx = v*10+i
            if len(str(nx)) == n:
                cnd.append(str(nx))
            else:
                dq.append(nx)
    ans = 0
    for i in cnd:
        ans = max(ans, f(data, q, i))
    print(ans) 
if __name__ == '__main__':
    main()


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement


def getPoint(data, A, q):
    res = 0
    for i in range(q):
        a, b, c, d = data[i]
        if A[b]-A[a] == c:
            res += d
    return res

def main():
    n, m, q = map(int, readline().split())    
    data = []
    for _ in range(q):
        a, b, c, d = map(int, readline().split())
        a-=1
        b-=1
        data.append((a, b, c, d))
    p = [int(i) for i in range(1, m+1)]
    ans = 0
    for A in combinations_with_replacement(p, n):
        ans = max(getPoint(data, A, q), ans)
    print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque

def f(data, q, cnd):
    res = 0
    for i in range(q):
        a, b, c, d = data[i]
        if int(cnd[b])-int(cnd[a]) == c:     
            res += d
    return res

def main():
    n, m, q = map(int, readline().split())
    data = []
    for _ in range(q):
        a, b, c, d = map(int, readline().split())
        a-=1
        b-=1
        data.append((a, b, c, d))  
    
    cnd = []
    dq = deque() 
    for i in range(1, m+1):
        dq.append(i)
    while len(dq)>0:
        v = dq.popleft() 
        u = v%10 
        for i in range(u, m+1):
            nx = v*10+i
            if len(str(nx)) == n:
                cnd.append(str(nx))
            else:
                dq.append(nx)
    ans = 0
    for i in cnd:
        ans = max(ans, f(data, q, i))
    print(ans) 
if __name__ == '__main__':
    main()


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement


def getPoint(data, A, q):
    res = 0
    for i in range(q):
        a, b, c, d = data[i]
        if A[b]-A[a] == c:
            res += d
    return res

def main():
    n, m, q = map(int, readline().split())    
    data = []
    for _ in range(q):
        a, b, c, d = map(int, readline().split())
        a-=1
        b-=1
        data.append((a, b, c, d))
    p = [int(i) for i in range(1, m+1)]
    ans = 0
    for A in combinations_with_replacement(p, n):
        ans = max(getPoint(data, A, q), ans)
    print(ans)
if __name__ == '__main__':
    main()