Buggy python submission (verdict: Time Limit Exceeded):
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
def dfs(d,s):
    global n,m,Q,ans
    if d==n-1:
        ans_=0
        for k in Q:
            a,b,c,d=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=d
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        [dfs(d+1,s+str(i)) for i in range(l,m+1)]
for i in range(1,m+1):
    dfs(0,str(i))

print(ans)

Fixed submission:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
R=[range(i,m) for i in range(m)]
def dfs(d,s):
    global n,m,Q,ans
    if d==n:
        ans_=0
        for k in Q:
            a,b,c,e=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=e
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in R[l]:
            dfs(d+1,s+str(i))
for i in R[0]:
    dfs(1,str(i))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
def dfs(d,s):
    global n,m,Q,ans
    if d==n-1:
        ans_=0
        for k in Q:
            a,b,c,d=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=d
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in range(l,m+1):
            dfs(d+1,s+str(i))
for i in range(1,m+1):
    dfs(0,str(i))

print(ans)

Fixed submission:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
R=[range(i,m) for i in range(m)]
def dfs(d,s):
    global n,m,Q,ans
    if d==n:
        ans_=0
        for k in Q:
            a,b,c,e=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=e
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in R[l]:
            dfs(d+1,s+str(i))
for i in R[0]:
    dfs(1,str(i))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
co=[]
def dfs(d,s):
    global n,co,m
    if d==n-1:
        co.append(s)
    else:
        l=int(s[-1])
        for i in range(l,m+1):
            dfs(d+1,s+str(i))
for i in range(1,m+1):
    dfs(0,str(i))
ans=0
for i in co:
    ans_=0
    for k in range(q):
        a,b,c,d=Q[k]
        if int(i[b-1])-int(i[a-1])==c:
            ans_+=d
    ans=max(ans,ans_)
print(ans)

Fixed submission:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
R=[range(i,m) for i in range(m)]
def dfs(d,s):
    global n,m,Q,ans
    if d==n:
        ans_=0
        for k in Q:
            a,b,c,e=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=e
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in R[l]:
            dfs(d+1,s+str(i))
for i in R[0]:
    dfs(1,str(i))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
co=[]
def dfs(d,s):
    global n,co,m
    if d==n-1:
        co.append(s)
    else:
        l=int(s[-1])
        for i in range(l,m+1):
            dfs(d+1,s+str(i))
for i in range(1,m+1):
    dfs(0,str(i))
ans=0
for i in co:
    ans_=0
    for k in range(q):
        a,b,c,d=Q[k]
        if int(i[b-1])-int(i[a-1])==c:
            ans_+=d
    ans=max(ans,ans_)
print(ans)

Fixed submission:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
R=[range(i,m) for i in range(m)]
def dfs(d,s):
    global n,m,Q,ans
    if d==n:
        ans_=0
        for k in Q:
            a,b,c,e=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=e
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in R[l]:
            dfs(d+1,s+str(i))
for i in R[0]:
    dfs(1,str(i))

print(ans)