Problem:


Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1



Sample input:
3 4 3
1 3 3 100
1 2 2 10
2 3 2 10


Sample output:
110

Submission status: Runtime Error

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

numbers = [i for i in range(1, m+1)]
p = list(itertools.permutations(numbers, n))

ans = []
for i in p:
  i = sorted(i)
  tmp = 0
  for pair in pairs:
    if (i[pair[1]-1] - i[pair[0]-1]) == pair[2]:
      tmp+=pair[3]
  ans.append(tmp)

print(max(ans))

Submission status: Runtime Error

Solution code:
import itertools
n, m, q = map(int, input().split())
I = list(range(1, m + 1))
A = list(map(list, itertools.combinations(I, n)))
l = len(A)
for i in range(l):
    if A[0][0] == 1:
        A.append(A[0])
    A.pop(0)
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))

Submission status: Runtime Error

Solution code:
n,m,q=map(int,input().split())
a=[0]*q
b=[0]*q
d=[0]*q
c=[0]*q
for i in range(q):
    a[i],b[i],c[i],d[i]=map(int,input().split())

ans=0

def chousa(list,a,b,c,d):
    global q
    ans=0
    for i in range(q):
        if list[b[i]]-list[a[i]]==c[i]:
            ans+=d[i]
    return ans



list=[1]*m
list[m-1]=m
u=0
while list[1]!=m:
    i=0

    while list[m-1-i]==m :
        i=i+1
#最後から数えて銃じゃなくなったところ
    if m-i!=1:
        list[m-1-i]+=1
        ans=max(chousa(list,a,b,c,d),ans)


print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations

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

a = []
b = []
c = []
d = []

for _ in range(q):
	A,B,C,D = map(int,input().split())
	a.append(A)
	b.append(B)
	c.append(C)
	d.append(D)
d_sum = sum(d)

l = list(range(1,m+1))
ans = list(combinations(l,n))

a_ns = []

for j in range(len(ans)):
	ANS = 0
	for i in range(q):
		if ans[j][b[i]-1] - ans[j][a[i]-1] == c[i]:
			ANS += d[i]
	if ANS == d_sum:
		print(ANS)
		exit()
	a_ns.append(ANS)

print(max(a_ns))

Submission status: Runtime Error

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

a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by)
    c.append(cy)
    d.append(dy)
s=0
smax=0
for i1 in range(0,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):
                                    for i10 in range(i9,m+1):
                                        s=0
                                        na=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for j in range(0,q):
                                            if na[b[j]]-na[a[j]]==c[j]:
                                                s+=d[j]
                                        if s>smax:
                                            smax=s


print(smax)

Submission status: Runtime Error

Solution code:
from itertools import combinations

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

lis = list(range(1, M+1))
box = list(combinations(lis, N))
print(abcd)

ans = []
for i in box:
    temp = 0
    for a, b, c, d in abcd:
        if i[b-1] - i[a-1] == c:
            temp += d
    ans.append(temp)

print(max(ans))

Submission status: Runtime Error

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

def point(x):
    temp = 0
    for i in range(Q):
        a, b, c, d = abcd[i]
        if x[b-1] - x[a-1] == c:
            temp += d
    return temp

def maker(x):
    dig = len(x)
    if  dig == N:
        ans.add(point(x))
    else:
        for i in range(x[-1], M+1):
            maker(x + [i])

for i in range(1, M):
    maker([i])
print(max(ans))

Submission status: Runtime Error

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

abcd.sort()
total = 0

for i in range(2 ** Q):
    now = str(format(i, 'b')).zfill(Q)
    A = [-1] * N
    flag = 0
    tmp = 0
    for j in range(Q):
        if now[j] == '1':
            mi = abcd[j][0] - 1
            ma = abcd[j][1] - 1
            if A[mi] == -1:
                A[mi] = 0
            if A[ma] == -1:
                A[ma] = A[mi] + abcd[j][2]
            elif A[ma] != A[mi] + abcd[j][2]:
                flag = 1
                break
            tmp += abcd[j][3]
    if tmp > total:
        total = tmp
print(total)

Submission status: Runtime Error

Solution code:
import itertools

N, M, Q = list(map(int,input().split()))
numbers = [i for i in range(M)]
array = itertools.combinations(numbers, N)

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

scores = []
for combi in array:
    score = 0
    for _ in range(Q):
        a, b, c, d = combinations[_]
        a = a - 1
        b = b - 1
        if combi[b] - combi[a] == c:
            score += d
    scores.append(score)
    
print(max(scores))

Submission status: Runtime Error

Solution code:
N, M, Q = [int(_) for _ in input().split()]
ABCD = [[int(_) for _ in input().split()] for _ in range(Q)]
cand = [[i] for i in list(range(1, M + 1))]
for i in range(M):
    candn = []
    for l in cand:
        j = l[-1]
        candn += [l + [k] for k in range(j, M + 1)]
    cand = candn
ans = 0
for l in cand:
    x = 0
    for a, b, c, d in ABCD:
        if l[b - 1] - l[a - 1] == c:
            x += d
    ans = max([ans, x])
print(ans)

Submission status: Runtime Error

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import itertools
n,m,q = iim()

A = list(itertools.combinations_with_replacement([1,2,3,4,5,6,7,8,9,10],m))
ans = 0
l = []
for i in range(q):
    l.append(iil())

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

Submission status: Runtime Error

Solution code:
import itertools

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

for i in range(Q):
  li.append(list(map(int,input().split())))
li.sort(key = lambda x:x[3])
num = [i for i in range(1,M+1)] 
l = list(itertools.combinations_with_replacement(num, M))
max_sum=-1
for i in range(len(l)):
  s = 0
  for j in range(Q):
    if l[i][li[j][1]-1] -  l[i][li[j][0]-1] == li[j][2]:
      s+= li[j][3]
      
  max_sum = max(max_sum,s)
print(max_sum)

Submission status: Runtime Error

Solution code:
N, M, Q = [int(_) for _ in input().split()]
ABCD = [[int(_) for _ in input().split()] for _ in range(Q)]
cand = [[i] for i in list(range(1, M + 1))]
for i in range(M):
    candn = []
    for l in cand:
        j = l[-1]
        candn += [l + [k] for k in range(j, M + 1)]
    cand = candn
ans = 0
for l in cand:
    x = 0
    for a, b, c, d in ABCD:
        if l[b - 1] - l[a - 1] == c:
            x += d
    ans = max([ans, x])
print(ans)
print(cand)

Submission status: Runtime Error

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

a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by)
    c.append(cy)
    d.append(dy)
s=0
smax=0
for i1 in range(0,11):
    for i2 in range(i1,11):
        for i3 in range(i2,11):
            for i4 in range(i3,11):
                for i5 in range(i4,11):
                    for i6 in range(i5,11):
                        for i7 in range(i6,11):
                            for i8 in range(i7,11):
                                for i9 in range(i8,11):
                                    for i10 in range(i9,11):
                                        s=0
                                        na=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for j in range(0,q):
                                            if na[b[j]]-na[a[j]]==c[j]:
                                                s+=d[j]
                                        if s>smax:
                                            smax=s



print(smax)

Submission status: Runtime Error

Solution code:
import itertools

N, M, Q = map(int, input().split())
comb = list(itertools.combinations(range(1,M+1), N))

ans = [0]*len(comb)
for i in range(Q):
    a,b,c,d = map(int, input().split())
    for j in range(len(comb)):
        if comb[j][b-1] - comb[j][a-1] == c:
            ans[j] += d
            
print(max(ans))

Submission status: Runtime Error

Solution code:
import itertools
N, M, Q = input().split()
N = int(N)
M = int(M)
Q = int(Q)
num = [0]*Q
for i in range(Q):
  num[i] = [list(map(int,input().split()))]
num = list(range(1, M+1))
A = []
for v in itertools.combinations(num, N):
  A.append(list(v))
score = 0
for w in A:
  scoreX = 0
  for j in range(Q):
    b = num[j][1]
    a = num[j][0]
    if num[j][2] == w[b-1]-w[a-1]:
      scoreX += num[j][3]
  if scoreX > score:
    score = scoreX
print(str(score))

Submission status: Runtime Error

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

aq = [None] * q
bq = [None] * q
cq = [None] * q
dq = [None] * q
for i in range(q):
    aq[i], bq[i], cq[i], dq[i] = list(map(int, input().split(' ')))

result = 0

def dfs(i, ary):
    global result
    if i == n:
        val = 0
        for j in range(q):
            if ary[bq[j] - 1] - ary[aq[j] - 1] == cq[j]:
                val += dq[i]
        if result < val:
            result = val
    else:
        if ary == []:
            for c in range(1, m + 1):
                dfs(i + 1, [c])
        else:
            l = ary[-1]
            for c in range(l, m + 1):
                dfs(i + 1, ary + [c])

dfs(0, [])

print(result)

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n, m, q = MAP()
tmp = list(range(1, m + 1)) * m
arr = list(combinations(tmp, n))
arr = [sorted(list(i)) for i in arr]
#print(arr)
point = [0 for _ in range(len(arr))]
for i in range(q):
    a, b, c, d = MAP()
    for j in range(len(arr)):
        if arr[j][b - 1] - arr[j][a - 1] == c:
            point[j] += d
#print(point)
print(max(point))

Submission status: Runtime Error

Solution code:
N, M, Q = [int(_) for _ in input().split()]
ABCD = [[int(_) for _ in input().split()] for _ in range(Q)]
cand = [[i] for i in list(range(1, M + 1))]
for i in range(M):
    candn = []
    for l in cand:
        j = l[-1]
        candn += [l + [k] for k in range(j, M + 1)]
    cand = candn
ans = 0
for l in cand:
    x = 0
    for a, b, c, d in ABCD:
        if l[b] - l[a] == c:
            x += d
    ans = max([ans, x])
print(ans)

Submission status: Runtime Error

Solution code:
n , m , q = map(int,input().split())
p = []
for i in range(q):
    a , b , c , d = map(int,input().split())
    p.append([a,b,c,d])
ans = 0
for a1 in range(1,m+1):
    for a2 in range(a1,m+1):
        for a3 in range(a2,m+1):
            for a4 in range(a3,m+1):
                for a5 in range(a4,m+1):
                    for a6 in range(a5,m+1):
                        for a7 in range(a6,m+1):
                            for a8 in range(a7,m+1):
                                for a9 in range(a8,m+1):
                                    for a10 in range(a9,m+1):
                                        tyu = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]
                                        kar = 0
                                        for x,y,z,w in p:
                                            if tyu[y] - tyu[x] == z:
                                                kar += w
                                        ans = max(kar,ans)
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int n, m, q, a[50], b[50], c[50], d[50];
int ans = 0;
int l[10];


void f(int i, int j) {
	if (i == n) {
		int res = 0;
		rep(i, q) {
			if (l[b[i]] - l[a[i]] == c[i]) res += d[i];
		}
		ans = max(ans, res);
		return;
	}
	for(int k = j; k <= m; k++) {
		l[i] = k;
		f(i+1, k);
	}
}

int main() {
	cin >> n >> m >> q;
	rep(i, q) {
		cin >> a[i] >> b[i] >> c[i] >> d[i];
		a[i]--; b[i]--;
	}
	l[0] = 1;
	f(1, 1);
	cout << ans << endl;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    n, m, q = map(int, args[0].split())
    ABCD = [tuple(map(int, abcd.split())) for abcd in args[1:]]

    ret = 0
    stack = [(0, '')]
    while stack:
        d, s = stack.pop()
        S = s+str(d)
        if len(S) == m:
            ret = max(
                ret,
                sum([d for a, b, c, d in ABCD if int(S[b-1])-int(S[a-1]) == c])
            )
            continue

        for i in range(d, 10):
            stack.append((i, S))

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Runtime Error

Solution code:
N,M,Q=map(int,input().split())
que=[tuple(map(int,input().split())) for _ in range(Q)]
task=[]
sta=[str(i) for i in range(1,M+1)]
while sta:
    s=sta.pop()
    if len(s)==N: task.append(s)
    else:
        for d in range(int(s[-1]),M+1):
            sta.append(s+str(d))
ans=0
for s in task:
    A=[int(i) for i in s]
    tmp=0
    for a,b,c,d in que:
        if A[b-1]-A[a-1]==c: tmp+=d
    ans=max(ans,tmp)
print(ans)

Submission status: Runtime Error

Solution code:
cin = open(0).read().strip().split('\n')
n, m, q = map(int, cin[0].split(' '))
abcd = [tuple(map(int, a.split(' '))) for a in cin[1:]]

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

Submission status: Runtime Error

Solution code:
n, m , q = map(int, input().split())
import itertools
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))
  
comb = itertools.combinations_with_replacement(range(1,m+1), n)

max_score = 0
for e in comb:
  score = 0
  for j in l:
    if e[j[1]-1]-e[j[0]-1]==j[2]:
      
      score += j[3]
  if score > max_score:
    max_score = score
print(max_score)

Submission status: Runtime Error

Solution code:
import collections
def main():

    N, M, Q = map(int, input().split())
    p = collections.defaultdict(int)
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        p[(a-1, b-1, c)] = d

    lv = [[1]]
    for i in range(M):
        temp = []
        for v in lv:
            for j in range(v[-1], M+1):
                temp.append(v + [j])
        lv = temp

    ans = 0
    for v in lv:
        q = 0
        for a, b, c in p:
            if v[b] - v[a] == c:
                q += p[(a, b, c)]
        ans = max(ans, q)
    return ans



if __name__ == '__main__':
    print(main())

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
import math
from collections import deque
import heapq
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M,Q = IL()
abcd = [IL() for i in range(Q)]

ans = 0
for i in range(1 << Q):
  used = [-MAX_INT]*M
  num = 0
  for j in range(Q):
    if (i >> j) & 1:
      a,b,c,d = abcd[j]
      if used[a-1] != -MAX_INT and used[b-1] != -MAX_INT:
        if used[b-1] - used[a-1] != c:
          break
      elif used[a-1] == -MAX_INT and used[b-1] != -MAX_INT:
        used[a-1] = used[b-1] - c
      elif used[a-1] != -MAX_INT and used[b-1] == -MAX_INT:
        used[b-1] = used[a-1] + c
      else:
        used[a-1] = 1
        used[b-1] = 1+c
      num += d
  ans = max(ans, num)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
from collections import deque
import heapq
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M,Q = IL()
abcd = [IL() for i in range(Q)]

ans = 0
for i in range(1 << Q):
  used = [-MAX_INT]*M
  num = 0
  for j in range(Q):
    if (i >> j) & 1:
      a,b,c,d = abcd[j]
      if used[a-1] != -MAX_INT and used[b-1] != -MAX_INT:
        if used[b-1] - used[a-1] != c:
          break
      elif used[a-1] == -MAX_INT and used[b-1] != -MAX_INT:
        used[a-1] = used[b-1] - c
      elif used[a-1] != -MAX_INT and used[b-1] == -MAX_INT:
        used[b-1] = used[a-1] + c
      else:
        used[a-1] = 1
        used[b-1] = 1+c      
      num += d
  ans = max(ans, num)
print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

N, M, Q, *abcd = map(int, read().split())
cand = [[0, 1]]

for _ in range(M):
    new_cand = []
    for j in cand:
        for k in range(j[-1], M + 1):
            new_cand.append(j[:] + [k])
    cand = new_cand[:]

answer = 0
abcd = tuple(zip(*[iter(abcd)] * 4))
for seq in cand:
    t = 0
    for a, b, c, d in abcd:
        if seq[b] - seq[a] == c:
            t += d

    answer = max(answer, t)

print(answer)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, m, q = LI()
    abcd = LIR(q)
    ans = 0
    fulls = itertools.combinations_with_replacement(range(m),m)
    for full in fulls:
        tmp = 0
        for a, b, c, d in abcd:
            if full[b-1] - full[a-1] == c:
                tmp += d
        ans = max(tmp, ans)
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
def cur(x):

    if len(x)<m+1:
        ans=0
        for i in range(x[-1],m+1):
            ans=max(ans,cur(x+[i]))
    else:
        ans=0
        for i in range(q):
            [a,b,c,d]=l[i]
            if x[b]-x[a]==c: ans+=d
    return ans

n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(q)]
print(cur([1]))

Submission status: Runtime Error

Solution code:
from itertools import permutations
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]

def calc(p):
    ret = 0
    # 各条件を満たしていれば、dを加算していく
    for a, b, c, d in abcd:
        if p[b-1] - p[a-1] == c:
            ret += d
    return ret

ans = 0
for p in permutations(range(1,1+M)):
    p = list(p)[:N]
    ans = max(ans, calc(p))
print(ans)