Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
List = [input() for i in range(H)]
queue = [(0,0,1)]
shape = sum(row.count("#") for row in List)
used = {(0,0)}

def check(x,y,c):
    if x > W-1 or y > H-1 or x < 0 or y < 0:
        return
    if List[y][x] == '#':
        return
    if (x,y) in used:
        return
    queue.append((x,y,c))
    used.add((x,y))
    
f = -1
while queue:
    x,y,c = queue.pop(0)
    if x == W-1 and y == H-1:
        f = 1
        break
    check(x+1, y, c+1)
    check(x,y+1,c+1)
    check(x-1,y,c+1)
    check(x,y-1,c+1)
print(f if f == -1 else(H*W - c - shape))
    

Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
c = [list(map(int, input().split())) for i in range(3)]

if 1 == len({r[0] - r[1] for r in c}) == len({r[1] - r[2] for r in c}):
    print("Yes")
else:
    print("No")

Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
c = [list(map(int, input().split())) for i in range(3)]

for i in range(3):
    c[i] = [j - c[i][0] for j in c[i]]
    
if c[0] == c[1] and c[1] == c[2]:
    print("Yes")
else:
    print("No")

    


Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
c = [list(map(int, input().split())) for i in range(3)]

for i in range(3):
    c[i] = [j - min(c[i]) for j in c[i]]
    
if c[0] == c[1] and c[1] == c[2]:
    print("Yes")
else:
    print("No")


Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
c_1 = [int(i) for i in input().split()]
c_2 = [int(i) for i in input().split()]
c_3 = [int(i) for i in input().split()]

c1 = [i - min(c_1) for i in c_1]
c2 = [i - min(c_2) for i in c_2]
c3 = [i - min(c_3) for i in c_3]

if c1 == c2 and c2 == c3:
    print("Yes")
else:
    print("No")

Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
N = int(input())
nlist = [int(i) for i in input().split()]
nlist.sort(reverse = True)
a = 0
b = 0
for j in range(N):
    if j % 2 == 0:
        a += nlist[j]
    else:
        b += nlist[j]

print(a-b)

Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
N = int(input())
nlist = [int(i) for i in input().split()]
a = 0
b = 0
for i in range(N):
    if N % 2 == 1:
        a += nlist[i]
    else:
        b += nlist[i]
print(a-b)


Fixed submission:
N = int(input())
A = int(input())

a = N % 500
if a <= A:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
gcd_memo = {}


def gcd(a, b):
    if b == 0:
        return a
    else:
        r = a % b
        gcd_memo[(a, b)] = gcd(b, r)
        return gcd_memo[(a, b)]


if __name__ == "__main__":
    K = int(input())
    ans = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                tmp = gcd(gcd(a, b), c)
                ans += tmp
    print(ans)


Fixed submission:
gcd_memo = {}


def gcd(a, b):
    if b == 0:
        return a
    elif (a, b) in gcd_memo:
        return gcd_memo[(a, b)]
    else:
        r = a % b
        gcd_memo[(a, b)] = gcd(b, r)
        gcd_memo[(b, a)] = gcd_memo[(a, b)]
        return gcd_memo[(a, b)]


if __name__ == "__main__":
    K = int(input())
    ans = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                tmp = gcd(gcd(a, b), c)
                ans += tmp
    print(ans)
Buggy python submission (verdict: Runtime Error):
a,b = input().split()
if (a%2==0) | (b%2==0):
  print("Even")
else:
  print("Odd")

Fixed submission:
a,b = map(int, input().split())
if (a%2==0) | (b%2==0):
  print("Even")
else:
  print("Odd")
Buggy python submission (verdict: Runtime Error):
N = int(input())
ipts = [input() for i in range(N)]
dic = {}
for i in ipts:
    if(i not in dic):
        dic[i] =  True
print(len(dic))


Fixed submission:
import re
ipt = input()
regText = r"(\d{4})/(\d{2})/(\d{2})"
repl = r"2018/\2/\3"
print(re.sub(regText,repl,ipt))
Buggy python submission (verdict: Runtime Error):
import sys

def all_lighted(input, mod, pattern):
    for i, bulb in enumerate(input):
        if bin(bulb and pattern).count('1') % 2 == pattern[i]:
            next
        else:
            return False
    
    return True


def main():
    num_switch, num_bulb = [int(x) for x in sys.stdin.readline().split()]
    input = [0] * num_bulb

    for bulb in input:
        for x in sys.stdin.readline().split()[1:]:
            bulb += 2**(int(x) - 1)

    mod = [int(x) for x in sys.stdin.readline().split()]

    count = 0

    for i in range(2 ** num_switch):
        if all_lighted(input, mod, i): count += 1

    print(count)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

def all_lighted(input, mod, pattern):
    for i, bulb in enumerate(input):
        if bin(bulb & pattern).count('1') % 2 == mod[i]:
            next
        else:
            return False
    
    return True


def main():
    num_switch, num_bulb = [int(x) for x in sys.stdin.readline().split()]
    input = [0] * num_bulb

    for i in range(num_bulb):
        for x in sys.stdin.readline().split()[1:]:
            input[i] += 2**(int(x) - 1)

    mod = [int(x) for x in sys.stdin.readline().split()]

    count = 0

    for pattern in range(2 ** num_switch):
        if all_lighted(input, mod, pattern): count += 1

    print(count)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
    import sys
     
    def main():
        sinputl = sys.stdin.readline
     
        num_card, num_gate = [int(x) for x in sinputl().split()]
        input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]
     
        under_limit = [x[0] for x in input]
        over_limit = [x[1] for x in input]
     
        print(max(min(over_limit) - max(under_limit) + 1, 0))
     
     
    if __name__ == "__main__":
        main()

Fixed submission:
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    card = [1, num_card]

    for gate in input:
        card[0] = max(card[0], gate[0])
        card[1] = min(card[1], gate[1])

    print(max(card[1] - card[0] + 1, 0))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    min_interval = [1, num_card]

    for gate in input:
        if (min_interval[1] - min_interval[0]) > (gate[1] - gate[0]):
            min_interval = gate

    cards = list(range(min_interval[0], min_interval[1] + 1))

    for gate in input:
        cards = list(filter(lambda x: gate[0] <= x <= gate[1], cards))

    print(len(cards))



if __name__ == "__main__":
    main()


Fixed submission:
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    card = [1, num_card]

    for gate in input:
        card[0] = max(card[0], gate[0])
        card[1] = min(card[1], gate[1])

    print(max(card[1] - card[0] + 1, 0))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    cards = list(range(1, num_card + 1))

    for gate in input:
        cards = list(filter(lambda x: gate[0] <= x <= gate[1], cards))

    print(len(cards))



if __name__ == "__main__":
    main()


Fixed submission:
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    card = [1, num_card]

    for gate in input:
        card[0] = max(card[0], gate[0])
        card[1] = min(card[1], gate[1])

    print(max(card[1] - card[0] + 1, 0))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys

def judge(string):
    mem = ""
    for char in string:
        if mem == char:
            return False
        mem = char

    return True


def main():
    sinputl = sys.stdin.readline

    input = sinputl().strip()
    length = len(input)
    input = int(input, base=2)

    paint = [length]

    for i in range(2 ** length):
        status = input ^ i
        if judge(bin(status)[2:].zfill(length)):
            paint.append(bin(i).count('1'))

        
    print(min(paint))


if __name__ == "__main__":
    main()

Fixed submission:
import sys

def main():
    sinputl = sys.stdin.readline

    input = sinputl().strip()
    length = len(input)
    string1 = "10" * (length // 2) + "1" * (length % 2)
    string2 = "01" * (length // 2) + "0" * (length % 2)
    count1 = 0
    count2 = 0

    for i in range(length):
        if input[i] != string1[i]:
            count1 += 1

    for i in range(length):
        if input[i] != string2[i]:
            count2 += 1

    print(min(count1, count2))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys

def judge(string):
    mem = ""
    for char in string:
        if mem == char:
            return False
        mem = char

    return True


def main():
    sinputl = sys.stdin.readline

    input = sinputl().strip()
    length = len(input)
    input = int(input, base=2)

    paint = [length]

    for i in range(2 ** length):
        status = input ^ i
        print(bin(status)[2:].zfill(length))
        if judge(bin(status)[2:].zfill(length)):
            paint.append(bin(i).count('1'))

        
    print(min(paint))


if __name__ == "__main__":
    main()

Fixed submission:
import sys

def main():
    sinputl = sys.stdin.readline

    input = sinputl().strip()
    length = len(input)
    string1 = "10" * (length // 2) + "1" * (length % 2)
    string2 = "01" * (length // 2) + "0" * (length % 2)
    count1 = 0
    count2 = 0

    for i in range(length):
        if input[i] != string1[i]:
            count1 += 1

    for i in range(length):
        if input[i] != string2[i]:
            count2 += 1

    print(min(count1, count2))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from functools import reduce

MOD = 1_000_000_000 + 7
LETTERS = ["A", "C", "G", "T"]
PROHIBIT = ["AGC", "ACG", "GAC", "AGGC", "AGTC", "ATGC"]

def main():
    n = int(input().strip())

    table = {}

    for first in LETTERS:
        for second in LETTERS:
            for third in LETTERS:
                codon = first+second+third
                if not codon in PROHIBIT:
                    table[codon] = [1]

    for i in range(n-3):
        for codon in table.keys():
            table[codon].append(0)

        for codon in table.keys():
            for append in LETTERS:
                three_letters = codon[1:] + append
                four_letters = codon + append

                if (not four_letters in PROHIBIT) and (not three_letters in PROHIBIT):
                    table[three_letters][i+1] = (table[three_letters][i+1] + table[codon][i]) % MOD

    print(reduce(lambda acc, x: (acc + x[-1]) % MOD, table.values(), 0))

if __name__ == "__main__":
    main()


Fixed submission:
from functools import reduce

MOD = 1000000000 + 7
LETTERS = ["A", "C", "G", "T"]
PROHIBIT = ["AGC", "ACG", "GAC", "AGGC", "AGTC", "ATGC"]

def main():
    n = int(input().strip())

    table = {}

    for first in LETTERS:
        for second in LETTERS:
            for third in LETTERS:
                codon = first+second+third
                if not codon in PROHIBIT:
                    table[codon] = [1]

    for i in range(n-3):
        for codon in table.keys():
            table[codon].append(0)

        for codon in table.keys():
            for append in LETTERS:
                three_letters = codon[1:] + append
                four_letters = codon + append

                if (not four_letters in PROHIBIT) and (not three_letters in PROHIBIT):
                    table[three_letters][i+1] = (table[three_letters][i+1] + table[codon][i]) % MOD

    print(reduce(lambda acc, x: (acc + x[-1]) % MOD, table.values(), 0))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import math
A, B, N = map(int, input().split())
x = N // 2
def floor(t):
    return math.floor(t)
li = []
if B > N:
    for i in range(1, N + 1):
        li.append(floor((A*i) / B) - A * floor(i/B))
else:
     for i in range(1, B + 1):
        li.append(floor((A*i) / B) - A * floor(i/B))
print(max(li))

Fixed submission:
import math
A, B, N = map(int, input().split())
x = N // 2
def floor(t):
    return math.floor(t)
li = []
if B < N:
    print(floor((A*(B - 1)) / B) - A * floor((B - 1)/B))
elif B == N:
    print(floor((A*(N - 1)) / B) - A * floor((N - 1)/B))
else:
    print(floor((A*(N)) / B) - A * floor((N)/B))

Buggy python submission (verdict: Time Limit Exceeded):
import math
A, B, N = map(int, input().split())
x = N // 2
def floor(t):
    return math.floor(t)
li = []
if B > N:
    for i in range(1, N + 1):
        li.append(floor((A*i) / B) - A * floor(i/B))
else:
     for i in range(1, B + 1):
        li.append(floor((A*i) / B) - A * floor(i/B))
print(max(li))

Fixed submission:
import math
A, B, N = map(int, input().split())
x = N // 2
def floor(t):
    return math.floor(t)
li = []
if B < N:
    print(floor((A*(B - 1)) / B) - A * floor((B - 1)/B))
elif B == N:
    print(floor((A*(N - 1)) / B) - A * floor((N - 1)/B))
else:
    print(floor((A*(N)) / B) - A * floor((N)/B))
Buggy python submission (verdict: Runtime Error):
H, N = map(int, input().split())
A = list(map(int, input().split()))
atk = 0
for i in A:
    atk += i
if H > atk:
    print("No")
else:
    print("Yes")

Fixed submission:
H, A = map(int, input().split())
count=0
while(H > 0):
    H -= A
    count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
210 5
31 41 59 26 53

Fixed submission:
H, N = map(int, input().split())
A = list(map(int, input().split()))
atk = 0
for i in A:
    atk += i
if H > atk:
    print("No")
else:
    print("Yes")
Buggy python submission (verdict: Runtime Error):
a1, b1 = map(int, input().split())
a2 = str(a1) * b1
b2 = str(b1) * a1
if int(a2[1]) > int(b2[1]):
    print(b2)
elif int(a2[1]) < int(b2[1]):
    print(a2)
else:
    print(a2)

Fixed submission:
a1, b1 = map(int, input().split())
a2 = str(a1) * b1
b2 = str(b1) * a1
if int(a2[0]) > int(b2[0]):
    print(b2)
elif int(a2[0]) < int(b2[0]):
    print(a2)
else:
    print(a2)
Buggy python submission (verdict: Runtime Error):
strlist = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y"]
print(strlist[strlist.index(input()) + 1])

Fixed submission:
strlist = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
print(strlist[strlist.index(input()) + 1])
Buggy python submission (verdict: Runtime Error):
K, X = map(int,input().split())
if 500 * K => X:
    print("Yes")
else:
    print("No")

Fixed submission:
K, X = map(int,input().split())
if 500 * K >= X:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
weather1 = input().split()
weather2 = input().split()
match = 0
for i in range(0,2):
    if weather1[i] == weather2[i]:
        match += 1
print(match)

Fixed submission:
weather1 = input()
weather2 = input()
match = 0
for i in range(0,3):
    if weather1[i] == weather2[i]:
        match += 1
print(match)

Buggy python submission (verdict: Runtime Error):
weather1 = input().split()
weather2 = input().split()
match = 0
for i in range(0,2):
    if weather1[i] == weather2[i]:
        match += 1
print(count)

Fixed submission:
weather1 = input()
weather2 = input()
match = 0
for i in range(0,3):
    if weather1[i] == weather2[i]:
        match += 1
print(match)

Buggy python submission (verdict: Runtime Error):
weather1 = input().split()
weather2 = input().split()
match = 0
for i in range(0,2):
    if weather1[i] == weather2[i]:
        count += 1
print(count)

Fixed submission:
weather1 = input()
weather2 = input()
match = 0
for i in range(0,3):
    if weather1[i] == weather2[i]:
        match += 1
print(match)
Buggy python submission (verdict: Runtime Error):
num = int(input())
num2 = 0
for i in range(1, len(str(num))):
    num2 += int(str(num)[i])
if num % num2 == 0:
    print("Yes")
else:
    print("No")

Fixed submission:
num = input()
num2 = 0
for i in range(0, len(num)):
    num2 += int(num[i])
if int(num) % num2 == 0:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
power = 1
for i in range(N):
    power = power * (i + 1)
print(power % ((10**9) + 7))

Fixed submission:
from math import factorial
N = int(input())
power = factorial(N)
print(power % ((10**9) + 7))
Buggy python submission (verdict: Runtime Error):
a, b, c = map(int, input().split())
if a+b+c = 19:
    print("Yes")
else:
    print("No")

Fixed submission:
a, b, c = map(int, input().split())
if a+b+c == 17:
    print("YES")
else:
    print("NO")
Buggy python submission (verdict: Runtime Error):
a, b, n = map(int, input().split())

if b-1 > n:
    x = n
else:
    x = (n // (b-1)) * (b-1)
answer = max(0, (a*x)//b - a*(x//b))

print(answer)


Fixed submission:
a, b, n = map(int, input().split())

if b > n:
    x = n
else:
    x = n // b * b - 1
answer = max(0, (a*x)//b - a*(x//b))

print(answer)

Buggy python submission (verdict: Runtime Error):
a, b, n = map(int, input().split())

answer = 0
if b-1 > n:
    x = n
else:
    x = (n // (b-1)) * (b-1)
answer = (a*x)//b - a*(x//b)

print(answer)


Fixed submission:
a, b, n = map(int, input().split())

if b > n:
    x = n
else:
    x = n // b * b - 1
answer = max(0, (a*x)//b - a*(x//b))

print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
k, n = map(int, input().split())
a = list(map(int, input().split()))

a += [k]
x = [a[i+1]-a[i] for i in range(n)]


answer = sum(x)
for i in range(n):
    _x = x[:i] + x[i+1:]
    answer = min(answer, sum(_x))
print(answer) 

Fixed submission:
k, n = map(int, input().split())
a = list(map(int, input().split()))
a += [k]
x = [a[0]]
for i in range(n):
    _a = a[i+1] - a[i]
    x += [_a]
x[0] = x[0]+x[-1]
x = x[:-1]
x.remove(max(x))
print(sum(x))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
p = list(map(int, input().split()))
for i in range(n):
    for j in range(i):
        if p[j] < p[i]:
            n -= 1
            break
print(n)

Fixed submission:
n = int(input())
p = list(map(int, input().split()))
_min = p[0]
for a in p:
    if a > _min:
        n -= 1
    _min = min(a, _min)
print(n)
Buggy python submission (verdict: Runtime Error):
n, x = map(int, input().split())
l = list(map(int, input().split()))
d = 0
answer = 1
while True:
    d += l[answer-1]
    if d > x:
        break
    answer += 1
print(answer)

Fixed submission:
n, x = map(int, input().split())
l = list(map(int, input().split()))
d = 0
answer = 1
for _ in range(n):
    d += l[answer-1]
    if d > x:
        break
    answer += 1
print(answer)
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
pre = s[0]
max_cnt = 0
stack = []
cnt = 0
s += '2'
if pre == '0':
    k -= 1
    stack = [0, 0]

for a in s:
    if a == pre:
        cnt += 1
    else:
        if a == '1':
            k -= 1
        if k < 0:
            stack.pop(0)
        stack.append(cnt)
        cnt = 1
        pre = a
        max_cnt = max(max_cnt, sum(stack))

print(max_cnt)

Fixed submission:
a, b = map(int, input().split())

print(max(max(a,b)*2-1, a+b))
Buggy python submission (verdict: Runtime Error):
s = input()
c = 0
for i, a in enumerate(s):
    if i % 2 == 0 and a == '0':
        c += 1
    if i % 2 == 1 and a == '1':
        c += 1
print(min(c, len(s)-c))

Fixed submission:
s = input()
c = 0
for i, a in enumerate(s):
    if i % 2 == 0 and a == '0':
        c += 1
    if i % 2 == 1 and a == '1':
        c += 1
print(min(c, len(s)-c))
Buggy python submission (verdict: Runtime Error):
import sys

def main():
    input = sys.stdin.readline
    N, M, Q = map(int, input().split())
    data = range(1, M+1)
    A = []
    for i in range(len(data)):
        for j in range(i, len(data)):
            for k in range(j, len(data)):
                A.append([data[i], data[j], data[k]])
    l = []
    m = 0
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        l.append([a, b, c, d])
    for a_list in A:
        tmp = 0
        for i in l:
            if a_list[i[1] -1] - a_list[i[0] - 1] == i[2]:
                tmp += i[3]
        if tmp > m:
            m = tmp
    return m


if __name__ == '__main__':
    print(main())

Fixed submission:
import sys
import itertools

def main():
    input = sys.stdin.readline
    N, M, Q = map(int, input().split())
    data = range(1, M+1)
    A = list(itertools.combinations_with_replacement(range(1, M+1), N))
    l = []
    m = 0
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        l.append([a, b, c, d])
    for a_list in A:
        tmp = 0
        for i in l:
            if a_list[i[1] -1] - a_list[i[0] - 1] == i[2]:
                tmp += i[3]
        if tmp > m:
            m = tmp
    return m


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools

def main():
    input = sys.stdin.readline
    N, M, Q = map(int, input().split())
    data = range(1, M+1)
    A = []
    for i in range(len(data)):
        for j in range(i, len(data)):
            for k in range(j, len(data)):
                A.append([data[i], data[j], data[k]])
    l = []
    m = 0
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        l.append([a, b, c, d])
    for a_list in A:
        tmp = 0
        for i in l:
            if a_list[i[1] -1] - a_list[i[0] - 1] == i[2]:
                tmp += i[3]
        if tmp > m:
            m = tmp
    return m


if __name__ == '__main__':
    print(main())



Fixed submission:
import sys
import itertools

def main():
    input = sys.stdin.readline
    N, M, Q = map(int, input().split())
    data = range(1, M+1)
    A = list(itertools.combinations_with_replacement(range(1, M+1), N))
    l = []
    m = 0
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        l.append([a, b, c, d])
    for a_list in A:
        tmp = 0
        for i in l:
            if a_list[i[1] -1] - a_list[i[0] - 1] == i[2]:
                tmp += i[3]
        if tmp > m:
            m = tmp
    return m


if __name__ == '__main__':
    print(main())