from sys import stdin

import itertools

import numpy as np

from numba import njit



@njit(cache=True)

def f():

    max_res=0

    for i in range(l):

        res=0

        for j in range(Q):

            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:

                res+=d[j]

        max_res=max(max_res,res)

    return max_res



#入力

readline=stdin.readline

N,M,Q=list(map(int,readline().split()))

a=np.zeros(Q,dtype=np.int64)

b=np.zeros(Q,dtype=np.int64)

c=np.zeros(Q,dtype=np.int64)

d=np.zeros(Q,dtype=np.int64)

for i in range(Q):

    a[i],b[i],c[i],d[i]=list(map(int,readline().split()))



arrays=np.array(list(itertools.combinations_with_replacement(list(range(1,M+1)),N)),dtype=np.int64)

l=len(arrays)



print((f()))
from sys import stdin

import itertools

import numpy as np

from numba import njit



@njit("i8(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:,:])",cache=True)

def f(l,Q,a,b,c,d,arrays):

    max_res=0

    for i in range(l):

        res=0

        for j in range(Q):

            if arrays[i][b[j]-1]-arrays[i][a[j]-1]==c[j]:

                res+=d[j]

        max_res=max(max_res,res)

    return max_res

  

def main():

#入力

    readline=stdin.readline

    N,M,Q=list(map(int,readline().split()))

    a=np.zeros(Q,dtype=np.int64)

    b=np.zeros(Q,dtype=np.int64)

    c=np.zeros(Q,dtype=np.int64)

    d=np.zeros(Q,dtype=np.int64)

    for i in range(Q):

        a[i],b[i],c[i],d[i]=list(map(int,readline().split()))



    arrays=np.array(list(itertools.combinations_with_replacement(list(range(1,M+1)),N)),dtype=np.int64)

    l=len(arrays)



    print((f(l,Q,a,b,c,d,arrays)))

    

if __name__=="__main__":

	main()
from sys import stdin
import itertools
import numpy as np
from numba import njit


@njit(cache=True)
def f():
    max_res = 0
    for i in range(l):
        res = 0
        for j in range(Q):
            if arrays[i][b[j] - 1] - arrays[i][a[j] - 1] == c[j]:
                res += d[j]
        max_res = max(max_res, res)
    return max_res


# 入力
readline = stdin.readline
N, M, Q = list(map(int, readline().split()))
a = np.zeros(Q, dtype=np.int64)
b = np.zeros(Q, dtype=np.int64)
c = np.zeros(Q, dtype=np.int64)
d = np.zeros(Q, dtype=np.int64)
for i in range(Q):
    a[i], b[i], c[i], d[i] = list(map(int, readline().split()))
arrays = np.array(
    list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)),
    dtype=np.int64,
)
l = len(arrays)
print((f()))

from sys import stdin
import itertools
import numpy as np
from numba import njit


@njit("i8(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:,:])", cache=True)
def f(l, Q, a, b, c, d, arrays):
    max_res = 0
    for i in range(l):
        res = 0
        for j in range(Q):
            if arrays[i][b[j] - 1] - arrays[i][a[j] - 1] == c[j]:
                res += d[j]
        max_res = max(max_res, res)
    return max_res


def main():
    # 入力
    readline = stdin.readline
    N, M, Q = list(map(int, readline().split()))
    a = np.zeros(Q, dtype=np.int64)
    b = np.zeros(Q, dtype=np.int64)
    c = np.zeros(Q, dtype=np.int64)
    d = np.zeros(Q, dtype=np.int64)
    for i in range(Q):
        a[i], b[i], c[i], d[i] = list(map(int, readline().split()))
    arrays = np.array(
        list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)),
        dtype=np.int64,
    )
    l = len(arrays)
    print((f(l, Q, a, b, c, d, arrays)))


if __name__ == "__main__":
    main()

-@njit(cache=True) -def f(): +@njit("i8(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:,:])", cache=True) +def f(l, Q, a, b, c, d, arrays): -# 入力 -readline = stdin.readline -N, M, Q = list(map(int, readline().split())) -a = np.zeros(Q, dtype=np.int64) -b = np.zeros(Q, dtype=np.int64) -c = np.zeros(Q, dtype=np.int64) -d = np.zeros(Q, dtype=np.int64) -for i in range(Q): -    a[i], b[i], c[i], d[i] = list(map(int, readline().split())) -arrays = np.array( -    list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)), -    dtype=np.int64, -) -l = len(arrays) -print((f())) +def main(): +    # 入力 +    readline = stdin.readline +    N, M, Q = list(map(int, readline().split())) +    a = np.zeros(Q, dtype=np.int64) +    b = np.zeros(Q, dtype=np.int64) +    c = np.zeros(Q, dtype=np.int64) +    d = np.zeros(Q, dtype=np.int64) +    for i in range(Q): +        a[i], b[i], c[i], d[i] = list(map(int, readline().split())) +    arrays = np.array( +        list(itertools.combinations_with_replacement(list(range(1, M + 1)), N)), +        dtype=np.int64, +    ) +    l = len(arrays) +    print((f(l, Q, a, b, c, d, arrays))) + + +if __name__ == "__main__": +    main()