Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for bit in product([1, 0], repeat=q):
    cnt = 0
    aaa = []
    aaaa = set()
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            aaa.append([a, b, c])
            cnt += d
    if ans < cnt:
        for a, b, c in aaa:
            for aa, bb, cc in aaa:
                if aa == a and b < bb:
                    aaaa.add([b, bb, cc - c])
                elif aa == a and b > bb:
                    aaaa.add([bb, b, c - cc])
                if b == bb and aa > a:
                    aaaa.add([a, aa, cc - c])
                elif b == bb and aa < a:
                    aaaa.add([aa, a, c - cc])
        aaa += list(aaaa)
        flag = True
        for i, (a, b, c, d) in enumerate(abcd):
            if bit[i] == 1:
                for aa, bb, cc in aaa:
                    if (aa <= a < b <= bb and c > cc) or (a <= aa < bb <= b and c < cc):
                        if aa == a and bb == b and c == cc:
                            continue
                        else:
                            flag = False
                            break
                if not flag:
                    cnt = 0
                    break
        if ans < cnt:
            ans = cnt
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import combinations_with_replacement

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(range(m), n):
    cnt = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for bit in product([1, 0], repeat=q):
    cnt = 0
    aaa = []
    aaaa = set()
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            aaa.append([a, b, c])
            cnt += d
    if ans < cnt:
        for a, b, c in aaa:
            for aa, bb, cc in aaa[]:
                if aa == a and b < bb:
                    aaaa.add([b, bb, cc - c])
                elif aa == a and b > bb:
                    aaaa.add([bb, b, c - cc])
                if b == bb and aa > a:
                    aaaa.add([a, aa, cc - c])
                elif b == bb and aa < a:
                    aaaa.add([aa, a, c - cc])
        aaa += list(aaaa)
        flag = True
        for i, (a, b, c, d) in enumerate(abcd):
            if bit[i] == 1:
                for aa, bb, cc in aaa:
                    if (aa <= a < b <= bb and c > cc) or (a <= aa < bb <= b and c < cc):
                        if aa == a and bb == b and c == cc:
                            continue
                        else:
                            flag = False
                            break
                if not flag:
                    cnt = 0
                    break
        if ans < cnt:
            ans = cnt
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import combinations_with_replacement

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(range(m), n):
    cnt = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for bit in product([1, 0], repeat=q):
    cnt = 0
    aaa = []
    aaaa = []
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            aaa.append([a, b, c])
            cnt += d
    if ans < cnt:
        for a, b, c in aaa[::-1]:
            for aa, bb, cc in aaa:
                if aa == a and b < bb:
                    aaaa.append([b, bb, cc - c])
                elif aa == a and b > bb:
                    aaaa.append([bb, b, c - cc])
                if b == bb and aa > a:
                    aaaa.append([a, aa, cc - c])
                elif b == bb and aa < a:
                    aaaa.append([aa, a, c - cc])
        aaa += aaaa
        flag = True
        for i, (a, b, c, d) in enumerate(abcd):
            if bit[i] == 1:
                for aa, bb, cc in aaa:
                    if (aa <= a < b <= bb and c > cc) or (a <= aa < bb <= b and c < cc):
                        if aa == a and bb == b and c == cc:
                            continue
                        else:
                            flag = False
                            break
                if not flag:
                    cnt = 0
                    break
        if ans < cnt:
            ans = cnt
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import combinations_with_replacement

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(range(m), n):
    cnt = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for bit in product([0, 1], repeat=q):
    cnt = 0
    aaa = []
    aaaa = []
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            aaa.append([a, b, c])
    for a, b, c in aaa[::-1]:
        for aa, bb, cc in aaa:
            if aa == a and b < bb:
                aaaa.append([b, bb, cc - c])
            elif aa == a and b > bb:
                aaaa.append([bb, b, c - cc])
            if b == bb and aa > a:
                aaaa.append([a, aa, cc - c])
            elif b == bb and aa < a:
                aaaa.append([aa, a, c - cc])
    aaa += aaaa
    flag = True
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            for aa, bb, cc in aaa:
                if (aa <= a < b <= bb and c > cc) or (a <= aa < bb <= b and c < cc):
                    if aa == a and bb == b and c == cc:
                        continue
                    else:
                        flag = False
                        break
            if flag:
                cnt += d
            else:
                break
    if ans < cnt:
        ans = cnt
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import combinations_with_replacement

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(range(m), n):
    cnt = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for bit in product([0, 1], repeat=q):
    cnt = 0
    aaa = []
    aaaa = []
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            aaa.append([a, b, c])
    for a, b, c in aaa[::-1]:
        for aa, bb, cc in aaa:
            if aa == a and b < bb:
                aaaa.append([b, bb, cc - c])
            elif aa == a and b > bb:
                aaaa.append([bb, b, c - cc])
            if b == bb and aa > a:
                aaaa.append([a, aa, cc - c])
            elif b == bb and aa < a:
                aaaa.append([aa, a, c - cc])
    aaa += aaaa
    for i, (a, b, c, d) in enumerate(abcd):
        if bit[i] == 1:
            for aa, bb, cc in aaa:
                if (aa <= a < b <= bb and c > cc) or (a <= aa < bb <= b and c < cc):
                    if aa == a and bb == b and c == cc:
                        continue
                    else:
                        break
            else:
                cnt += d
    if ans < cnt:
        ans = cnt
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import combinations_with_replacement

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(range(m), n):
    cnt = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)