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):
#!/usr/bin/env python3
import sys
import numpy as np
from itertools import *


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    l = [i for i in range(1,M+1)]
    A = []
    if N==M:
        A.append(l)
    else:
        for v in combinations_with_replacement(l, N):
            if sorted(v) not in A:
                A.append(sorted(v))

    ans = [0] * len(A)

    for i in range(len(A)):
        print(A[i])
        for j in range(Q):
            #print(a[j],b[j],c[j],d[j],A[i][b[j]-1]-A[i][a[j]-1], c[j], d[j])
            #print(A[i][b[j]-1]-A[i][a[j]-1], c[j])
            if (A[i][b[j]-1]-A[i][a[j]-1])==c[j]:
                ans[i] += d[j]
        #print(ans[i])
    #print(ans)
    print(max(ans))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
import sys
import numpy as np
from itertools import *


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    l = [i for i in range(1,M+1)]
    ans = 0
    for v in combinations_with_replacement(l, N):
        score = 0
        for j in range(Q):
            #print(a[j],b[j],c[j],d[j],A[i][b[j]-1]-A[i][a[j]-1], c[j], d[j])
            #print(A[i][b[j]-1]-A[i][a[j]-1], c[j])
            if (v[b[j]-1]-v[a[j]-1])==c[j]:
                score += d[j]
        #print(ans[i])
        ans = max(ans, score)
    #print(ans)
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted