Problem: Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).  Consider a sequence A satisfying the following conditions:    * A is a sequence of N positive integers.   * 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.  Let us define a score of this sequence as follows:    * The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)  Find the maximum possible score of A.

Submission attempts (in order):
N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
print(N, M, Q)
# print(abcd)

def calculate(A):
    total = 0
    for abcdi in abcd:
        a = abcdi[0] - 1
        b = abcdi[1] - 1
        c = abcdi[2]
        d = abcdi[3]
        if A[b] - A[a] == c:
            total += d
    

    return total

maximum = 0
for i in range(1, M + 1):
    if N == 1:
        maximum = max(calculate([i]), maximum)
    for j in range(i, M + 1):
        if N == 2:
            maximum = max(calculate([i, j]), maximum)
        for k in range(j, M + 1):
            if N == 3:
                maximum = max(calculate([i, j, k]), maximum)
            for l in range(k, M + 1):
                if N == 4:
                    maximum = max(calculate([i, j, k, l]), maximum)
                for m in range(l, M + 1):
                    if N == 5:
                        maximum = max(calculate([i, j, k, l, m]), maximum)
                    for n in range(m, M + 1):
                        if N == 6:
                            maximum = max(calculate([i, j, k, l, m, n]), maximum)
                        for o in range(n, M + 1):
                            if N == 7:
                                maximum = max(calculate([i, j, k, l, m, n, o]), maximum)
                            for p in range(o, M + 1):
                                if N == 8:
                                    maximum = max(calculate([i, j, k, l, m, n, o, p]), maximum)
                                for q in range(p, M + 1):
                                    if N == 9:
                                        maximum = max(calculate([i, j, k, l, m, n, o, p, q]), maximum)
                                    for r in range(q, M + 1):
                                        if N == 10:
                                            maximum = max(calculate([i, j, k, l, m, n, o, p, q, r]), maximum)

print(maximum)


N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
# print(N, M, Q)
# print(abcd)

for i in range(1, M + 1):
    if M == 1: break
    for j in range(i, M + 1):
        if M == 2: break
        for k in range(j, M + 1):
            if M == 3: break
            for l in range(k, M + 1):
                if M == 4: break
                for m in range(l, M + 1):
                    if M == 5: break
                    for n in range(m, M + 1):
                        if M == 6: break
                        for o in range(n, M + 1):
                            if M == 7: break
                            for p in range(o, M + 1):
                                if M == 8: break
                                for q in range(p, M + 1):
                                    if M == 9: break
                                    for r in range(q, M + 1):
                                        if M == 10: break

print(1)


import itertools

N, M, Q = list(map(int, input().split()))
abcd = [list(map(int, input().split())) for i in range(Q)]
# print(N, M, Q)
# print(abcd)

def calculate(A, abcd):
    total = 0
    for abcdi in abcd:
        a = abcdi[0] - 1
        b = abcdi[1] - 1
        c = abcdi[2]
        d = abcdi[3]
        if A[b] - A[a] == c:
            total += d
    

    return total

A = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# print(A)

maximum = max([calculate(Ai, abcd) for Ai in A])

print(maximum)


Verdicts: Wrong Answer

Wrong Answer

Accepted