Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03721 (python)

Fixed submission:
# https://atcoder.jp/contests/abc061/tasks/abc061_c
import sys

def main():
    n, k = map(int, input().split())
    ans = [0 for _ in range(10**5+1)]
    for x in sys.stdin.readlines():
        a, b = map(int, x.split())
        ans[a] += b

    cnt = 0
    for i in range(10**5+1):
        cnt += ans[i]
        if cnt >= k:
            print(i)
            exit()

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n, k = map(int, input().split())
    ans = [0 for i in range(n+1)]
    for x in sys.stdin.readlines():
        a, b = map(int, x.split())
        ans[a] += b

    cnt = 0
    for i in range(n+1):
        cnt += ans[i]
        if cnt >= k:
            print(ans[i])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n, k = map(int, input().split())
    ans = [0 for i in range(n+1)]
    for x in sys.stdin.readlines():
        a, b = map(int, x.split())
        ans[a] += b

    cnt = 0
    for i in range(n+1):
        cnt += ans[i]
        if cnt >= k:
            print(ans[i])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n, k = map(int, input().split())
    cnt = 0
    for x in sys.stdin.readlines:
        a, b = map(int, x.split())
        if cnt + b < k:
            cnt += b
        else:
            print(a)
            exit()

if __name__ == "__main__":
    main()
Problem p03730 (python)

Fixed submission:
a, b, c = map(int, input().split())
print("YES" if any((a*i%b) == c for i in range(1,b+1)) else "NO")

Buggy submission (verdict: Runtime Error):
import math
a, b, c = map(int, input().split())
if math.gcd(a,b) == c:
  print("YES")
else:
  print("NO")
Problem p03773 (python)

Fixed submission:
a, b = map(int, input().split())
print((a + b) % 24)

Buggy submission (verdict: Runtime Error):
n = int(input())
ans = len(str(n))
for i in range(1, int(n**(1/2))+1):
  if n%i == 0:
    ans = min(ans, max(len(str(i)), len(str(n//i))))
print(ans)
Problem p03777 (python)

Fixed submission:
a, b = input().split()
if a == b :
  print("H")
else:
  print("D")

Buggy submission (verdict: Runtime Error):
a, b = input()
if a == b :
  print("H")
else:
  print("D")
Problem p03817 (python)

Fixed submission:
def main():
    x = int(input())
    ans = x//11
    chk = x%11
    if chk == 0:
        print(2*ans)
    elif chk <=6:
        print(2*ans+1)
    else:
        print(2*ans+2)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    x = int(input())
    s = 0
    f = 0
    while ans < x:
        s += 1
        tmp = 6*s + 5*f
        if tmp >= x:
            print(s+f)
            exit()
        f += 1
        tmp += 5
        if tmp >= x:
            print(s+f)
            exit()

if __name__ == "__main__":
    main()
Problem p03838 (python)

Fixed submission:
def main():
    x, y = map(int, input().split())
    print(min(abs(x+y), abs(~x+y))+1)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    x, y = map(int, input().split())
    print(min(max(y-x, x-y+2), abs(x+y)+1)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    x, y = map(int, input().split())
    print(min(max(x+y, y-x+2), abs(x+y)+1)
if __name__ == '__main__':
    main()
Problem p03994 (python)

Fixed submission:
s = list(input())
k = int(input())
n = len(s)
map = {(chr(ord("a") + i)): (26-i)%26 for i in range(26)}
for i in range(n):
    if k >= map[s[i]]:
        k -= map[s[i]]
        s[i] = "a"
if k > 0:
    s[n-1] = chr((ord(s[n-1])+k%26)%123)
print("".join(s))

Buggy submission (verdict: Runtime Error):
s = list(input())
k = int(input())
n = len(s)
c = ord("z") + 1
i = 0
for i in range(n):
    tmp = c - ord(s[i])
    if k >= tmp:
        s[i] = "a"
        k -= tmp
if k > 0:
    s[n-1] = chr(ord(s[n-1])+k)
print("".join(s))

Buggy submission (verdict: Runtime Error):
s = list(input())
k = int(input())
n = len(s)
c = ord("z") + 1
i = 0
for i in range(n):
    tmp = c - ord(s[i])
    if k > 0 and k >= tmp:
        s[i] = "a"
        k -= tmp
else:
    s[n-1] = chr(ord(s[n-1])+k)
print("".join(s))

Buggy submission (verdict: Runtime Error):
s = list(input())
k = int(input())
n = len(s)
c = ord("z") + 1
i = 0
for i in range(n):
    tmp = c - ord(s[i])
    if k >= tmp:
        s[i] = "a"
        k -= tmp
else:
    s[n-1] = chr(ord(s[n-1])+k)
print("".join(s))

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
n = len(s)
c = ord("z") + 1
i = 0
for i in range(n):
    tmp = c - ord(s[i])
    if k >= tmp:
        s = s[:i] + "a" + s[i+1:]
        k -= tmp
else:
    s = s[:n-1] + chr(ord(s[n-1])+k)
print(s)

Buggy submission (verdict: Runtime Error):
a = input().replace("S", " S").replace("H", " H").replace("D", " D").replace("C", " C").split()
m, s, h, d, c = [], [], [], [], []
ans = []
for i in range(len(a)):
    if a[i][1] in ["2", "3", "4", "5", "6", "7", "8", "9"]:
        m.append(i)
    else:
        if a[i][0] == "S":
            s.append(i)
            if len(s) == 5:
                ans = s
                break
        elif a[i][0] == "H":
            h.append(i)
            if len(h) == 5:
                ans = h
                break
        elif a[i][0] == "D":
            d.append(i)
            if len(d) == 5:
                ans = d
                break
        else:
            c.append(i)
            if len(c) == 5:
                ans = c
                break
ch = 0
d = ""
for i in ans:
    d += "".join(a[ch:i])
    ch = i+1
if d == "":
    print(0)
else:
    print(d)
Problem p04011 (python)

Fixed submission:
n, k, x, y = (int(input()) for i in range(4))
print(min(n, k) * x + max(0, n - k) * y)

Buggy submission (verdict: Runtime Error):
n, k, x, y = (input() for i in range(4))
print(min(n, k) * x + max(0, n - k) * y)
Problem p04012 (python)

Fixed submission:
w = input()
print("Yes" if all([w.count(i)%2 == 0 for i in set(w)]) else "No")

Buggy submission (verdict: Runtime Error):
w = input()
print("Yes" if all[s.count(i)%2 == 0 for i in set(s)] else "No")
Problem p02615 (python)

Fixed submission:
input()
A=list(map(int,input().split()))
A.sort(reverse=True)
B=[]
ans=0
for i in A:
  if len(B)>=1:
    ans+=B[len(B)//2]
  B.append(i)
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
list.sort(A)

l=[]
n=A.pop()
n2=A.pop()
sum=n
comfort={n2:[[n,n2],[n2,n]]}

while A:
  max=sorted(comfort.keys())[-1]
  n=comfort[max].pop()
  sum+=max
  n2=A.pop()
  if comfort.get(min(n[0],n2))==None:comfort[min(n[0],n2)]=[]
  if comfort.get(min(n[1],n2))==None:comfort[min(n[1],n2)]=[]
  comfort[min(n[0],n2)].append([n[0],n2])
  comfort[min(n[1],n2)].append([n[1],n2])
print(sum)
Problem p02616 (python)

Fixed submission:
from collections import deque
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
d=deque(A)
import time
l=[]
if K%2==0 or (K%2==1 and A[0]>=0):
  for i in range(K//2):
    if d[0]*d[1]>d[-1]*d[-2] and (i==K//2-1 or (len(d)>2 and  d[2]>0)):
      l.append(d.popleft())
      l.append(d.popleft())
    else:
      l.append(d.pop())
      l.append(d.pop())
  if K%2==1:
    l.append(d[0])
else:
  for i in range(K):
    l.append(d.popleft())
      
ans=1
for i in l:
  ans*=i
  ans%=10**9+7
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
d=deque(A)


l=[]
if K%2==0 or (K%2==1 and A[0]>0):
  for i in range(K//2):
    if d[0]*d[1]>=d[-1]*d[-2] and d[2]>0:
      l.append(d.popleft())
      l.append(d.popleft())
    else:
      l.append(d.pop())
      l.append(d.pop())
  if K%2==1:
    l.append(d[0])
else:
  for i in range(K):
    l.append(d.popleft())
      
ans=1
for i in l:
  ans*=i
print(ans%(10**9+7))
Problem p02641 (python)

Fixed submission:
import numpy as np
X,N=map(int,input().split())
p=np.array(list(map(int,input().split())))
ans=np.array([X,X])
while True:
  if ans[0] not in p:
    print(ans[0])
    exit()
  if ans[1] not in p:
    print(ans[1])
    exit()
  ans+=[-1,1]

Buggy submission (verdict: Runtime Error):
import numpy as np
X,N=map(int,input().split())
p=np.array(list(map(int,input().split())))
ans=np.array([X,X])
while True:
  for x in ans:
  if x not in p:
    print(x)
    exit()
  ans+=[-1,1]
Problem p02642 (python)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))

A.sort()

dp=[False]*(10**6*2)
ans=0
for idx,i in enumerate(A):
  if dp[i]:continue
  tmp=i
  ans+=1
  while tmp<10**6*2-1:
    dp[tmp]=True
    tmp+=i
  if idx!=len(A)-1 and A[idx+1]==i:
    ans-=1
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))

A.sort()

dp=[False]*(10**6)
ans=0
for idx,i in enumerate(A):
  if dp[i]:continue
  tmp=i
  ans+=1
  while tmp<10**6:
    dp[tmp]=True
    tmp+=i
  if idx!=len(A)-1 and A[idx+1]==i:
    ans-=1
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
A.sort()

dp=[False]*(10**6)
ans=0
for i in A:
  if dp[i]:continue
  tmp=i
  ans+=1
  while tmp>10**6:
    dp[tmp]=True
    tmp+=i
print(ans)
Problem p02658 (python)

Fixed submission:
N=int(input())

X=list(map(int,input().split()))

result=1
for i in X:
  result*=i
  if result>10**18:
    result=-1
    break
for i in X:
  if i==0:result=0

print(result)

Buggy submission (verdict: Runtime Error):
X,Y=input().split()

N=int(input())
 
X=list(map(int,input().split()))
for i in X:
  if i==0:
    print(0)
    exit(0)

result=1
for i in X:
  result*=i
  if result>10**18:
    result=-1
    break
 
print(result)
Problem p02675 (python)

Fixed submission:
n=int(input())

if n%10==3:
  print('bon')
elif n%10==0 or n %10 == 1 or n%10 == 6 or n%10 == 8:
  print('pon')
else:
  print('hon')

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split(' '))

m=[[] for i in range(N)]

for i in range(M):
  x,y=map(int,input().split(' '))
  m[x-1].append(y-1)
  m[y-1].append(x-1)

sign=[0]
signed_map=[-1]*N
signed_map[0]=1
while True:
  signed=sign
  sign=[]
  for s in signed:
    for room in m[s]:#サインされた場所から繋がってるとこを回る
      if signed_map[room]==-1:
        signed_map[room]=s+1
        sign.append(room)
  if sign==[]:break

print('Yes')
for ch in signed_map[1:]:
  print(ch)
Problem p02683 (python)

Fixed submission:
import numpy as np

N,M,X = map(int, input().split(' '))
C=[]
maxNum=999999999
dmaxNum=999999999
for i in range(N):
  C.append(list(map(int, input().split(' '))))
for i in range(2**N):
  ar=[0]*(M+1)
  for j in range(N):
    if (i & 2**j) != 0:
      ar = [x + y for (x, y) in zip(ar, C[j])]
  if (np.array(ar[1:])>=X).all()>0:
    maxNum=min(maxNum,ar[0])
if maxNum==dmaxNum:
  print(-1)
else:
  print(maxNum)

Buggy submission (verdict: Runtime Error):
N,M,X = map(int, input().split(' '))
C=[]
maxNum=999999999
dmaxNum=999999999
for i in range(N):
  C.append(map(int, input().split(' ')))

for i in range(2**(N+1)-1):
  ar=[]
  for i in range(M):
    if (i & 2**i) == 0:
      ar = [x * y for (x, y) in zip(ar, C[i])]
  print(ar)
  for i in ar:
    if i < X:
      break
Problem p02684 (python)

Fixed submission:
N,K = map(int, input().split(' '))

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

if K<len(log):
  print(log[K]+1)
else:
  print(log[(K-loop_start)%loop+loop_start]+1)

Buggy submission (verdict: Runtime Error):
N,K = map(int, input().split(' '))

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  now=A[now]
  if(cache.get(now)):break
  cache[now]=1

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

#print(loop_end-loop_start,loop_start)

if len(log) >loop:
  if loop==0 and loop_start>K:
    print(now)
  else:
    print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)

Buggy submission (verdict: Runtime Error):
N,K = map(int, input().split(' '))

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  now=A[now]
  if(cache.get(now)):break
  cache[now]=1

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

#print(loop_end-loop_start,loop_start)

if len(log) >loop:
  print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)
Problem p02787 (python)

Fixed submission:
H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
dp=[10**8]*(H+1)
dp[0]=0
i=0

for i in range(H+1):
  for A,B in AB:
    if i-A>=0:
      dp[i]=min(dp[i-A]+B,dp[i])
  for A,B in AB:
    if i+A>H:
      dp[-1]=min(dp[-1],dp[i]+B)
print(dp[-1])

Buggy submission (verdict: Runtime Error):
H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
dp=[0]*(10**9)
dp[0]=0
i=0

while True:
  i+=1
  for A,B in AB:
    if i-B>=0:
      dp[i]=max(dp[i-B]+A,dp[i])
  if dp[i]>=H:
    break
print(i)

Buggy submission (verdict: Runtime Error):
H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
INF=10**8
dp=[0]*(10**7)
dp[0]=0
i=0

while True:
  i+=1
  for A,B in AB:
    if i-B>=0:
      dp[i]=max(dp[i-B]+A,dp[i])
  if dp[i]>=H:
    break
print(i)
Problem p02555 (python)

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    n=I()
    dp=[1 for i in range(n)]
    dp[0]=0
    if n==1:
        print(0)
        sys.exit()
    dp[1]=0
    for i in range(3,n):
        for j in range(i-2):
            dp[i]=(dp[i]+dp[j])%mod
    #print(dp)
    print(dp[-1]%mod)
    

        
        
        
        
    
if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    n=I()
    dp=[1 for i in range(n)]
    dp[0]=0
    dp[1]=0
    for i in range(3,n):
        for j in range(i-2):
            dp[i]=(dp[i]+dp[j])%mod
    #print(dp)
    print(dp[-1]%mod)
    

        
        
        
        
    
if __name__=="__main__":
    main()
Problem p02639 (python)

Fixed submission:
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

lis=LI()
for i in range(5):
    if lis[i]==0:
        print(i+1)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()

n, q = map(int, input().split())
y = [[] for _ in range(2*10**5)] # i番目の幼稚園の(-rate, 幼児) #heap
data = [(0, 0)]*n # i番目の(rate, 所属) #list
import heapq
for i in range(n):
    a, b = map(int, input().split())
    data[i] = (a, b - 1)
    heapq.heappush(y[b - 1], (-a, i))
max_rate = [] # (rate, 幼児, 所属) #heap
max_rate_list = [()]*(2*10**5) #list
for i in range(2*10**5):
    if y[i]:
        s, t = heapq.heappop(y[i])
        heapq.heappush(max_rate, (-s, t, i))
        heapq.heappush(y[i], (s, t))
        max_rate_list[i] = (-s, t)
from collections import defaultdict
ng_num = defaultdict(set)
for _ in range(q):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    a, b = data[c]
    data[c] = (a, d)
    heapq.heappush(y[d], (-a, c))
    ng_num[b].add(c)
    if c in ng_num[d]:
        ng_num[d].discard(c)
    #print(ng_num)
    while y[b]:
        s, t = heapq.heappop(y[b])
        if t not in ng_num[b]:
            heapq.heappush(max_rate, (-s, t, b))
            heapq.heappush(y[b], (s, t))
            max_rate_list[b] = (-s, t)
            break
    while y[d]:
        s, t = heapq.heappop(y[d])
        if t not in ng_num[d]:
            heapq.heappush(max_rate, (-s, t, d))
            heapq.heappush(y[d], (s, t))
            max_rate_list[d] = (-s, t)
            break
    #print(max_rate)
    while max_rate:
        s, t, r = heapq.heappop(max_rate)
        if t not in ng_num[r] and (s, t) == max_rate_list[r]:#heapqの弱点(不要になった要素を楽に抜くことができない)をリストでカバーしている
            print(s)
            heapq.heappush(max_rate, (s, t, r))
            break
#popしてその要素を戻すという操作でキューの利便性は上がるということに注意
#どの情報を持てばいいのかをしっかり考えたい
Problem p02642 (python)

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
p = [True] * (10 ** 6 + 1)
s = list(set(a))
for x in s:
    t = x * 2
    while t <= 10 ** 6:
        p[t] = False
        t += x
ans = 0
for x in a:
    if c[x] == 1 and p[x] == True:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
m=2*(10**5)+1
dp=[0 for i in range(m)]
#print(dp)
for i in range(n):
    x=lis[i]
    #print(x)
    for j in range(2,((m-1)//x)+1):
        dp[x*j]=1
        #print(x*j)
#print(dp)
ans=0
c=Counter(lis)
for y in lis:
    if c[y]<=1:
        ans+=1-dp[y]
print(ans)
#print(dp)
#print(dp[2*(10**5)])
Problem p02696 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()

a,b,n=MI()
s=min(n,b-1)
ans=floor(a*s/b)-a*floor(s/b)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()

n,m,q=MI()
strs=[[[] for i in range(m)] for i in range(n)]
for i in range(m):
    strs[0][i].append(str(i))
#print(strs)
for i in range(n-1):
    for j in range(m):
        for k in range(j,m):
            for s in strs[i][k]:
                strs[i+1][j].append(str(j)+s)
#print(strs)

L=strs[n-1]
#print(L)
lis=[]
for i in range(m):
    for s in L[i]:
        lis.append(s)
#print(lis)
mx=0
zyou=[LI() for i in range(q)]
for u in lis:
    po=0
    for k in range(q):
        a,b,c,d=zyou[k]
        if int(u[b-1])-int(u[a-1])==c:
            po+=d
    if po>mx:
        mx=po
print(mx)