Problem p02695 (python)

Fixed submission:
n, m, q = map(int, input().split())
x = []
for i in range(q):
    a, b, c, d = map(int, input().split())
    x.append([a, b, c, d])
ans = 0

if n == 2:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            l = [i1, i2]
            v = 0
            for j in x:
                if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                    v += j[3]
            ans = max(ans, v)
elif n == 3:
    for i1 in range(1, m + 1):
        for i2 in range(i1, m + 1):
            for i3 in range(i2, m + 1):
                l = [i1, i2, i3]
                v = 0
                for j in x:
                    if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                        v += j[3]
                ans = max(ans, v)
elif n == 4:
    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):
                    l = [i1, i2, i3, i4]
                    v = 0
                    for j in x:
                        if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                            v += j[3]
                    ans = max(ans, v)
elif n == 5:
    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):
                        l = [i1, i2, i3, i4, i5]
                        v = 0
                        for j in x:
                            if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                v += j[3]
                        ans = max(ans, v)
elif n == 6:
    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):
                            l = [i1, i2, i3, i4, i5, i6]
                            v = 0
                            for j in x:
                                if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                    v += j[3]
                            ans = max(ans, v)
elif n == 7:
    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):
                                l = [i1, i2, i3, i4, i5, i6, i7]
                                v = 0
                                for j in x:
                                    if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                        v += j[3]
                                ans = max(ans, v)
elif n == 8:
    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):
                                    l = [i1, i2, i3, i4, i5, i6, i7, i8]
                                    v = 0
                                    for j in x:
                                        if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                            v += j[3]
                                    ans = max(ans, v)
elif n == 9:
    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):
                                        l = [i1, i2, i3, i4, i5, i6, i7, i8, i9]
                                        v = 0
                                        for j in x:
                                            if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                                v += j[3]
                                        ans = max(ans, v)
else:
    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):
                                            l = [i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
                                            v = 0
                                            for j in x:
                                                if l[j[1] - 1] - l[j[0] - 1] == j[2]:
                                                    v += j[3]
                                            ans = max(ans, v)

print(ans)

Buggy submission (verdict: Runtime Error):
# 重複組み合わせの全列挙

import itertools

# n+m-1個からn個を選ぶ組み合わせ
n, m, q = map(int, input().split())
seq = list(range(n+m-1))
p = list(itertools.combinations(seq, n))

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

res = 0
for i in range(p):
    t = [0] * n
    pp = sorted(i)
    idx = 0
    for e in pp:
        t[idx] = 1 + e - idx
        idx += 1
    
    tmp = 0
    for j in range(q):
        if t[b[j]-1] - t[a[j]-1] == c[j]:
            tmp += d[j]
    
    res = max(res, tmp)

print(res)