qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N, M, Q = map(int, input().split())
Q_list = []
for i in range(Q):
  Q_list.append(list(map(int, input().split())))


max_cost = 0
#use dfs to find all possible sequences
def dfs(A):
  global max_cost
  #termination condition
  if (len(A)==N):
    cost = 0
    #calculate cost
    for Q in Q_list:
      a = Q[0]
      b = Q[1]
      c = Q[2]
      d = Q[3]
      if (A[b-1]-A[a-1]==c):
        cost += d
    if (cost>max_cost):
      max_cost = cost
    return 1
  #dfs recursion part
  low = 0
  if (A == []):
    low = 1
  else:
  	low = A[-1]+1
  for i in range(low, M-(N-len(A)-2)):
    dfs(A + [i])
    
dfs([])
print(max_cost)

Fixed code:
N, M, Q = map(int, input().split())
Q_list = []
for i in range(Q):
  Q_list.append(list(map(int, input().split())))


max_cost = 0
#use dfs to find all possible sequences
def dfs(A):
  global max_cost
  #termination condition
  if (len(A)==N):
    cost = 0
    #calculate cost
    for Q in Q_list:
      a = Q[0]
      b = Q[1]
      c = Q[2]
      d = Q[3]
      if (A[b-1]-A[a-1]==c):
        cost += d
    if (cost>max_cost):
      max_cost = cost
    return 1
  #dfs recursion part
  low = 0
  if (A == []):
    low = 1
  else:
  	low = A[-1]
  for i in range(low, M+1):
    dfs(A + [i])
    
dfs([])
print(max_cost)
Buggy code:
N, M, Q = (int(i) for i in input().split())  
ABCD = []
ans = -1

for _ in range(Q):
  ABCD.append([int(i) for i in input().split()] )

comb_list = []

import sys
sys.setrecursionlimit(100000000)
def recur(N, lis, c):
  if len(lis) == N:
    comb_list.append(lis)
    return

  if lis == []:
    for i in range(1, N+1):
      recur(N, [i], c+1)
  else:
    for i in range(lis[c], N+1):
      recur(N, lis + [i], c+1)     

recur(N, [], -1)
for x in comb_list:
    tmp = 0
    for a, b, c, d in ABCD:
        if x[b-1] - x[a-1] == c:
             tmp += d
                
    ans = max(ans, tmp)   
print(ans)

Fixed code:
N, M, Q = (int(i) for i in input().split())  
ABCD = []
ans = -1

for _ in range(Q):
  ABCD.append([int(i) for i in input().split()] )

comb_list = []

import sys
sys.setrecursionlimit(100000000)
def recur(N, lis, c):
  if len(lis) == N:
    comb_list.append(lis)
    return

  if lis == []:
    for i in range(1, M+1):
      recur(N, [i], c+1)
  else:
    for i in range(lis[c], M+1):
      recur(N, lis + [i], c+1)     

recur(N, [], -1)
for x in comb_list:
    tmp = 0
    for a, b, c, d in ABCD:
        if x[b-1] - x[a-1] == c:
             tmp += d           
    ans = max(ans, tmp)   
print(ans)
Buggy code:
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]

ans = []
import itertools
for A in itertools.combinations_with_replacement(range(1, 10), N):
    tmp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            tmp += d
    ans.append(tmp)
print(max(ans))

Fixed code:
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]

ans = []
import itertools
for A in itertools.combinations_with_replacement(range(1, M+1), N):
    tmp = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            tmp += d
    ans.append(tmp)
print(max(ans))
Buggy code:
import itertools

#19 choose 9
#92378

def main():
    N, M, Q = map(int, input().split())
    Q = []
    final_score = 0
    for i in range(Q):
        Q.append(list(map(int, input().split())))
    for A in itertools.combinations_with_replacement(range(1, M+1), N):
        score = 0
        for q in Q:
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                score += q[3]
    out = max([final_score, score])
    print(out)
main()

Fixed code:
import itertools

#19 choose 9
#92378

def main():
    N, M, Q = map(int, input().split())
    Q_l = []
    final_score = 0
    for i in range(Q):
        Q_l.append(list(map(int, input().split())))
    for A in itertools.combinations_with_replacement(range(1, M+1), N):
        score = 0
        for q in Q_l:
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                score += q[3]
            final_score = max([final_score, score])
    print(final_score)
main()
Buggy code:
N, M, Q = map(int, input().split())
a = [0 for _ in range(Q)]
b = [0 for _ in range(Q)]
c = [0 for _ in range(Q)]
d = [0 for _ in range(Q)]
for k in range(Q):
  a[k], b[k], c[k], d[k] = map(int, input().split())
A = [1 for _ in range(N)]

def point(A):
  p = 0
  for ai, bi, ci, di in zip(a, b, c, d):
    if A[bi-1] - A[ai-1] == ci:
      p += di
  return p

def next(A):
  k = 1
  while k < N+1:
    if A[-k] < M:
      A[-k] += 1
      return A
      break
    else:
      for j in range(1, k+1):
        A[-j] = A[-k-1] + 1
      k += 1
      
ans = max(ans, point([M for _ in range(N)]))
while A[0] < M:
  ans = max(ans, point(A))
  A = next(A)
print(ans)

Fixed code:
N, M, Q = map(int, input().split())
a = [0 for _ in range(Q)]
b = [0 for _ in range(Q)]
c = [0 for _ in range(Q)]
d = [0 for _ in range(Q)]
for k in range(Q):
  a[k], b[k], c[k], d[k] = map(int, input().split())
A = [1 for _ in range(N)]

def point(A):
  p = 0
  for ai, bi, ci, di in zip(a, b, c, d):
    if A[bi-1] - A[ai-1] == ci:
      p += di
  return p

def next(A):
  k = 1
  while k < N+1:
    if A[-k] < M:
      A[-k] += 1
      return A
      break
    else:
      for j in range(1, k+1):
        A[-j] = A[-k-1] + 1
      k += 1

ans = point([M for _ in range(N)])
while A[0] < M:
  ans = max(ans, point(A))
  A = next(A)
print(ans)
Buggy code:
N, M, Q = map(int, input().split())
vec = [list(map(int, input().split())) for i in range(Q)]
A = itertools.combinations_with_replacement(range(1,M+1), N)
ans = 0

for a in A:
    tmp = 0
    for v in vec:
        if (a[v[1]-1] - a[v[0]-1]) == v[2]:
            tmp += v[3]
    if ans < tmp:
        ans = tmp
        
print(ans)

Fixed code:
import itertools
N, M, Q = map(int, input().split())
vec = [list(map(int, input().split())) for i in range(Q)]
A = itertools.combinations_with_replacement(range(1,M+1), N)
ans = 0

for a in A:
    tmp = 0
    for v in vec:
        if (a[v[1]-1] - a[v[0]-1]) == v[2]:
            tmp += v[3]
    if ans < tmp:
        ans = tmp
        
print(ans)
Buggy code:
# 2020/05/02
# AtCoder Beginner Contest 165 - C

a = list()
def mkArray(in_list, sav_val, cnt):
    if cnt >= m - 1:
        a.append(in_list)
        return 0
    else:
        for x in range(sav_val, m + 1):
            wk_list = list()
            wk_list.extend(in_list)
            wk_list.append(x)
            mkArray(wk_list, x, cnt + 1)

# Input
n, m, q = map(int,input().split())
abcd = list()

for i in range(q):
    w_abcd = list(map(int,input().split()))
    abcd.append(w_abcd)

# Make Array
w_a = list()
mkArray(w_a, 1, 0)

# Search
ans = 0
for i in range(len(a)):
    wans = 0
    for j in range(q):
        if a[i][abcd[j][1]-1] - a[i][abcd[j][0]-1] == abcd[j][2]:
            wans = wans + abcd[j][3]

    if wans > ans:
        ans = wans

# Output
print(ans)


Fixed code:
# 2020/05/02
# AtCoder Beginner Contest 165 - C

a = list()
def mkArray(in_list, sav_val, cnt):
    if cnt >= n:
        a.append(in_list)
        return 0
    else:
        for x in range(sav_val, m + 1):
            wk_list = list()
            wk_list.extend(in_list)
            wk_list.append(x)
            mkArray(wk_list, x, cnt + 1)

# Input
n, m, q = map(int,input().split())
abcd = list()

for i in range(q):
    w_abcd = list(map(int,input().split()))
    abcd.append(w_abcd)

# Make Array
w_a = list()
mkArray(w_a, 1, 0)

# Search
ans = 0
for i in range(len(a)):
    wans = 0
    for j in range(q):
        if a[i][abcd[j][1]-1] - a[i][abcd[j][0]-1] == abcd[j][2]:
            wans = wans + abcd[j][3]

    if wans > ans:
        ans = wans

# Output
print(ans)
Buggy code:
import itertools

N,M,Q = map(int, input().split())
series = list()

for i in range(Q):
	series.append(list(map(int,input().split())))

A = [x for x in range(1,M+1)]

max_score = 0
for a in itertools.combinations(A,N):
	score = 0
	for s in series:
		if (a[s[1]-1] - a[s[0]-1]) == s[2]:
			score += s[3]
			
	if score > max_score: max_score = score

print(max_score)

Fixed code:
import itertools

N,M,Q = map(int, input().split())
series = list()

for i in range(Q):
	series.append(list(map(int,input().split())))

A = [x for x in range(1,M+1)]

max_score = 0
for a in itertools.combinations_with_replacement(A,N):
	score = 0
	for s in series:
		if (a[s[1]-1] - a[s[0]-1]) == s[2]:
			score += s[3]

	if score > max_score: max_score = score

print(max_score)
Buggy code:
n,m,q=map(int,input().split())
abcd=[]
x=[1]
for i in range(q):
    abcd.append(list(map(int,input().split())))
ans=0
def dfs(s):
    global ans
    if(len(s)==n):
        t=0
        for a,b,c,d in abcd:
            if(s[b-1]-s[a-1]==c):
                t+=d
        ans=max(t,ans)
        return 
    p=s[-1]
    print(s)
    while p<=m:
        s.append(p)
        dfs(s)
        p=s.pop()+1
dfs(x)
print(ans)

Fixed code:
n,m,q=map(int,input().split())
abcd=[]
x=[1]
for i in range(q):
    abcd.append(list(map(int,input().split())))
ans=0
def dfs(s):
    global ans
    if(len(s)==n):
        t=0
        for a,b,c,d in abcd:
            if(s[b-1]-s[a-1]==c):
                t+=d
        ans=max(t,ans)
        return 
    p=s[-1]
    while p<=m:
        s.append(p)
        dfs(s)
        p=s.pop()+1
dfs(x)
print(ans)
Buggy code:
n,m,q = map(int,input().split())

a=[]
b=[]
c=[]
d=[]

max_= 0

def dfs(A):
  global max_
  ans = 0
  last = A[-1]
  if len(A) == n+1:
    for i in range(n):
      if A[b[i]]-A[a[i]] == c[i]:
        ans += d[i]
    max_ = max(max_, ans)
    return
  for i in range(last, m+1):
    dfs(A+[i])  


for i in range(q):
  aa,bb,cc,dd = map(int, input().split())
  a.append(aa)
  b.append(bb)
  c.append(cc)
  d.append(dd)
  
dfs([1])

print(max_)





Fixed code:
n,m,q = map(int,input().split())

a=[]
b=[]
c=[]
d=[]

max_= 0

def dfs(A):
  global max_
  ans = 0
  last = A[-1]
  if len(A) == n+1:
    for i in range(q):
      if A[b[i]]-A[a[i]] == c[i]:
        ans += d[i]
    max_ = max(max_, ans)
    return
  for i in range(last, m+1):
    dfs(A+[i])  


for i in range(q):
  aa,bb,cc,dd = map(int, input().split())
  a.append(aa)
  b.append(bb)
  c.append(cc)
  d.append(dd)
  
dfs([1])

print(max_)
Buggy code:
from itertools import combinations
from itertools import combinations_with_replacement

N, M, Q = map(int,input().split())

x = [_ for _ in range(1, M+1)]
y = list(combinations(x, N))
#y = list(combinations_with_replacement(x, N))

l = [list(map(int, input().split())) for _ in range(Q)]

ans = 0
z = 0
for i in range(len(y)):
    for j in range(Q):
        if y[i][l[j][1]-1] - y[i][l[j][0]-1] == l[j][2]:
            z += l[j][3]
        ans = max(ans, z)
    z = 0
print(ans)
    
            
    
    

Fixed code:
from itertools import combinations_with_replacement

N, M, Q = map(int,input().split())
x = [_ for _ in range(1, M+1)]
y = list(combinations_with_replacement(x, N))
l = [list(map(int, input().split())) for _ in range(Q)]

ans = 0
z = 0
for i in range(len(y)):
    for j in range(Q):
        if y[i][l[j][1]-1] - y[i][l[j][0]-1] == l[j][2]:
            z += l[j][3]
        ans = max(ans, z)
    z = 0
print(ans)
Buggy code:
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        print(a,b,c,d)
        if int(A[b-1])-int(A[a-1]) == c:
            pointA += d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

Fixed code:
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)
Buggy code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

n=0
m=0
ans=-inf
l=[]

def dfs(s):
  global n
  global m
  global ans
  global l

  if len(s)==n:
    _ans=0
    for a,b,c,d in l:
      a-=1
      b-=1
      if int(s[b])-int(s[a])==c:
        _ans+=d
    ans=max(ans,_ans)

  if len(s)!=n:
    x=int(s[-1])
    for i in range(x,m+1):
      dfs(s+str(i))

def main():
  global n
  global m
  global ans
  global l

  n,m,q=LI()
  l=[LI() for _ in range(q)]

  for i in range(1,m+1):
    dfs(str(i))

  return ans

# main()
print(main())


Fixed code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

n=0
m=0
ans=-inf
l=[]

def dfs(s):
  global n
  global m
  global ans
  global l

  if len(s)==n:
    _ans=0
    for a,b,c,d in l:
      a-=1
      b-=1
      if s[b]-s[a]==c:
        _ans+=d
    ans=max(ans,_ans)

  if len(s)!=n:
    x=s[-1]
    for i in range(x,m+1):
      dfs(s+[i])

def main():
  global n
  global m
  global ans
  global l

  n,m,q=LI()
  l=[LI() for _ in range(q)]

  for i in range(1,m+1):
    dfs([i])

  return ans

# main()
print(main())
Buggy code:
import itertools
n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations(I, n)))
l = len(A)
for i in range(l):
    if A[0][0] == 1:
        A.append(A[0])
    A.pop(0)
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d

print(max(ans))

Fixed code:
import itertools

n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations_with_replacement(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))
Buggy code:
import itertools

N, M, Q = map(int, input().split())
comb = list(itertools.combinations(range(1,M+1), N))

ans = [0]*len(comb)
for i in range(Q):
    a,b,c,d = map(int, input().split())
    for j in range(len(comb)):
        if comb[j][b-1] - comb[j][a-1] == c:
            ans[j] += d
            
print(max(ans))

Fixed code:
import itertools

N, M, Q = map(int, input().split())
comb = list(itertools.combinations_with_replacement(range(1,M+1), N))

ans = [0]*len(comb)
if ans == []:
    print(0)
else:
    for i in range(Q):
        a,b,c,d = map(int, input().split())
        for j in range(len(comb)):
            if comb[j][b-1] - comb[j][a-1] == c:
                ans[j] += d
    print(max(ans))
Buggy code:
N, M, Q = map(int, input().split())
A = []

for i in range(Q):
  A.append(list(map(int, input().split())))

def calc(B, n):
  ans = 0
  #not enough length
  if len(B) < N:
    for i in range(n, M + 1):
      ans = max(ans, calc(B + [i], i))
  #enough length
  else:
    print("B:", B)
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    print("ans:", ans)
  return ans



print(calc([], 1))


Fixed code:
N, M, Q = map(int, input().split())
A = []

for i in range(Q):
  A.append(list(map(int, input().split())))

def calc(B, n):
  ans = 0
  #not enough length
  if len(B) < N:
    for i in range(n, M + 1):
      ans = max(ans, calc(B + [i], i))
  #enough length
  else:
    #print("B:", B)
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    #print("ans:", ans)
  return ans



print(calc([], 1))
Buggy code:
import itertools

N,M,Q = map(int,input().split())
a=[]
b=[]
c=[]
d=[]
for i in range(0,Q,1):
    tmp = list(map(int,input().split()))
    a.append(tmp[0])
    b.append(tmp[1])
    c.append(tmp[2])
    d.append(tmp[3])

max_score = -1
for i in itertools.combinations_with_replacement(range(1,M,1),N):
    score = 0
    for j in range(0,Q,1):
        if i[b[j]-1]-i[a[j]-1]==c[j]:
            score += d[j]
    if max_score < score:
        max_score = score

print(max_score)

Fixed code:
import itertools

N,M,Q = map(int,input().split())
a=[]
b=[]
c=[]
d=[]
for i in range(0,Q,1):
    tmp = list(map(int,input().split()))
    a.append(tmp[0])
    b.append(tmp[1])
    c.append(tmp[2])
    d.append(tmp[3])

max_score = -1
for i in itertools.combinations_with_replacement(range(1,M+1,1),N):
    score = 0
    for j in range(0,Q,1):
        if i[b[j]-1]-i[a[j]-1]==c[j]:
            score += d[j]
    if max_score < score:
        max_score = score

print(max_score)
Buggy code:
n, m, q = map(int, input().split())

a = [0] * 55; b = [0] * 55; c = [0] * 55; d = [0] * 55

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

def socer(A):
    now = 0
    for i in range(q):
        if A[b[i]-1] - A[a[i]-1] == c[i]: now += d[i]
    return now

def dfs(A):
    print(A)
    if len(A) == n: return socer(A)
    res = 0
    last = A[-1] if A else 0
    for i in range(last, m):
        A.append(i)
        res = max(res, dfs(A))
        A.pop()
    return res
      
print(dfs([]))

Fixed code:
n, m, q = map(int, input().split())

a = [0] * 55; b = [0] * 55; c = [0] * 55; d = [0] * 55

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

def socer(A):
    now = 0
    for i in range(q):
        if A[b[i]-1] - A[a[i]-1] == c[i]: now += d[i]
    return now

def dfs(A):
    # print(A)
    if len(A) == n: return socer(A)
    res = 0
    last = A[-1] if A else 0
    for i in range(last, m):
        A.append(i)
        res = max(res, dfs(A))
        A.pop()
    return res
      
print(dfs([]))
Buggy code:
import itertools
import numpy as np

N, M, Q = map(int, input().split())

A = np.array(list(itertools.combinations_with_replacemeent(range(1, M+1), N)))
s = np.zeros(len(A), np.int32)

for i in range(Q):
    a, b, c, d = map(int, input().split())
    s += d * (A[:, b-1] - A[:, a-1] == c)

print(s.max())

Fixed code:
import itertools
import numpy as np

N, M, Q = map(int, input().split())

A = np.array(list(itertools.combinations_with_replacement(range(1, M+1), N)))
s = np.zeros(len(A), np.int32)

for i in range(Q):
    a, b, c, d = map(int, input().split())
    s += d * (A[:, b-1] - A[:, a-1] == c)

print(s.max())
Buggy code:
import itertools

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N,M,Q = map(int, input().split())
L = []

for i in range(1,M+1,1):
    L.append(i)

A = []
B = []
C = []
D = []
for _ in range(Q):
    a,b,c,d = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

List = list(itertools.combinations(L, N))
ans = 0
for l in List:
    temp = 0
    for q in range(Q):
        if (l[B[q]-1]-l[A[q]-1])==C[q]:
            temp = temp + D[q]
    ans = max(ans, temp)
print(ans)
            

Fixed code:
import itertools

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N,M,Q = map(int, input().split())
L = []

for i in range(1,M+1,1):
    L.append(i)

A = []
B = []
C = []
D = []
for _ in range(Q):
    a,b,c,d = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

List = list(itertools.combinations_with_replacement(L, N))
ans = 0
for l in List:
    temp = 0
    for q in range(Q):
        if (l[B[q]-1]-l[A[q]-1])==C[q]:
            temp = temp + D[q]
    ans = max(ans, temp)
print(ans)
Buggy code:
N, M, Q = map(int, input().split())
s = list(list(map(int, input().split())) for _ in range(Q))

mx = 0
a = [1]*10

for s1 in range(1, M+1):
    a[0] = s1
    for s2 in range(s1, M+1):
        a[1] = s2
        for s3 in range(s2, M+1):
            a[2] = s3
            for s4 in range(s3, M+1):
                a[3] = s4
                for s5 in range(s4, M+1):
                    a[4] = s5
                    for s6 in range(s5, M+1):
                        a[5] = s6
                        for s7 in range(s6, M+1):
                            a[6] = s7
                            for s8 in range(s7, M+1):
                                a[7] = s8
                                for s9 in range(s8, M+1):
                                    a[8] = s9
                                    for s10 in range(s9, M+1):
                                        a[9] = s10
                                        
                                        cnt = 0
                                        for i in range(Q):
                                            si = s[i]
                                            if a[si[1]] - a[si[0]] == si[2]:
                                                cnt += si[3]
                                            mx = max(mx, cnt)
                                            
print(mx)

Fixed code:
N, M, Q = map(int, input().split())
s = list(list(map(int, input().split())) for _ in range(Q))

mx = 0
a = [1]*10

for s1 in range(1, M+1):
    a[0] = s1
    for s2 in range(s1, M+1):
        a[1] = s2
        for s3 in range(s2, M+1):
            a[2] = s3
            for s4 in range(s3, M+1):
                a[3] = s4
                for s5 in range(s4, M+1):
                    a[4] = s5
                    for s6 in range(s5, M+1):
                        a[5] = s6
                        for s7 in range(s6, M+1):
                            a[6] = s7
                            for s8 in range(s7, M+1):
                                a[7] = s8
                                for s9 in range(s8, M+1):
                                    a[8] = s9
                                    for s10 in range(s9, M+1):
                                        a[9] = s10
                                        
                                        cnt = 0
                                        for i in range(Q):
                                            si = s[i]
                                            if a[si[1]-1] - a[si[0]-1] == si[2]:
                                                cnt += si[3]
                                            mx = max(mx, cnt)
                                            
print(mx)
Buggy code:
N,M,Q=map(int,input().split())
que=[tuple(map(int,input().split())) for _ in range(Q)]
task=[]
sta=[str(i) for i in range(1,M+1)]
while sta:
    s=sta.pop()
    if len(s)==N: task.append(s)
    else:
        for d in range(int(s[-1]),M+1):
            sta.append(s+str(d))
ans=0
for s in task:
    A=[int(i) for i in s]
    tmp=0
    for a,b,c,d in que:
        if A[b-1]-A[a-1]==c: tmp+=d
    ans=max(ans,tmp)
print(ans)

Fixed code:
N,M,Q=map(int,input().split())
que=[tuple(map(int,input().split())) for _ in range(Q)]
task=[]
sta=[[i] for i in range(1,M+1)]
while sta:
    s=sta.pop()
    if len(s)==N: task.append(s)
    else:
        for d in range(int(s[-1]),M+1):
            sta.append(s+[d])
ans=0
for s in task:
    tmp=0
    for a,b,c,d in que:
        if s[b-1]-s[a-1]==c: tmp+=d
    ans=max(ans,tmp)
print(ans)