qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.buffer.readline

n, m, q = [int(i) for i in input().strip().split()]
abcd = []
canditates = []
ans = 0


for _ in range(q):
    abcd.append([int(i) for i in input().strip().split()])


def helper(_min, _max, cur, res):
    if res == 0:
        canditates.append(list(cur))
    else:
        for i in range(_min, _max):
            cur.append(i)
            helper(i, _max, cur, res - 1)
            cur.pop()


helper(1, m + 1, [], m)

for A in canditates:
    _temp = 0
    for a, b, c, d in abcd:

        if 1 <= a and a < n and 1 <= b and b < n and A[b - 1] - A[a - 1] == c:
            _temp += d
    ans = max(ans, _temp)

print(ans)


Fixed code:
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.buffer.readline

n, m, q = [int(i) for i in input().strip().split()]
abcd = []
canditates = []
ans = 0


for _ in range(q):
    abcd.append([int(i) for i in input().strip().split()])


def helper(_min, _max, cur, res):
    if res == 0:
        canditates.append(list(cur))
    else:
        for i in range(_min, _max):
            cur.append(i)
            helper(i, _max, cur, res - 1)
            cur.pop()


helper(1, m + 1, [], n)

for A in canditates:
    _temp = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            _temp += d

    ans = max(ans, _temp)

print(ans)
Buggy code:
def combinations_with_replacement(elements, N):
    for element in elements:
        if N == 1:
            yield [element]
        else:
            for combination in combinations_with_replacement(elements, N-1):
                yield [element] + combination


N, M, Q = map(int, input().split())
a = [None for _ in range(Q)]
b = [None for _ in range(Q)]
c = [None for _ in range(Q)]
d = [None for _ in range(Q)]
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

ans = 0
elements = list(range(1, M+1))
for combination in combinations_with_replacement(elements, N):
    score = 0
    for i in range(Q):
        if combination[b[i]-1] - combination[a[i]-1] == c[i]:
            score += d[i]
    ans = max(score, ans)
print(ans)


Fixed code:
def combinations_with_replacement(elements, N):
    for i, element in enumerate(elements):
        if N == 1:
            yield [element]
        else:
            for combination in combinations_with_replacement(elements[:i+1], N-1):
                combination.append(element)
                yield combination


N, M, Q = map(int, input().split())
a = [None for _ in range(Q)]
b = [None for _ in range(Q)]
c = [None for _ in range(Q)]
d = [None for _ in range(Q)]
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

ans = 0
elements = list(range(1, M+1))
for combination in combinations_with_replacement(elements, N):
    score = 0
    for i in range(Q):
        if combination[b[i]-1] - combination[a[i]-1] == c[i]:
            score += d[i]
    ans = max(score, ans)
print(ans)
Buggy code:
mport itertools
 
N, M, Q = map(int, input().split())
c = [list(map(int, input().split())) for _ in range(Q)]
 
def score(seq):
  s = 0
  for q in range(Q):
    if seq[c[q][1]-1] - seq[c[q][0]-1] == c[q][2]:
      s += c[q][3]
  return s
 
p = [i for i in range(1, M+1)]
A = itertools.combinations_with_replacement(p, N)
ans = 0
for a in A:
  ans = max(ans, score(a))
print(ans)

Fixed code:
import itertools
 
N, M, Q = map(int, input().split())
cmb = [list(map(int, input().split())) for _ in range(Q)]
 
def score(seq):
  s = 0
  for q in range(Q):
    if seq[cmb[q][1]-1] - seq[cmb[q][0]-1] == cmb[q][2]:
      s += cmb[q][3]
  return s
 
p = [i for i in range(1, M+1)]
A = itertools.combinations_with_replacement(p, N)
ans = 0
for a in A:
  ans = max(ans, score(a))
print(ans)
Buggy code:
import itertools

N, M, Q = map(int, input().split())
ABCD = []
for _ in range(Q):
    ABCD.append(list(map(int, input().split())))
M_list = [i+1 for i in range(M)]
N_list = itertools.combinations(M_list, N)
max_point = 0
for n in N_list:
    point = 0
    for abcd in ABCD:
        if n[abcd[1]-1] - n[abcd[0]-1] == abcd[2]:
            point += abcd[3]
    if max_point < point:
        max_point = point
print(max_point)

Fixed code:
import itertools

N, M, Q = map(int, input().split())
ABCD = []
for _ in range(Q):
    ABCD.append(list(map(int, input().split())))
M_list = [i+1 for i in range(M)]
N_list = itertools.combinations_with_replacement(M_list, N)
max_point = 0
for n in N_list:
    point = 0
    for abcd in ABCD:
        if n[abcd[1]-1] - n[abcd[0]-1] == abcd[2]:
            point += abcd[3]
    if max_point < point:
        max_point = point
print(max_point)
Buggy code:
import itertools
N, M, Q = map(int, input().split())
a = [None for _ in range(Q)]
b = [None for _ in range(Q)]
c = [None for _ in range(Q)]
d = [None for _ in range(Q)]
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

ans = 0
elements = list(range(1, M+1))
for combination in itertools.combinations_with_replacement(elements, N):
    score = 0
    for i in range(Q):
        print(combination, b[i], a[i])
        if combination[b[i]-1] - combination[a[i]-1] == c[i]:
            score += d[i]
    ans = max(score, ans)
print(ans)


Fixed code:
import itertools
N, M, Q = map(int, input().split())
a = [None for _ in range(Q)]
b = [None for _ in range(Q)]
c = [None for _ in range(Q)]
d = [None for _ in range(Q)]
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

ans = 0
elements = list(range(1, M+1))
for combination in itertools.combinations_with_replacement(elements, N):
    score = 0
    for i in range(Q):
        if combination[b[i]-1] - combination[a[i]-1] == c[i]:
            score += d[i]
    ans = max(score, ans)
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(q):
    a[i],b[i],c[i],d[i] = map(int, input().split())
    a[i] -=1
    b[i] -=1

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

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

print(dfs([]))  

Fixed code:
n, m, q = list(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

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

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

print(dfs([]))
Buggy code:
import itertools
n,m,q = map(int, input().split())
candidates = [i for i in range(1,m+1)]
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())

max_score = 0
for A in itertools.combinations(candidates, n):
  A = list(A)
  score = 0
  for i in range(q):
    if A[b[i]-1] - A[a[i]-1] == c[i]:
      score += d[i]
  max_score = max(max_score, score)
print(max_score)

Fixed code:
import itertools
n,m,q = map(int, input().split())
candidates = [i for i in range(1,m+1)]
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())

max_score = 0
for A in itertools.combinations_with_replacement(candidates, n):
  A = list(A)
  score = 0
  for i in range(q):
    if A[b[i]-1] - A[a[i]-1] == c[i]:
      score += d[i]
  max_score = max(max_score, score)
print(max_score)
Buggy code:
from itertools import combinations_with_replacement

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_list=[]  
from copy import deepcopy
def dfs(x,M,N,ans):
  if len(ans)<=N:
    ans.append(x)
    for i in range(x,M+1):
      tmp=deepcopy(ans)
      dfs(i,M,N,tmp)
  else:    
    if ans not in ans_list:
      ans_list.append(ans)
      
q=dfs(1,M,N,[])

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

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())

ans_list=[]  
from copy import deepcopy
def dfs(x,M,N,ans):
  if len(ans)<=N:
    ans.append(x)
    if len(ans)==N:
      ans_list.append(ans)
    else:
      for i in range(x,M+1):
        tmp=deepcopy(ans)
        dfs(i,M,N,tmp)   
      
q=dfs(1,M,N,[])

f=0
for A in ans_list:
  cnt=0
  for i in range(Q):
    if A[b[i]-1]-A[a[i]-1]==c[i]:
      cnt += d[i]
  f=max(f,cnt)
  
print(f)
Buggy code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
import itertools

N, M, Q = MAP()
a, b, c, d = [], [], [], []
for _ in range(Q):
    ai, bi, ci, di = MAP()
    a.append(ai-1); b.append(bi-1); c.append(ci); d.append(di)


def solve(lst):
    ans = 0
    for i in range(Q):
        if lst[b[i]] - lst[a[i]] == c[i]:
            ans += d[i]
    return ans

ans = 0
for A in itertools.combinations(range(1, 11), N):
    ans = max(ans, solve(A))

print(ans)

Fixed code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
import itertools

N, M, Q = MAP()
a, b, c, d = [], [], [], []
for _ in range(Q):
    ai, bi, ci, di = MAP()
    a.append(ai-1); b.append(bi-1); c.append(ci); d.append(di)


def solve(lst):
    ans = 0
    for i in range(Q):
        if lst[b[i]] - lst[a[i]] == c[i]:
            ans += d[i]
    return ans

ans = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
    ans = max(ans, solve(A))

print(ans)
Buggy code:
import itertools
n, m, q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
Max = 0
for A in itertools.combinations_with_replacement(range(1, m + 1) n):
    #score = sum([d for a, b, c, d in abcd if C[b - 1] - C[a - 1] == c])
    score = 0
    for ai, bi, ci, di in abcd:
        if A[bi - 1] - A[ai - 1] == ci:
            score += di
    Max = max(Max, score)
    #print(C)
print(Max)

Fixed code:
import itertools
n, m, q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
Max = 0
for A in itertools.combinations_with_replacement(range(1, m + 1), n):
    #score = sum([d for a, b, c, d in abcd if C[b - 1] - C[a - 1] == c])
    score = 0
    for ai, bi, ci, di in abcd:
        if A[bi - 1] - A[ai - 1] == ci:
            score += di
    Max = max(Max, score)
    #print(C)
print(Max)
Buggy code:
import itertools

abcd = []
N,M,Q = map(int,input().split())
for i in range(Q):
    a,b,c,d = map(int,input().split())
    abcd.append([a,b,c,d])
  
A = (list(itertools.product(list(range(1,M+1)),repeat=N)))

ans = 0
for i in A: )
    _sum = 0
    for j in abcd: 
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            _sum += j[3]
    if ans < _sum:
        ans = _sum
    
print(ans)        
            

Fixed code:
import itertools

abcd = []
N,M,Q = map(int,input().split())
for i in range(Q):
    a,b,c,d = map(int,input().split())
    abcd.append([a,b,c,d])
  
A = itertools.combinations_with_replacement(list(range(1,M+1)),N)
ans = 0
for i in A: 
    _sum = 0
    for j in abcd: 
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            _sum += j[3]
    if ans < _sum:
        ans = _sum
    
print(ans)
Buggy code:
import itertools

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

ans = 0

nums = range(1,M+1)
arrs = itertools.combinations(nums, N)

for a in arrs:
  _sum = 0
  a = list(a)
  for b in arr:
    if a[b[1]-1]-a[b[0]-1]==b[2]:
      _sum += b[3]
  ans = max(ans,_sum)
  
print(ans)

Fixed code:
import itertools

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

ans = 0

nums = range(1,M+1)
arrs = itertools.combinations_with_replacement(nums, N)

for a in arrs:
  _sum = 0
  a = list(a)
  for b in arr:
    if a[b[1]-1]-a[b[0]-1]==b[2]:
      _sum += b[3]
  ans = max(ans,_sum)
  
  
print(ans)
Buggy code:
a = input().split()
N = int(a[0])
M = int(a[1])
Q = int(a[2])
current = []
for i in range(Q):
    e = input().split()
    f = [int(i) for i in e]
    current.append(f)
import itertools

c = list(itertools.combinations_with_replacement(range(1, M + 1), N))
value_list = []
for item in c:
    value = 0
    for k in current :
        if item[k[1]]-item[k[0]]==k[2]:
            value+=k[3]
    value_list.append(value)
    
print(max(value_list))

Fixed code:
a = input().split()
N = int(a[0])
M = int(a[1])
Q = int(a[2])
current = []
for i in range(Q):
    e = input().split()
    f = [int(i) for i in e]
    current.append(f)
import itertools

c = list(itertools.combinations_with_replacement(range(1, M + 1), N))
value_list = []
for item in c:
    value = 0
    for k in current :
        if item[k[1]-1]-item[k[0]-1]==k[2]:
            value+=k[3]
    value_list.append(value)

print(max(value_list))
Buggy code:
N, M, Q = map(int,input().split())
L = []
def dfs(List):
    if len(List) == N - 1:
        L.append([0] + List)
    else:
        k = List[-1]
        for i in range(M-k):
            n_L = List[:]
            n_L.append(k+i)
            dfs(n_L)
for i in range(M):
    dfs([i])

MAX = 0
for l in L:
    cnt = 0
    for _ in range(Q):
        a, b, c, d = map(int,input().split())
        if l[b-1] - l[a-1] == c:
            cnt += d
    MAX = max(cnt, MAX)
print(MAX)

Fixed code:
N, M, Q = map(int,input().split())
L = []
def dfs(List):
    if len(List) == N - 1:
        L.append([0] + List)
    else:
        k = List[-1]
        for i in range(M-k):
            n_L = List[:]
            n_L.append(k+i)
            dfs(n_L)
for i in range(M):
    dfs([i])

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

MAX = 0
for l in L:
    cnt = 0
    for a, b, c, d in Qs:
        if l[b-1] - l[a-1] == c:
            cnt += d
    MAX = max(cnt, MAX)
print(MAX)
Buggy code:
from itertools import combinations

n, m, q = map(int, input().split())
comb = list(combinations(range(1, m+1), n))
ans = [0] * len(comb)
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(len(comb)):
        if comb[i][b-1] - comb[i][a-1] == c:
            ans[i] += d
print(max(ans))

Fixed code:
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
comb = list(combinations_with_replacement(range(1, m+1), n))
ans = [0] * len(comb)
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(len(comb)):
        if comb[i][b-1] - comb[i][a-1] == c:
            ans[i] += d
print(max(ans))
Buggy code:
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
A_itr = itertools.combinations(range(1, M+1), N)
max_score = 0
for A in A_itr:
    score = 0
    for j_abcd in abcd:
        if A[j_abcd[1]-1]-A[j_abcd[0]-1] == j_abcd[2]:
            score += j_abcd[3]
    max_score = max(max_score, score)
print(max_score)

Fixed code:
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
A_itr = itertools.combinations_with_replacement(range(1, M+1), N)
max_score = 0
for A in A_itr:
    score = 0
    for j_abcd in abcd:
        if A[j_abcd[1]-1]-A[j_abcd[0]-1] == j_abcd[2]:
            score += j_abcd[3]
    max_score = max(max_score, score)
print(max_score)
Buggy code:
import itertools as it

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

ar = [[0]*4]*q

for i in range(q):
    ar[i] = [int(s) for s in input().split()]

A = it.combinations_with_replacement(list(range(1,m+1)),n)
mx = 0

for cnd in A:
    mx_tmp=0
    for i in range(q):
        if(A[ar[i][1]-1] - A[ar[i][0]-1] == ar[i][2]):
            mx_tmp += ar[i][3]
    mx = max(mx,mx_tmp)

print(mx)

Fixed code:
import itertools as it

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

ar = [[0]*4]*q

for i in range(q):
    ar[i] = [int(s) for s in input().split()]

A = list(it.combinations_with_replacement(list(range(1,m+1)),n))
mx = 0

for cnd in A:
    mx_tmp=0
    for i in range(q):
        if(cnd[ar[i][1]-1] - cnd[ar[i][0]-1] == ar[i][2]):
            mx_tmp += ar[i][3]
    mx = max(mx,mx_tmp)

print(mx)
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(Q):
  a[i],b[i],c[i],d[i]=map(int,input().split())
import itertools
for A in itertools.combinations_with_replacement(range(1,M+1),N):
    ans=0
    if i in range(Q):
        s=0
        A[b[i]]-A[a[i]]=c[i]
        s+=d[i]
        ans=max(ans,s)
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())
import itertools
ans=0
for A in itertools.combinations_with_replacement(range(1,M+1),N):
    A=list(A)
    s=0
    
    for i in range(Q):
        if A[b[i]-1]-A[a[i]-1]==c[i]:
           s+=d[i]

           ans=max(ans,s)
print(ans)
Buggy code:
def main():
    from itertools import combinations_with_replacement
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]
    ans = 0
    for A in combinations_with_replacement(range(1, M+1), N):
        A.sort()
        cur = 0
        for a, b, c, d in T:
            if A[b-1] - A[a-1] == c:
                cur += d
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()


Fixed code:
def main():
    from itertools import combinations_with_replacement
    import sys
    input = sys.stdin.buffer.readline
    N, M, Q = (int(i) for i in input().split())
    T = [[int(i) for i in input().split()] for j in range(Q)]
    ans = 0
    for C in combinations_with_replacement(range(1, M+1), N):
        A = sorted(C)
        cur = 0
        for a, b, c, d in T:
            if A[b-1] - A[a-1] == c:
                cur += d
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()
Buggy code:
import itertools
n,m,q=map(int, input().split())
arr=[[0]*4 for i in range(q)]

for i in range(q):
    arr[i][0],arr[i][1],arr[i][2],arr[i][3]=map(int, input().split())

ans=0
for i in itertools.product([k for k in range(1,m+1)],repeat=n):
    score=0
    for j in range(q):
        tmp1=i[arr[j][1]-1]
        tmp0=i[arr[j][0]-1]
        if tmp1-tmp0==arr[j][2]:
            score+=arr[j][3]
    if ans<score:
        ans=score
print(ans)

Fixed code:
import itertools
n,m,q=map(int, input().split())
arr=[[0]*4 for i in range(q)]

for i in range(q):
    arr[i][0],arr[i][1],arr[i][2],arr[i][3]=map(int, input().split())

ans=0
for i in itertools.combinations_with_replacement(range(1,m+1),n):
    score=0
    for j in range(q):
        tmp1=i[arr[j][1]-1]
        tmp0=i[arr[j][0]-1]
        if tmp1-tmp0==arr[j][2]:
            score+=arr[j][3]
    if ans<score:
        ans=score
print(ans)
Buggy code:
import random as rng
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)


def dump_impl(*objects):
    print(*objects, file=sys.stderr)


def dump_dummy(*objects):
    pass


dump = dump_impl if "DEBUG" in sys.argv else dump_dummy

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())

dump(N, M, Q)
dump(a)
dump(b)
dump(c)
dump(d)
ans = 0
for A in it.combinations(range(1, M+1), N):
    A = list(A)
    x = [0]
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            x.append(d[i])
    dump(x, A)
    ans = max(ans, sum(x))
print(ans)


Fixed code:
import random as rng
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)


def dump_impl(*objects):
    print(*objects, file=sys.stderr)


def dump_dummy(*objects):
    pass


dump = dump_impl if "DEBUG" in sys.argv else dump_dummy

N, M, Q = map(int, input().split())
a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q
for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a[i], b[i], c[i], d[i] = ai, bi, ci, di

dump(N, M, Q)
dump(a)
dump(b)
dump(c)
dump(d)

ans = 0


for A in it.combinations_with_replacement(range(1, M+1), N):
    A = list(A)
    x = [0]

    for i in range(Q):
        if A[b[i] - 1] - A[a[i] - 1] == c[i]:
            x.append(d[i])
    ans = max(ans, sum(x))
print(ans)
Buggy code:
import itertools

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

A = [[ int(n) for n in input().split()] for _ in range(Q) ]
n = [i for i in range(1,M+1)]
numbers = list(itertools.combinations_with_replacement(n, N))
ans = [0 for _ in range(len(numbers))]

q = Q
while  q> 0:
    q -= 1
    a, b, c, d = A[q][0], A[q][1], A[q][2], A[q][3]
    print(a,b,c,d)

    for i in range(len(numbers)):
        if numbers[i][b-1] - numbers[i][a-1] == c:
            ans[i] += d


print(max(ans))


Fixed code:
import itertools

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

A = [[ int(n) for n in input().split()] for _ in range(Q) ]
n = [i for i in range(1,M+1)]
numbers = list(itertools.combinations_with_replacement(n, N))
ans = [0 for _ in range(len(numbers))]

q = Q
while  q> 0:
    q -= 1
    a, b, c, d = A[q][0], A[q][1], A[q][2], A[q][3]

    for i in range(len(numbers)):
        if numbers[i][b-1] - numbers[i][a-1] == c:
            ans[i] += d


print(max(ans))
Buggy code:
import itertools
N, M, Q = map(int, input().split())
m = list()
res = -100
for i in range(Q):
    n = list(map(int, input().split()))
    m.append(n)
l = [x for x in range(1, M + 1)]
p = itertools.permutations(l, N)
for p in p:
    total = 0
    for i in range(Q):
        a = m[i][0] - 1
        b = m[i][1] - 1
        A = p[a]
        B = p[b]
        C = B - A
        if m[i][2] == C:
            total = total + m[i][3]
    if res < total:
        res = total
print(res)

Fixed code:
import itertools
N, M, Q = map(int, input().split())
m = list()
res = -100
for i in range(Q):
    n = list(map(int, input().split()))
    m.append(n)
l = [x for x in range(1, M + 1)]
p = itertools.combinations_with_replacement(l, N)
for p in p:
    total = 0
    for i in range(Q):
        a = m[i][0]
        b = m[i][1]
        A = p[a - 1]
        B = p[b - 1]
        C = B - A
        if m[i][2] == C:
            total = total + m[i][3]
    if res < total:
        res = total
print(res)
Buggy code:
import itertools
 
def check(lis, a):
    return sum((i[3] for i in a if lis[i[1]-1]-lis[i[0]-1]==i[2]))
   
n, m, q = map(int, input().split())
a = ((int(i) for i in input().split(" ")) for j in range(q))
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
    ans = max(ans, check(i, a))
print(ans)

Fixed code:
import itertools

def check(lis, a):
    return sum([i[3] for i in a if lis[i[1]-1]-lis[i[0]-1]==i[2]])
   
n, m, q = map(int, input().split())
a = [[int(i) for i in input().split(" ")] for j in range(q)]
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
    ans = max(ans, check(i, a))
print(ans)
Buggy code:
from itertools import combinations
N, M, Q = map(int, input().split())
chk = []
for _ in range(Q):
    *abcd, = map(int, input().split())
    chk.append(abcd)

li = [list(x) for x in list(combinations(range(1, M+1), N))]
ans = 0
for item in li:
    sub = 0
    for c in chk:
        if item[c[1]-1] - item[c[0]-1] == c[2]:
            sub += c[3]
    ans = max(ans, sub)
print(ans)

Fixed code:
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
chk = []
for _ in range(Q):
    *abcd, = map(int, input().split())
    chk.append(abcd)

li = [list(x) for x in list(combinations_with_replacement(range(1, M+1), N))]
ans = 0
for item in li:
    sub = 0
    for c in chk:
        if item[c[1]-1] - item[c[0]-1] == c[2]:
            sub += c[3]
    ans = max(ans, sub)
print(ans)