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
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n,m,q=l_in()

abcd=[l_in() for _ in range(q)]

s = []


def check(s):
    return sum(d for a,b,c,d in abcd if s[b-1] - s[a-1] == c)

def search(s, current):
    tmp = 0
    for d in range(current, m+1):
        s.append(d)
        if len(s) == n:
            tmp = max(tmp, check(s))
        else:
            tmp = max(tmp, search(s, d))
        s.pop()
        
    return tmp

print(search(s,1))

Solution (Python):
import itertools
n,m,q=map(int,input().split())
lis=[i for i  in range(1,m+1)]
lis=list(itertools.combinations_with_replacement(lis,n))
box=[0 for j in range(len(lis))]
for i in range(q):
    que=list(map(int,input().split()))
    for j in range(len(lis)):
        if lis[j][que[1]-1]-lis[j][que[0]-1]==que[2]:
            box[j]+=que[3] 
print(max(box))

Solution (Python):
import numpy as np
import itertools


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

vals = []
for i in range(Q):
    vals.append(list(map(int, input().split(" "))))
    
maxv = 0
cand = list(itertools.combinations_with_replacement(range(1, M+1), N))
for c in cand:
    sumv = 0
    for v in vals:
        if (c[v[1]-1] - c[v[0]-1]) == v[2]:
            sumv += v[3]
    if maxv < sumv:
        maxv = sumv
print(maxv)

Solution (Python):
import sys

sys.setrecursionlimit(10**6)

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

def search(lis):
    global n,m,score
    if len(lis)==n:
        total = 0
        for s in score:
            if lis[s[1]-1]-lis[s[0]-1]==s[2]: total += s[3]
        return total
        
    first = 1 if len(lis) == 0 else lis[-1]
    res = []
    for i in range(first, m+1):
        nlis = lis + [i]
        res.append(search(nlis))
    return max(res)

print(search([]))

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


l = []
for j in range(1, m+1):
    l.append(j)
ans = 0
for a in itertools.combinations_with_replacement(l, n):
    
    
    score = 0
    for i in range(q):
        if a[LIST[i][1]-1]-a[LIST[i][0]-1] == LIST[i][2]:
            score += LIST[i][3]
    ans = max(score, ans)
print(ans)

Solution (Python):
n,m,q = map(int,input().split())
u = []
for i in range(q):
    u.append(list(map(int,input().split())))
"""
a = [i[0] for i in u]
b = [i[1] for i in u]
c = [i[2] for i in u]
d = [i[3] for i in u]
"""
z = []
import itertools
a = itertools.combinations_with_replacement([i for i in range(1,m+1)],n)
for e in a:
    sum = 0
    for i in range(q):
        if e[u[i][1]-1]-e[u[i][0]-1] == u[i][2]:
            sum += u[i][3]
    z.append(sum)

print(max(z))

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 = [1] * 10

def check(A, ans):
  t = 0
  for i in range(q):
    if A[b[i]-1] - A[a[i]-1] == c[i]:
      t += d[i]
  if ans < t:
    return t
  else:
    return ans

ans = 0
for i1 in range(1, m+1):
  A[1] = i1
  if n >= 3:
    for i2 in range(i1, m+1):
      A[2] = i2
      if n >= 4:
        for i3 in range(i2, m+1):
          A[3] = i3
          if n >= 5:
            for i4 in range(i3, m+1):
              A[4] = i4
              if n >= 6:
                for i5 in range(i4, m+1):
                  A[5] = i5
                  if n >= 7:
                    for i6 in range(i5, m+1):
                      A[6] = i6
                      if n >= 8:
                        for i7 in range(i6, m+1):
                          A[7] = i7
                          if n >= 9:
                            for i8 in range(i7, m+1):
                              A[8] = i8
                              if n >= 10:
                                for i9 in range(i8, m+1):
                                  A[9] = i9
                                  ans = check(A, ans)
                              else:
                                ans = check(A, ans)
                          else:
                            ans = check(A, ans)
                      else:
                        ans = check(A, ans)
                  else:
                    ans = check(A, ans)
              else:
                ans = check(A, ans)
          else:
            ans = check(A, ans)
      else:
        ans = check(A, ans)
  else:
    ans = check(A, ans)
print(ans)

Solution (Python):
import copy
n,m,q=map(int,input().split())
Q=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    Q.append([a-1,b-1,c,d])    
B=[]
def dfs(A,l):
    if l==n:
        B.append(A)
    else:
        for i in range(A[-1],m+1):
            nA=copy.copy(A)
            nA.append(i)
            dfs(nA,l+1)
dfs([1],1)

ans=0
for ls in B:
    cnt=0
    for que in Q:
        if ls[que[1]]-ls[que[0]]==que[2]:
            cnt+=que[3]
    ans=max(cnt,ans)
print(ans)

Solution (Python):
import sys,collections as cl,bisect as bs,itertools
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

N,M,Q = m()

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

data = []

ma = 0

for i in range(Q):
    data.append(l())
    
for i in dp:
    po = i
    te = 0
    for j in range(len(data)):
        a,b,c,d = data[j]
        if po[b-1]-po[a-1] == c:
            te += d
    ma = max(ma,te)
print(ma)

Solution (Python):
import itertools
def main():
    N, M, Q = map(int, input().split())
    query = [[int(a) for a in input().split()] for i in range(Q)]
    st = ""
    for i in range(M):
        st += str(i)

    total = 0
    for comb in itertools.combinations_with_replacement(st, N):
        A = [ int(a)+1 for a in comb]
        s = 0
        for a, b, c, d in query:
            if A[b-1] - A[a-1] == c:
                s += d
        
        if total < s:
            total = s

    print(total)

if __name__ == "__main__":
    main()

Solution (Python):
from itertools import combinations_with_replacement

def main():
  N, M, Q = map(int, input().split())
  abcd = [list(map(int, input().split())) for x in range(Q)]
  ans = 0
  for x in combinations_with_replacement(range(1, M+1), N):
    r = 0
    for a,b,c,d in abcd:
      if x[b-1] - x[a-1] == c:
        r += d
    ans = max(ans,r)
  print(ans)

if __name__ == '__main__':
    main()

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

nums_list = []
for i in range(q):
    nums = list(map(int, input().split()))
    nums_list.append(nums)

s_max = 0
for l in list(itertools.combinations_with_replacement(range(1, m+1), n)):
    score = 0
    for nums in nums_list:
        if l[nums[1]-1] - l[nums[0]-1] == nums[2]:
            score += nums[3]
    if score > s_max:
        s_max = score

print(s_max)

Solution (Python):
from itertools import combinations_with_replacement

def readlines(n):
  for _ in range(n):
    a,b,c,d = map(int,input().split())
    yield a,b,c,d
    
def main():
  N,M,Q = map(int, input().split())
  X = list(readlines(Q))
  
  for comb in combinations_with_replacement(range(1,M+1),N):

    x = 0
    for a,b,c,d in X:
      if comb[b-1] - comb[a-1] == c:
        x += d
    yield x

print(max(main()))

Solution (Python):



n,m,q=map(int, input().split( ))
qs=tuple(tuple(map(int, input().split( ))) for _ in range(q))
import itertools
ans=0
for comb in itertools.combinations(range(1,n+m),n):
    tmp=0
    comb2=[comb[i]-i for i in range(n)]
    for ai,bi,ci,di in qs:
        if comb2[bi-1]-comb2[ai-1]==ci:
            tmp+=di 
    ans=max(ans,tmp)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement
n,m,q = map(int,input().split())
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
for i in range(q):
  a[i],b[i],c[i],d[i] = map(int,input().split())
  
ans = 0      
def dfs(A):
  global ans
  if len(A) >= n:    
    tmp = 0
    for i in range(q):
      if A[b[i]-1] - A[a[i]-1] == c[i]:
        tmp += d[i]
    ans = max(ans,tmp)
  else:
    s = 1
    if len(A)>0:
      s = A[-1]    
    for i in range(s-1,m):
      dfs(A+[i+1])

A = []
dfs(A) 
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()
 
import itertools

n,m,q = map(int,input().split())
all_num = [i for i in range(1, m+1)]
M = []
ans = []
for _ in range(q):
    a,b,c,d = map(int,input().split())
    M.append([a,b,c,d])


ans = 0
for com in list(itertools.combinations_with_replacement(all_num, n)):
    score = 0
    for m in M:
        if com[m[1]-1]-com[m[0]-1] == m[2]:
            score += m[3]
    
    ans = max(ans,score)

print(ans)

Solution (Python):
import itertools

def main():
    n,m,q = tuple([int(t)for t in input().split()])

    a = [[int(t)for t in input().split()]for _ in [0]*q]
    
    ans = []
    for lst in itertools.combinations_with_replacement(range(1,m+1),n):
        t = 0
        for a_i,b_i,c_i,d_i in a:
            t +=  d_i if lst[b_i-1]-lst[a_i-1]==c_i else 0
        ans.append(t)
    
    print(max(ans))


if __name__ == "__main__":
    main()

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

Solution (Python):
import itertools

def main():
    n,m,q = map(int,input().split())
    a = [0] * (q+1)
    b = [0] * (q+1)
    c = [0] * (q+1)
    d = [0] * (q+1)

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

    Alist = itertools.combinations_with_replacement(range(1,m+1),n)

    ans = 0
    for A in Alist:
        cnt = 0
        for i in range(1,q+1):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                cnt += d[i]
        ans = max(ans,cnt)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]

N,M,Q = inputlist()
l = [0]*Q
for i in range(Q):
    l[i] = inputlist()
li = [int(i) for i in range(1,M+1)]
from itertools import combinations_with_replacement
h = combinations_with_replacement(li,N)
ans = 0
for v in h:
    va = list(v)
    tmp = 0
    for i in range(Q):
        a,b,c,d = l[i]
        a-=1
        b-=1
        if va[b] - va[a] == c:
            tmp += d
    ans = max(tmp,ans)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement as combr

ii = lambda:int(input())
li = lambda:list(map(int,input().split()))

def main(n,m,q,l):
    res = 0
    for item in combr(range(1,m+1),n):
        res = max(res, calc(item, l))
    return res

def calc(item,l):
    res = 0
    for i in l:
        a,b,c,d = i[0],i[1],i[2],i[3]
        if item[b-1] - item[a-1] == c:
            res += d
    return res

n,m,q = li()
l = list(li() for _ in range(q))

print(main(n,m,q,l))

Solution (Python):
import itertools
 
N, M, Q = map(int, input().split())
cmb = [list(map(int, input().split())) for _ in range(Q)]
 
def score(seq):
  s = 0
  for q in range(Q):
    if seq[cmb[q][1]-1] - seq[cmb[q][0]-1] == cmb[q][2]:
      s += cmb[q][3]
  return s
 
p = [i for i in range(1, M+1)]
A = itertools.combinations_with_replacement(p, N)
ans = 0
for a in A:
  ans = max(ans, score(a))
print(ans)

Solution (Python):
import itertools

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


nums=[i for i in range(1,M+1)]
count=0
inp=[]
for i in range(Q):
    I=list(map(int,input().split()))
    inp.append(I)

for v in itertools.combinations_with_replacement(nums, N):
    c=0
    v=list(v)
    for i in inp:

        if v[i[1]-1] - v[i[0]-1]==i[2]:
            c+=i[3]
    count=max(count,c)
print(count)

Solution (Python):
import itertools

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

ans = 0
for c in itertools.combinations_with_replacement(range(1,M+1),N):
        tmp = 0
        for q in abcd:
            if c[q[1]-1] == c[q[0]-1] + q[2]:
                tmp += q[3]
            ans = max(ans,tmp)

print(ans)

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

max_ans = 0
for mid in itertools.combinations_with_replacement(range(1,m+1), n):
    sum_ans=0
    for q in qs:
        if mid[q[1]-1]-mid[q[0]-1] == q[2]:
            sum_ans += q[3]
    if sum_ans > max_ans:
        max_ans = sum_ans
        
print(max_ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement


def getPoint(data, A, q):
    res = 0
    for i in range(q):
        a, b, c, d = data[i]
        if A[b]-A[a] == c:
            res += d
    return res

def main():
    n, m, q = map(int, readline().split())    
    data = []
    for _ in range(q):
        a, b, c, d = map(int, readline().split())
        a-=1
        b-=1
        data.append((a, b, c, d))
    p = [int(i) for i in range(1, m+1)]
    ans = 0
    for A in combinations_with_replacement(p, n):
        ans = max(getPoint(data, A, q), ans)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import itertools
def main():
    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 = list(itertools.combinations_with_replacement([i for i in range(1,M+1)], N))
    D = []
    for f in A:
        e=0
        for i in range(Q):
            if f[b[i]-1]-f[a[i]-1] == c[i]:
                e = e + d[i]
        D.append(e)
    print(max(D))
main()

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

Solution (Python):
def dfs(l, A):
    for i, val in enumerate(l):
        A.append(val)
        if len(A) == N:
            As.append(A)
            A = A.copy()
            A.pop()
        else:
            sub_l = l[i:]
            A = dfs(sub_l, A)
            A.pop()
    return A


N, M, Q = map(int, input().split())
ls = []
for _ in range(Q):
    a, b, c, d = map(int, input().split())
    ls.append([a, b, c, d])

l = list(range(1, M + 1))
As = []
A = []
_ = dfs(l, A)

ans = 0
for A in As:
    
    score = 0
    for i, l in enumerate(ls):
        
        if A[l[1] - 1] - A[l[0] - 1] == l[2]:
            score += l[3]
    if score >= ans:
        ans = score

print(ans)

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

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(Q)]
ans = 0
for i in itertools.combinations_with_replacement(range(1,M+1),N):
    tmp = 0
    for item in arr:
        if i[item[1]-1] - i[item[0]-1] == item[2]:
            tmp += item[3]
    ans = max(ans,tmp)
print(ans)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
abcd_lis = []
for i in range(Q):
	abcd_lis.append(list(map(int, input().split())))
lis = [i+1 for i in range(M)]

lis = list(itertools.combinations_with_replacement(lis, N))
maxi = 0
for i in lis:
  count = 0
  for j in range(Q):
    idx1 = abcd_lis[j][0]-1
    idx2 = abcd_lis[j][1]-1
    if i[idx2]-i[idx1] == abcd_lis[j][2]:
      count += abcd_lis[j][3]
  if maxi < count:
    maxi = count
print(maxi)

Solution (Python):
import itertools
def generateA(N, M, minv=1):
    if N < 0:
        return
    if N == 0:
        yield []
    if N == 1:
        for i in range(minv, M):
            yield [i]
    for v in range(minv, M+1):
        for subA in generateA(N-1, M, v):
            yield [v] + subA

def point(A, L):
    result = 0
    for (a, b, c, d) in L:
        if A[b-1] - A[a-1] == c:
            result += d

    return result

def summary(N, M, L):
    As = generateA(N, M)

    results = []
    for A in As:
        results.append(point(A,L))
    return results

def test():
    As = generateA(3,4)
    for A in As:
        print(A)

def main():
    
    N, M ,Q = map(int, input().split())
    L = []
    for i in range(Q):
        a, b, c, d = map(int, input().split())
        L.append((a,b,c,d))
    results = summary(N,M,L)

    print(max(results))

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
abcd_lis = []
for i in range(Q):
	abcd_lis.append(list(map(int, input().split())))
lis = [i+1 for i in range(M)]

lis = list(itertools.combinations_with_replacement(lis, N))
maxi = 0
for i in lis:
  count = 0
  for j in range(Q):
    idx1 = abcd_lis[j][0]-1
    idx2 = abcd_lis[j][1]-1
    if i[idx2]-i[idx1] == abcd_lis[j][2]:
      count += abcd_lis[j][3]
  if maxi < count:
    maxi = count
print(maxi)

Solution (Python):

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

def dfs(v,pre):
    global ans
    if len(v) >= n:
        cnt = 0
        for a,b,c,d in abcd:
            if (v[b-1] - v[a-1] == c):
                cnt += d
        ans = max(ans, cnt)
        return 

    for i in range(pre, m+1):
        v.append(i);
        dfs(v, i)
        v.pop(-1)


v = [1]
dfs(v,1)
print(ans)

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

def score(lists):
    tmp = 0
    for i in T:
        if lists[i[1] - 1] - lists[i[0] - 1] == i[2]:
            tmp += i[3]
    return tmp
        
def make_lists(lists=[], s=0, ans=0, times=0):
    if times == N:
        return score(lists)

    for i in range(s, M):
        ans = max(ans, make_lists(lists + [i], i, ans, times + 1))
    return ans
        
print(make_lists())

Solution (Python):
import itertools

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

def score(seq):
  s = 0
  for q in range(Q):
    if seq[cmb[q][1]-1] - seq[cmb[q][0]-1] == cmb[q][2]:
      s += cmb[q][3]
  return s

p = [i for i in range(1, M+1)]
A = itertools.combinations_with_replacement(p, N)
ans = 0
for a in A:
  ans = max(ans, score(a))
print(ans)

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]

N,M,Q = inputlist()
l = [0]*Q
for i in range(Q):
    l[i] = inputlist()
li = [int(i) for i in range(1,M+1)]
from itertools import combinations_with_replacement
h = combinations_with_replacement(li,N)
ans = 0
for v in h:
    va = list(v)
    tmp = 0
    for i in range(Q):
        a,b,c,d = l[i]
        a-=1
        b-=1
        if va[b] - va[a] == c:
            tmp += d
    ans = max(tmp,ans)
print(ans)

Solution (Python):
from itertools import combinations_with_replacement


N, M, Q = map(int, input().split())
nums = [list(map(int, input().split())) for _ in range(Q)]
A = list(combinations_with_replacement(range(1, M+1), N))
ans = 0
for i in range(len(A)):
    tmp = 0
    for j in range(Q):
        if A[i][nums[j][1]-1] - A[i][nums[j][0]-1] == nums[j][2]:
            tmp += nums[j][3]
    ans = max(tmp, ans)
print(ans)

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

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

for v in itertools.combinations_with_replacement(l, n):
    point=0
    
    for j in ary:
        if v[j[1]-1]-v[j[0]-1]==j[2]:
            point+=j[3]
    if point_max < point:
        point_max = point
print(point_max)

Solution (Python):

from itertools import combinations_with_replacement


n, m, q = map(int, input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0]*q
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
    
numbers = range(1, m+1)
arrays = list(combinations_with_replacement(numbers, n))
ans = []


for i in range(len(arrays)):
    dm = 0
    l = arrays[i]
    for j in range(q):
        if l[b[j]-1] - l[a[j]-1] == c[j]:
            dm = dm+d[j]
    ans.append(dm)
    
print(max(ans))

Solution (Python):
import itertools

n,m,q = input().split()
n = int(n)
m = int(m)
q = int(q)
num_list = []
a = list(range(1, n+1))
for i in range(q):
    num_list.append(list(map(int,input().split())))
ff = list(itertools.combinations_with_replacement(range(1, m + 1), n))
max = 0
for i in range(len(ff)):
  s = 0
  for j in range(q):
    a,b,c,d = num_list[j]
    if ff[i][b-1] - ff[i][a-1] == c:
      s = s + d
  if max < s:
        max = s
print(max)

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 com
ans = 0
for A in com(range(1, M+1), N):
    score = 0
    for a, b, c, d in abcd:
        if A[b-1] - A[a-1] == c:
            score += d
    if ans < score:
        ans = score
print(ans)

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())
import itertools
ans = 0
for seq in itertools.combinations_with_replacement(range(1,M+1),N):
    score = 0
    for i in range(Q):
        if seq[b[i]-1] - seq[a[i]-1] == c[i]:
            score += d[i]
    ans = max(ans,score)
print(ans)

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

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

def search(A, req_head):
    if req_head >= len(reqs):
        return 0

    max_score = 0
    for e in range(A[-1], M+1):
        score = 0
        i = req_head
        while i < len(reqs) and reqs[i][1] < len(A):
            i += 1
        while i < len(reqs) and reqs[i][1] == len(A):
            a, b, c, d = reqs[i]
            if e - A[a] == c:
                score += d
            i += 1
        score += search(A + [e], i)
        max_score = max(max_score, score)

    return max_score

print(search([None, 1], 0))

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

bar_m = [i for i in range(m)]
bar_l = list(itertools.combinations_with_replacement(bar_m,n))
score = 0
for bar in bar_l:
    score_tmp=0
    for queue in q_l:
        if bar[queue[1]-1]-bar[queue[0]-1]==queue[2]:
            score_tmp+=queue[3]
    
    if score<=score_tmp:
        score=score_tmp
        
print(score)

Solution (Python):
N,M,Q = map(int,input().split(" "))
rule_list = []
for i in range(Q):
        rule_list.append(list(map(int,input().split(" "))))
basic_list = [0]*N
pattern_list = [basic_list]
def mklist(temp,temp2):
    global pattern_list
    global basic_list
    if temp>0:
        for j in range(temp2,N):
            basic_list[j] +=1
            pattern_list.append([*basic_list])
            mklist(temp-1,j)
            basic_list[j] -=1

temp = M-1
mklist(temp,0)
max_d = 0
for i in pattern_list:
    sum_d = 0
    for j in rule_list:
        a = j[0]
        b = j[1]
        c = j[2]
        d = j[3]
        dis = 0
        for k in range(a-1,b-1):
            dis += i[k]
        if dis == c:
            sum_d += d
    max_d = max(max_d,sum_d)
print(max_d)

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

n,m,q = map(int,input().split())
seq = list(range(n+m-1))
p = list(itertools.combinations(seq,n))

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

res = 0
for i in range(len(p)):
    t = [0]*n
    pp = sorted(p[i])
    idx = 0
    for e in pp:
        t[idx] = 1+e-idx
        idx += 1
    
    tmp = 0
    for j in range(q):
        if t[b[j]-1] - t[a[j]-1] == c[j]:
            tmp += d[j]
    
    res = max(res,tmp)

print(res)