Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
 
s=[list(map(int,list(input().split()))) for i in range(q)] 
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[s[i][1]-1]) - int(retsu[s[i][0]-1]) == s[i][2]:
            tmp = tmp + s[i][3]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

Fixed submission:
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
for i in range(q):
  A, B, C, D = map(int, input().split())
  a.append(A)
  b.append(B)
  c.append(C)
  d.append(D)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
c = []
 
for i in range(q):
    c.append([int(j) for j in input().split()])
 
nums = range(1, m + 1)
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[c[i][1]-1]) - int(retsu[c[i][0]-1]) == c[i][2]:
            tmp = tmp + c[i][3]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

Fixed submission:
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
for i in range(q):
  A, B, C, D = map(int, input().split())
  a.append(A)
  b.append(B)
  c.append(C)
  d.append(D)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
for i in range(q):
    c.append([int(j) for j in input().split()])
 
nums = range(1, m + 1)
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[c[i][1]-1]) - int(retsu[c[i][0]-1]) == c[i][2]:
            tmp = tmp + c[i][3]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

Fixed submission:
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
for i in range(q):
  A, B, C, D = map(int, input().split())
  a.append(A)
  b.append(B)
  c.append(C)
  d.append(D)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

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

tmp = 0
max = 0
tmpmax=0
c = []

for i in range(q):
    c.append([int(j) for j in input().split()])

for i in range(q):
    tmpmax = tmpmax + c[i][3]

for i in range(1,m+1):
    l.append(str(i))
a = list(itertools.combinations_with_replacement(l, n))

for k in range(len(a)): 
    for i in range(q):
        if int(a[k][c[i][1]-1]) - int(a[k][c[i][0]-1]) == c[i][2]:
            tmp = tmp + c[i][3]
        if max < tmp:
            max = tmp
        if tmpmax == max:
            print(max)
            exit()
    tmp = 0

print(max)


Fixed submission:
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
for i in range(q):
  A, B, C, D = map(int, input().split())
  a.append(A)
  b.append(B)
  c.append(C)
  d.append(D)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)

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

tmp = 0
max = 0
c = []

for i in range(q):
    c.append([int(j) for j in input().split()])

for i in range(1,m+1):
    l.append(str(i))
a = list(itertools.combinations_with_replacement(l, n))

for k in range(len(a)): 
    for i in range(q):
        if int(a[k][c[i][1]-1]) - int(a[k][c[i][0]-1]) == c[i][2]:
            tmp = tmp + c[i][3]
        if max < tmp:
            max = tmp
    tmp = 0

print(max)


Fixed submission:
import itertools
n,m,q=map(int, input().split())
 
tmp = 0
max = 0
a = []
b = []
c = []
d = []
 
for i in range(q):
  A, B, C, D = map(int, input().split())
  a.append(A)
  b.append(B)
  c.append(C)
  d.append(D)
 
nums = range(1, m + 1)
 
for retsu in itertools.combinations_with_replacement(nums, n): 
    for i in range(q):
        if int(retsu[b[i]-1]) - int(retsu[a[i]-1]) == c[i]:
            tmp = tmp + d[i]
        if max < tmp:
            max = tmp
    tmp = 0
 
print(max)