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

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

n, m, q = LI()
abcd = [LI() for _ in range(q)]
ans = dfs([1])
print(ans)

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

def conb(l,m):
    ls = l.copy()
    if ls[len(ls)-1] < m:
            ls[len(ls)-1] += 1
            return ls
    for i in range(len(ls)-2,-1,-1):
        if ls[i] < m:
            ls[i] += 1
            for j in range(len(ls)-1,i,-1):
                ls[j] = ls[i]
            return ls
    return None
ls = [[1]*n]
i = 0
while(1):
    x = conb(ls[i],m)
    if x != None:
        ls.append(x)
    else:
        break
    i += 1
abcd = []
l = [i for i in range(1,m+1)]

ans = 0

for j in range(q):
    abcd.append(list(map(int,input().split())))
for v in ls:
    
    sum = 0
    for j in range(q):
        if v[abcd[j][1]-1] - v[abcd[j][0]-1] == abcd[j][2]:
            sum += abcd[j][3]
    ans = max(ans,sum)
print(ans)

Solution (Python):
import itertools

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

ls = itertools.combinations_with_replacement([i + 1 for i in range(m)], n)

ans = 0
for A in ls:
    total = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            total += d
    ans = max(ans, total)
print(ans)

Solution (Python):
import itertools

n,m,q = map(int,input().split())
abcd = []
l = [i for i in range(1,m+1)]
h = itertools.combinations_with_replacement(l, n)
ans = 0

for i in range(q):
    abcd.append(list(map(int,input().split())))
for v in h:
    sum = 0
    for i in range(q):
        if v[abcd[i][1]-1] - v[abcd[i][0]-1] == abcd[i][2]:
            sum += abcd[i][3]
    ans = max(ans,sum)
print(ans)

Solution (Python):


from itertools import combinations_with_replacement as com

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

C = []
for q in range(Q):
  C.append(tuple(map(int, input().split())))

As = com([i for i in range(1, M+1)], N)

ans = 0
for A in As:
  point = 0
  for Ci in C:
    if A[Ci[1]-1] - A[Ci[0]-1] == Ci[2]:
      point += Ci[3]
  ans = max(ans, point)

print(ans)

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

from itertools import combinations_with_replacement as comb_rplc
cmb = comb_rplc(range(1,m+1),n)
result = 0
for A in cmb:
  tmp = 0
  for a,b,c,d in abcd:
    if A[b-1] - A[a-1] == c:
      tmp += d
  result = max(tmp, result)
print(result)

Solution (Python):
import itertools
n,m,q=map(int,input().split())
abcd=[tuple(map(int,input().split())) for _ in range(q)]
h=[]
for i in range(1,m+1):
    h.append(i)
ans=0
for j in itertools.combinations_with_replacement(h,n):
    temp=0
    for a,b,c,d in abcd:
        if j[b-1]-j[a-1]==c:
            temp+=d 
    ans=max(ans,temp)
print(ans)

Solution (Python):
import sys,itertools
input = sys.stdin.readline

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

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

for Ai in A:
    point = 0
    for a,b,c,d in abcd:
        if Ai[b-1] - Ai[a-1] == c:
            point += d
    ans= max(ans,point)
print(ans)

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

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

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
ans = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
  score = []
  for i in range(Q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    if A[b-1]-A[a-1] == c:
      score.append(d)
  ans = max(ans,sum(score))
print(ans)

Solution (Python):
import itertools
import numpy as np

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


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

Solution (Python):
import itertools
import numpy as np

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

score = []
ans = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
  for i in range(Q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    if A[b-1]-A[a-1] == c:
      score.append(d)
  if ans <= sum(score):
    ans = sum(score)
  score.clear()
print(ans)

Solution (Python):
import itertools
import numpy as np

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

d_list = []
sum_d = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
  for i in range(Q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    if A[b-1]-A[a-1] == c:
      d_list.append(d)
  if sum_d <= sum(d_list):
    sum_d = sum(d_list)
  d_list.clear()
print(sum_d)

Solution (Python):
import itertools
import numpy as np

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

d_list = []
sum_d = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
  for i in range(Q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    if A[b-1]-A[a-1] == c:
      d_list.append(abcd[i][3])
  if sum_d <= sum(d_list):
    sum_d = sum(d_list)
  d_list.clear()
print(sum_d)

Solution (Python):
import itertools
import numpy as np
d_list = []
sum_d = 0
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
ans = 0
A = [1 for i in range(N)]
for A in itertools.combinations_with_replacement(range(1, M+1), N):
  for i in range(Q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    if A[b-1]-A[a-1] == c:
      d_list.append(abcd[i][3])
  if sum_d <= sum(d_list):
    sum_d = sum(d_list)
  d_list.clear()
print(sum_d)

Solution (Python):
import itertools
import numpy as np
d_list = []
sum_d = 0
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
ans = 0
A = [1 for i in range(N)]
for A in itertools.combinations_with_replacement(range(1, M+1), N):
  for i in range(Q):
    if A[abcd[i][1]-1]-A[abcd[i][0]-1] == abcd[i][2]:
      d_list.append(abcd[i][3])
  if sum_d <= sum(d_list):
    sum_d = sum(d_list)
  d_list.clear()
print(sum_d)

Solution (Python):


class Combination_with_repetition():
    def getPattern(self, arr :list, r :int) -> list:
        self._selected=[]
        self._all_pattern=[]
        
        self._make(arr,r)
        return self._all_pattern
        
    def _make(self, arr :list, r :int):
        if r == 0:
            self._all_pattern.append(self._selected[:])
            return 
        
        for i in range(len(arr)):
            self._selected.append( arr[i] )
            self._make(arr[i:],r-1)
            
            self._selected.pop()  


N,M,Q = list(map(int, input().rstrip().split()))
abcd_list = [list(map(int, input().rstrip().split())) for i in range(Q)]


comb_rep = Combination_with_repetition()
pattern = comb_rep.getPattern( list(range(1, M+1)) , N)


max_sum = 0

for pat in pattern:
    tmp_sum = 0
    
    for a,b,c,d in abcd_list:
        if ( pat[b-1] - pat[a-1] ) == c:
            tmp_sum += d
    
    max_sum = max(max_sum, tmp_sum)

print(max_sum)

Solution (Python):
from copy import deepcopy
n, m, q = map(int, input().split())
tuples = [tuple(map(int, input().split())) for i in range(q)]
As = [[1]*n]
def next(A, i):
    if i == 0 and A[i] == m:
        return
    if A[i] == m:
        next(A, i-1)
    elif A[i] < m:
        if (i < n - 1 and A[i] < A[i+1]) or (i == n-1):
            A1 = deepcopy(A)
            A1[i] += 1
            As.append(A1)
            next(A1, i)
        if i > 0 and A[i] > A[i-1]:
            A2 = deepcopy(A)
            next(A2, i-1)
    elif i == 0:
        A1 = deepcopy(A)
        next(A1, n-1)
next([1]*(n), n-1)
def check(A):
    score = 0
    for a, b, c, d in tuples:
        if A[b-1] - A[a-1] == c:
            score += d
    return score
ans = 0
for A in As:
    score = check(A)
    ans = max(ans, score)
print (ans)

Solution (Python):




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


def rec(arr):
    if len(arr) == n:
        koho.append(arr)
    else:
        for i in range(arr[-1], m + 1):
            nex_arr = arr[:]
            nex_arr.append(i)
            rec(nex_arr)



for i in range(1, m + 1):
    rec([i])
ans = 0

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

for arr in koho:
    tmp = 0
    for abcd in qs:
        if arr[abcd[1] - 1] - arr[abcd[0] - 1] == abcd[2]:
            tmp += abcd[3]
    ans = max(ans, tmp)
print(ans)

Solution (Python):






def main():
    N,M,Q = map(int, input().split())
    pair = []
    for i in range(Q):
        pair += list(map(int, input().split())),

    def score(A):
        nonlocal pair
        sum = 0
        for a,b,c,d in pair:
            if A[b-1] - A[a-1] == c:
                sum += d
        return sum

    ans = 0
    A = [1]*N
    i = N-1
    while True:
        ans = max(ans, score(A))
        while i >= 0 and A[i] == M:
            i -= 1
        if i < 0:
            break
        A[i] += 1
        j = i
        while i <= N-1:
            A[i] = A[j]
            i += 1
        i = N-1
        if A == [M]*N:
            break
        
    print(ans)
main()

Solution (Python):
import itertools
n,m,q=map(int,input().split())
array=[list(map(int,input().split())) for _ in range(q)]
a=list(range(1,m+1))
A=[]
for i in itertools.combinations_with_replacement(a,n):
  A.append(list(i))
ans=[0]
for j in range(len(A)):
  AA=A[j]
  count=0
  for i in range(q):
    if AA[array[i][1]-1]-AA[array[i][0]-1]==array[i][2]:
      count+=array[i][3]
    else:
      pass
  ans.append(count)
print(max(ans))

Solution (Python):

import itertools

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


A_list = list(itertools.combinations_with_replacement(range(1, M+1), N))
ans = 0
for A in A_list:
  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)

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



import itertools
cnbr = []  
for i in range(m+1):
    for j in itertools.combinations_with_replacement(range(m), n):
        cnbr.append(j)


points = [0] * len(cnbr)

for _ in range(len(cnbr)):
    for a,b,c,d in query:
        if cnbr[_][b - 1] - cnbr[_][a - 1] == c:
            points[_] += d

print(max(points))

Solution (Python):
from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
list_ABCD = [ list(map(int,input().split(" "))) for _ in range(q)]
list_N = [i for i in range(1, m+1)]
ans = 0

for l in combinations_with_replacement(list_N, n):
    score = 0
    for a, b, c, d in list_ABCD:
        if l[b-1] - l[a-1] == c:
            score += d
    ans = max(ans, score)

print(ans)

Solution (Python):

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


N, M, Q = 0, 0, 0
A, B, C, D = [0], [0], [0], [0]
ans = 0

def dfs(i, l):
    global ans, N, M, Q, A, B, C, D
    if i >= N:
        tmp = 0
        for j in range(Q):
            if l[B[j]] - l[A[j]] == C[j]:
                tmp += D[j]
        ans = max(ans, tmp)
        return

    for j in range(1, M + 1):
        if l:
            if j >= l[-1]:
                l.append(j)
                dfs(i + 1, l)
                l.pop()
        else:
            l.append(j)
            dfs(i + 1, l)
            l.pop()


def solve():
    global ans, N, M, Q, A, B, C, D
    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())
        A[i] -= 1
        B[i] -= 1

    l = []
    dfs(0, l)
    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Solution (Python):


from itertools import combinations_with_replacement

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

res = []
for A in combinations_with_replacement(range(1,M+1), N):
    score = 0
    for i in range(Q):
        a,b,c,d = data[i]
        if A[b-1] - A[a-1] == c:
            score += d
    res.append(score)

print(max(res))

Solution (Python):
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from itertools import combinations_with_replacement
N,M,Q=MI()
A=[]
B=[]
C=[]
D=[]
ans=0
for i in range(Q):
  a,b,c,d=MI()
  a-=1
  b-=1
  A.append(a)
  B.append(b)
  C.append(c)
  D.append(d)

Num=list(range(1,M+1))


for j in list(combinations_with_replacement(Num,N)):
  tmp=0
  for i in range(Q):
    if j[B[i]]-j[A[i]]==C[i]:
      tmp+=D[i]
  ans=max(ans,tmp)  
print(ans)

Solution (Python):
import itertools
N, M, Q = map(int,input().split())
ans = 0
A = []
B = []
C = []
D = []
for _ in range(Q):
    a, b, c, d = map(int,input().split())
    A.append(a-1)
    B.append(b-1)
    C.append(c)
    D.append(d)

ans = 0
for seq in itertools.combinations_with_replacement(range(M),N):
    tmp = 0
    for i in range(Q):
        if seq[B[i]] - seq[A[i]] == C[i]:
            tmp += D[i]
    ans = max(ans,tmp)

print(ans)

Solution (Python):
(N,M,Q) = map(int,input().split())
Qs = []
for i in range(Q):
    Qs.append(list(map(int,input().split())))
A = [1]*N
score = 0
while(1):
    if A[0]>M:
        break
    current_score = 0
    for i in range(Q):
        if A[Qs[i][1]-1]-A[Qs[i][0]-1] == Qs[i][2]:
            current_score += Qs[i][3]
    score = max(score,current_score)
    for i in range(1,N):
        if A[i]== M:
            A[i-1] += 1
            for j in range(i,N):
                A[j] = A[i-1]
            break
        if i == N-1:
            A[N-1] += 1
print(score)

Solution (Python):
N, M, Q = map(int, input().split())
a = [0 for _ in range(Q)]
b = [0 for _ in range(Q)]
c = [0 for _ in range(Q)]
d = [0 for _ in range(Q)]
for k in range(Q):
  a[k], b[k], c[k], d[k] = map(int, input().split())
A = [1 for _ in range(N)]

def point(A):
  p = 0
  for ai, bi, ci, di in zip(a, b, c, d):
    if A[bi-1] - A[ai-1] == ci:
      p += di
  return p

def next(A):
  k = 1
  while k < N+1:
    if A[-k] < M:
      A[-k] += 1
      return A
      break
    else:
      for j in range(1, k+1):
        A[-j] = A[-k-1] + 1
      k += 1

ans = point([M for _ in range(N)])
while A[0] < M:
  ans = max(ans, point(A))
  A = next(A)
print(ans)

Solution (Python):
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
a,b,c,d = [],[],[],[]
for i in range(q):
  a.append(abcd[i][0])
  b.append(abcd[i][1])
  c.append(abcd[i][2])
  d.append(abcd[i][3])

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+1:
    return score(A)
  ans = 0
  for i in range(A[-1],m):
    A.append(i)
    ans = max(ans,dfs(A))
    A.pop()
  return ans

print(dfs([0]))

Solution (Python):
import itertools
n, m, q=map(int, input().split())

l=[[0]*4 for i in range(q)]

for i in range(q):
    l[i][0],l[i][1],l[i][2],l[i][3]=map(int,input().split())

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

mx=0
for mask in range(len(arr)):
    tpl=arr[mask]
    ans=0
    for i in range(q):
        if tpl[l[i][1]-1]-tpl[l[i][0]-1]==l[i][2]:
            ans+=l[i][3]
    mx=max(ans,mx)

print(mx)

Solution (Python):
import sys
from itertools import combinations_with_replacement as comb

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())

Max = 0

for A in comb(range(1, M + 1), N):
    score = 0
    for j in range(Q):
        if (A[b[j] - 1] - A[a[j] - 1]) == c[j]:
            score += d[j]

    Max = max(Max, score)

print(Max)

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

if __name__ == "__main__":
    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())
    ans = 0
    dfs([1])
    print(ans)

Solution (Python):

n,m,q=map(int,input().split())
al=[0]*q
bl=[0]*q
cl=[0]*q
dl=[0]*q
for i in range(q):
    al[i],bl[i],cl[i],dl[i]=map(int,input().split())

def score(A):
    res=0
    for ai,bi,ci,di in zip(al,bl,cl,dl):
        if A[bi-1]-A[ai-1]==ci:
            res+=di
    return res
scorelist=[]
def dfs(A):
    global scorelist
    lastone=A[-1] if len(A)>0 else 1
    if len(A)==n:
        scorelist.append(score(A))
        return 
    for v in range(lastone,m+1):
        A.append(v)
        dfs(A)
        A.pop()

dfs([])
print(max(scorelist))

Solution (Python):
n,m,q = map(int,input().split())
l = []
def dfs(a):
    if len(a)==n: l.append(a)
    else:
        for i in range(a[-1],m+1):
            dfs(a+[i])
for i in range(m):
    dfs([i+1])
Query = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for li in l:
    tmp = 0
    for a,b,c,d in Query:
        if li[b-1]-li[a-1]==c:
            tmp += d
    ans = max(ans,tmp)
print(ans)

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

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

ary = [1 for _ in range(N)]
ans = 0
while True:
  
  pt = 0
  for j in range(Q):
    rq = Rq[j]
    if ary[rq[1]-1]-ary[rq[0]-1] == rq[2]:
      pt += rq[3]
  ans = max(ans,pt)
  
  if ary[0] == M:
    break
  
  
  for i in range(-1, -N-1, -1):
    if ary[i] < M:
      ary[i] += 1
      tmp = ary[i]
      for l in range(i+1, 0):
        ary[l] = tmp
      break
      
print(ans)

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

abcd=[]
for i in range(Q):
  abcd.append(list(map(int,input().split())))
  abcd[i][0]-=1
  abcd[i][1]-=1

  
def calcScore(target):
  score=0
  for i in range(Q):
    if target[abcd[i][1]]-target[abcd[i][0]]==abcd[i][2]:
      score+=abcd[i][3]
  return score

def makeTarget():
  list=[1]*N
  list[-1]=0
  while True:
    list[-1]+=1
    for i in range(len(list)):
      if list[-i-1]>M:
        if len(list)==i+1:return
        list[-i-2]+=1
        list[-i-1]=0
    for i in range(len(list)-1):
      if list[i]>list[i+1]:list[i+1]=list[i]
    yield list.copy()

ans=0
for i in makeTarget():
  ans=max(ans,calcScore(i))
print(ans)

Solution (Python):
import itertools 
N, M, Q = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(Q)]
ans= 0
AA = [i for i in range(1, M+1)]
A = []
for i in sorted(list(itertools.combinations_with_replacement(AA, N))):
    A.append(i)

for i in A:
    result = 0
    for j in range(Q):
        if i[li[j][1]-1]-i[li[j][0]-1] == li[j][2]:
            result += li[j][3]
    ans = max(ans, result)

print(ans)

Solution (Python):
from itertools import combinations
N, M, Q = map(int, input().split())
A = []
for i in range(Q):
    A.append(list(map(int, input().split())))

ans = 0
for c in combinations(range(N+M-1), N):
    x = sorted(list(c))
    ans_new = 0
    for i in range(Q):
        if x[A[i][1]-1] - A[i][1] - x[A[i][0]-1] + A[i][0] == A[i][2]:
            ans_new += A[i][3]
    if ans_new > ans:
        ans = ans_new
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
s_l = []
for i in range(q):
    s = list(map(int, input().split()))
    s_l.append(s)

ans = 0
for v in combinations_with_replacement(range(1, m+1), n):
    cnt = 0
    for i in range(q):
        if v[s_l[i][1] - 1] - v[s_l[i][0] - 1] == s_l[i][2]:
            cnt += s_l[i][3]
    ans = max(ans, cnt)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
s_l = []
for i in range(q):
    s = list(map(int, input().split()))
    s_l.append(s)

A = []
for v in combinations_with_replacement(range(1,m+1), n):
    A.append(v)

ans = 0
for a in A:
    cnt = 0
    for i in range(q):
        if a[s_l[i][1] - 1] - a[s_l[i][0] - 1] == s_l[i][2]:
            cnt += s_l[i][3]
    ans = max(ans, cnt)

print(ans)

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

ans = 0
for iter in itertools.combinations_with_replacement(range(1,m+1),n):
    result = 0
    for i in range(q):
        a,b,c,d = abcd[i][0],abcd[i][1],abcd[i][2],abcd[i][3]
        if iter[b-1] - iter[a-1] == c:
            result += d
    ans = max(ans, result)

print(ans)

Solution (Python):
import itertools

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

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


list_tmp = []
for i in range(1, M + 1):
    list_tmp.append(i)

comb_A = list(itertools.combinations_with_replacement(list_tmp, N))


max_score = 0
for A in comb_A:
    tmp_score = 0
    for q in range(Q):
        A_a = A[abcd_list[q][0] - 1]
        A_b = A[abcd_list[q][1] - 1]

        if(A_b - A_a == abcd_list[q][2]):
            tmp_score += abcd_list[q][3]
        
    max_score = max(max_score, tmp_score)

print(max_score)

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


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([]))

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m, q = rm()
li = [rl() for _ in range(q)]
max_ = 0
for A in list(itertools.combinations_with_replacement(list(range(1, m+1)), n)):
    score = 0
    for abcd in li:
        a, b, c, d = abcd
        a -= 1
        b -= 1
        if A[b] - A[a] == c:
            score += d
    else:
        max_ = max(score, max_)
print(max_)

Solution (Python):
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()

from itertools import combinations_with_replacement
n,m,q=mi()
ll=[i for i in range(1,m+1)]

input_list=[]
for i in range(q):
  abcd=lmi()
  input_list.append(abcd)

ans=0
la=list(combinations_with_replacement(ll,n))
for i in la:
  suma=0
  
  for j in input_list:
    a=j[0]
    b=j[1]
    c=j[2]
    d=j[3]
    if (i[b-1]-i[a-1])==c:
      suma+=d
      
  ans=max(ans,suma)
  
print(ans)

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

AA = [[1]*n]

while True:
    for i in reversed(range(n)):
        if AA[-1][i] != m:
            break
    else:
        break
    tmp = copy.deepcopy(AA[-1])
    tmp[i:] = [tmp[i]+1]*(n-i)
    AA.append(tmp)

ans = 0
for i in range(len(AA)):
    pt = 0
    for j in range(q):
        if AA[i][b[j]-1] - AA[i][a[j]-1] == c[j]:
            pt = pt + d[j]
    ans = max(ans,pt)
    
print(ans)

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

Solution (Python):
import copy

N, M, Q = [int(_) for _ in input().split()]
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q

for i in range(Q):
    xs = [int(_) for _ in input().split()]
    a[i] = xs[0] - 1
    b[i] = xs[1] - 1
    c[i] = xs[2]
    d[i] = xs[3]


def calc_score(A):
    acc = 0
    for i in range(Q):
        if c[i] == A[b[i]] - A[a[i]]:
            acc += d[i]
    return acc


max_score = -1


def dfs(A):
    if len(A) == N:
        global max_score
        max_score = max(calc_score(A), max_score)
    else:
        if len(A) == 0:
            A.append(1)
        else:
            A.append(A[-1])
        while (A[-1] <= M):
            dfs(copy.deepcopy(A))
            A[-1] += 1


dfs([])
print(max_score)

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

a_opt = []
for ni in range(n):
    a_opt_next = []
    if len(a_opt) == 0:
        for mi in range(1, m + 1):
            a_next = [mi]
            a_opt_next.append(a_next)
    else:
        for a in a_opt:
            for mi in range(a[-1], m + 1):
                a_next = a + [mi]
                a_opt_next.append(a_next)
    a_opt = a_opt_next[:]

ans = 0
for a in a_opt:
    sumd = 0
    for num_set in num_sets:
        if a[num_set[1] - 1] - a[num_set[0] - 1] == num_set[2]:
            sumd += num_set[3]
    ans = max(ans, sumd)

print(ans)

Solution (Python):

import itertools
import numpy as np

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

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

def make_group(now, n, i):
    if n == 0:
        groups.append(now)
    else:
        for j in range(i, M+1): 
            make_group(now+[j], n-1, j)

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())

groups = []
now = []
make_group(now, N, 1)

ans = 0
for group in groups:
    tmp = 0
    for a, b, c, d in zip(A, B, C, D):
        if group[b-1]-group[a-1] == c:
            tmp += d
    ans = max(ans, tmp)
print(ans)

Solution (Python):
n,m,q = map(int,input().split())
pairs = []
def makelen(a):
    if len(a) == n:
        pairs.append(a)
    else:
        for i in range(a[-1],m+1):
            makelen(a + [i])
for i in range(m):
    makelen([i+1])
Query = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for pair in pairs:
    tmpans = 0
    for a,b,c,d in Query:
        if pair[b-1] - pair[a-1] == c:
            tmpans += d
    ans = max(tmpans,ans)
print(ans)

Solution (Python):
n,m,q = map(int,input().split())
pairs = []
def makelen(a):
    if len(a) == n:
        pairs.append(a)
    else:
        for i in range(a[-1],m+1):
            makelen(a + [i])
for i in range(m):
    makelen([i+1])
Query = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for pair in pairs:
    tmpans = 0
    for a,b,c,d in Query:
        if pair[b-1] - pair[a-1] == c:
            tmpans += d
    ans = max(tmpans,ans)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n, m, q = map(int, input().split())
si, res = [1] + [0 for i in range(55)], 0
a, b, c, d = [0 for i in range(55)], [0 for i in range(55)], [0 for i in range(55)], [0 for i in range(55)]
for i in range(1, q+1): a[i], b[i], c[i], d[i] = map(int, input().split())

def solve(x: int):
    global res
    if x == n + 1: res = max(res, sum([d[i] for i in range(1, q+1) if si[b[i]] - si[a[i]] == c[i]]))
    else:
        for i in range(si[x-1], m + 1): si[x] = i; solve(x+1)
solve(1); print(res)