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):
from itertools import combinations_with_replacement

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], b[i] = a[i]-1, b[i]-1

ans = 0
for A in combinations_with_replacement(range(M), N):
  tmp = 0
  for i in range(Q):
    if A[b[i]] - A[a[i]] == c[i]:
      tmp += d[i]
  ans = max(ans, tmp)

print(ans)

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

max_ans = 0


def dfs(s):
    global max_ans
    if len(s) < N:
        for i in range(s[-1], M + 1):
            dfs(s + [i])
    else:
        ans = 0
        for a, b, c, d in reqs:
            a -= 1
            b -= 1
            if s[b] - s[a] == c:
                ans += d
        max_ans = max(max_ans, ans)


for j in range(M):
    dfs([j + 1])
print(max_ans)

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

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

def dfs(A, max_a):
    if len(A) == N:
        num = 0
        for a, b, c, d in conditions:
            if A[b-1] - A[a-1] == c:
                num += d
        return num
    ans = 0
    for i in range(max_a, M+1):
        ans = max(ans, dfs(A + [i], i))
    return ans

print(dfs([1], 1))

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

ls = list(range(n + m - 1))
mx = 0
for l in itertools.combinations(ls, m - 1):
    a = [0]
    idx = 0
    val = 1
    for i in range(n + m - 1): 
        if idx < m - 1 and i == l[idx]:
            val += 1
            idx += 1
        else: 
            a.append(val)
    
    v = 0
    for i in query:
        if a[i[1]] - a[i[0]] == i[2]: v += i[3]
    mx = max(v, mx)
print(mx)

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

from collections import namedtuple
Val = namedtuple('val', ('a', 'b', 'c', 'd'))
vals = []
for _ in range(q):
    a,b,c,d = map(int, input().split())
    vals.append(Val(a, b, c, d))

import itertools

l = range(1, m+1)
combis = list(itertools.combinations_with_replacement(l, n))

totals = [(0, 0)]
for combi in list(combis):

    total = 0
    combi1 = sorted(combi)
    for val in vals:
        cDiff = combi1[val.b - 1] - combi1[val.a - 1]
        if cDiff == val.c:
            total += val.d

    totals.append((total, combi1))

maxT = max(totals, key= lambda x: x[0])

print(maxT[0])

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

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

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

max_point = 0
for t in all_lists:
  point = 0
  for p in point_list:
    a, b, c, d = p
    if t[ b - 1 ] - t[ a - 1 ] == c:
      point += d
  max_point = max( max_point, point )
print( max_point )

Solution (Python):
n,m,q=list(map(int, input().split()))
conditions=[]
for i in range(q):
    conditions.append(list(map(int, input().split())))
max=0
for i0 in range(1,m+1):
    for i1 in range(i0, m+1):
        for i2 in range(i1, m+1):
            for i3 in range(i2, m+1):
                for i4 in range(i3, m+1):
                    for i5 in range(i4, m+1):
                        for i6 in range(i5, m+1):
                            for i7 in range(i6, m+1):
                                for i8 in range(i7, m+1):
                                    for i9 in range(i8, m+1):
                                        a=[i0,i1,i2,i3,i4,i5,i6,i7,i8,i9]
                                        score=0
                                        for condition in conditions:
                                            if(a[condition[1]-1]-a[condition[0]-1]==condition[2]):
                                                score+=condition[3]
                                        if score>max:
                                            max=score

print(max)

Solution (Python):
import itertools
N, M, Q = map(int, input().split())
A = []
for i in range(0, Q):
        a, b, c, d = map(int,input().split())
        A.append([a,b,c,d])

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

ans_l = [0]
for A_n in itertools.combinations_with_replacement(nums, N):
        
        ans = 0
        for i in range(0, Q):
                if A_n[(A[i][1] - 1)] - A_n[(A[i][0] - 1)] == A[i][2]:
                        
                        ans += A[i][3]
        ans_l.append(ans)


print(max(ans_l))

Solution (Python):
from collections import defaultdict

import itertools

n, m, q = map(int, input().split())
ABCD = [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:
  prs = 0
  for a, b, c, d in ABCD:
    if i[b - 1] - i[a - 1] == c:
      prs += d
  ans = max(ans, prs) 

print(ans)

Solution (Python):
N,M,Q=map(int,input().split(' '))
abcd = [list(map(int,input().split(' '))) for i in range(Q)]
def bfs(n,l):
    if n==N:
        res = 0
        for i in abcd:
            a,b,c,d=i
            if l[b-1]-l[a-1]==c:
                res += d
        return res
    else:
        return max([bfs(n+1,l+[i]) for i in range(l[-1],M+1)])
ans = 0
for i in range(1,M+1):
    ans = max(ans,bfs(1,[i]))
print(ans)

Solution (Python):
import itertools

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

comb = list(itertools.combinations_with_replacement(range(1, m+1), n))
sum_d = 0
for c in comb:
    tmp = 0
    for a in abcd:
        if(c[a[1]-1] - c[a[0]-1] == a[2]):
            tmp += a[3]
    sum_d = max(tmp, sum_d)

print(sum_d)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import combinations_with_replacement

n, m, q = map(int, readline().split())
abcd = [list(map(int, readline().split())) for _ in range(q)]
ans = 0
for seq in combinations_with_replacement(range(m), n):
    cnt = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            cnt += d
    ans = max(ans, cnt)
print(ans)

Solution (Python):
import sys
from itertools import combinations_with_replacement

read = sys.stdin.read
readline = sys.stdin.readline

N, M, Q, *abcd = map(int, read().split())

answer = 0
abcd = tuple(zip(*[iter(abcd)] * 4))
for seq in combinations_with_replacement(range(1, M + 1), N):
    t = 0
    for a, b, c, d in abcd:
        if seq[b - 1] - seq[a - 1] == c:
            t += d
    answer = max(answer, t)

print(answer)

Solution (Python):



import sys
import itertools

def input():
    return sys.stdin.readline()


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

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

    ans_dic = {}
    for combi in combi_list:
        ans_dic[str(combi)] = 0

    for combi in combi_list:

        for i, a_d in enumerate(a_d_list):
            a = a_d[0]
            b = a_d[1]
            c = a_d[2]
            d = a_d[3]

            if combi[b-1] - combi[a-1] == c:

                ans_dic[str(combi)] += d


    print(max(ans_dic.values()))





if __name__ == "__main__":
    resolve()

Solution (Python):
import itertools
def main():
    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])
    A = [i for i in range(1,M+1)]
    
    
    
    
    
    
    
    
    
    aa = list(itertools.combinations_with_replacement(A,N))
    ans = 0

    for i, cc in enumerate(aa):
        ans1 = 0
        for j, qq in enumerate(q):
            if cc[qq[1]] - cc[qq[0]] == qq[2]:
                ans1 += qq[3]
            
        ans = max(ans,ans1)
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import itertools

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

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

saidai = 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]
    saidai = max(saidai, score)

print(saidai)

Solution (Python):
import itertools
n,m,q=map(int,input().split())
A,B,C,D=[],[],[],[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)
ans=0
 
L=[i+1 for i in range(m)]
for l in itertools.combinations_with_replacement(L, n):

    s=0
    for j in range(q):
        if int(l[B[j]-1])-int(l[A[j]-1])==C[j]:
            s+=D[j]
    ans=max(ans,s)
    if ans==sum(D):
        print(ans)
        exit()
print(ans)

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

all_sequence_list = list()
score_list = list()
for sequence in itertools.combinations_with_replacement(m_list, N):
    
    score_tmp = 0
    for ai, bi, ci, di in int_tuple_list:
        if ci == sequence[bi - 1] - sequence[ai - 1]:
            score_tmp += di
 
    
    score_list.append(score_tmp)
 
print(max(score_list))

Solution (Python):
import sys
import itertools


sys.setrecursionlimit(10 ** 7)

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

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


ans = 0
for lst in itertools.combinations_with_replacement(range(1, M + 1), N):
    s = 0
    for a, b, c, d in ABCD:
        if (lst[b - 1] - lst[a - 1]) == c:
            s += d

    ans = max(ans, s)

print(ans)

Solution (Python):
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
import itertools

N, M, Q = MAP()
a, b, c, d = [], [], [], []
for _ in range(Q):
    ai, bi, ci, di = MAP()
    a.append(ai-1); b.append(bi-1); c.append(ci); d.append(di)


def solve(lst):
    ans = 0
    for i in range(Q):
        if lst[b[i]] - lst[a[i]] == c[i]:
            ans += d[i]
    return ans

ans = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
    ans = max(ans, solve(A))

print(ans)

Solution (Python):
from collections import defaultdict
from math import gcd
import time
import sys
from collections import Counter
import itertools
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
start = time.time()


N, M, Q = map(int, input().split())
big = 0
s = 2**N
l = []

moto = list(range(1, Q+1))

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

A = []
for i in range(N):
    AA = []
    if A:
        for k in A:
            for j in range(1, M+1):
                if k[-1] <= j:
                    AA.append(k+[j])
        A = AA
    else:
        for j in range(1, M+1):
            A.append([j])

for i in A:
    wa = 0
    for j in l:
        if (i[j[1]-1]-i[j[0]-1]) == j[2]:
            wa += j[3]

    if big < wa:
        big = wa
print(big)

Solution (Python):
from itertools import combinations_with_replacement


def main():
    N, M, Q = list(map(int, input().split(' ')))
    constraint_list = list()
    for _ in range(Q):
        constraint_list.append(list(map(int, input().split(' '))))
    ans = 0
    for A in combinations_with_replacement(range(1, M + 1), N):
        score = 0
        for constraint in constraint_list:
            a, b, c, d = constraint
            if A[b - 1] - A[a - 1] == c:
                score += d
        ans = max([ans, score])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

def main():
    n,m,q=map(int,input().split())
    
    a = [ [int(x) for x in input().split()] for _ in range(q) ]
    
    ans=0

    import itertools
    for b in itertools.combinations_with_replacement([i for i in range(1,m+1)],n):
        cnt=0
        for v in a:
            if b[v[1]-1]-b[v[0]-1]==v[2]:
                cnt+=v[3]
        ans=max(ans,cnt)

    print(ans)



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

def main():
    nmq = list(map(int, input().split()))
    abcd = [None]*50
    for i in range(nmq[2]):
        abcd[i] = list(map(int, input().split()))
    
    ma = 0

    qset = bf([1], nmq[0], nmq[1], [])

    for q in qset:
        score = 0
        for j in range(nmq[2]):
            if q[abcd[j][1]-1] - q[abcd[j][0]-1] == abcd[j][2]:
                score += abcd[j][3]
        if score > ma:
            ma = score

    print(ma)

    
def bf(now, n, m, stock):
    if len(now) == n:
        stock.append(now)
        return now
    for i in range(now[-1], m+1):
        aa = list(now)
        aa.append(i)
        re = bf(aa, n, m, stock)
    return stock
    



if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations_with_replacement

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

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

Solution (Python):
from collections import defaultdict
from math import gcd
import time
import sys
from collections import Counter
import itertools
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
start = time.time()


N, M, Q = map(int, input().split())
big = 0
s = 2**N
l = []

moto = list(range(1, Q+1))

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

A = []
for i in range(N):
    AA = []
    if A:
        for k in A:
            for j in range(1, M+1):
                if k[-1] <= j:
                    AA.append(k+[j])
        A = AA
    else:
        for j in range(1, M+1):
            A.append([j])

for i in A:
    wa = 0
    for j in l:
        if (i[j[1]-1]-i[j[0]-1]) == j[2]:
            wa += j[3]

    if big < wa:
        big = wa
print(big)

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

Solution (Python):
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    
    n,m,q = map(int,input().strip().split())
    abcd = []
    for i in range(q):
        
        temp_a,temp_b,temp_c,temp_d = map(int,input().strip().split())
        abcd.append((temp_a - 1, temp_b - 1, temp_c, temp_d ))

    
    eprint('n,m,q ',end=':\n')
    eprint(n,m,q)
    eprint('abcd ',end=':\n')
    eprint(abcd)

    
    ans=0
    ll=[i for i in range(1,m+1)]
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    for A in itertools.combinations_with_replacement(ll,n):
        
        eprint('A ',end=':\n')
        eprint(A)

        point = 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] - A[a] == c:
                point+=d
        ans = max(point,ans)
    print(ans)

if __name__ == '__main__':
    main()

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

from itertools import combinations_with_replacement as cwr

def main():
    N, M, Q = map(int, input().split())
    I = [list(map(int, input().split())) for _ in range(Q)]
    J = cwr(range(1, M+1), N)
    ans = []

    for j in J:
        tmp = 0
        for i in I:
            a, b, c, d = i
            tmp += d if j[b-1] - j[a-1] == c else 0
        ans.append(tmp)

    print(max(ans))

    
if __name__  == '__main__':
    main()

Solution (Python):
import itertools

n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations_with_replacement(I, n)))
l = len(A)
ans = [0 for i in range(l)]
for _ in range(q):
    a, b, c, d = map(int, input().split())
    for i in range(l):
        if A[i][b - 1] - A[i][a - 1] == c:
            ans[i] += d
print(max(ans))

Solution (Python):

from itertools import combinations_with_replacement

def resolve():
    N, M, Q = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(Q)]
    cmb_A = list(combinations_with_replacement(range(1,M+1), N))

    ans = 0
    for cmb in cmb_A:
        tot = 0
        for x in A:
            a,b,c,d = x
            a -= 1
            b -= 1
            if cmb[b]-cmb[a] == c:
                tot += d
        ans = max(ans, tot)
    print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):
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_list = []
for p in itertools.combinations_with_replacement(range(1,m+1),n):
    A_list.append(p)


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

Solution (Python):
from itertools import combinations_with_replacement
def main():
    N, M, Q = map(int, input().split())
    abcd = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    for x in combinations_with_replacement(range(1, M+1), N):
        point = 0
        for a, b, c, d in abcd:
            if x[b-1] - x[a-1] == c:
                point += d
        if ans < point:
            ans = point
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):



a = list()
def mkArray(in_list, sav_val, cnt):
    if cnt >= n:
        a.append(in_list)
        return 0
    else:
        for x in range(sav_val, m + 1):
            wk_list = list()
            wk_list.extend(in_list)
            wk_list.append(x)
            mkArray(wk_list, x, cnt + 1)


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

for i in range(q):
    w_abcd = list(map(int,input().split()))
    abcd.append(w_abcd)


w_a = list()
mkArray(w_a, 1, 0)


ans = 0
for i in range(len(a)):
    wans = 0
    for j in range(q):
        if a[i][abcd[j][1]-1] - a[i][abcd[j][0]-1] == abcd[j][2]:
            wans = wans + abcd[j][3]

    if wans > ans:
        ans = wans


print(ans)

Solution (Python):
import itertools

def calc(A_list):
    ans = 0
    for A in A_list:
        score = 0     
        for i in range(Q):
            a, b, c, d = X[i]
            if A[b-1] -A[a-1] == c:
                score += d
        ans = max(ans, score)
    return ans

N, M, Q = map(int, input().split())
X = [[0]*4 for _ in range(Q)]

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

l = [i for i in range(1, M+1)]
A_list = list(itertools.combinations_with_replacement(l, N))
print(calc(A_list))

Solution (Python):
N,M,Q = list(map(int, input().split()))
C = []
for i in range(Q):
    C.append(list(map(int, input().split())))
    
import itertools
res = 0
ls=list(range(1,M+1))
for a in itertools.combinations_with_replacement(ls, N):
    p = 0
    for c in C:
        if a[c[1]-1]-a[c[0]-1]==c[2]:
            p += c[3]
        res = max(res,p)
print(res)

Solution (Python):
import itertools

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

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

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

d=deque([[1]])
for i in range(n):
    for j in range(len(d)):
        tmp=d.popleft()
        for k in range(m-tmp[-1]+1):
            d.append(tmp+[tmp[-1]+k])

ans=0


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

Solution (Python):
import itertools

N, M, Q = map(int, input().split())
x1 = [0]*Q
x2 = [0]*Q
x3 = [0]*Q
x4 = [0]*Q

for i in range(Q):
    x1[i], x2[i], x3[i], x4[i] = map(int, input().split())
score = 0
tmp = 0
lis = range(1, M+1)
A = []

for i in itertools.combinations_with_replacement(lis, N):
    A.append(list(i))

for i in range(len(A)):
  for j in range(Q):
    if(A[i][x2[j]-1] - A[i][x1[j]-1] == x3[j]): tmp += x4[j]
  if(tmp>=score): score = tmp
  tmp = 0

print(score)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def dfs(aa):
        if len(aa)==n+1:
            res=0
            for a,b,c,d in ad:
                if aa[b]-aa[a]==c:res+=d
            return res

        res=-1
        for a in range(aa[-1],m+1):
            res=max(dfs(aa+[a]),res)
        return res

    n,m,q=MI()
    ad=LLI(q)
    print(dfs([1]))

main()

Solution (Python):
import sys
import math
from collections import Counter
import itertools
import fractions



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

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

def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'


p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")


 
N,M,Q=MI()
a=LLI(Q)
x=[0]*M
for i in range(1,M+1):
    x[i-1]=i
A=itertools.combinations_with_replacement(x,N)

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

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

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

def judge(l):
    ans=0
    for i in range(q):
        a,b,c,d=t[i]
        if l[b]-l[a]==c:
            ans+=d
    return ans

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

for df in itertools.combinations_with_replacement(num,n):

    ty=list(df)
    f=judge(ty)
    ans=max(ans,f)

print(ans)

Solution (Python):
from itertools import combinations_with_replacement as cr
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(q)]
nl=[i for i in range(1,m+1)]
ans=0
for v in cr(nl,n):
  s=0
  for a,b,c,d in l:
    if v[b-1]-v[a-1]==c:
      s+=d
  if ans<s:
    ans=s
print(ans)

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

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

l = range(1,m+1)
point = 0
for v in itertools.combinations_with_replacement(l,n):
    p = 0
    for a,b,c,d in abcd:
        if v[b-1] - v[a-1] == c:
            p += d
    if p > point: point = p
print(point)

Solution (Python):


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

from itertools import combinations_with_replacement
AA = combinations_with_replacement(list(range(1,M+1)),N)
ans = 0
for A in AA:
    now = 0
    for q in range(Q):
        a,b,c,d = C[q]
        if A[b-1]-A[a-1]==c:
            now += d
    ans = max(ans,now)
print(ans)

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

    b = []
    for i in range(m):
        b.append(i + 1)

    A = list(combinations_with_replacement(b, n))

    ans = 0
    for Ai in A:
        tmp = 0
        for a, b, c, d in li:
            a -= 1
            b -= 1
            if Ai[b] - Ai[a] == c:
                tmp += d

        ans = max(ans, tmp)
    print(ans)
    
resolve()

Solution (Python):
import itertools
import numpy
N, M, Q = map(int,input().split())
abcd = []*Q
d_list = []
sum_d = 0
for _ in range(Q):
  abcd.append(list(map(int,input().split())))

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):
import itertools


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

a = [None] * Q
b = [None] * Q
c = [None] * Q
d = [None] * Q

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

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

max_v = 0
for v in itertools.combinations_with_replacement(A, N):
    tmp_sum = 0
    for i in range(Q):
        if v[b[i]] - v[a[i]] == c[i]:
            tmp_sum += d[i]
    max_v = max(max_v, tmp_sum)

print(max_v)

Solution (Python):
import itertools

N,M,Q = map(int,input().split())
num = [[0 for i in range(4)] for j in range(Q)]
test = list()

k = 1
for i in range(M):
    test.append(k)
    k += 1

k = 0
for i in range(Q):
    num[k][0], num[k][1], num[k][2], num[k][3] = map(int,input().split())
    k += 1

k = 0
score = 0
maxscore = 0
for list in itertools.combinations_with_replacement(test, N):
    for i in range(Q):
        if list[num[k][1]-1] - list[num[k][0]-1] == num[k][2]:
            score += num[k][3]
        k += 1
        if score > maxscore:
            maxscore = score
    score = 0
    k = 0

print(maxscore)

Solution (Python):
import sys
import os
from itertools import combinations_with_replacement

def file_input():
    f = open('../Beginner_Contest_165/input.txt', 'r')
    sys.stdin = f

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

    ans=0

    for A in list(combinations_with_replacement(range(1,M+1),N)):
        
        score=0
        for Q in Q_set:
            if A[Q[1]-1]-A[Q[0]-1]==Q[2]:
                score+=Q[3]
        if ans<score:
            ans=score

    print(ans)


if __name__ == '__main__':
    main()