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 = [int(x) for x in 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] = [int(x) for x in input().split()]

ans = 0

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

Submission status: Accepted

Solution code:
import itertools

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

comb = list(itertools.combinations_with_replacement(range(1, M + 1), N))

ma = 0
for c in comb:
    score = 0
    for a in A:
        if (c[a[1] - 1] - c[a[0] - 1] == a[2]):
            score += a[3]
    ma = max(ma, score)
print(ma)

Submission status: Accepted

Solution code:
def main():
    n,m,q = map(int,input().split())
    a = [list(map(int, input().split())) for _ in range(q)]
    #a = sorted(a, key=lambda x:-x[3])
    ans = 0
    from itertools import combinations_with_replacement
    for i in combinations_with_replacement(range(1,m+1),n):
        dp = i
        res = 0
        for j in range(q):
            if dp[a[j][1]-1] - dp[a[j][0]-1] == a[j][2]:
                res += a[j][3]
        ans = max(res,ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
N, M, Q = map(int,input().split())
P = [list(map(int,input().split())) for i in range(Q)]
 
comb = list(itertools.combinations_with_replacement(range(1, M+1), N))
 
max_score = 0
for c in comb:
    score = 0
    #print(c)
    for p in P:
        if c[p[1]-1] - c[p[0]-1] == p[2]:
            score += p[3]
    if score > max_score:
        max_score = score
 
print(max_score)

Submission status: Accepted

Solution code:
def saiki(N,M,A,MAX,queri,ans):
	
	if len(A) == N:
		ret = 0
		for q in queri:
			if (A[q[1]] - A[q[0]]) == q[2]:
				ret += q[3]
		if ret > ans[0]:
			ans[0] = ret

	else:
		for i in range(1,MAX+1):
			a = [i] + A[:]
			saiki(N,M,a,i,queri,ans)




if __name__ == '__main__':
	N,M,Q = map(int, input().split())

	queri = []
	for _ in range(Q):
		a,b,c,d = map(int, input().split())
		queri.append([a-1,b-1,c,d])

	ans = [0]
	saiki(N,M,[],M,queri,ans)

	print(ans[0])

Submission status: Accepted

Solution code:
import itertools
n,m,q = list(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] = list(map(int,input().split()))
    b[i]-=1
    a[i]-=1
ans=0

for seq in itertools.combinations_with_replacement(range(m),n):
    tmp = 0
    #print(seq)
    for i in range(q):
        if seq[b[i]]-seq[a[i]]==c[i]:
            tmp+=d[i]
    ans = max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import itertools

N, M, Q = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(Q)]
l = [i for i in range(1, M + 1)]
A = list(itertools.combinations_with_replacement(l, N))

maxans = 0

for j in A:
    ans = 0
    for i in lst:
        if j[i[1]-1] - j[i[0]-1] == i[2]:
            ans = ans + i[3]
    maxans = max(maxans, ans)

print(maxans)

Submission status: Accepted

Solution code:
n,m,q = map(int,input().split())
a = [[int(i) for i in input().split()] for _ in range(q)]
b = []
for i in range(m):
    b.append(i+1)
import itertools
l = list(itertools.combinations_with_replacement(b,n))
max=0
#print(l)                                                                      

#for _ in range(int(c)):                                                       
for ll in l:
    m=0
#    print(ll)                                                                 
    for aa in a:
#        print(ll[aa[1]-1],ll[aa[0]-1],aa[2])                                  
        if ll[aa[1]-1]-ll[aa[0]-1]==aa[2]:
 #           print(aa[3])                                                      
            m+=aa[3]
    if max < m:
        max=m
  #  print(m,max)                                                              
print(max)

Submission status: Accepted

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

_A=0
for v in itertools.combinations_with_replacement(range(1,m+1), n):
    A=list(v)
    _d=0
    for q in _q:
        a,b,c,d=q
        if A[b-1]-A[a-1]==c:
            _d+=d

    if _d > _A:
        _A = _d

print(_A)

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 MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc
from itertools import combinations_with_replacement as combi
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    n, m, q = MII()
    query = [MII() for _ in range(q)]
    l = [i for i in range(1,m+1)]
    maxv = 0
    for v in combi(l,n):
        sumv = 0
        for a,b,c,d in query:
            if v[b-1]-v[a-1] == c:
                sumv += d
        maxv = max(maxv, sumv)
    print(maxv)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
n,m,q = map(int, input().split())
qlist = []

for _ in range(q):
    qlist.append(list(map(int,input().split())))

tmplist = list(range(1,m+1))
point = 0

for A in list(itertools.combinations_with_replacement(tmplist, n)):
    pointtmp = 0
    for tmp in qlist:
        if A[tmp[1]-1] - A[tmp[0]-1] == tmp[2]:
            pointtmp += tmp[3]
    if pointtmp > point:
        point = pointtmp

print(point)

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from itertools import combinations_with_replacement
def resolve():
    N, M, Q = lr()
    ans = 0
    rest = []
    tmp = 0
    for i in range(Q):
        rest.append(lr())
    for seq in combinations_with_replacement(range(1, M+1), N):
        tmp = 0
        for i in range(Q):
            a, b, c, d = rest[i]
            if seq[b-1] - seq[a-1] == c:
                tmp += d
        ans = max(tmp, ans)
    print(ans)
resolve()

Submission status: Accepted

Solution code:
import sys
import itertools

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, M, Q = [int(x) for x in input().split()]
    ABCD = [[int(x) for x in input().split()] for _ in range(Q)]

    ans = 0

    A = [[0] * N for j in range(N)]

    for m in itertools.combinations_with_replacement(range(1, M + 1), r=N):
        m = list(m)
        m.sort()
        tmp = 0
        for a, b, c, d in ABCD:
            if m[b - 1] - m[a - 1] == c:
                tmp += d
        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

N,M,Q = list(map(int, input().split()))
inputs = [list(map(int, input().split())) for i in range(Q)]
items = [1] * N

finish = [M] * N

# (a,b,c,d)
def calc(A, tup):
    a,b,c,d = tup
    if A[b-1] - A[a-1] == c:
        return d
    else:
        return 0

RES = 0
    
seq = [i for i in range(1, M+1)]

all_col = itertools.combinations_with_replacement(seq, N)

for col in all_col:
    tmp = col[0]
    flg = True
    for c in col:
        if c < tmp:
            flg = False
            break
    if flg == False:
        continue
    res = 0
    for inp in inputs:
        res += calc(col, inp) 
    RES = max(RES, res)


print(RES)

Submission status: Accepted

Solution code:
from collections import deque
de = deque()
n,m,q = map(int,input().split())
queries = []
for i in range(q):
    a,b,c,d = map(int,input().split())
    queries.append((a,b,c,d))
de.append([1])
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n,m,q = map(int,input().split())
array = []
A = [i for i in range(1,m+1)]
A_list = list(itertools.combinations_with_replacement(A,n))
max_sum = [0]*len(A_list)

for i in range(q):
    a,b,c,d = map(int,input().split())
    for j in range(len(A_list)):
        if A_list[j][b-1]-A_list[j][a-1] == c:
            max_sum[j] += d
print(max(max_sum))

Submission status: Accepted

Solution code:
import itertools
n,m,q=map(int,input().split())
abcd=[None]*q
for i in range(q):
    abcd[i]=list(map(int,input().split()))
num=0
for l in list(map(list, itertools.combinations_with_replacement(range(1,m+1), n))):
    num_ = 0
    for x in abcd:
        if l[x[1]-1]-l[x[0]-1] == x[2]:
            num_ += x[3]
    num = max(num ,num_)
print(num)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys

sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque



def main():
    list_num, max_num, num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    kari_list = [i + 1 for i in range(max_num)]

    ans = 0
    for key_list in itertools.combinations_with_replacement(kari_list, list_num):
        now_list = sorted(key_list)
        now_cost = 0
        for i in range(num):
            if now_list[data[i][1] - 1] - now_list[data[i][0] - 1] == data[i][2]:
                now_cost += data[i][3]

        ans = max(ans, now_cost)

    print(ans)






if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
N, M, Q = map(int, input().split())
l = []
for _ in range(Q):
    l.append(tuple(map(int, input().split())))
    
c = list(itertools.combinations_with_replacement(list(range(1, M+1)), N))

m = 0
for i in c:
    flag = True
    sum = 0
    for j in l:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            sum += j[3]
    m = max(m, sum)
        
print(m)

Submission status: Accepted

Solution code:
import itertools

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

for i in range(1,M+1,1):
    L.append(i)

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)

List = list(itertools.combinations_with_replacement(L, N))
ans = 0
for l in List:
    temp = 0
    for q in range(Q):
        if (l[B[q]-1]-l[A[q]-1])==C[q]:
            temp = temp + D[q]
    ans = max(ans, temp)
print(ans)

Submission status: Accepted

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

if N==10:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    for a9 in range(a8,M+1):
                                        for a10 in range(a9,M+1):
                                            A.append([a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])
                                            
if N==9:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    for a9 in range(a8,M+1):
                                        A.append([a1,a2,a3,a4,a5,a6,a7,a8,a9])
if N==8:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):
                                for a8 in range(a7,M+1):
                                    A.append([a1,a2,a3,a4,a5,a6,a7,a8])
                                        
if N==7:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):
                            for a7 in range(a6,M+1):

                                A.append([a1,a2,a3,a4,a5,a6,a7])
if N==6:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):
                        for a6 in range(a5,M+1):

                            A.append([a1,a2,a3,a4,a5,a6])
if N==5:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    for a5 in range(a4,M+1):

                        A.append([a1,a2,a3,a4,a5])                                
if N==4:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                for a4 in range(a3,M+1):
                    

                    A.append([a1,a2,a3,a4])
if N==3:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):
            for a3 in range(a2,M+1):
                    

                A.append([a1,a2,a3])
if N==2:
    for a1 in range(1,M+1):
        for a2 in range(a1,M+1):                    


            A.append([a1,a2])
ans=0

abcd=[]
for i in range(Q):
    abc = list(map(int,(input().split())))
    abcd.append(abc)
dd=[]
for i in range(len(A)):
    d=0
    for j in range(Q):
        if A[i][abcd[j][1]-1]-A[i][abcd[j][0]-1] == abcd[j][2]:
            d+=abcd[j][3]
    dd.append(d)
print(str(max(dd)))

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement

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

numbers = [(i+1) for i in range(M)]

number_pattern = list(combinations_with_replacement(numbers,N))
right_pattern = [[] for i in range(len(number_pattern))]

for i in range(Q):
    a,b,c,d = map(int,input().split())
    for j in range(len(number_pattern)):
        if number_pattern[j][b-1] - number_pattern[j][a-1] == c:
            right_pattern[j].append(d)

ans_list = []

for i in right_pattern:
    if not i:
        ans_list.append(0)
    else:
        ans_list.append(sum(i))

ans_list.sort(reverse=True)

print(ans_list[0])

Submission status: Accepted

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

t = []
for _ in range(q):
   a, b, c, d = map(int, input().split())
   t.append([a-1, b-1, c, d])

#t = sorted(t, key=lambda x:x[1])

tmp = [i for i in range(1, m+1)]

from itertools import combinations_with_replacement

ans = 0
for v in combinations_with_replacement(tmp, n):
   A = list(v)
   score = 0
   for i in range(q):
      a, b, c, d = t[i]
      if A[b] - A[a] == c:
         score += d
   ans = max(ans, score)

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement
from collections import defaultdict


def main():
    n, m, q = map(int, input().split())
    abcd = [list(map(int, input().split())) for _ in range(q)]

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

    ans = 0
    for l in ll:
        tmp = 0
        for data in abcd:
            if l[data[1]-1] - l[data[0]-1] == data[2]:
                tmp += data[3]
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,m,q = map(int,input().split())
a = [[int(i) for i in input().split()] for _ in range(q)]
b = []
for i in range(m):
    b.append(i+1)
import itertools
l = list(itertools.combinations_with_replacement(b,n))
max=0
#print(l)                                                                      
for ll in l:
    m=0
    for aa in a:
        if ll[aa[1]-1]-ll[aa[0]-1]==aa[2]:
            m+=aa[3]
    if max < m:
        max=m
print(max)

Submission status: Accepted

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

import itertools
A = list(itertools.combinations_with_replacement(range(1,M+1),N))
# print(A)
ans = 0
for item in A:
    tmp = 0
    for req in abcd:
        if item[req[1]-1] - item[req[0]-1] == req[2]:
            tmp+= req[3]
    ans = max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections
import copy
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())

dq = collections.deque([])
list_val_a = []

for i in range(1, m+1):
    dq.append([i])

while(len(dq) > 0):
    tmp_la = dq.popleft()
    # print(tmp_la)
    last_ai = tmp_la[-1]
    # print(last_ai)
    for j in range(last_ai, m+1):
        t_tmp_la = copy.copy(tmp_la)
        # print("j: " + str(j))
        t_tmp_la.append(j)
        if len(t_tmp_la) >= n:
            list_val_a.append(t_tmp_la)
        else:
            dq.append(t_tmp_la)

max_score = 0
for lai in list_val_a:
    tmp_score = 0
    for j in range(q):
        if lai[b[j]-1]-lai[a[j]-1] == c[j]:
            tmp_score += d[j]
    if tmp_score > max_score:
        max_score = tmp_score

print(max_score)

Submission status: Accepted

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

    res = []
    dfs(list(), res,M,N)

    mx = 0
    for p in res:
        point = 0
        for a,b,c,d in req:
            if p[b] - p[a] == c:
                point += d
        mx = max(point, mx)
    
    print(mx)


def dfs(now,res,M,N):
    if len(now) == N:
        res.append(now)
        return
    start = 1
    if len(now) != 0:
        start = now[-1]

    if start > M:
        return

    for i in range(start,M+1):
        nxt = now[:]
        nxt.append(i)
        dfs(nxt, res,M,N)
    
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import itertools
N,M,Q=map(int,input().split())
a=[]
for _ in range(Q):
    b=list(map(int,input().split()))
    a.append(b)
s=[]
for i in range(1,M+1):
    s.append(i)
alist=list(itertools.combinations_with_replacement(s, r=N+1))


tmp=0
for j in alist:
    s=0
    for i in range(Q):
        j=list(j)
        if j[a[i][1]]-j[a[i][0]]==a[i][2]:
            s+=a[i][3]
    tmp=max(tmp,s)
print(tmp)

Submission status: Accepted

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

A = [1]*N
ans = 0
while A[0] == 1:
    tmp = 0
    for a,b,c,d in ABCD:
        if A[b-1]-A[a-1]==c: tmp += d
    ans = max(ans,tmp)
    A[N-1] += 1
    for i in range(N-1,-1,-1):
        if A[i]>M:
            A[i-1] += 1
            for j in range(i,N):
                A[j] = A[i-1]
print(ans)

Submission status: Accepted

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

import itertools
a = []
for i in range(q):
    a.append(input().split())

suma = sum([int(aa[3]) for aa in a])

ans = 0
l = [i for i in range(m)]
for v in itertools.combinations_with_replacement(l, n-1):
    ans_ = 0
    v = [0]+list(v)
    for j in range(q):
        if v[int(a[j][1])-1] - v[int(a[j][0])-1] == int(a[j][2]):
            ans_ += int(a[j][3])
    if ans_ > ans:
        ans = ans_
        if ans == suma:
            break
print(ans)

Submission status: Accepted

Solution code:
n,m,q = map(int,input().split())
a = [0 for i in range(q)]
ans = 0
for i in range(q):
    a[i] = list(map(int,input().split()))
for i in range(1,m+1):
    for j in range(i,m+1):
        for k in range(j,m+1):
            for s in range(k,m+1):
                for p in range(s,m+1):
                    for l in range(p,m+1):
                        for e in range(l,m+1):
                            for u in range(e,m+1):
                                for v in range(u,m+1):
                                    b = [1,i,j,k,s,p,l,e,u,v]
                                    nn = 0
                                    for h in range(q):
                                        if b[a[h][1]-1] - b[a[h][0]-1] == a[h][2]:
                                            nn += a[h][3]
                                    ans = max(ans,nn)
print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement

def main():
    N,M,Q=map(int,input().split())
    abcd_lst=[list(map(int,input().split())) for _ in range(Q)]
    num=list(range(1,M+1))
    ans=0
    for A in list(combinations_with_replacement(num, N)):
        tmp=0
        for ai,bi,ci,di in abcd_lst:
            if A[bi-1]-A[ai-1]==ci:
                tmp+=di
                ans=max(ans,tmp)
    print(ans)

main()

Submission status: Accepted

Solution code:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
 
import itertools
n,m,q=n1()
s=n3(q)
 
Alist=list(itertools.combinations_with_replacement(range(1,m+1), n))
ans=0
for A in Alist:
    tmp=0
    for r in s:
        if A[r[1]-1]-A[r[0]-1]==r[2]:
            tmp+=r[3]
    ans=max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for _ in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
import sys,bisect,math,itertools
sys.setrecursionlimit(15000)

N,M,Q = map(int,sys.stdin.readline().split())
qs = [list(map(int,sys.stdin.readline().split())) for _ in range(Q)]
com = list(itertools.combinations_with_replacement(range(1,M+1), N))
m = 0
#print(qs)
#print(com)
for A in com:
    s = 0
    for a,b,c,d in qs:
        if A[b-1] - A[a-1] == c:
            s += d
    m = max(m,s)
print(m)

Submission status: Accepted

Solution code:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
 
import itertools
n,m,q=n1()
s=n3(q)

Alist=list(itertools.combinations_with_replacement(range(1,m+1), n))
ans=0
for A in Alist:
    tmp=0
    for r in s:
        if A[r[1]-1]-A[r[0]-1]==r[2]:
            tmp+=r[3]
    ans=max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import itertools

n,m,Q = map(int,input().split())
q = []
number = [i for i in range(1,m+1)]
kouho = itertools.combinations_with_replacement(number, n)
for i in range(Q):
    q.append(list(map(int,input().split())))

ans = []

for i in kouho:
    tmpans = 0
    for j in q:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            tmpans +=j[3]
    ans.append(tmpans)

print(max(ans))

Submission status: Accepted

Solution code:
N, M, Q = map(int,input().split())
L = []
def dfs(List):
    if len(List) == N - 1:
        L.append([0] + List)
    else:
        k = List[-1]
        for i in range(M-k):
            n_L = List[:]
            n_L.append(k+i)
            dfs(n_L)
for i in range(M):
    dfs([i])

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

MAX = 0
for l in L:
    cnt = 0
    for a, b, c, d in Qs:
        if l[b-1] - l[a-1] == c:
            cnt += d
    MAX = max(cnt, MAX)
print(MAX)

Submission status: Accepted

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

A = itertools.combinations_with_replacement([q for q in range(1,(M+1))], N)
count=[]
for a in A:
    tmp=0
    for l in L:
        
        if a[l[1]-1] - a[l[0]-1] == l[2]:
            tmp+=l[3]
    count.append(tmp)

print(max(count))

Submission status: Accepted

Solution code:
n, m, q = map(int, input().split())
a = [0 for i in range(q)]
b = [0 for i in range(q)]
c = [0 for i in range(q)]
d = [0 for i in range(q)]
v = [0 for i in range(n)]
for i in range(q):
	a[i],b[i],c[i],d[i]=map(int,input().split())
import itertools
maxcnt = 0
for pair in itertools.combinations_with_replacement(range(1,m+1), n):
	cnt = 0
	for i in range(q):
		if pair[b[i]-1]-pair[a[i]-1] == c[i]:
			cnt += d[i]
	if maxcnt < cnt:
		maxcnt = cnt
print(maxcnt)

Submission status: Accepted

Solution code:
import itertools
F=list(map(int, input().split()))
s = [list(map(int,input().split())) for i in range(int(F[2]))]

# print(s)
flist=list(range(1,F[1]+1))
comb=list(itertools.combinations_with_replacement(flist,F[0]))
result=[0]*len(comb)
# print(comb)

for i,a in enumerate(comb):
    for row in s:
        if a[row[1]-1]-a[row[0]-1]==row[2]:
            result[i]+=row[3]

print(max(result))
# print(comb[result.index(max(result))])

Submission status: Accepted

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

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

# for i in range(Q):
#     print(A[i])

numbers = []
count = 0
def dfs(num_list):
    last = num_list[-1]
    if len(num_list) == N:
        numbers.append(num_list)
    else:
        for i in range(last, M+1):
            tmp = num_list[:]
            tmp.append(i)
            dfs(tmp)

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

_max = 0
for number in numbers:
    _sum = 0
    for i in range(Q):
        if number[A[i][1] - 1] - number[A[i][0] - 1] == A[i][2]:
            _sum += A[i][3]
    if _max < _sum:
        _max = _sum
print(_max)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,m,q = map(int,readline().split())
lst1 = []
for i in range(q):
    a,b,c,d = map(int,readline().split())
    lst1.append([a-1,b-1,c,d])

def func(lst):
    res = 0
    for a,b,c,d in lst1:
        if lst[b]-lst[a] == c:
            res += d
    return res
lst2 = []
def func2(lst,now,num):
    if now == -1:
        lst2.append(lst)
        return
    else:
        for i in range(1,num+1):
            res = lst.copy()
            res[now] = i
            func2(res,now-1,i)

func2([1]*n,n-1,m)

def dfs(lst):
    ans = 0
    for i in lst2:
        ans = max(ans,func(i))
    return ans

print(dfs([1]*n))

Submission status: Accepted

Solution code:
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())

s = [1] * n
#st[b[i]] - st[a[i]]  = c[i]
num = n-1
#ans1 = [0] * 10000000
ans = 0
pre_ans = 0
t = 0

while(s[0] != m ):

    for i in range(q):
        if s[b[i]-1] - s[a[i]-1]  == c[i]:
            ans += d[i]
    #print(s)
    #print(ans)
    ans = max(pre_ans,ans)
    pre_ans = ans
    #t += 1

    ans = 0

    if s[num] < m:
        s[num] += 1
    elif num > 0 :
        num -= 1
        temp = s[num]+1
        if temp < m:
            for i in range(num,n):
                s[i] = temp
        else:
            while(s[num] == m):
                num -= 1
            temp = s[num]+1
            for i in range(num,n):
                s[i] = temp

        num = n-1

for i in range(q):
    if s[b[i]-1] - s[a[i]-1]  == c[i]:
        ans += d[i]

ans = max(pre_ans,ans)

print(ans)