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:
N,M,Q = [int(x) for x in input().split()]
#全探索する
from collections import deque
nyu = []
for i in range(Q):
    nyu.append([int(x) for x in input().split()])
d = deque()
a = [0] * (N+1)
e = []
ima = 0
ans = 0
if N == 2:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for i in range(Q):
                aa = nyu[i][0]
                bb = nyu[i][1]
                cc = nyu[i][2]
                dd = nyu[i][3]
                if a[bb] - a[aa] == cc:
                    ima += dd
            ans = max(ans,ima)
            ima = 0
elif N == 3:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for i in range(Q):
                    aa = nyu[i][0]
                    bb = nyu[i][1]
                    cc = nyu[i][2]
                    dd = nyu[i][3]
                    if a[bb] - a[aa] == cc:
                        ima += dd
                ans = max(ans,ima)
                ima = 0

elif N == 4:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for i in range(Q):
                        aa = nyu[i][0]
                        bb = nyu[i][1]
                        cc = nyu[i][2]
                        dd = nyu[i][3]
                        if a[bb] - a[aa] == cc:
                            ima += dd
                    ans = max(ans,ima)
                    ima = 0
elif N == 5:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for i in range(2):
                        sinigami = 2
                    for m in range(l,M+1):
                        a[5] = m
                        for i in range(Q):
                            aa = nyu[i][0]
                            bb = nyu[i][1]
                            cc = nyu[i][2]
                            dd = nyu[i][3]
                            if a[bb] - a[aa] == cc:
                                ima += dd
                        ans = max(ans,ima)
                        ima = 0
elif N == 6:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for i in range(Q):
                                aa = nyu[i][0]
                                bb = nyu[i][1]
                                cc = nyu[i][2]
                                dd = nyu[i][3]
                                if a[bb] - a[aa] == cc:
                                    ima += dd
                            ans = max(ans,ima)
                            ima = 0
elif N == 7:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for i in range(Q):
                                    aa = nyu[i][0]
                                    bb = nyu[i][1]
                                    cc = nyu[i][2]
                                    dd = nyu[i][3]
                                    if a[bb] - a[aa] == cc:
                                        ima += dd
                                ans = max(ans,ima)
                                ima = 0
elif N == 8:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for q in range(p,M+1):
                                    a[8] = q
                                    for i in range(Q):
                                        aa = nyu[i][0]
                                        bb = nyu[i][1]
                                        cc = nyu[i][2]
                                        dd = nyu[i][3]
                                        if a[bb] - a[aa] == cc:
                                            ima += dd
                                    ans = max(ans,ima)
                                    ima = 0
elif N == 9:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for q in range(p,M+1):
                                    a[8] = q
                                    for r in range(q,M+1):
                                        a[9] = r
                                        for i in range(Q):
                                            aa = nyu[i][0]
                                            bb = nyu[i][1]
                                            cc = nyu[i][2]
                                            dd = nyu[i][3]
                                            if a[bb] - a[aa] == cc:
                                                ima += dd
                                        ans = max(ans,ima)
                                        ima = 0
elif N == 10:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for q in range(p,M+1):
                                    a[8] = q
                                    for r in range(q,M+1):
                                        a[9] = r
                                        for s in range(r,M+1):
                                            a[10] = s
                                            for i in range(Q):
                                                aa = nyu[i][0]
                                                bb = nyu[i][1]
                                                cc = nyu[i][2]
                                                dd = nyu[i][3]
                                                if a[bb] - a[aa] == cc:
                                                    ima += dd
                                            ans = max(ans,ima)
                                            ima = 0                                                                                  
print(ans)

Submission status: Wrong Answer

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

b = [chr(i) for i in range(N)]

for b in itertools.product(range(1,M+1),repeat=N):
  #print(b)
  for i in range(Q):
    #print('b[b]',b[abcd[i][1]-1])
    #print('b[a]',b[abcd[i][0]-1])
    #print('c',abcd[i][2])
    if b[abcd[i][1]-1]-b[abcd[i][0]-1] == abcd[i][2]:
      d_list.append(abcd[i][3])
      #print(d_list)
  if sum_d <= sum(d_list):
    sum_d = sum(d_list)
  d_list.clear()
print(sum_d)

Submission status: Wrong Answer

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

A
sum = 0

B = sorted(A, key=lambda x: x[3], reverse=False)
B
Array = [1]*N
Array

for i in range(Q):
    x = Array[B[i][0]-1]+B[i][2]
    y = Array[B[i][1]-1] - B[i][2]
    if(Array[B[i][1]-1]-x!=0):
        if(x<=M):
            Array[B[i][1]-1] = x
        elif(y>=1):
            Array[B[i][0]-1] = y

for j in range(1,N):
    if(Array[j-1]>Array[j]):
        Array[j] = Array[j]

for i in range(Q):
    if(Array[B[i][1]-1] - Array[B[i][0]-1] == B[i][2]):
        sum += B[i][3]
print(sum)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
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 li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, M, Q = mi()
abcd = li2(Q)

#N = 10

a = '0' * N
m = str(M)
l = [a]
#for length in range(N):
while True:
    i = N-1
    while i >= 0 and a[i] == m:
        i -= 1
    if i == -1:
        break
    a = list(a)
    num = str(int(a[i]) + 1)
    a[i] = num
    if i !=N-1:
        for j in range(i+1, N):
            a[j] = num
    tmp = ''.join(a)
    l.append(tmp)

ans = 0
for num in l:
    cnt = 0
    num = list(map(int, list(num)))
    for i in range(Q):
        if num[abcd[i][1]-1] - num[abcd[i][0]-1] == abcd[i][2]:
            cnt += abcd[i][3]

    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q= list(map(int, input().split()))
li=[list(map(int,input().split())) for _ in range(q)]
A=[1 for _ in range(n)]
ans=0
for i in range((m-1)**(n-1)):
    for keta in range(1,n):
        for num in range(1,m):
            tmp=0
            A[-keta]+=1
            for a,b,c,d in li:
                if A[b-1]-A[a-1]==c:
                    tmp+=d
            ans=max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


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

numbers = []
for i in range(Q):
    numbers.append([int(j) for j in input().split()])

numbers = sorted(numbers , key = lambda x:-x[1])
numbers = sorted(numbers , key = lambda x:-x[0])
#print(numbers)

kouho = ["9"*(N)]
#print(kouho)
for a, b , c ,d in numbers:
    for k in kouho:
        if int(k[b-1]) - int(k[a-1]) != c:
            new_k = k[:a-1] + str(int(k[b-1])-c) +  k[a:]
            if int(k[b-1])-c >= 0:
                if int(k[b-1])-c <= int(k[a]):
                    if new_k not in kouho:
                        kouho.append(new_k)

#print(sorted(kouho))

answer = 0
for i, k in  enumerate(kouho):
    ans = 0
    for a, b, c, d in numbers:
        if int(k[b-1]) - int(k[a-1]) == c:
            ans += d
    if answer < ans:
        answer = ans
print(answer)
'''
kouho = []
for password in range(0,10**(N-1)):
    password = "9" + str(password).zfill(N-1)
    kouho.append(''.join(list(reversed(password))))

answers = [0] * len(kouho)
for i, k in  enumerate(kouho):
    ans = 0
    for a, b, c, d in numbers:
        if int(k[b-1]) - int(k[a-1]) == c:
            ans += d
    answers[i] = ans
print(max(answers))
'''

Submission status: Wrong Answer

Solution code:
import itertools
l = []
n,m,q=map(int, input().split())

tmp = 0
max = 0
flg=0
c = []

for i in range(q):
    c.append([int(j) for j in input().split()])

for i in range(q):
    if c[i][2] == 0:
        flg = 1

if flg == 1:
    for i in range(1,m+1):
        l.append(str(i))
    a = list(itertools.combinations_with_replacement(l, n))
else:
    for i in range(1,m+1):
        l.append(str(i))
    a = list(itertools.combinations(l, n))

for k in range(len(a)): 
    for i in range(q):
        if int(a[k][c[i][1]-1]) - int(a[k][c[i][0]-1]) == c[i][2]:
            tmp = tmp + c[i][3]
        if max < tmp:
            max = tmp
    tmp = 0

print(max)

Submission status: Wrong Answer

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

li = [list(x) for x in list(combinations(range(1, M+1), N))]
ans = 0
for item in li:
    sub = 0
    for c in chk:
        if item[c[1]-1] - item[c[0]-1] == c[2]:
            sub += c[3]
    ans = max(ans, sub)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations as co
n,m,q=map(int,input().split())

a=[]
for i in range(q):
    zx,bx,cx,dx=map(int,input().split())
    a.append([zx,bx,cx,dx])
comb=list(co(range(1,m+1),n))

ma=0
for i in comb:
    for j in range(q):
        #print("f",i[a[j][1]-1], i[a[j][0]-1],a[j][2])
        if i[a[j][1]-1]- i[a[j][0]-1]==a[j][2]:
            m+=a[j][3]
    if ma<m:
        ma=m
    m=0
print(ma)

Submission status: Wrong Answer

Solution code:
import itertools

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

elements = [i for i in range(1, m + 1)]
A = list(itertools.product(elements, repeat=n))

point_max = 0
for a in A:
    point = 0
    for c in conds:
        if a[c[1] - 1] - a[c[0] - 1] == c[2]:
            point += c[3]
    point_max = max(point_max, point)

print(point_max)

Submission status: Wrong Answer

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

max_score = 0

p = [1] * n
cur = n-1
while cur >= 0:
    score = 0
    for a, b, c, d in queries:
        if p[b - 1] - p[a - 1] == c:
            score += d
    max_score = max(score, max_score)
    updated = False
    for i in range(n - 1, -1, -1):
        if p[i] != 9:
            p[i] += 1
            for j in range(i + 1, n):
                p[j] = p[i]
            updated = True
            break
    if not updated:
        break

print(max_score)

Submission status: Wrong Answer

Solution code:
def main():
    n,m,q=map(int,input().split())
    a=[]
    b=[]
    c=[]
    d=[]
    ans=0
    for i in range(q):
        ai,bi,ci,di=map(int,input().split())
        a.append(ai)
        b.append(bi)
        c.append(ci)
        d.append(di)
    indices = [*range(q)]
    sorted_indices = sorted(indices, key=lambda i: -d[i])
    sorted_num = [d[i] for i in sorted_indices]
    A=[1]*(n+1)
    times=0
    for i in sorted_indices:
        #A[b[i]]-A[a[i]]=c[i]→A[b[i]=c[i]+A[a[i]]
        pre=A[b[i]-1]
        A[b[i]-1]=c[i]+A[a[i]-1]
        if A[b[i]]>m:
            break
        
        #All Check
        times+=1
        for j in range(1,n+1):
            if A[j]<A[j-1]:
                for k in range(times):
                    si=sorted_indices[k]
                    A[b[si]-1]=c[si]+A[a[si]-1]
        tmp=0
        for j in range(q):
            if A[b[j]-1]-A[a[j]-1]==c[j]:
                tmp+=d[j]
        
        if ans>tmp:
            A[b[i]-1]=pre
        ans=max(ans,tmp)
        
    print(ans)
    
    

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

candi = []
def pp(res, i, j):
    rr = res + [i]
    if j == 0:
        candi.append(res)
        return 
    else:
        for st in range(i, 11):
            pp(rr, st, j-1)

for i in range(1, 11):
    pp([], i, 10)


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

ans = 0
for k in candi:
    res = 0
    for a, b, c, d in ABCD:
        if k[b-1] - k[a-1] == c:
            res += d
    ans = max(res, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

kouho = []
for i in range(1, m+1):
  kouho.append(i)
#print(kouho)

A = list(itertools.combinations(kouho,n))
#print(A)

point = 0
maxp = 0
for a in A:
  point = 0
  #print(a)
  for my in mylist:
    #print(my)
    if a[my[1]-1] - a[my[0]-1] == my[2]:
      point += my[3]
      #print("yes")
  #print(point)
  if point > maxp:
    maxp = point
    #print("max更新")
    #print(maxp)
print(maxp)

Submission status: Wrong Answer

Solution code:
N,M,Q = [int(x) for x in input().split()]
#全探索する
from collections import deque
nyu = []
for i in range(Q):
    nyu.append([int(x) for x in input().split()])
d = deque()
a = [0] * (N+1)
e = []
ima = 0
ans = 0
if N == 2:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for i in range(Q):
                aa = nyu[i][0]
                bb = nyu[i][1]
                cc = nyu[i][2]
                dd = nyu[i][3]
                if a[bb] - a[aa] == cc:
                    ima += dd
            ans = max(ans,ima)
            ima = 0
elif N == 3:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for i in range(Q):
                    aa = nyu[i][0]
                    bb = nyu[i][1]
                    cc = nyu[i][2]
                    dd = nyu[i][3]
                    if a[bb] - a[aa] == cc:
                        ima += dd
                ans = max(ans,ima)
                ima = 0

elif N == 4:
    for z in range(1,M+1):
        a[1] = z
        for i in range(2):
            sinigami = 2
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for i in range(Q):
                        aa = nyu[i][0]
                        bb = nyu[i][1]
                        cc = nyu[i][2]
                        dd = nyu[i][3]
                        if a[bb] - a[aa] == cc:
                            ima += dd
                    ans = max(ans,ima)
                    ima = 0
elif N == 5:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for i in range(Q):
                            aa = nyu[i][0]
                            bb = nyu[i][1]
                            cc = nyu[i][2]
                            dd = nyu[i][3]
                            if a[bb] - a[aa] == cc:
                                ima += dd
                        ans = max(ans,ima)
                        ima = 0
elif N == 6:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for i in range(Q):
                                aa = nyu[i][0]
                                bb = nyu[i][1]
                                cc = nyu[i][2]
                                dd = nyu[i][3]
                                if a[bb] - a[aa] == cc:
                                    ima += dd
                            ans = max(ans,ima)
                            ima = 0
elif N == 7:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for i in range(Q):
                                    aa = nyu[i][0]
                                    bb = nyu[i][1]
                                    cc = nyu[i][2]
                                    dd = nyu[i][3]
                                    if a[bb] - a[aa] == cc:
                                        ima += dd
                                ans = max(ans,ima)
                                ima = 0
elif N == 8:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for q in range(p,M+1):
                                    a[8] = q
                                    for i in range(Q):
                                        aa = nyu[i][0]
                                        bb = nyu[i][1]
                                        cc = nyu[i][2]
                                        dd = nyu[i][3]
                                        if a[bb] - a[aa] == cc:
                                            ima += dd
                                    ans = max(ans,ima)
                                    ima = 0
elif N == 9:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for q in range(p,M+1):
                                    a[8] = q
                                    for r in range(q,M+1):
                                        a[9] = r
                                        for i in range(Q):
                                            aa = nyu[i][0]
                                            bb = nyu[i][1]
                                            cc = nyu[i][2]
                                            dd = nyu[i][3]
                                            if a[bb] - a[aa] == cc:
                                                ima += dd
                                        ans = max(ans,ima)
                                        ima = 0
elif N == 10:
    for z in range(1,M+1):
        a[1] = z
        for j in range(i,M+1):
            a[2] = j
            for k in range(j,M+1):
                a[3] = k
                for l in range(k,M+1):
                    a[4] = l
                    for m in range(l,M+1):
                        a[5] = m
                        for n in range(m,Q+1):
                            a[6] = n
                            for p in range(n,M+1):
                                a[7] = p
                                for q in range(p,M+1):
                                    a[8] = q
                                    for r in range(q,M+1):
                                        a[9] = r
                                        for s in range(r,M+1):
                                            a[10] = s
                                            for i in range(Q):
                                                aa = nyu[i][0]
                                                bb = nyu[i][1]
                                                cc = nyu[i][2]
                                                dd = nyu[i][3]
                                                if a[bb] - a[aa] == cc:
                                                    ima += dd
                                            ans = max(ans,ima)
                                            ima = 0                                                                                  
print(ans)

Submission status: Wrong Answer

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

for i in range(1):
    for j in range(1,m):
        for z in range(1,m+1):
            f=[1,int(j),int(z)]
            for l in range(q):
                
                r=a[l][1]-1
                d=a[l][0]-1
                if r>=3 or d>=3:
                    continue
                if int(a[l][2]) == int(f[r] - f[d]):
                    b+=int(a[l][3])
            
            if b>v:
                v=b
            b=0
print(v)

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
numbers=[]
for _ in range(Q):
    a,b,c,d,=map(int,input().split())
    numbers.append([a-1,b-1,c,d])
a=int('1'*N)
c=int(str(M)*N)
S=[str(b) for b in range(a,c+1)]
SS=[]
for a in S:
    n=[]
    for b in range(N):
        if int(a[b]) >=1 and int(a[b])<=M:
            n+=a[b]
    if len(n)==N:
        SS.append(n)
answers=[]
for a in SS:
    answer=0
    for b in numbers:
        if int(a[b[1]])-int(a[b[0]])==b[2]:
            answer+=b[3]
    answers.append(answer)
print(max(answers))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


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

numbers = []
for i in range(Q):
    numbers.append([int(j) for j in input().split()])

numbers = sorted(numbers , key = lambda x:-x[1])
numbers = sorted(numbers , key = lambda x:-x[0])
#print(numbers)

kouho = ["9"*(N)]
#print(kouho)
for a, b , c ,d in numbers:
    for k in kouho:
        if int(k[b-1]) - int(k[a-1]) != c:
            new_k = k[:a-1] + str(int(k[b-1])-c) +  k[a:]
            if int(k[b-1])-c >= 0:
                if int(k[b-1])-c <= int(k[a]):
                    kouho.append(new_k)
                    kouho = list(set(kouho))

#print(sorted(kouho))

answer = 0
for i, k in  enumerate(kouho):
    ans = 0
    for a, b, c, d in numbers:
        if int(k[b-1]) - int(k[a-1]) == c:
            ans += d
    if answer < ans:
        answer = ans
print(answer)
'''
kouho = []
for password in range(0,10**(N-1)):
    password = "9" + str(password).zfill(N-1)
    kouho.append(''.join(list(reversed(password))))

answers = [0] * len(kouho)
for i, k in  enumerate(kouho):
    ans = 0
    for a, b, c, d in numbers:
        if int(k[b-1]) - int(k[a-1]) == c:
            ans += d
    answers[i] = ans
print(max(answers))
'''

Submission status: Wrong Answer

Solution code:
import numpy as np


N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
j = np.ones(N)
ans = 0
i = N - 1
k = 0

c = 0

for A in l:
    a, b, c, d = A
    if j[b-1] - j[a-1] == c:
        ans += d

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

    if j[-1] == N + 1:
        for i in range(1, N):
            j[-(i+1)] += 1
            j[-(i+1):] = j[-(i+1)]
            if j[-(i+1)] != N + 1:
                break
    if np.sum(j) == M * N:
        break
print(ans)

Submission status: Wrong Answer

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

ab.sort(key = lambda x: x[3],reverse=True)
ans = 0
sub = [0] * n
for i in range(q):
    if sub[ab[i][1] - 1] - sub[ab[i][0] - 1] == ab[i][2]:
        ans += ab[i][3]
    else:
        if sub[ab[i][0] - 1] == 0:
            sub[ab[i][0] - 1] = sub[ab[i][1] - 1] - ab[i][2]
            ans += ab[i][3]
        elif sub[ab[i][1] - 1] == 0:
            sub[ab[i][1] - 1] = sub[ab[i][0] - 1] + ab[i][2]
            ans += ab[i][3]

ans2 = 0
ab.sort(key=lambda x: x[2], reverse=True)
for i in range(q):
    if sub[ab[i][1] - 1] - sub[ab[i][0] - 1] == ab[i][2]:
        ans2 += ab[i][3]
    else:
        if sub[ab[i][0] - 1] == 0:
            sub[ab[i][0] - 1] = sub[ab[i][1] - 1] - ab[i][2]
            ans2 += ab[i][3]
        elif sub[ab[i][1] - 1] == 0:
            sub[ab[i][1] - 1] = sub[ab[i][0] - 1] + ab[i][2]
            ans2 += ab[i][3]



print(ans2)

Submission status: Wrong Answer

Solution code:
import sys

N, M, Q = map(int, sys.stdin.readline().strip().split())
con = []  # condition
for i in range(Q):
    con.append(list(map(int, sys.stdin.readline().strip().split())))

dig = N + 1  # dig -1 桁の数字を作る
A = [1] * dig
point = 0


def make(min=1, digit=1):
    global point
    for i in range(min, 11):
        A[digit] = i
        if digit != dig - 1:
            make(min=i, digit=digit + 1)
        else:
            temp_point = 0
            for c in con:
                if A[c[1]] - A[c[0]] == c[2]:
                    temp_point += c[3]
            point = max(point, temp_point)


for i in range(1, 11):
    make(min=i, digit=1)

print(point)