qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n,m,q=map(int,input().split(" "))
arr=[list(map(int, input().split(" "))) for i in range(q)]
ans=0
for i1 in range(1,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):
										tmp=0
										arrs=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
										for a,b,c,d in arr:
											if arrs[b]-arrs[a]==c:
												tmp+=d
										ans=max(ans,tmp)
print(ans)

Fixed code:
n,m,q=map(int,input().split(" "))
arr=[list(map(int, input().split(" "))) for i in range(q)]
ans=0
for i1 in range(1,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):
										tmp=0
										arrs=[0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
										for a,b,c,d in arr:
											if arrs[b]-arrs[a]==c:
												tmp+=d
										ans=max(ans,tmp)
print(ans)
Buggy code:
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(q)]

a=[]
for i in range(n-1):
  a.append(1)
a.append(0)
current=0
b=[]
for i in range(n):
  b.append(m)
while a!=b:
  i=n-1
  while a[i]==m:
    i=i-1
  a[i]+=1
  for j in range(i+1,n):
      a[j]=1
  d=0
  for j in range(q):
    if (a[l[j][1]-1]-a[l[j][0]-1])==l[j][2]:
      d+=l[j][3]
  if d>current:
    current=d

print(current)

Fixed code:
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(q)]

a=[]
for i in range(n-1):
  a.append(1)
a.append(0)
current=0
b=[]
for i in range(n):
  b.append(m)

while True:
  i=n-1
  while a[i]==m:
    i=i-1
    if i==-1:
      break
  if i==-1:
    break
  a[i]+=1
  for j in range(i+1,n):
      a[j]=a[i]
  d=0
  for j in range(q):
    if (a[l[j][1]-1]-a[l[j][0]-1])==l[j][2]:
      d+=l[j][3]
  if d>current:
    current=d

print(current)
Buggy code:
import itertools

n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]

A_list = list(itertools.combinations_with_replacement(range(1,m),n))

ans = 0
for A in A_list:
    sum = 0
    for a,b,c,d in abcd:
        if A[b-1] - A[a-1] == c:
            sum += d
    ans = max(ans, sum)

print(ans)

Fixed code:
import itertools

n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]

A_list = list(itertools.combinations_with_replacement(range(1,m+1),n))

ans = 0
for A in A_list:
    sum = 0
    for a,b,c,d in abcd:
        if A[b-1] - A[a-1] == c:
            sum += d
    ans = max(ans, sum)

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

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

import itertools
seq = list(range(1,m+1))
lis_p = list(itertools.combinations(seq, n))

ans = 0
ans_max = 0

for li_p in lis_p:
    for li in lis:
        if li_p[li[1]-1] - li_p[li[0]-1] == li[2]:
            ans += li[3]
    #print(li_p, ans)
    if ans > ans_max:
        ans_max = ans
    ans = 0

print(ans_max)

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

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

import itertools
seq = list(range(1,m+1))
lis_p = list(itertools.combinations_with_replacement(seq, n))

ans = 0
ans_max = 0

for li_p in lis_p:
    for li in lis:
        if li_p[li[1]-1] - li_p[li[0]-1] == li[2]:
            ans += li[3]
    #print(li_p, ans)
    if ans > ans_max:
        ans_max = ans
    ans = 0

print(ans_max)
Buggy code:
n , m , q = map(int,input().split())
p = []
for i in range(q):
    a , b , c , d = map(int,input().split())
    p.append([a,b,c,d])
ans = 0
for a1 in range(1,m+1):
    for a2 in range(a1,m+1):
        for a3 in range(a2,m+1):
            for a4 in range(a3,m+1):
                for a5 in range(a4,m+1):
                    for a6 in range(a5,m+1):
                        for a7 in range(a6,m+1):
                            for a8 in range(a7,m+1):
                                for a9 in range(a8,m+1):
                                    for a10 in range(a9,m+1):
                                        tyu = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]
                                        kar = 0
                                        for x,y,z,w in p:
                                            if tyu[y] - tyu[x] == z:
                                                kar += w
                                        ans = max(kar,ans)
print(ans)
                                        
                        

Fixed code:
n , m , q = map(int,input().split())
p = []
for i in range(q):
    a , b , c , d = map(int,input().split())
    p.append([a,b,c,d])
ans = 0
for a1 in range(1,m+1):
    for a2 in range(a1,m+1):
        for a3 in range(a2,m+1):
            for a4 in range(a3,m+1):
                for a5 in range(a4,m+1):
                    for a6 in range(a5,m+1):
                        for a7 in range(a6,m+1):
                            for a8 in range(a7,m+1):
                                for a9 in range(a8,m+1):
                                    for a10 in range(a9,m+1):
                                        tyu = [0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]
                                        kar = 0
                                        for x,y,z,w in p:
                                            if tyu[y] - tyu[x] == z:
                                                kar += w
                                        ans = max(kar,ans)
print(ans)
Buggy code:
N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
A = [1] * N
ans = 0

for a, b, c, d in l:
    if A[b-1] - A[a-1] == c:
        ans += d
if M > 1:
    while True:
        A[-1] += 1
        ans_ = 0
        for a, b, c, d in l:
            if A[b-1] - A[a-1] == c:
                ans_ += d
        if ans_ > ans:
            ans = ans_

        if A[-1] == M + 1:
            for i in range(1, N):
                A[-(i+1)] += 1
                for k in range(N-(i+1), N):
                    A[k] = A[-(i+1)]
                if A[-(i+1)] != M + 1:
                    break
        print(A)
        if A[0] == M:
            break
print(ans)
    

    
    






Fixed code:
N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
A = [1] * N
ans = 0

for a, b, c, d in l:
    if A[b-1] - A[a-1] == c:
        ans += d

if M > 1:
    while True:
        A[-1] += 1
        if A[-1] == M + 1:
            for i in range(1, N):
                A[-(i+1)] += 1
                for k in range(N-(i+1), N):
                    A[k] = A[-(i+1)]
                if A[-(i+1)] != M + 1:
                    break
            
        ans_ = 0
        for a, b, c, d in l:
            if A[b-1] - A[a-1] == c:
                ans_ += d
        ans = max(ans, ans_)

        if A[0] == M:
            break
print(ans)
Buggy code:
def dfs(A):
  global ans
  if len(A)==N: 
    now=0
    for i in l:
      if A[i[1]-1]-A[i[0]-1]==i[2]:
        now+=i[3]
    ans=max(ans,now)
    return A


  A.append(A[-1])
  while A[-1]<=M:
    dfs(A)
    A[-1]+=1
    
    
  



N,M,Q=[int(x) for x in input().rstrip().split()]
A=[1]
l=[]
ans=0
for i in range(Q):
  abcd=[int(x) for x in input().rstrip().split()]
  l.append(abcd)
dfs(A)
print(ans)
  

Fixed code:
def dfs(A):
  global ans
  if len(A)==N: 
    now=0
    for i in l:
      if A[i[1]-1]-A[i[0]-1]==i[2]:
        now+=i[3]
    ans=max(ans,now)
    return A

  A.append(A[-1])
  while A[-1]<=M:
    dfs(A[:])
    A[-1]+=1
    
  
N,M,Q=[int(x) for x in input().rstrip().split()]
A=[1]
l=[]
ans=0
for i in range(Q):
  abcd=[int(x) for x in input().rstrip().split()]
  l.append(abcd)
dfs(A)
print(ans)
Buggy code:
n,m,q = map(int,input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(n):
    a[i],b[i],c[i],d[i] = map(int,input().split())
    a[i] -= 1
    b[i] -= 1
A = [0]*n
ans = 0
def dfs(h):
    global ans
    if h == n-1:
        cnt = 0
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                cnt += d[i]
        ans = max(ans,cnt)
    else:
        for i in range(A[h],m+1):
            A[h+1] = i
            dfs(h+1)
    return

for i in range(1,m+1):
    A[0] = i
    dfs(0)
print(ans)


Fixed code:
n,m,q = map(int,input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(q):
    a[i],b[i],c[i],d[i] = map(int,input().split())
    a[i] -= 1
    b[i] -= 1
A = [0]*n
ans = 0
def dfs(h):
    global ans
    if h == n-1:
        cnt = 0
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                cnt += d[i]
        ans = max(ans,cnt)
    else:
        for i in range(A[h],m+1):
            A[h+1] = i
            dfs(h+1)
    return

for i in range(1,m+1):
    A[0] = i
    dfs(0)
print(ans)
Buggy code:
import sys
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

sys.setrecursionlimit(3000)
print(sys.getrecursionlimit())


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,n+2-h[len(h)-1]):
        
        h2=h[:]
        h2.append(h[len(h)-1]+i)
        dfs(h2)

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

Fixed code:
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 code:
import itertools

def main():
    n,m,q = map(int,input().split())
    a = [0] * (q+1)
    b = [0] * (q+1)
    c = [0] * (q+1)
    d = [0] * (q+1)

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

    Alist = itertools.combinations_with_replacement(range(1,m+1),n)

    ans = 0
    print(a,b,c,d)
    for A in Alist:
        cnt = 0
        for i in range(1,q+1):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                cnt += d[i]
        ans = max(ans,cnt)
    print(ans)


if __name__ == "__main__":
    main()


Fixed code:
import itertools

def main():
    n,m,q = map(int,input().split())
    a = [0] * (q+1)
    b = [0] * (q+1)
    c = [0] * (q+1)
    d = [0] * (q+1)

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

    Alist = itertools.combinations_with_replacement(range(1,m+1),n)

    ans = 0
    for A in Alist:
        cnt = 0
        for i in range(1,q+1):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                cnt += d[i]
        ans = max(ans,cnt)
    print(ans)


if __name__ == "__main__":
    main()
Buggy code:

import itertools

def iln(): return list(map(int, input().split()))

def solve(N, M, constraints):
    max_so_far = 0
    for seq in itertools.combinations(range(1, M + 1), N):
        #print("Processing sequence: ", seq)
        curr_score = 0
        for const in constraints:
            a,b,c,d = const

            if seq[b-1] - seq[a-1] == c:
                curr_score += d

        max_so_far = max(max_so_far, curr_score)

    return max_so_far


N, M, Q = iln()
constraints = []
for i in range(Q):
    constraint = tuple(iln())
    constraints.append(constraint)

print(solve(N, M, constraints))


Fixed code:
import itertools

def iln(): return list(map(int, input().split()))

def solve(N, M, constraints):
    max_so_far = 0
    for seq in itertools.combinations_with_replacement(range(1, M + 1), N):
        #print("Processing sequence: ", seq)
        curr_score = 0
        for const in constraints:
            a,b,c,d = const
            if seq[b-1] - seq[a-1] == c:
                curr_score += d

        max_so_far = max(max_so_far, curr_score)

    return max_so_far


N, M, Q = iln()
constraints = []
for i in range(Q):
    constraint = tuple(iln())
    constraints.append(constraint)

print(solve(N, M, constraints))
Buggy code:
import itertools
n,m,q = map(int,input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(q):
    a[i],b[i],c[i],d[i] = map(int,input().split())
ans = 0
for A in itertools.combinations_with_replacement(range(1,m+1),n):
    cnt = 0
    for i in range(q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            cnt += d[i]
    ans = max(ans,tmp)
print(ans)


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

n,m,q=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(q)]

ms=[i for i in range(1,m+1)]
ans=0
for k in itertools.permutations(ms, n):
  x=0
  for j in range(q):
    if a[j][2]==k[a[j][1]-1]-k[a[j][0]-1]:
      x+=a[j][3]
      print(x)
  if ans<x:
    ans=x
print(ans)

Fixed code:
import itertools
 
n,m,q=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(q)]
 
ms=[i for i in range(1,m+1)]
ans=0
for k in itertools.combinations_with_replacement(ms, n):
  x=0
  for j in range(q):
    if a[j][2]==k[a[j][1]-1]-k[a[j][0]-1]:
      x+=a[j][3]
  if ans<x:
    ans=x
print(ans)
Buggy code:
import copy

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

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)

seq = [[] for i in range(n)]

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

for i in range(1, n):
  for j in range(len(seq[i - 1])):
    s = copy.copy(seq[i - 1][j])
    s.append(0)
    for k in range(s[-2], m + 1):
      s[-1] = k
      s2 = copy.copy(s)
      seq[i].append(s2)

score = [0 for i in range(len(seq[n - 1]))]
for i in range(len(seq[n - 1])):
  for j in range(q):
    if seq[n - 1][i][b[j] - 1] - seq[n - 1][i][a[j] - 1] == c:
      score[i] += d[j]

print(max(score))

Fixed code:
import copy

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

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)

seq = [[] for i in range(n)]

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

for i in range(1, n):
  for j in range(len(seq[i - 1])):
    s = copy.copy(seq[i - 1][j])
    s.append(0)
    for k in range(s[-2], m + 1):
      s[-1] = k
      s2 = copy.copy(s)
      seq[i].append(s2)

score = [0 for i in range(len(seq[n - 1]))]
for i in range(len(seq[n - 1])):
  for j in range(q):
    if seq[n - 1][i][b[j] - 1] - seq[n - 1][i][a[j] - 1] == c[j]:
      score[i] += d[j]

print(max(score))
Buggy code:
# ABC165 C

import itertools

def main():
    n, m, q = map(int, input().split())
    List = []
    for i in range(q):
        a, b, c, d = map(int, input().split())
        List.append([a, b, c, d])
    
    max_total = 0

    for A in list(itertools.combinations(range(1, m+1),n)):
        total = 0
        for abcd in List:
            if A[abcd[1]-1] - A[abcd[0]-1] == abcd[2]:
                total += abcd[3]
        #print(total)
        if total >= max_total:
            max_total = total
    
    print(max_total)

if __name__ == '__main__':
    main()

Fixed code:
# ABC165 C

import itertools

def main():
    n, m, q = map(int, input().split())
    List = []
    for i in range(q):
        a, b, c, d = map(int, input().split())
        List.append([a, b, c, d])
    
    max_total = 0

    for A in list(itertools.combinations_with_replacement(range(1, m+1),n)):
        total = 0
        for abcd in List:
            if A[abcd[1]-1] - A[abcd[0]-1] == abcd[2]:
                total += abcd[3]
        #print(total)
        if total >= max_total:
            max_total = total
    
    print(max_total)

if __name__ == '__main__':
    main()
Buggy code:
import itertools

n, m, q = map(int, input().split())
conds = [list(map(int, input().split())) for _ in range(q)]

elements = [i for i in range(1, m + 1)]
A = list(itertools.product(elements, repeat=n))

point_max = 0
for a in A:
    point = 0
    for c in conds:
        if a[c[1] - 1] - a[c[0] - 1] == c[2]:
            point += c[3]
    point_max = max(point_max, point)

print(point_max)

Fixed code:
import itertools

n, m, q = map(int, input().split())
conds = [list(map(int, input().split())) for _ in range(q)]

elements = [i for i in range(1, m + 1)]
A = list(itertools.combinations_with_replacement(elements, n))

point_max = 0
for a in A:
    point = 0
    for c in conds:
        if a[c[1] - 1] - a[c[0] - 1] == c[2]:
            point += c[3]
    point_max = max(point_max, point)

print(point_max)
Buggy code:
import sys
input = sys.stdin.readline

n, m, q = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for _ in range(q)]
all_pattern = []
start = '1'
def dfs(s):
    last_s = int(s[-1])
    next_s = [str(x) for x in range(last_s, m + 1)]
    for i in next_s:
        t = s + i
        if len(t) == n + 1:
            all_pattern.append(t[1:])
        else:
            dfs(t)
dfs(start)
ans = 0
for pattern in all_pattern:
    res = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if int(pattern[b - 1]) - int(pattern[a - 1]) == c:
            res += d
    ans = max(ans, res)
print(ans)

    





Fixed code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m, q = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for _ in range(q)]
all_pattern = []
start = ['1']

def dfs(s):
    last_s = int(s[-1])
    next_s = [str(x) for x in range(last_s, m + 1)]
    for i in next_s:
        t = s[:]
        t.append(i)
        if len(t) == n + 1:
            all_pattern.append(t[1:])
        else:
            dfs(t)
dfs(start)
ans = 0
for pattern in all_pattern:
    res = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if int(pattern[b - 1]) - int(pattern[a - 1]) == c:
            res += d
    ans = max(ans, res)
#print(all_pattern)
print(ans)
Buggy code:
from itertools import combinations
N, M, Q = map(int,input().split())
P = [list(map(int,input().split())) for i in range(Q)]

A = list(itertools.combinations(range(1,M+1), 3))
number = len(A)

amount = 0
max_amount = 0
for i in range(number): # 0,1,2,3 A
    for j in range(Q): # 0,1,2
        bi = P[j][1]
        ai = P[j][0]
        
        if(A[i][bi-1] - A[i][ai-1] == P[j][2]):
            amount += P[j][3]
    
   # print(amount)
    if(amount >= max_amount):
        max_amount = amount
    
    amount = 0
    
print(max_amount)

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

A = list(itertools.combinations_with_replacement(range(1,M+1), N))
number = len(A)

amount = 0
max_amount = 0
for i in range(number): #A
    
    for j in range(Q): # P
        bi = P[j][1]
        ai = P[j][0]
        if(A[i][bi-1] - A[i][ai-1] == P[j][2]):
            amount += P[j][3]
        
   # print(amount)
    
   # print(amount)
    if(amount >= max_amount):
        max_amount = amount
    
    amount = 0
    
print(max_amount)
Buggy code:
import itertools
N,M,Q=list(map(int,input().split()))
src=[list(map(int,input().split())) for q in range(Q)]
ML=[n+1 for n in range(M)]
X=list(itertools.combinations_with_replacement(ML,N))
L=[]
for x in X:
  a=0
  for s in src:
    if x[s[1]] - x[s[0]] == s[2]:
      a+=s[3]
  L.append(a)
print(max(L))

Fixed code:
import itertools
N,M,Q=list(map(int,input().split()))
src=[]
for q in range(Q):
  src.append(list(map(int,input().split())))
ML=[n+1 for n in range(M)]
X=list(itertools.combinations_with_replacement(ML,N))
L=[]
for x in X:
  a=0
  for s in src:
    if x[s[1]-1] - x[s[0]-1] == s[2]:
      a+=s[3]
  L.append(a)
print(max(L))
Buggy code:
import itertools
n,m,q=map(int,input().split())
lis=[i for i  in range(1,m+1)]
lis=list(itertools.combinations(lis,n))
box=[0 for j in range(len(lis))]
for i in range(q):
    que=list(map(int,input().split()))
    for j in range(len(lis)):
        if lis[j][que[1]-1]-lis[j][que[0]-1]==que[2]:
            box[j]+=que[3] 
print(max(box))

Fixed code:
import itertools
n,m,q=map(int,input().split())
lis=[i for i  in range(1,m+1)]
lis=list(itertools.combinations_with_replacement(lis,n))
box=[0 for j in range(len(lis))]
for i in range(q):
    que=list(map(int,input().split()))
    for j in range(len(lis)):
        if lis[j][que[1]-1]-lis[j][que[0]-1]==que[2]:
            box[j]+=que[3] 
print(max(box))
Buggy code:
import itertools
N, M, Q = map(int, input().split())
a_b_c_d = [list(map(int, input().split())) for i in range(Q)]

M = [int(i) for i in range(1, M+1)]
A_possible = list(itertools.combinations(M, N))
A_score = 0
for A in A_possible:
    A_sum = 0
    for i in range(Q):
        A_test = A[a_b_c_d[i][1]-1] - A[a_b_c_d[i][0]-1]
        if A_test == a_b_c_d[i][2]:
            A_sum += a_b_c_d[i][3]
    if A_score < A_sum:
        A_score = A_sum
print(A_score)

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

M = [i for i in range(1, M+1)]
A_possible = list(itertools.combinations_with_replacement(M, N))

A_score = 0
for A in A_possible:
    A_sum = 0
    for i in range(Q):
        A_test = A[a_b_c_d[i][1]-1] - A[a_b_c_d[i][0]-1]
        if A_test == a_b_c_d[i][2]:
            A_sum += a_b_c_d[i][3]
    if A_score < A_sum:
        A_score = A_sum
print(A_score)
Buggy code:
                                                                               │➜  typical
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

import itertools

def main():
  N, M, Q = map(int, input().split())
  all_num = [i for i in range(1, M+1)]
  q_kumi = []
  for i in range(Q):
    q_kumi.append(list(map(int, input().split())))

  ans = 0
  for com in list(itertools.combinations_with_replacement(all_num, N)):
    tmp_ans = 0
    if com != tuple(sorted(com)):
      continue
    for q in q_kumi:
      if com[q[1]-1] - com[q[0]-1] == q[2]:
        tmp_ans += q[3]
    ans = max(ans, tmp_ans)
  print(ans)

if __name__ == '__main__':
  main()

Fixed code:
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

import itertools

def main():
  N, M, Q = map(int, input().split())
  all_num = [i for i in range(1, M+1)]
  q_kumi = []
  for i in range(Q):
    q_kumi.append(list(map(int, input().split())))

  ans = 0
  for com in list(itertools.combinations_with_replacement(all_num, N)):
    tmp_ans = 0
    if com != tuple(sorted(com)):
      continue
    for q in q_kumi:
      if com[q[1]-1] - com[q[0]-1] == q[2]:
        tmp_ans += q[3]
    ans = max(ans, tmp_ans)
  print(ans)

if __name__ == '__main__':
  main()