Buggy python submission (verdict: Time Limit Exceeded):
import  itertools
n,m,q=map(int,input().split())

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    cv=[1 for h in range(n)]
    for i in range(q):
        if l[i]==1:
            cv[t[i][1]]=cv[t[i][0]]+t[i][2]
    for i in range(n):
        if cv[i]>m:
            return False
    for i in range(q):
        if l[i]==1:
            if cv[t[i][1]]-cv[t[i][0]]!=t[i][2]:
                return False
    return True

num=[0,1]
ans=0

for df in itertools.product(num,repeat=q):
    ty=list(df)
    if judge(ty):
        to=0

        for i in range(q):
            if ty[i]==1:
                to+=t[i][3]
        ans=max(ans,to)

print(ans)





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

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    ans=0
    for i in range(q):
        a,b,c,d=t[i]
        if l[b]-l[a]==c:
            ans+=d
    return ans

num=[]
ans=0
for i in range(1,m+1):
    num.append(i)

for df in itertools.combinations_with_replacement(num,n):

    ty=list(df)
    f=judge(ty)
    ans=max(ans,f)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import  itertools
n,m,q=map(int,input().split())

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    cv=[1 for h in range(n)]
    for i in range(q):
        if l[i]==1:
            cv[t[i][1]]=cv[t[i][0]]+t[i][2]
    for i in range(n):
        if cv[i]>m:
            return False
    for i in range(q):
        if l[i]==1:
            if cv[t[i][1]]-cv[t[i][0]]!=t[i][2]:
                return False
    return True

num=[0,1]
ans=0

for df in itertools.product(num,repeat=q):
    ty=list(df)
    if judge(ty):
        to=0

        for i in range(q):
            if ty[i]==1:
                to+=t[i][3]
        ans=max(ans,to)

print(ans)




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

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    ans=0
    for i in range(q):
        a,b,c,d=t[i]
        if l[b]-l[a]==c:
            ans+=d
    return ans

num=[]
ans=0
for i in range(1,m+1):
    num.append(i)

for df in itertools.combinations_with_replacement(num,n):

    ty=list(df)
    f=judge(ty)
    ans=max(ans,f)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import  itertools
n,m,q=map(int,input().split())

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    cv=[1 for h in range(n)]
    for i in range(q):
        if l[i]==1:
            cv[t[i][1]]=cv[t[i][0]]+t[i][2]
    flag=True
    for i in range(n):
        if cv[i]>m:
            flag= False

    for i in range(q):
        if l[i]==1:
            if cv[t[i][1]]-cv[t[i][0]]!=t[i][2]:
                flag=False
    if flag:
        return True
    else:
        return False

num=[0,1]
ans=0

for df in itertools.product(num,repeat=q):
    ty=list(df)
    if judge(ty):
        to=0
        #print(ty)
        for i in range(q):
            if ty[i]==1:
                to+=t[i][3]

        ans=max(ans,to)

print(ans)





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

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    ans=0
    for i in range(q):
        a,b,c,d=t[i]
        if l[b]-l[a]==c:
            ans+=d
    return ans

num=[]
ans=0
for i in range(1,m+1):
    num.append(i)

for df in itertools.combinations_with_replacement(num,n):

    ty=list(df)
    f=judge(ty)
    ans=max(ans,f)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import  itertools
n,m,q=map(int,input().split())

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    cv=[1 for h in range(n)]
    for i in range(q):
        if l[i]==1:
            cv[t[i][1]]=cv[t[i][0]]+t[i][2]
    flag=True
    for i in range(n):
        if cv[i]>m:
            flag= False

    for i in range(q):
        if l[i]==1:
            if cv[t[i][1]]-cv[t[i][0]]!=t[i][2]:
                flag=False
    if flag:
        return True
    else:
        return False

num=[0,1]
ans=0

for df in itertools.product(num,repeat=q):
    ty=list(df)
    if judge(ty):
        to=0
        #print(ty)
        for i in range(q):
            if ty[i]==1:
                to+=t[i][3]

        ans=max(ans,to)

print(ans)





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

t=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a-=1
    b-=1
    t.append([a,b,c,d])

def judge(l):
    ans=0
    for i in range(q):
        a,b,c,d=t[i]
        if l[b]-l[a]==c:
            ans+=d
    return ans

num=[]
ans=0
for i in range(1,m+1):
    num.append(i)

for df in itertools.combinations_with_replacement(num,n):

    ty=list(df)
    f=judge(ty)
    ans=max(ans,f)

print(ans)