Buggy python submission (verdict: Runtime Error):
n,m,q=map(int,input().split())

a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by)
    c.append(cy)
    d.append(dy)
s=0
smax=0
for i1 in range(0,m+1):
    for i2 in range(i1,m+1):
        for i3 in range(i2,m+1):
            for i4 in range(i3,m+1):
                for i5 in range(i4,m+1):
                    for i6 in range(i5,m+1):
                        for i7 in range(i6,m+1):
                            for i8 in range(i7,m+1):
                                for i9 in range(i8,m+1):
                                    for i10 in range(i9,m+1):
                                        s=0
                                        na=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for j in range(0,q):
                                            if na[b[j]]-na[a[j]]==c[j]:
                                                s+=d[j]
                                        if s>smax:
                                            smax=s


print(smax)

Fixed submission:
import sys
n,m,q=map(int,input().split())


a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by1,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by1)
    c.append(cy)
    d.append(dy)
s=0
smax=-100

sys.setrecursionlimit(3000)



def dfs(h):
    global smax
    if len(h)==n+1:
        s=0
        for j in range(0,q):
            if h[b[j]]-h[a[j]]==c[j]:
                s+=d[j]
        if s>smax:
            smax=s

        return
    
    for i in range(0,m+1-h[len(h)-1]):
        
        h2=h[:]
        h2.append(h[len(h)-1]+i)
        dfs(h2)

sta=[1]
dfs(sta)
print(smax)

Buggy python submission (verdict: Runtime Error):
n,m,q=map(int,input().split())

a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by)
    c.append(cy)
    d.append(dy)
s=0
smax=0
for i1 in range(0,11):
    for i2 in range(i1,11):
        for i3 in range(i2,11):
            for i4 in range(i3,11):
                for i5 in range(i4,11):
                    for i6 in range(i5,11):
                        for i7 in range(i6,11):
                            for i8 in range(i7,11):
                                for i9 in range(i8,11):
                                    for i10 in range(i9,11):
                                        s=0
                                        na=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for j in range(0,q):
                                            if na[b[j]]-na[a[j]]==c[j]:
                                                s+=d[j]
                                        if s>smax:
                                            smax=s



print(smax)

Fixed submission:
import sys
n,m,q=map(int,input().split())


a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by1,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by1)
    c.append(cy)
    d.append(dy)
s=0
smax=-100

sys.setrecursionlimit(3000)



def dfs(h):
    global smax
    if len(h)==n+1:
        s=0
        for j in range(0,q):
            if h[b[j]]-h[a[j]]==c[j]:
                s+=d[j]
        if s>smax:
            smax=s

        return
    
    for i in range(0,m+1-h[len(h)-1]):
        
        h2=h[:]
        h2.append(h[len(h)-1]+i)
        dfs(h2)

sta=[1]
dfs(sta)
print(smax)