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: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product
import sys
import bisect
import string
import math
import time



def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

def rec(li, i, M, N, a, b, c, d, cans):
    # print(i, li, len(li), N)
    if len(li) >= N:
        # print(li, N)
        ans = 0
        for j in range(len(a)):
            if li[b[j]-1] - li[a[j]-1] == c[j]:
                ans += d[j]
        # print(ans, li)
        return ans

    # for j in range(1, len(lis[i])):
    #     for v in lis[i][j]:

    ans = 0
    if len(cans[i]) == 0:
        ans = max(ans, rec(li + [1], i+1, M, N, a, b, c, d, cans))
    else:
        for v in cans[i]:
            ans = max(ans, rec(li + [v], i+1, M, N, a, b, c, d, cans))

    return ans


def main():
    N, M, Q = MI()
    a = [0] * Q
    b = [0] * Q
    c = [0] * Q
    d = [0] * Q
    didx = [None] * Q
    lis = [[[] for i in range(N+1)] for i in range(N+1)]
    cans = [set() for i in range(N+1)]

    for i in range(Q):
        a[i], b[i], c[i], d[i] = MI()
        # didx[i] = (d[i], i)

    # cans = []
    # for i in range(1, M+1):
    #     cans += [i] * N

    # for li in combinations(cans, N):
    #     print(li)

    # didx.sort(reverse=True)

    for i in range(Q):
        # lis[b[i]][a[i]].append(c[i])
        lis[a[i]][b[i]].append(c[i])

    # for i in range(1, M):
    cans[1].add(1)

    for i in range(1, N+1):
        # print(i, lis[i])
        for j in range(len(lis[i])):
            for k in lis[i][j]:
                for v in cans[i]:
                    # print(v, k)
                    if v + k <= M:
                        cans[j].add(v + k)
    # print(cans)
    # print(didx)
    print(rec([], 1, M, N, a, b, c, d, cans))
    # print(sum(d))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def gen(size, max):
    res = []

    s = [1] * size
    s[size-1] = max

    for i in range(size-1, 0, -1):
        for j in range(1, max+1):

            tmp = s[0:size]
            tmp[i] = j
            res.append(tmp)
            s = tmp
    return res


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

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

max = 0
x = gen(N, M)
for i in x:
    now = 0
    for j in range(Q):
        if i[Arr[j][1]-1]-i[Arr[j][0]-1] == Arr[j][2]:
            now += Arr[j][3]
    if now > max:
        max = now

print(max)

Submission status: Wrong Answer

Solution code:
n, m, q = input().split()
n = int( n )
m = int( m )
q = int( q )
a = []
b = []
c = []
d = []
for i in range( int( q ) ):
    a_i, b_i, c_i, d_i = input().split()
    a.append( int( a_i ) )
    b.append( int( b_i ) )
    c.append( int( c_i ) )
    d.append( int( d_i ) )

array = [ 1 ] * n
array_list = [ array ]

i = 0
while True:
    for k in range( array_list[i][-1], m + 1 ):
        array_list.append( array_list[i][1:] + [ k ] )
        #print( array_list[i][1:] + [ k ] )
    i = i + 1
    if len( array_list ) > 10000:
        break

max_score = 0
for array in array_list:
    score = 0
    for i in range( q ):
        #print( i, a[i], b[i], c[i], array )
        if array[b[i]-1] - array[a[i]-1] == c[i]:
            score = score + d[i]
    if max_score < score:
        max_score = score

print( max_score )

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

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)

seq = [[] for i in range(n)]

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

for i in range(1, n):
  for j in range(len(seq[i - 1])):
    s = copy.copy(seq[i - 1][j])
    s.append(0)
    for k in range(s[-2], m + 1):
      s[-1] = k
      s2 = copy.copy(s)
      seq[i].append(s2)

score = [0 for i in range(len(seq[n - 1]))]
for i in range(len(seq[n - 1])):
  for j in range(q):
    if seq[n - 1][i][b[j] - 1] - seq[n - 1][i][a[j] - 1] == c:
      score[i] += d[j]

print(max(score))

Submission status: Wrong Answer

Solution code:
import itertools

def main():
    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 i in range(Q):
        a[i], b[i], c[i], d[i] = map(int, input().split())

    seq = [i for i in range(1, max(11, M+1))]

    max_point = 0

    for e in list(itertools.combinations_with_replacement(seq, N)):
        point = 0
        for i in range(Q):
            if e[b[i]-1] - e[a[i]-1] == c[i]:
                point += d[i]
        if point >= max_point:
            max_point = point

    print(max_point)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = []
    for e in range(1, 11):
        for f in range(e, 11):
            for g in range(f, 11):
                for h in range(g, 11):
                    for i in range(h, 11):
                        for j in range(i, 11):
                            for k in range(j, 11):
                                for l in range(k, 11):
                                    for o in range(l, 11):
                                        for p in range(o, 11):
                                            res.append([e, f, g, h, i, j, k, l, o, p])

    ans = 0
    for p in res:
        score = 0
        for a, b, c, d in ABCD:
            if p[b - 1] - p[a - 1] == c:
                score += d

        ans = max(ans, score)

    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
li = list(map(int, input().split()))
lists = []
for a in range(li[2]):
    lists.append(list(map(int, input().split())))
listA = []
listB = []
for i in range(1, li[0] + 1):
    if i > li[1]:
        listA.append(li[1])
    else:
        listA.append(i)
totals = []
listB=listA
for i in range(li[2]):
    listA=listB
    ai = lists[i][0]-1
    bi = lists[i][1]-1
    ci = lists[i][2]
    di = lists[i][3]
    total = 0
    if not listA[bi] - listA[ai] == ci:
        listA[bi] = listA[ai] + ci
    for j in range(li[2]):
        if listA[lists[j][1]-1] - listA[lists[j][0]-1] == lists[j][2]:
            total = total + lists[j][3]
        elif listA[lists[j][0]-1]+lists[j][2] < listA[lists[j][0]]:
            listA[lists[j][1]-1] = listA[lists[j][0]-1]+lists[j][2]
            total = total+lists[j][3]
    totals.append(total)
print(max(totals))

Submission status: Wrong Answer

Solution code:
n, m, q = input().split()
n = int( n )
m = int( m )
q = int( q )
a = []
b = []
c = []
d = []
for i in range( int( q ) ):
    a_i, b_i, c_i, d_i = input().split()
    a.append( int( a_i ) )
    b.append( int( b_i ) )
    c.append( int( c_i ) )
    d.append( int( d_i ) )

array = [ 1 ] * n
array_list = [ array ]

i = 0
while True:
    for k in range( array_list[i][-1], m + 1 ):
        array_list.append( array_list[i][1:] + [ k ] )
        #print( array_list[i][1:] + [ k ] )
    i = i + 1
    if len( array_list ) > 1200:
        break

max_score = 0
for array in array_list:
    score = 0
    for i in range( q ):
        #print( i, a[i], b[i], c[i], array )
        if array[b[i]-1] - array[a[i]-1] == c[i]:
            score = score + d[i]
    if max_score < score:
        max_score = score

print( max_score )

Submission status: Wrong Answer

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

a_l = [1] * n
ans = 0
while True:
    ans_tmp = 0
    if '0' not in list(map(str,a_l)):
        for i in range(q):
            a = abcd[i][0]
            b = abcd[i][1]
            c = abcd[i][2]
            d = abcd[i][3]
            if a_l[b-1] - a_l[a-1] == c:
                ans_tmp += d
    ans = max(ans,ans_tmp)
    a_str = ''.join(map(str,a_l))
    a_int = int(a_str)
    a_int += 1
    a_str = str(a_int)
    a_l_str = list(a_str)
    a_l = list(map(int,a_str))
    if a_l == [m]*n:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
b=0
aa=[]
shurui=[]
point=[]
for i in range(q):
  a=list(map(int,input().split()))
  aa.append(a)

A="1"*n
d=0

while int(A)<=int(str(m)*n):
  for i in aa:
    if int(A[i[1]-1])-int(A[i[0]-1])==i[2]:
      b=b+(i[3])
  if d<b:
    d=b
  b=0
  A=str(int(A)+1)
  for l in range(n-1):
    if int(A[n-l-1])>m:
      A=str(int(A)+10**(l+1)-10**l)

print(d)

Submission status: Wrong Answer

Solution code:
import itertools

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

ms=[i for i in range(1,m+1)]
ans=0
for k in itertools.permutations(ms, n):
  x=0
  for j in range(q):
    if a[j][2]==k[a[j][1]-1]-k[a[j][0]-1]:
      x+=a[j][3]
      print(x)
  if ans<x:
    ans=x
print(ans)

Submission status: Wrong Answer

Solution code:
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(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
    print(list)
    print(score)
    score = 0
    k = 0

print(maxscore)

Submission status: Wrong Answer

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

ps_ = sorted(ps_, key=lambda x: x[3], reverse=True)
ps = list()
for p in ps_:
    ps.append([p[0]-1, p[1]-1, p[2], p[3]])

score = 0
s = [0] * n
for p in ps:
    a, b, c, d = p
    if s[a] == 0 and s[b] == 0:
        s[a] = 1 if a == 0 else max(min(s[:a]), 1)
        if s[a] + c > m: continue
        s[b] = s[a] + c
        score += d
    elif s[a] == 0:
        if s[b] - c < 1: continue
        s[a] = s[b] - c
        score += d
    elif s[b] == 0:
        if s[a] + c > m: continue
        s[b] = s[a] + c
        score += d
    elif s[b] == s[a] + c:
        score += d
print(score)

Submission status: Wrong Answer

Solution code:
import math
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())

maximum = 0
lis = list(range(1, M+1))
for pair in itertools.combinations(lis, N):
    sum = 0
    pair = list(pair)
    for i in range(Q):
        if pair[b[i]-1]-pair[a[i]-1] == c[i]:
            sum += d[i]
            
    if maximum < sum:
        maximum = sum

print(maximum)

Submission status: Wrong Answer

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

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

A = [1 for i in range(N)]
ans = []
wa = 0
for i in range(N):
  for j in range(M):
    A[N - i - 1] = 1 + j
    for k in range(Q):
      if A[x[k][1] - 1] - A[x[k][0] - 1] == x[k][2]:
        wa += x[k][3]
    ans.append(wa)
    wa = 0
    
ans.sort(reverse=True)
print(ans[0])

Submission status: Wrong Answer

Solution code:
n, m, q = input().split()
n = int( n )
m = int( m )
q = int( q )
a = []
b = []
c = []
d = []
for i in range( int( q ) ):
    a_i, b_i, c_i, d_i = input().split()
    a.append( int( a_i ) )
    b.append( int( b_i ) )
    c.append( int( c_i ) )
    d.append( int( d_i ) )

array = [ 1 ] * n
array_list = [ array ]

i = 0
while True:
    for k in range( array_list[i][-1], m + 1 ):
        array_list.append( array_list[i][1:] + [ k ] )
        #print( array_list[i][1:] + [ k ] )
    i = i + 1
    if len( array_list ) > 1000:
        break

max_score = 0
for array in array_list:
    score = 0
    for i in range( q ):
        #print( i, a[i], b[i], c[i], array )
        if array[b[i]-1] - array[a[i]-1] == c[i]:
            score = score + d[i]
    if max_score < score:
        max_score = score

print( max_score )

Submission status: Wrong Answer

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

query = []

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

query.sort(key= lambda x: x[1])

if len(query) == 1:
    print(query[0][3])
    exit()
num = '1' * n
rows = [num]

for a,b,c,d in query:
    for row in rows:
        if int(row[b]) - int(row[a]) == c:
            continue
        else:
            row = list(row)
            row[b] = str(int(row[a]) + c)
            row = ''.join(row)
            rows.append(row)
            rows = set(rows)
            rows = list(rows)

rows = set(rows)

max_ans = 0
for row in rows:
    ans = 0
    row = list(row)
    for a,b,c,d in query:
        if int(row[b]) - int(row[a]) == c:
            ans += d
    if ans > max_ans:
        max_ans = ans

print(max_ans)

Submission status: Wrong Answer

Solution code:
x = list(map(int, input().split()))
y = []

for i in range(x[2]):
    y.append(list(map(int, input().split())))
for i in range(len(y)):
    y[i][0] -= 1
    y[i][1] -= 1
y.sort(key=lambda a: a[3], reverse=True)

def srch(arr: list, tr: list):
    tree = tr[:]
    for l in tree:
        tree.remove(l)

#def check(arr:list, ele: list):
    

r = []
for i in range(x[0]):
    r.append(-1)

score = 0
for n in y:
    #print(n)
    if r[n[1]] == -1 or r[n[0]] == -1:
        r[n[1]] = 0
        r[n[0]] = n[2]
        score += n[3]
    elif r[n[1]] - r[n[0]] == n[2]:
        score += n[3]
print(score)

Submission status: Wrong Answer

Solution code:
import copy

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

rules = list()

for _ in range(q):
    a, b, c, d = list(map(int, input().split()))
    rules.append((a, b, c, d))

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

answers = [[-1 if i > 1 else 1 for i in range(n + 1)]]
answers[0][0] = 0
for r in rules:
    a, b, c, d = r
    new_answers = list()
    for ans in answers:
        if ans[b] == -1 or ans[b] == ans[a] + c:
            if ans[a] + c <= m:
                new_answer = copy.copy(ans)
                new_answer[0] = ans[0] + d
                new_answer[b] = ans[a] + c

                new_answers.append(new_answer)
    answers.extend(new_answers)

print(max([ans[0] for ans in answers]))
exit()


As = [[-1 for i in range(m + 1)] for j in range(n + 1)]

As[1][1] = 0

for r in rules:
    a, b, c, d = r
    for i in range(1, m + 1):
        if i + c <= m:
            if As[a][i] < 0:
                continue
            if As[b][i+c] >= 0:
                As[b][i + c] += As[a][i] + d
            else:
                As[b][i + c] = As[a][i] + d

    print(As)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = []
    for e in range(1, 11):
        for f in range(e, 11):
            for g in range(f, 101):
                for h in range(g, 11):
                    for i in range(h, 11):
                        for j in range(i, 11):
                            for k in range(j, 11):
                                for l in range(k, 11):
                                    for o in range(l, 11):
                                        for p in range(o, 11):
                                            res.append([e, f, g, h, i, j, k, l, o, p])

    ans = 0
    for p in res:
        score = 0
        for a, b, c, d in ABCD:
            if p[b - 1] - p[a - 1] == c:
                score += d

        ans = max(ans, score)

    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque
de = deque()
n,m,q = map(int,input().split())
queries = []
for i in range(q):
    a,b,c,d = map(int,input().split())
    queries.append((a,b,c,d))
de.append([1])
maxscore = 0
while de: 
    li = de.popleft()
    if len(li) < n:
        for i in range(li[-1],11):
            new_li = li + [i]
            de.append(new_li)
    else:
        score = 0
        for i in range(q):
            a,b,c,d = queries[i]
            if li[b-1]-li[a-1] == c:
                score += d
        maxscore = max(maxscore,score)
print(maxscore)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,q = map(int,input().split())
l = list(list(map(int,input().split())) for _ in range(q))
if m<7:
  num = ["1"*n]
  k = deque(["1"*n])
  while len(k)!=0:
    h = k.popleft()
    if int(h[-1])<m:
      num.append(str(int(h)+1))
      num.append(str(int(h)+11))
      num.append(str(int(h)+111))
      k.append(str(int(h)+1))
      k.append(str(int(h)+11))
      k.append(str(int(h)+111))
  num = list(set(num))
  ma = 0
  for i in num:
    s = 0
    for a,b,c,d in l:
      if int(i[b-1])-int(i[a-1])==c:
        s += d
        ma = max(ma,s)
  print(ma)
else:
  ma = 0
  for i in l:
    ma += i[-1]
  print(ma)

Submission status: Wrong Answer

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

#print(numbers)
num_list = list(range(M))

ans = 0
for bit in range(1<<len(num_list)):
    suuretu = []

    for i in num_list:
        mask = 1 << i
        if bit & mask:
            suuretu.append(i)
    suuretu = list(map(lambda x: x+1, suuretu))

    tmp = 0

    if len(suuretu) == N:
#        print(suuretu)
        for number in numbers:
            if suuretu[number[1]-1] - suuretu[number[0]-1] == number[2]:
                tmp += number[3]
        
    ans = max(ans, tmp)
#    print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
A = [1] * N
ans = 0

for a, b, c, d in l:
    if A[b-1] - A[a-1] == c:
        ans += d
if M > 1:
    while True:
        A[-1] += 1
        ans_ = 0
        for a, b, c, d in l:
            if A[b-1] - A[a-1] == c:
                ans_ += d
        if ans_ > ans:
            ans = ans_

        if A[-1] == M + 1:
            for i in range(1, N):
                A[-(i+1)] += 1
                for k in range(N-(i+1), N):
                    A[k] = A[-(i+1)]
                if A[-(i+1)] != M + 1:
                    break
        if A[0] == M:
            break
print(ans)

Submission status: Wrong Answer

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

al=list()

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

al.sort(reverse=True)

Al=[0]*(N+1)

sum=0
for i in range(Q): 
    if(Al[al[i][1]]==0 or Al[al[i][2]]==0):
        if(Al[al[i][1]]==0 and Al[al[i][2]]==0):
            Al[al[i][1]]=1
            Al[al[i][2]]=1+al[i][3]
        elif(Al[al[i][1]]==0):
            Al[al[i][1]]=Al[al[i][2]]-al[i][3]
        else:
            Al[al[i][2]]=Al[al[i][1]]+al[i][3]
for i in range(Q): 
    if(Al[al[i][2]]-Al[al[i][1]]==al[i][3]):
        sum+=al[i][0]


print(sum)

Submission status: Wrong Answer

Solution code:
n, m, q = input().split()
n = int( n )
m = int( m )
q = int( q )
a = []
b = []
c = []
d = []
for i in range( int( q ) ):
    a_i, b_i, c_i, d_i = input().split()
    a.append( int( a_i ) )
    b.append( int( b_i ) )
    c.append( int( c_i ) )
    d.append( int( d_i ) )

array = [ 1 ] * n
array_list = [ array ]

i = 0
while True:
    for k in range( array_list[i][-1], m + 1 ):
        array_list.append( array_list[i][1:] + [ k ] )
        #print( array_list[i][1:] + [ k ] )
    i = i + 1
    if len( array_list ) > 800:
        break

max_score = 0
for array in array_list:
    score = 0
    for i in range( q ):
        #print( i, a[i], b[i], c[i], array )
        if array[b[i]-1] - array[a[i]-1] == c[i]:
            score = score + d[i]
    if max_score < score:
        max_score = score

print( max_score )

Submission status: Wrong Answer

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

s = [1] * n
#st[b[i]] - st[a[i]]  = c[i]
num = n-1
#ans1 = [0] * 10000000
ans = 0
pre_ans = 0
t = 0

while(s[0] != m):

    for i in range(q):
        if s[b[i]-1] - s[a[i]-1]  == c[i]:
            ans += d[i]
    #print(s)
    #print(ans)
    ans = max(pre_ans,ans)
    pre_ans = ans
    #t += 1

    ans = 0

    if s[num] < m:
        s[num] += 1
    elif num > 0 :
        num -= 1
        temp = s[num]+1
        if temp < m:
            for i in range(num,n):
                s[i] = temp
        else:
            while(s[num] == m):
                num -= 1
            temp = s[num]+1
            for i in range(num,n):
                s[i] = temp

        num = n-1






print(pre_ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = []
    for e in range(1, 11):
        for f in range(e, 11):
            for g in range(f, 101):
                for h in range(g,11):
                    for i in range(h,11):
                        for j in range(i,11):
                            for k in range(j,11):
                                for l in range(k,11):
                                    for o in range(l,11):
                                        for p in range(m,11):
                                            res.append([e,f,g,h,i,j,k,l,o,p])

    ans = 0
    for p in res:
        score = 0
        for a, b, c, d in ABCD:
            if p[b - 1] - p[a - 1] == c:
                score+= d

        ans = max(ans, score)

    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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


A_list = [1]*N

Submission status: Wrong Answer

Solution code:
A_list = []

def calc():
    global A_list
    ans = 0
    for A in A_list:
        score = 0     
        for i in range(Q):
            a, b, c, d = X[i]
            # print(a,b,c,d,score)

            if A[b-1] -A[a-1] == c:
                score += d
        ans = max(ans, score)
    return ans
    
def create_A(A, others, index):
    global A_list
    if len(A) == N:
        # calc(A)
        A_list.append(A)
        return
    if index >= len(others):
        return
    for i in range(index, len(others)):
        new_A = A[:]
        new_A.append(others[i])
        create_A(new_A, others, i+1)

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

for i in range(Q):
    # X[i][0], X[i][1], X[i][2], X[i][3] = map(int, input().split())
    X[i] = list(map(int, input().split()))

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

create_A([], others, 0)
print(calc())

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools

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

d_sum_max=0

for i in  itertools.product(range(1,M+1), repeat=N):
    d_sum=0
    i = list(i)
    #print(i)
    for j in range(Q):
        if i[abcd[j][1]-1] - i[abcd[j][0]-1] == abcd[j][2]:
            d_sum += abcd[j][3]
        if d_sum_max < d_sum:
            d_sum_max = d_sum
    #print(d_sum)
print(d_sum_max)

Submission status: Wrong Answer

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

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


def calcPoint(num):
    ret = 0
    for q in range(Q):
        if num[query[q][1] - 1] - num[query[q][0] - 1] == query[q][2]:
            ret += query[q][3]

    return ret


suu = [0] * N
now = [0] * N
nowPoint = 0
ans = 0

for q in range(Q):
    suu[query[q][1] - 1] = suu[query[q][0] - 1] + query[q][2]
    if calcPoint(suu) >= nowPoint:
        nowPoint = calcPoint(suu)
        now = suu.copy()

print(nowPoint)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

arr = list(range(1, m + 1))
lst = [list(map(int, input().split())) for _ in range(q)]

max_score = 0
for a in combinations(list(range(m)), n):
    score = 0
    _arr = [arr[i] for i in a]
    #print('aaa', a)
    for i in lst:
        if _arr[i[1] - 1] - _arr[i[0] - 1] == i[2]:
            # print('a', a)
            # print('i: ', i)
            #print('i: ', i)
            #print('check', _arr[i[0] - 1], _arr[i[1] - 1], i[2])
            score += i[3]
            # print('score: ', score)
    if score > max_score:
        # print('a', a)
        # print('_arr', _arr)
        max_score = score
        #print(f'answer  a: {[i + 1 for i in a]}: score: {score}')

print(max_score)

Submission status: Wrong Answer

Solution code:
import itertools

def main():
    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 i in range(Q):
        a[i], b[i], c[i], d[i] = map(int, input().split())

    seq = [i for i in range(1, 11)]

    max_point = 0

    for e in list(itertools.combinations_with_replacement(seq, N)):
        point = 0
        for i in range(Q):
            if e[b[i]-1] - e[a[i]-1] == c[i]:
                point += d[i]
        if point >= max_point:
            max_point = point

    print(max_point)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from math import factorial as F
import itertools

N,M,Q = map(int,input().split())
kumi = []
SUM = 0

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

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

for i in itertools.permutations(l,N):
    sum = 0
    A = sorted(list(i))
    for x in range(Q):
        if A[kumi[x][1]-1] - A[kumi[x][0]-1] == kumi[x][2]:
            sum += kumi[x][3]
    if sum > SUM:
        SUM = sum

print(SUM)