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 sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
import numpy as np
def main():
    n, m, q = map(int, input().split())
    A = np.array(list(combinations_with_replacement(range(1, m + 1), n)))
    numA = len(A)
    score = np.zeros(numA, np.int32)
    ma = map(int, read().split())
    for a, b, c, d in zip(ma, ma, ma, ma):
        a -= 1
        b -= 1
        eachA_is_equalOrNot = A[:, b] - A[:, a] == c
        score += d * eachA_is_equalOrNot
        # print(score)
    print(score.max())

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
def main():
    n, m, q = map(int, input().split())
    qs = []
    for _ in range(q):
        abcd = tuple(map(int, input().split()))
        qs.append(abcd)
    A = tuple(combinations_with_replacement(range(1, m+1), n))
    r = 0
    for Ae in A:
        t0 = 0
        for qe in qs:
            if Ae[qe[1]-1] - Ae[qe[0]-1] == qe[2]:
                t0 += qe[3]
        r = max(r, t0)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
a = []
b = []
c = []
d = []
for _ in range(Q):
    ad, bd, cd, dd = map(int, input().split())
    a.append(ad-1)
    b.append(bd-1)
    c.append(cd)
    d.append(dd)
def score(A):
    tmp = 0
    for ai, bi, ci, di in zip(a, b, c, d):
        if A[bi] - A[ai] == ci:
            tmp += di
    return tmp
def dfs(A):
    if len(A) == N:
        return score(A) 
    res = 0
    prev_last = A[-1] if len(A) > 0 else 0
    for v in range(prev_last, M):
        A.append(v)
        res = max(res, dfs(A)) 
        A.pop()
    return res
print(dfs([]))

Submission status: Accepted

Solution code:
# ABC165 C
from itertools import combinations as com

N,M,Q=map(int,input().split())
query=[tuple(map(int,input().split())) for _ in [0]*Q]

res=0
for p in com(list(range(N+M-1)),M-1):
    m=1
    A=[]
    for i in range(N+M-1):
        if i in p:
            m+=1
        else:
            A.append(m)
    tmpres=0
    for a,b,c,d in query:
        if A[b-1]-A[a-1]==c:
            tmpres+=d
#     print(p,A,tmpres)
    res=max(tmpres,res)
print(res)

Submission status: Accepted

Solution code:
def dfs(cnt, lowest, lst):
    global ans
    if cnt > n:
        return
    for i in range(lowest, m+1):
        lst.append(i)
        dfs(cnt+1, i, lst)
        lst.pop()
    if cnt == n:
        num = 0
        for i in range(q):
            a, b, c, d = ab[i]
            if lst[b-1] - lst[a-1] == c:
                num += d
        ans = max(num, ans)

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

print(ans)

Submission status: Accepted

Solution code:
def dfs(lst):
    global ans
    if len(lst) > n:
        return
    lowest = 1
    if lst != []:
        lowest = lst[-1]
    for i in range(lowest, m+1):
        lst.append(i)
        dfs(lst)
        lst.pop()
    if len(lst) == n:
        cnt = 0
        for i in range(q):
            a, b, c, d = ab[i]
            if lst[b-1] - lst[a-1] == c:
                cnt += d
        ans = max(cnt, ans)

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as combi

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

a = [0] * 55
b = [0] * 55
c = [0] * 55
d = [0] * 55

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

candidate = combi(range(1, M+1), N)

ans = 0

for A in candidate:
    score = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            score += d[i]

    ans = max(ans, score)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
def calc(seq):
  score = 0
  for a,b,c,d in array:
    if seq[b-1] - seq[a-1] == c:
      score += d
  return score
  
N,M,Q = map(int,input().split())
array = [ list(map(int,input().split())) for _ in range (Q) ]
ans = 0
que = deque()
for i in range(1,M+1):
  que.append([i])
  
while que:
  seq = que.popleft()
  if len(seq) == N:
    score = calc(seq)
    ans = max(ans,score)
  else:
    for i in range(seq[-1],M+1):
      seq_next = seq + [i]
      que.append(seq_next)
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)]

ans = 0
for a in combinations_with_replacement(range(1,m+1), n):
  t = 0
  for i in abcd:
    if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3]
  if ans < t: ans = t
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)]

nums = combinations_with_replacement(range(1,m+1), n)
ans = 0
for a in nums:
  t = 0
  for i in abcd:
    if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3]
  if ans < t: ans = t
print(ans)

Submission status: Accepted

Solution code:
import copy
Z=[]

def dfs(A,n,m):
    global Z
    if len(A)==n:
        Z.append(copy.deepcopy(A))
        return
    
    first = A[-1] if len(A)>0 else 1
    
    for i in range(first, m+1):
        
        A.append(i)
        
        dfs(A,n,m)
        A.pop()
    

n,m,q = map(int,input().split()) 

dfs([],n,m)


A=[]
B=[]
C=[]
D=[]

for _ in range(q):
    a,b,c,d = map(int,input().split()) 
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)
    
ans = 0    
for z in Z:
    score=0
    for i in range(q):
        if z[B[i]-1] - z[A[i]-1] == C[i]:
            score += D[i]
            
    ans=max(ans,score)
        
    
print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as comb_rplc
N,M,Q = map(int,input().split())
array = [ list(map(int,input().split())) for _ in range(Q) ]
ans = 0
for seq in comb_rplc(range(1, M + 1), N):
  score = 0
  for a,b,c,d in array:
    if seq[b-1] - seq [a-1] == c:
      score += d
  ans = max(score,ans)
print(ans)

Submission status: Accepted

Solution code:
import itertools
n,m,q=list(map(int,input().split()))
abcd=[[] for i in range(q)]
for i in range(q):
    abcd[i]=list(map(int,input().split()))
ans=0
l=list(itertools.combinations_with_replacement([a for a in range(1,m+1)],n))
x=(len(l))
for i in range(x):
    e=l[i]
    
    wa=0
    for t in range(q):
        if e[abcd[t][1]-1]-e[abcd[t][0]-1]==abcd[t][2]:
            wa+=abcd[t][3]
    ans=max(ans,wa)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import chain

# import numpy as np
# from itertools import combinations as comb
# from bisect import bisect_left, bisect_right, insort_left, insort_right
# from collections import Counter


def solve(
    N: int, M: int, Q: int, ABCD: "List[(int,int,int,int)]",
):
    def dfs(A, l, r, n):
        if n > 0:
            max_score = 0
            for Ai in range(l, r + 1):
                A.append(Ai)
                score = dfs(A, Ai, r, n - 1)
                if score > max_score:
                    max_score = score
                A.pop()
            return max_score
        else:
            score = 0
            for a, b, c, d in ABCD:
                if A[b - 1] - A[a - 1] == c:
                    score += d
            return score

    return dfs([], 1, M, N)


def main():
    tokens = chain(*(line.split() for line in sys.stdin))
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    ABCD = []
    for i in range(Q):
        a = int(next(tokens))
        b = int(next(tokens))
        c = int(next(tokens))
        d = int(next(tokens))
        ABCD.append((a, b, c, d))
    answer = solve(N, M, Q, ABCD)
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement

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

cands = []
for i in combinations_with_replacement(list(range(1,m+1)),n):
    cands.append(i)

maxi = 0
for cand in cands:
    score = 0
    for condition in conditions:
        if cand[condition[1]-1] - cand[condition[0]-1]  == condition[2]:
            score += condition[3]
    if score > maxi:
        maxi = score
print(maxi)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as cwr
N,M,Q=map(int,input().split())
ABCD=[]
for _ in range(Q):
    ABCD.append(list(map(int,input().split())))
maxscore=0
for comb in cwr(range(1,M+1),N):
    score=0
    for row in ABCD:
        if comb[row[1]-1]-comb[row[0]-1]==row[2]:
            score+=row[3]
    maxscore=max(maxscore,score)
print(maxscore)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,m,q = LI()
    require = [LI() for _ in range(q)]
    itr = itertools.combinations_with_replacement(list(range(1,n+2)),m-1)     
    ans = 0
    for i in itr:
        lst = [0 for _ in range(n+2)]
        lst[0] = 1
        cnt = 0
        for j in i:
            lst[j] += 1
        lst = list(itertools.accumulate(lst))
        for re in require:
            if lst[re[1]]-lst[re[0]]==re[2]:
                cnt += re[3]
        ans = max(cnt,ans)
    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
A = []
def rec(itr, lst):
    if itr == N:
        res = 0
        for a, b, c, d in abcd:
            if A[b-1] - A[a-1] == c:
                res += d
        return res
    else:
        res = 0
        for i in range(lst, M):
            A.append(i)
            res = max(res, rec(itr + 1, i))
            A.pop()
        return res
print(rec(0, 0))

Submission status: Accepted

Solution code:
import sys
import itertools

N, M, Q = map(int, input().split())
abcd = []
z = [x for x in range(1,M+1)]
s = 0
d = 0
for i in range(Q):
    abcd.append(list(map(int, input().split())))

for A in itertools.combinations_with_replacement(z, N):
    d = 0
    for y in abcd:
        if A[y[1]-1] - A[y[0]-1] == y[2]:
            d += y[3]
    s = max(d, s)

print(s)

Submission status: Accepted

Solution code:
import itertools

N, M, Q = map(int, input().split())
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())
    a[i] -= 1
    b[i] -= 1

result = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
    score = 0
    for i in range(Q):
        if A[b[i]] - A[a[i]] == c[i]:
            score += d[i]
    result = max(result, score)
print(result)

Submission status: Accepted

Solution code:
N,M,Q=list(map(int,input().split()))
from itertools import combinations_with_replacement as comb
l=comb(range(1,M+1),N)
q=[list(map(int,input().split())) for i in range(Q)]
ans=0
for i in l:
   tmp=0
   for a,b,c,d in q:
      if i[b-1]-i[a-1]==c:
         tmp+=d
   ans=max(tmp,ans)
print(ans)

Submission status: Accepted

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

v = list(itertools.combinations_with_replacement(range(1, m+1), n))
ans = 0
for i in v:
    s = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            s += d
    ans = max(ans, s)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  3 23:43:13 2020

@author: liang
"""

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

A = list()
lis = list()
ans = 0

def make_list(n,m):
    if n == N:
        A.append(lis.copy())
        return 
    for i in range(m,M+1):
        lis.append(i)
        make_list(n+1,i)
        lis.pop()

make_list(0,1)
#print(A)

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

for a in A:
    tmp = 0
    for c in calc:
        if a[c[1]-1] - a[c[0]-1] == c[2]:
            tmp += c[3]
    if tmp > ans:
        ans = tmp
       
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, M, Q = MI()
    targets = [[] for i in range(Q)]
    for i in range(Q):
        target = LI()
        targets[i] = target
    ans = 0
    for i in range(1, 2**(M+N)):
        i_str = format(i, f"0{M+N-1}b")
        A = []
        tmp = 1
        for k in range(len(i_str)):
            if i_str[k] == "1":
                tmp += 1
            else:
                A.append(tmp)
        if len(A) != N:
            continue
        score = 0
        for query in targets:
            a, b, c, d = query
            if A[b-1] - A[a-1] == c:
                score += d
        ans = max(ans, score)

    print(ans)


main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
N, M, Q = map(int, input().split())
Q = [list(map(int,input().split())) for _ in range(Q)]
ans = 0
A = []


def dfs():
    global A
    if len(A) == N:
        return
    global ans
    if len(A) == 0:
        start = 1
    else:
        start = A[-1]
    for i in range(start, M + 1):
        A.append(i)
        dfs()
        if len(A) != N:
            A.pop()
            continue
        # print(A)
        tmp_ans = 0
        for a, b, c, d in Q:
            if A[b - 1] - A[a - 1] == c:
                tmp_ans += d
        ans = max(ans, tmp_ans)
        A.pop()


dfs()
print(ans)

Submission status: Accepted

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

ans = 0
for a in range(M):
    for b in range(a, M):
        for c in range(b, M):
            for d in range(c, M):
                for e in range(d, M):
                    for f in range(e, M):
                        for g in range(f, M):
                            for h in range(g, M):
                                for i in range(h, M):
                                    for j in range(i, M):
                                        tmp = [a, b, c, d, e, f, g, h, i, j]
                                        tmp_ans = 0
                                        for q in range(Q):
                                                if tmp[L[q][1]-1] - tmp[L[q][0]-1] == L[q][2]:
                                                    tmp_ans += L[q][3]

                                        ans = max(ans, tmp_ans)

print(ans)

Submission status: Accepted

Solution code:
import itertools

n,m,q=map(int,input().split())
lst=[]
ans=0

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

for x in list(itertools.combinations_with_replacement(range(1,m+1),n)):
  cnt=0
  for i in lst:
    if x[i[1]-1]-x[i[0]-1]==i[2]:
      cnt+=i[3]

  ans=max(ans,cnt)


print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as c
N, M, Q = map(int, input().split())
l1 = list(list(map(int, input().split())) for _ in range(Q))
ans = 0

for l2 in c(range(1, M + 1), N):
    total = 0
    for i in range(Q):
        if l2[l1[i][1] - 1] - l2[l1[i][0] - 1] == l1[i][2]:
            total += l1[i][3]
    ans = max(ans, total)

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as c
N, M, Q = map(int,input().split())
l1 = list(list(map(int,input().split())) for _ in range(Q))

ans = 0

for l2 in c(range(1, M + 1), N):
  total = 0
  for i in range(Q):
    if l2[l1[i][1] - 1] - l2[l1[i][0] - 1] == l1[i][2]:
      total += l1[i][3]
  ans = max(ans, total)

print(ans)

Submission status: Accepted

Solution code:
import itertools
N, M, Q = map(int, input().split())
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())

cc = [i for i in range(1, M+1)]
ans = 0

for A in itertools.combinations_with_replacement(cc, N):
    A = list(A)
    t = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            t += d[i]
    ans = max(ans, t)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import itertools
N, M, Q = map(int, input().split())
G = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    G.append([a, b, c, d])

ans = 0

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

n = len(A)
score = np.zeros(n, np.int32)
for a, b, c, d in G:
    cond = A[:, b - 1] - A[:, a - 1] == c
    score += d * cond

print(score.max())

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
G = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    G.append([a, b, c, d])

ans = 0


def dfs(s):
    global ans
    if len(s) == N:
        now = 0
        for a, b, c, d in G:
            if s[b - 1] - s[a - 1] == c:
                now += d
        ans = max(ans, now)
        return

    for i in range(s[-1], M):
        dfs(s + [i])


s = [0]
dfs(s.copy())

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement
N, M, Q = map(int,input().split())
ls = []
for _ in range(Q):
    a,b,c,d = map(int,input().split())
    ls.append((a-1,b-1,c,d))
ans = -1
for A in combinations_with_replacement(range(1,M+1),r=N):
    sm = 0
    for a,b,c,d in ls:
        if A[b]-A[a] == c:
            sm += d
    ans = max(ans, sm)
print(ans)

Submission status: Accepted

Solution code:
def f(n, m, arrs=[], arr=[]):
    if len(arr) == n:
        arrs.append(arr)
    else:
        if len(arr) == 0:
            for i in range(1, m + 1):
                f(n, m, arrs, arr + [i])
        else:
            for i in range(arr[-1], m + 1):
                f(n, m, arrs, arr + [i])
    return arrs


n, m, q = map(int, input().split())
Q = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    Q.append((a, b, c, d))
#print(memo)
ans = 0
for ball in f(n, m):
    a = 0
    #print(ball)
    for q in Q:
        if ball[q[1] - 1] - ball[q[0] - 1] == q[2]:
            a += q[3]
    ans = max(ans, a)
print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())

candidate = []


def gen(cur):
    if len(cur) == n:
        candidate.append(cur)
    else:
        t = cur[-1]
        for tv in range(t, m + 1):
            nex = cur[:]
            nex.append(tv)
            gen(nex)


for i in range(1, m + 1):
    arr = [i]
    gen(arr)

ask = []
for _ in range(q):
    ask.append(list(map(int, input().split())))

ans = 0
for cv in candidate:
    tmp = 0
    for av in ask:
        if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
            tmp += av[3]
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())

candidate = []


def gen(cur):
    if len(cur) == n:
        candidate.append(cur)
    else:
        t = cur[-1]
        for tv in range(t, m + 1):
            nex = cur[:]
            nex.append(tv)
            gen(nex)


for i in range(1, 10):
    arr = [i]
    gen(arr)

ask = []
for _ in range(q):
    ask.append(list(map(int, input().split())))

ans = 0
for cv in candidate:
    tmp = 0
    for av in ask:
        if cv[av[1] - 1] - cv[av[0] - 1] == av[2]:
            tmp += av[3]
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
import itertools
N, M, Q = map(int,input().split())
ABCD_list = [[int(_) for _ in input().split()] for i in range(Q)]
combs = itertools.combinations_with_replacement(range(1,M+1),N)
points = []
for comb in combs:
    A_list = list(comb)
    point = 0
    for inner in ABCD_list:
        if A_list[inner[1]-1]-A_list[inner[0]-1] == inner[2]:
            point += inner[3]
        points.append(point)
print(max(points))

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
A = []

for i in range(Q):
  A.append(list(map(int, input().split())))

def calc(B, n):
  ans = 0
  #not enough length
  if len(B) < N:
    for i in range(n, M + 1):
      ans = max(ans, calc(B + [i], i))
  #enough length
  else:
    #print("B:", B)
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    #print("ans:", ans)
  return ans



print(calc([], 1))

Submission status: Accepted

Solution code:
def dfs(i, p, seq):
    global N, M
    ret = 0
    if i == N:
        ret = check(seq)
    else:
        for x in range(p, M + 1):
            ret = max(ret, dfs(i + 1, x, tuple(list(seq) + [x])))
    return ret


def check(seq):
    cnt = 0
    for a, b, c, d in query:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    return cnt


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

print(dfs(0, 1, tuple()))

Submission status: Accepted

Solution code:
def resolve():
    N, M, Q = list(map(int, input().split()))
    Q = [list(map(int, input().split())) for _ in range(Q)]
    import itertools
    maxpoint = 0
    for seq in itertools.combinations_with_replacement(range(1, M+1), N):
        point = 0
        for a, b, c, d in Q:
            if seq[b-1] - seq[a-1] == c:
                point += d
        maxpoint = max(maxpoint, point)
    print(maxpoint)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
def return_d(A,abcds):
    sum_d=0
    for abcd in abcds:
        a,b,c,d=abcd
        if A[b]-A[a]==c:
            sum_d+=d
    return sum_d

n,m,q=[int(i) for i in input().split()]
abcds=[[int(i) for i in input().split()] for j in range(q)]


As=[]
for i_1 in range(1,m+1):
    for i_2 in range(i_1, m + 1):
        for i_3 in range(i_2, m + 1):
            for i_4 in range(i_3, m + 1):
                for i_5 in range(i_4, m + 1):
                    for i_6 in range(i_5, m + 1):
                        for i_7 in range(i_6, m + 1):
                            for i_8 in range(i_7, m + 1):
                                for i_9 in range(i_8, m + 1):
                                    for i_10 in range(i_9, m + 1):
                                        As.append([0,i_1,i_2,i_3,i_4,i_5,i_6,i_7,i_8,i_9,i_10])


ans=0
for A in As:
    ans=max(ans,return_d(A,abcds))

print(ans)

Submission status: Accepted

Solution code:
N, M, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(list(map(int, input().split())))
def f(B, n):
  ans = 0
  if len(B) < N:
    for i in range(n, M+1):
      ans = max(ans, f(B+[i], i))
  else:
    for j in range(Q):
      if B[A[j][1]-1] - B[A[j][0]-1] == A[j][2]:
        ans += A[j][3]
    return ans
  return ans
print(f([], 1))

Submission status: Accepted

Solution code:

def f(A, N, M, Q, conditions):
    L = len(A) - 1

    if L > N:
        return

    elif L < N:
        fans = 0
        for j in range(A[L], M + 1):
            fans = max(fans ,f(A + [j], N, M, Q, conditions))

    else: # L = N
        fans = 0
        for i in range(Q):
            a, b, c, d = conditions[i]
            if A[b] - A[a] == c:
                fans += d
        
    return fans

N, M, Q = [int(x) for x in input().split()]

conditions = []
for i in range(Q):
    c = [int(x) for x in input().split()]
    conditions.append(c)

ans = f([1], N, M, Q, conditions)

print(ans)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if lst else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        len_lst = len(lst)
        if len_lst == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len_lst > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1] if len(lst) > 0 else 1
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]

    def calc_point(lst):
        sumv = 0
        for a, b, c, d in query:
            if lst[b-1] - lst[a-1] == c:
                sumv += d
        return sumv

    def dfs(lst):
        if len(lst) == n:
            return calc_point(lst)
        point = 0
        last_elm = lst[-1]
        for i in range(last_elm, m+1):
            point = max(point, dfs(lst+[i]))
        return point

    print(dfs([1]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
n,m,q = map(int, input().split())
a,b,c,d = [0]*q,[0]*q,[0]*q,[0]*q

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

ans = 0

for C in itertools.combinations_with_replacement(range(1,m+1),n):
    x = 0
    for i in range(q):
        if C[b[i]-1] - C[a[i]-1] == c[i]:
            x += d[i]

    ans = max(ans,x)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

# 1 ~ Mまでの数を用いたN項の重複組合せを作成する
arrs = []
for arr in itertools.combinations_with_replacement(range(1, M + 1), N):
    arrs.append(list(arr))
#print(len(arrs))

# 各組合せに対し、全クエリを実行して最大値を求める　(計算量: 10H10 x 50)
answer = - float("inf")
for arr in arrs:
    tmp = 0
    for a, b, c, d in ABCD:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    answer = max(answer, tmp)

print(answer)

Submission status: Accepted

Solution code:
import itertools

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

# 1 ~ Mまでの数を用いたN項の重複数列を作成する
arrs = []
for arr in itertools.combinations_with_replacement(range(1, M + 1), N):
    arrs.append(list(arr))

answer = - float("inf")
for arr in arrs:
    tmp = 0
    for a, b, c, d in ABCD:
        if arr[b - 1] - arr[a - 1] == c:
            tmp += d
    answer = max(answer, tmp)

print(answer)

Submission status: Accepted

Solution code:
def backtrack(nums, N, cur_state, result):
    if len(cur_state) == N:
        result.append(cur_state[:])
    else:
        for i in range(len(nums)):
            cur_state.append(nums[i])
            backtrack(nums[i:], N, cur_state, result)
            cur_state.pop()

def solve():
    N,M,Q = [int(i) for i in input().split()]
    quads = []
    for i in range(Q):
        quads.append([int(i) for i in input().split()])
    candidates = []
    backtrack(list(range(1, M+1)), N, [], candidates)

    ans = 0
    for candidate in candidates:
        score = 0
        for a,b,c,d in quads:
            if candidate[b-1] - candidate[a-1] == c:
                score += d
        ans = max(score, ans)
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement
ans = 0
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
for A in combinations_with_replacement(range(1, M+1), N):
    tmp = 0
    for i in range(Q):
        a,b,c,d = abcd[i]
        a-=1
        b-=1
        if A[b]-A[a] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)