Problem:


Score : 300 points

Problem Statement

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.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1



Sample input:
3 4 3
1 3 3 100
1 2 2 10
2 3 2 10


Sample output:
110

Submission status: Accepted

Solution code:
import itertools
n,m,q= map(int, input().split())
g = []
for _ in range(q):
    z = list(map(int, input().split()))
    g.append(z)
seq = []
for i in range(1,m+1):
  seq.append(i)
f = [0]
for i in list(itertools.combinations_with_replacement(seq, n)):
  ans = 0
  for j in g:
    if i[j[1]-1]-i[j[0]-1] == j[2]:
      ans += j[3]
  f.append(ans)
print(max(f))

Submission status: Accepted

Solution code:
N,M,Q=map(int,input().split())
L = []
for i in range(Q):
  L.append(list(map(int, input().split())))
  L[i][0]-=1
  L[i][1]-=1

import itertools
l =list(range(1,M+1))
v=list(itertools.combinations_with_replacement(l,N))
ans=0
for arr in v:
  score=0
  for a,b,c,d in L:
    if (arr[b] - arr[a])==c:      
      score+=d
  ans=max(ans,score)
print(ans)

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
reqs = [list(map(int, input().split())) for _ in range(Q)]

As = []

def make(A):
  global As, N, M
  # print(A)
  if len(A) == N+1:
    As.append(A)
  else:
    for i in range(A[len(A)-1], M+1):
      make(A+[i])

make([1])

maxs = 0
for A in As:
  score = 0
  for req in reqs:
    if A[req[1]] - A[req[0]] == req[2]:
      score += req[3]
  maxs = max(score, maxs)

print(maxs)

Submission status: Accepted

Solution code:
n, m , q = list(map(int, input().split()))
lists = []
sequence = [1] * n
output = 0
for i in range(q):
  lists.append(list(map(int, input().split())))
  
while (True):
  score = 0
  limit_digit = -1
  for i in range(q):
    if sequence[lists[i][1] - 1] - sequence[lists[i][0] - 1] == lists[i][2]:
      score += lists[i][3]
  if output < score:
    output = score
  for i in range(n):
    if sequence[n - i - 1] == m:
      limit_digit = n - i - 1
  if limit_digit == 0:
    break
  elif limit_digit == -1:
    sequence[n - 1] += 1
  else:
    if limit_digit == 1:
      sequence = [sequence[0] + 1] * n
    else:
      sequence[limit_digit - 1] += 1
      for k in range(limit_digit, n):
        sequence[k] = sequence[limit_digit - 1]

print(output)

Submission status: Accepted

Solution code:
#

import sys
from collections import deque
from itertools import combinations_with_replacement
input=sys.stdin.readline

def main():
    N,M,Q=map(int,input().split())
    g=[i for i in range(1,M+1)]
    abcds=[tuple(map(int,input().split())) for i in range(Q)]
    maxp=0
    for A in combinations_with_replacement(g, N):
        p=0
        for abcd in abcds:
            if A[abcd[1]-1]-A[abcd[0]-1]==abcd[2]:
                p+=abcd[3]
        maxp=max(maxp,p)
    print(maxp)
    
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N, M, Q = map(int, input().split())
P = list([int(j) for j in input().split()] for i in range(Q))

A = list(i for i in range(1, M+1))
X = list(itertools.combinations_with_replacement(A, N))
R = 0
T = 0
for x in X:
    for i in range(Q):
        if x[P[i][1]-1]-x[P[i][0]-1]==P[i][2]:
            R += P[i][3]
    T = max(T, R)
    R = 0
print(T)

Submission status: Accepted

Solution code:
# C - Many Requirements
import sys
sys.setrecursionlimit(10000)

N,M,Q = map(int,input().split())
q = []
for _ in range(Q):
    q.append(list(map(int,input().split())))
A_list = []

def make(a):
    global A_list,N,M
    if len(a)==N:
        A_list.append(a)
    else:
        for i in range(a[len(a)-1],M+1):
            make(a+[i])
make([1])

ans = 0
for A in A_list:
    tmp = 0
    for a,b,c,d in q:
        if A[b-1]-A[a-1]==c:
            tmp += d
    ans = max(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement
n,m,q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]

num = list(combinations_with_replacement(range(1,m+1), n))

ans = 0
for i in num:
  t = 0
  for j in abcd:
    if i[j[1]-1] - i[j[0]-1] == j[2]:
      t += j[3]
  ans = max(ans, t)
  
print(ans)

Submission status: Accepted

Solution code:
import itertools
N, M, Q = map(int,input().split())

cond_list = [list(map(int,input().split())) for i in range(Q)]

point_max=0
for i in itertools.combinations_with_replacement(range(1,M+1),N):
    point = 0
    for cond in cond_list:

        if (i[cond[1]-1]-i[cond[0]-1]) == cond[2]:

            point = point + cond[3]
    if point>point_max:
        point_max = point

print(point_max)

Submission status: Accepted

Solution code:
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,M,Q = map(int, readline().split())
A = np.array(list(itertools.combinations_with_replacement(range(1,M+1), N)))

score = np.zeros(len(A), int)
m = map(int, read().split())
for a,b,c,d in zip(m,m,m,m):
    score += (A[:,b-1] - A[:,a-1] == c) * d
print(score.max())

Submission status: Accepted

Solution code:
n,m,q = map(int,input().split())
li2=[input() for i in range(q)]
abcd = []
for i in range(q):
    abcd.append([int(x.strip()) for x in li2[i].split()])

ans = 0

import itertools
l = list(range(1,m+1))

for v in itertools.combinations_with_replacement(l, n):
    memo = 0
    for i in range(q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = v[b-1] - v[a-1]
        if c == abcd[i][2]:
            memo += abcd[i][3]

    ans = max(ans,memo)

print(ans)

Submission status: Accepted

Solution code:
import itertools

N,M,Q = map(int, input().split())
AA = itertools.combinations_with_replacement(range(1,M+1), N)
abcd = [list(map(int, input().split())) for _ in range(Q)]

ans = -10**18
for A in AA:
    score = 0
    for a,b,c,d in abcd:
        if A[b-1] - A[a-1] == c:
            score += d
    ans = max(ans, score)
print(ans)

Submission status: Accepted

Solution code:
N,M,Q = (int(x) for x in input().split())
abcd  = [[]]*Q
for T in range(0,Q):
    abcd[T] = [int(x) for x in input().split()]
    
def PointCal(A,Q,abcd,MaxP):
    Point = 0
    for TT in range(0,Q):
        if A[abcd[TT][1]-1]-A[abcd[TT][0]-1]==abcd[TT][2]:
            Point += abcd[TT][3]
    return max(Point,MaxP)

MaxP  = 0
for A1 in range(1,M+1):
    for A2 in range(A1,M+1):
        if N==2:
            A    = [A1,A2]
            MaxP = PointCal(A,Q,abcd,MaxP)
        else:
            for A3 in range(A2,M+1):
                if N==3:
                    A    = [A1,A2,A3]
                    MaxP = PointCal(A,Q,abcd,MaxP)
                else:
                    for A4 in range(A3,M+1):
                        if N==4:
                            A    = [A1,A2,A3,A4]
                            MaxP = PointCal(A,Q,abcd,MaxP)
                        else:
                            for A5 in range(A4,M+1):
                                if N==5:
                                    A    = [A1,A2,A3,A4,A5]
                                    MaxP = PointCal(A,Q,abcd,MaxP)
                                else:
                                    for A6 in range(A5,M+1):
                                        if N==6:
                                            A    = [A1,A2,A3,A4,A5,A6]
                                            MaxP = PointCal(A,Q,abcd,MaxP)                                    
                                        else:
                                            for A7 in range(A6,M+1):
                                                if N==7:
                                                    A    = [A1,A2,A3,A4,A5,A6,A7]
                                                    MaxP = PointCal(A,Q,abcd,MaxP)
                                                else:
                                                    for A8 in range(A7,M+1):
                                                        if N==8:
                                                            A    = [A1,A2,A3,A4,A5,A6,A7,A8]
                                                            MaxP = PointCal(A,Q,abcd,MaxP)   
                                                        else:
                                                            for A9 in range(A8,M+1):
                                                                if N==9:
                                                                    A    = [A1,A2,A3,A4,A5,A6,A7,A8,A9]
                                                                    MaxP = PointCal(A,Q,abcd,MaxP)
                                                                else:
                                                                    for A10 in range(A9,M+1):
                                                                        A    = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10]
                                                                        MaxP = PointCal(A,Q,abcd,MaxP)
print(MaxP)

Submission status: Accepted

Solution code:
N,M,Q = (int(x) for x in input().split())
ABCD  = [0]*Q
for T in range(0,Q):
    ABCD[T] = [int(x) for x in input().split()]
    
MaxP  = 0
for A1 in range(1,M+1):
    for A2 in range(A1,M+1):
        if N<=2:
            Point = 0
            A = [A1,A2]
            for TT in range(0,Q):
                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                    Point = Point + ABCD[TT][3]
            if Point > MaxP:
                MaxP = Point
        else:
            for A3 in range(A2,M+1):
                if N<=3:
                    Point = 0
                    A = [A1,A2,A3]
                    for TT in range(0,Q):
                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                            Point = Point + ABCD[TT][3]
                    if Point > MaxP:
                        MaxP = Point                    
                else:
                    for A4 in range(A3,M+1):
                        if N<=4:
                            Point = 0
                            A = [A1,A2,A3,A4]
                            for TT in range(0,Q):
                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                    Point = Point + ABCD[TT][3]
                            if Point > MaxP:
                                MaxP = Point
                        else:
                            for A5 in range(A4,M+1):
                                if N<=5:
                                    Point = 0
                                    A = [A1,A2,A3,A4,A5]
                                    for TT in range(0,Q):
                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                            Point = Point + ABCD[TT][3]
                                    if Point > MaxP:
                                        MaxP = Point                                    
                                else:
                                    for A6 in range(A5,M+1):
                                        if N<=6:
                                            Point = 0
                                            A = [A1,A2,A3,A4,A5,A6]
                                            for TT in range(0,Q):
                                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                    Point = Point + ABCD[TT][3]
                                            if Point > MaxP:
                                                MaxP = Point                                        
                                        else:
                                            for A7 in range(A6,M+1):
                                                if N<=7:
                                                    Point = 0
                                                    A = [A1,A2,A3,A4,A5,A6,A7]
                                                    for TT in range(0,Q):
                                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                            Point = Point + ABCD[TT][3]
                                                    if Point > MaxP:
                                                        MaxP = Point                                                
                                                else:
                                                    for A8 in range(A7,M+1):
                                                        if N<=8:
                                                            Point = 0
                                                            A = [A1,A2,A3,A4,A5,A6,A7,A8]
                                                            for TT in range(0,Q):
                                                                if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                    Point = Point + ABCD[TT][3]
                                                            if Point > MaxP:
                                                                MaxP = Point                                                            
                                                        else:
                                                            for A9 in range(A8,M+1):
                                                                if N<=9:
                                                                    Point = 0
                                                                    A = [A1,A2,A3,A4,A5,A6,A7,A8,A9]
                                                                    for TT in range(0,Q):
                                                                        if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                            Point = Point + ABCD[TT][3]
                                                                    if Point > MaxP:
                                                                        MaxP = Point                                                                
                                                                else:
                                                                    for A10 in range(A9,M+1):
                                                                        Point = 0
                                                                        A = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10]
                                                                        for TT in range(0,Q):
                                                                            if A[ABCD[TT][1]-1]-A[ABCD[TT][0]-1]==ABCD[TT][2]:
                                                                                Point = Point + ABCD[TT][3]
                                                                        if Point > MaxP:
                                                                            MaxP = Point
print(MaxP)

Submission status: Accepted

Solution code:
import itertools
n, m, q = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(q)]
r = 0
for A in itertools.combinations_with_replacement(range(m), n):
    s = 0
    for i in range(q):
        if A[x[i][1] - 1] - A[x[i][0] - 1] == x[i][2]:
            s += x[i][3]
    r = max(r, s)
print(r)

Submission status: Accepted

Solution code:
n,m,q = map(int,input().split())
P = [list(map(int,input().split())) for i in range(q)]

import itertools
num_list = list(itertools.combinations_with_replacement(range(1,m+1), n))

base = 0
for i in range(len(num_list)):
    sum = 0
    for j in range(q):
        if num_list[i][P[j][1]-1] - num_list[i][P[j][0]-1] == P[j][2]:
            sum += P[j][3]
    
    if base  < sum:
        base = sum

print(base)

Submission status: Accepted

Solution code:
def dfs(A):
    if len(A) > n:
        total = 0
        for i in range(q):
            if A[b[i]]-A[a[i]] == c[i]:
                total += d[i]
        global ans
        if total > ans:
            ans = total
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A[:])
        A[-1] += 1

if __name__ == "__main__":
    ans = 0
    n, m, q = map(int, input().split())
    a, b, c, d = [None]*q, [None]*q, [None]*q, [None]*q
    for i in range(q):
        a[i], b[i], c[i], d[i] = map(int, input().split())
    dfs([1])
    print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0
 
def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score
 
def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        f(p + [x])
 
f([])
 
print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n:
        return calc_score(p)
    ans = 0
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        p.append(x)
        ans = max(ans, f(p))
        p.pop()
    return ans

print(f([]))

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        p.append(x)
        f(p)
        p.pop()

f([])

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as comb

n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

for p in comb(range(1, m+1), n):
    ans = max(ans, calc_score(p))

print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n:
        global ans
        ans = max(ans, calc_score(p))
        return
    l = p[-1] if len(p) else 1
    for x in range(l, m+1):
        f(p + [x])

f([])

print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
ans = 0

def calc_score(p):
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    return score

def f(p):
    if len(p) == n+1:
        global ans
        ans = max(ans, calc_score(p))
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
arr = []

def f(p):
    if len(p) == n+1:
        arr.append(p[:-1])
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

ans = 0
for p in arr:
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

n, m, q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(q)]
arr = []

def f(p):
    if len(p) == n+1:
        arr.append(p[:-1])
        return
    while p[-1] <= m:
        f(p + [p[-1]])
        p[-1] += 1

f([1])

ans = 0
for p in arr:
    score = 0
    for a, b, c, d in queries:
        if p[b-1] - p[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
qry = [list(map(int, input().split())) for _ in range(q)]

ans = 0


def dfs(A):
    global ans
    if len(A) == n:
        score = 0
        for a, b, c, d in qry:
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max(ans, score)
        return
    elif len(A) == 0:
        for nx in range(1, m + 1):
            dfs(A + [nx])
    else:
        for nx in range(A[-1], m + 1):
            dfs(A + [nx])


dfs([])
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)


def main():
    n, m, q = map(int, input().split())
    al = [None]*q
    bl = [None]*q
    cl = [None]*q
    dl = [None]*q
    for i in range(q):
        a, b, c, d = map(int, input().split())
        al[i] = a
        bl[i] = b
        cl[i] = c
        dl[i] = d
    
    def dfs(A):
        if len(A) > n:
            total = 0
            for i in range(q):
                if A[bl[i]]-A[al[i]] == cl[i]:
                    total += dl[i]
            global ans
            if total > ans:
                ans = total
            return
        A.append(A[-1])
        while A[-1] <= m:
            dfs(A[:])
            A[-1] += 1
    
    dfs([1])
    print(ans)
                
                

if __name__ == "__main__":
    ans = 0
    main()

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
Q_list = []
for i in range(Q):
  Q_list.append(list(map(int, input().split())))


max_cost = 0
#use dfs to find all possible sequences
def dfs(A):
  global max_cost
  #termination condition
  if (len(A)==N):
    cost = 0
    #calculate cost
    for Q in Q_list:
      a = Q[0]
      b = Q[1]
      c = Q[2]
      d = Q[3]
      if (A[b-1]-A[a-1]==c):
        cost += d
    if (cost>max_cost):
      max_cost = cost
    return 1
  #dfs recursion part
  low = 0
  if (A == []):
    low = 1
  else:
  	low = A[-1]
  for i in range(low, M+1):
    dfs(A + [i])
    
dfs([])
print(max_cost)

Submission status: Accepted

Solution code:
from sys import stdin
import itertools
import numpy as np
from numba import njit

#入力
readline=stdin.readline
N,M,Q=map(int,readline().split())
a=np.zeros(Q,dtype=np.int64)
b=np.zeros(Q,dtype=np.int64)
c=np.zeros(Q,dtype=np.int64)
d=np.zeros(Q,dtype=np.int64)
for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int,readline().split())

arrays=np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)),dtype=np.int64)
l=len(arrays)

@njit
def f():
    max_res=0
    for i in range(l):
        res=0
        for j in range(Q):
            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:
                res+=d[j]
        max_res=max(max_res,res)
    return max_res

print(f())

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc165/submissions/12672182
#写経
import itertools
import numpy as np

def resolve():
    N,M,Q = map(int,input().split())
    A = np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)))
    s = np.zeros(len(A), np.int32)
    for i in range(Q):
        a,b,c,d = map(int,input().split()) 
        s += d * (A[:,b-1] - A[:,a-1] == c)
    print(s.max())
resolve()

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
ans = float("-inf")
def calcu(arr):
    res = 0
    for a, b, c, d in abcd:
        if arr[b-1] - arr[a-1] == c:
            res += d
    return res

def dfs(A):
    global ans
    if len(A) == N:
        ans = max(ans, calcu(A))
        return
    else:
        start = A[-1] if len(A) > 0 else 1
        for num in range(start, M+1):
            A.append(num)
            dfs(A)
            A.pop()

dfs([])
print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
ans = 0
a, b, c, d = [None]*q, [None]*q, [None]*q, [None]*q
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

def dfs(A):
    if len(A) == n+1:
        now = 0
        for i in range(q):
            if A[b[i]]-A[a[i]] == c[i]:
                now += d[i]
        global ans
        ans = max(ans, now)
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A[:])
        A[-1] += 1

if __name__ == "__main__":
    dfs([1])
    print(ans)

Submission status: Accepted

Solution code:
import itertools
n,m,q = (int(x) for x in input().split())
l = []
a = [1,2,3,4,5,6,7,8,9,10]
b = []
c = []
d = []


for i in range(q):
    l.append([])
    l[i]=list(map(int, input().split()))
    
for i in itertools.combinations_with_replacement(a[:m], r=n):#考えられる数列
        b.append(i)        
    
for i in range(len(b)):#それぞれの数列の得点表
    d.append(0)

for i in range(q):
    w=l[i][0]-1#a
    x=l[i][1]-1#b
    y=l[i][2]#c
    z=l[i][3]#d
    for j in range(len(b)):
        if b[j][x]-b[j][w]==y:
            d[j]+=z

print(max(d))

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement
 
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
 
ans = 0
for pat in combinations_with_replacement(range(1, m + 1), n):
    score = 0
    for a, b, c, d in abcd:
        a -= 1
        b -= 1
        if pat[b] - pat[a] == c:
            score += d
 
    ans = max(ans, score)
 
print(ans)

Submission status: Accepted

Solution code:
import copy


def dfs(A):
    global ans
    if len(A) == n + 1:
        now = 0
        for j in range(q):
            if A[b[j]] - A[a[j]] == c[j]:
                now += d[j]
        ans = max(ans, now)
        return

    B = copy.copy(A)
    B.append(A[-1])
    while B[-1] <= m:
        dfs(B)
        B[-1] += 1


n, m, q = map(int, input().split())
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
ans = 0

for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

dfs([1])

print(ans)

Submission status: Accepted

Solution code:
def dfs(A):
    global ans
    if len(A) == n + 1:
        now = 0
        for j in range(q):
            if A[b[j]] - A[a[j]] == c[j]:
                now += d[j]
        ans = max(ans, now)
        return

    B = list(A)
    B.append(A[-1])
    while B[-1] <= m:
        dfs(tuple(B))
        B[-1] += 1


n, m, q = map(int, input().split())
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
ans = 0

for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

dfs(tuple([1]))

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
a = [0] * q
b = [0] * q
c = [0] * q
d = [0] * q
ans = 0

for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

for seq in combinations_with_replacement([i for i in range(1, m+1)], n):
    now = 0
    for j in range(q):
        if seq[b[j] - 1] - seq[a[j] - 1] == c[j]:
            now += d[j]
    ans = max(ans, now)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import itertools

N, M, Q = map(int,input().split())

A = np.array(list(itertools.combinations_with_replacement(range(1,M+1),N)))

n = len(A)
s = np.zeros(n, np.int32)

for i in range(Q):
    a, b, c, d = map(int,input().split())
    s += d * (A[:,b-1] - A[:,a-1] == c)

print(max(s))

Submission status: Accepted

Solution code:
from collections import deque

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

def calc(seq):
    points = 0
    for a,b,c,d in abcd:
        if seq[b-1]-seq[a-1]==c:
            points += d
    return points

q = deque()
q.append([1])
ans = 0
while q:
    head = q.popleft()
    if len(head)==N:
        pts = calc(head)
        ans = max(ans, pts)
    else:
        for i in range(head[-1], M+1):
            new = head + [i]
            q.append(new)

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement
import numpy as np 
N,M,Q = (int(x) for x in input().split())
abcd =np.zeros((Q,4),dtype=int)
for i in range(Q):
    abcd[i] = list(map(int, input().split()))
abcd[:,0] -=1 #添え字にするから、-1
abcd[:,1] -=1 #添え字にするから、-1

#リストAを要素数N,最大値Mで全列挙
ans =[]
#for i in range(1,N+1):
for a in combinations_with_replacement (range(1,M+1),N):
    A =np.array(a,dtype=int)
    X = np.where( A[abcd[:,1] ] -A[abcd[:,0]] ==abcd[:,2] )
    ans.append(np.sum(abcd[X,3]))
print(max(ans))

Submission status: Accepted

Solution code:
import itertools

N, M, Q = map(int, input().split())

a, b, c, d = [], [], [], []
for i in range(Q):
    _a, _b, _c, _d = map(int, input().split())
    a.append(_a)
    b.append(_b)
    c.append(_c)
    d.append(_d)

l = [v for v in range(1, M+1)]
ll = list(itertools.combinations_with_replacement(l, N))

ans = 0
for i in ll:
    anstmp = 0
    for j in range(Q):
        if i[b[j]-1] - i[a[j]-1] == c[j]:
            anstmp += d[j]
    if anstmp > ans:
        ans = anstmp

print(ans)

Submission status: Accepted

Solution code:
import itertools

N, M, Q = map(int, input().split())
# q回入力させる
abcd = [tuple(map(int, input().split())) for _ in range(Q)]

Max = 0
# 順列なのでM+1=Nになったらストップ
for C in itertools.combinations_with_replacement(range(1, M + 1), N):
  score = sum([d for a, b, c, d in abcd if C[b-1] - C[a-1] == c])
  Max = max(Max, score)
print(Max)

Submission status: Accepted

Solution code:
from collections import deque
de = deque()
n,m,q = map(int,input().split())
queries = []
for _ in range(q):
    a,b,c,d = map(int,input().split())
    queries.append((a,b,c,d))
de.append([1])#bfsで作成する数列
maxscore = 0
while de:
    li = de.popleft()
    if len(li) < n:
        for i in range(li[-1],m+1):
            new_li = li + [i]
            de.append(new_li)
    else:
        score = 0
        for i in range(q):
            a,b,c,d = queries[i]
            if li[b-1] - li[a-1] == c:
                score += d
        maxscore = max(maxscore,score)
print(maxscore)