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:
import numpy as np

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

A = np.sort(np.random.randint(1, m+1, n))


def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 20000
temperature = 100
temperature_init = 10
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)

    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature  = temperature_init*(1/(1+t**4))

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest
import itertools

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,m,q=map(int, readline().rstrip().split())

    aa=[0]*q
    bb=[0]*q
    cc=[0]*q
    dd=[0]*q

    for i in range(q):
        aa[i],bb[i],cc[i],dd[i]=map(int, readline().rstrip().split())

    max_score=0

    for a in itertools.combinations_with_replacement(list(range(1,m+1)),n):
        score=0
        for i in range(q):
            if (a[bb[i]-1]-a[aa[i]-1])==cc[i]:
                score+=dd[i]
        max_score=max(max_score,score)

    print(max_score)

    return

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10**9)

def calc(LL):
    global n,m,q,D,ans
    ret = 0
    for dd in D:
        a,b,c,d = dd[0],dd[1],dd[2],dd[3]
        if LL[b-1]-LL[a-1] == c:
            ret += d
    ans = max(ans,ret)

def func(LL):
    global n,m
    if len(LL) >= n:
        return calc(LL)
    for i in range(LL[-1],m+1):
        func(LL+[i])
       
n,m,q = map(int,input().split())
D = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for i in range(1,m):
    func([i])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import itertools

n, m, q = list(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] = list(map(int, input().split()))
# Aは単調増加数列でNこの要素を持って最大でもMまで
ans = 0
for seq in itertools.combinations_with_replacement(range(m+1), n+1):
    tmp = 0
    for i in range(q):
        if seq[b[i]]-seq[a[i]] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1, m+1, n))

# @jit


def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 20000
temperature = 100
temperature_init = 100
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    A = np.sort(A)
    energy = calc_energy(A)

    if np.random.rand() > (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature = temperature_init*(1/(1+t))

print(calc_energy(A))

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
S=[[0 for i in range(4)] for j in range(Q)]
for i in range(Q):
    inputS=list(map(int,input().split()))
    S[i][0]=inputS[0]
    S[i][1]=inputS[1]
    S[i][2]=inputS[2]
    S[i][3]=inputS[3]
S2=[]
maxNum=0
for a in range(11):
    for b in range(a,11):
        for c in range(b,11):
            for d in range(c,11):
                for e in range(d,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):
                                        S2.append([a,b,c,d,e,f,g,h,i,j])
                                        

for i in range(len(S2)):
    count=0
    for k in range(Q):
        if S2[i][S[k][1]-1]-S2[i][S[k][0]-1]==S[k][2]:
            count+=S[k][3]
    maxNum=max(maxNum,count)
print(maxNum)

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1, m+1, n))

# @jit


def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 15000
temperature = 100
temperature_init = 1000
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    A = np.sort(A)
    energy = calc_energy(A)

    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature = temperature_init*(1/(1+t))

print(calc_energy(A))

Submission status: Wrong Answer

Solution code:
import numpy as np
from numba import jit

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

A = np.sort(np.random.randint(1, m+1, n))

@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 15000
temperature = 100
temperature_init = 1000
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    A = np.sort(A)
    energy = calc_energy(A)

    if np.random.rand() > (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= temperature_init*(1/(1+t))

print(calc_energy(A))

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
S=[[0 for i in range(4)] for j in range(Q)]
for i in range(Q):
    inputS=list(map(int,input().split()))
    S[i][0]=inputS[0]
    S[i][1]=inputS[1]
    S[i][2]=inputS[2]
    S[i][3]=inputS[3]
S2=[]
maxNum=0
for a in range(10):
    for b in range(a,10):
        for c in range(b,10):
            for d in range(c,10):
                for e in range(d,10):
                    for f in range(e,10):
                        for g in range(f,10):
                            for h in range(g,10):
                                for i in range(h,10):
                                    for j in range(i,10):
                                        S2.append([a,b,c,d,e,f,g,h,i,j])
                                        

for i in range(len(S2)):
    count=0
    for k in range(Q):
        if S2[i][S[k][1]-1]-S2[i][S[k][0]-1]==S[k][2]:
            count+=S[k][3]
    maxNum=max(maxNum,count)
print(maxNum)

Submission status: Wrong Answer

Solution code:
import itertools

abcd = []
N,M,Q = map(int,input().split())
for i in range(Q):
    a,b,c,d = map(int,input().split())
    abcd.append([a,b,c,d])
  
A = itertools.product(list(range(1,M+1)),repeat=N)
ans = 0
for i in A: 
    _sum = 0
    for j in abcd: 
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            _sum += j[3]
    if ans < _sum:
        ans = _sum
    
print(ans)

Submission status: Wrong Answer

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

import numpy as np

def main():
    N, M, Q = map(int, input().split())
    I = [list(map(int, input().split())) for _ in range(Q)]
    A = np.array([M] * N)
    ans = []
    
    j = 1
    while True:
        tmp = 0
        for i in I:
            a, b, c, d = i
            tmp += d if A[b-1] - A[a-1] == c else 0

        ans.append(tmp)

        if A[-1] == 1:
            break

        if A[0] == 1:
            n = A[j]
            A[:j+1] = n - 1
            if n - 1 == 1:
                j += 1
        else:
            A[0] -= 1
            
    print(max(ans))    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
a=[]
b=[]
c=[]
d=[]
for i in range(Q):
  ta,tb,tc,td=map(int,input().split())
  a+=[ta]
  b+=[tb]
  c+=[tc]
  d+=[td]
maxs=0
A=[0 for i in range(10)]
for i in range(10):
  for j in range(i,10):
    for k in range(j,10):
      for l in range(k,10):
        for m in range(l,10):
          for n in range(m,10):
            for o in range(n,10):
              for p in range(o,10):
                for q in range(p,10):
                  for r in range(q,10):
                    A[0]=i
                    A[1]=j
                    A[2]=k
                    A[3]=l
                    A[4]=m
                    A[5]=n
                    A[6]=o
                    A[7]=p
                    A[8]=q
                    A[9]=r
                    su=0
                    for z in range(Q):
                      if A[b[z]-1]-A[a[z]-1]==c[z]:
                        su+=d[z]
                    maxs=max(maxs,su)
print(maxs)

Submission status: Wrong Answer

Solution code:
import itertools

abcd = []
N,M,Q = map(int,input().split())
for i in range(Q):
    a,b,c,d = map(int,input().split())
    abcd.append([a,b,c,d])
  
A = (list(itertools.product(list(range(1,M+1)),repeat=N)))

ans = 0
for i in A: 
    _sum = 0
    for j in abcd: 
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            _sum += j[3]
    if ans < _sum:
        ans = _sum
    
print(ans)

Submission status: Wrong Answer

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

abcd = []
for i in range(q):
    abcd.append([int(i) for i in input().split()])

ans = 0
for a1 in range(1, 11):
    for a2 in range(a1, 11):
        for a3 in range(a2, 11):
            for a4 in range(a3, 11):
                for a5 in range(a4, 11):
                    for a6 in range(a5, 11):
                        for a7 in range(a6, 11):
                            for a8 in range(a7, 11):
                                for a9 in range(a8, 11):
                                    for a10 in range(a9, 11):
                                        # print(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
                                        A = [0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
                                        ans_ = 0
                                        for i in range(q):
                                            if A[abcd[i][1]] - A[abcd[i][0]] == abcd[i][2]:
                                                ans_ += abcd[i][3]
                                        ans = max(ans_, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement as cwd

N, M, Q = map(int, input().split())
A = list(cwd(range(1, M + 1), N))
ABCD = []

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

res = 0
for i in A:
    tmp = 0
    for j in ABCD:
        if i[j[1]] - i[j[0]] == j[2]:
            tmp += j[3]
    res = max(res, tmp)

print(A)

Submission status: Wrong Answer

Solution code:
import os
import sys
from itertools import permutations, product

def main():
	n,m,q = map(int, input().split())
	a,b,c,d = [],[],[],[]
	A = list(product(range(1,m+1),repeat=n))
	for j in range(q):
		w,x,y,z = map(int, input().split())
		a.append(w)
		b.append(x)
		c.append(y)
		d.append(z)
		
	max_value = 0	

	for v in A:
		result = 0
		for j in range(q):
			n = v[b[j]-1] - v[a[j]-1]
			if n == c[j]:
				result += d[j]
			else: 		
				pass
	
		if max_value <= result:
			max_value = result
		else:
			pass
	print(max_value)

if __name__ == "__main__":
        main()

Submission status: Wrong Answer

Solution code:
import itertools

def c165(n, m, q, adli):

    ans = 0
    li = list(itertools.combinations_with_replacement((1, m+1), n))
    for i in li:
        count = 0
        for a, b, c, d in adli:
            if i[b-1] - i[a-1] == c:
                count += d
        ans = max(ans, count)

    return ans

def main():
    n, m, q = map(int, input().split())
    adli = [list(map(int, input().split()))for i in range(q)]
    print(c165(n, m, q, adli))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools

abcd = []
N,M,Q = map(int,input().split())
for i in range(Q):
    a,b,c,d = map(int,input().split())
    abcd.append([a,b,c,d])
  
A = (list(itertools.permutations(list(range(1,M+1)),N)))

ans = 0
for i in A: #(1,2,3,4)
    _sum = 0
    for j in abcd: #[2 4 1 86568]
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            _sum += j[3]
    if ans < _sum:
        ans = _sum
    
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

def get_score(arr: list):
    score = 0
    for yy in y:
        if arr[yy[1]] - arr[yy[0]] == yy[2]:
            score += yy[3]
    return score

scores = []
for t in itertools.combinations_with_replacement(range(1, 11), x[0]):
    isError = False
    for i in range(x[0] -1):
        if t[i] > t[i+1]:
            isError = True
            break
    if isError == True:
        continue
    scores.append(get_score(t))

print(max(scores))

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 25000
temperature = 150
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.99

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 25000
temperature = 150
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.9999

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
import itertools

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

def get_score(arr: list):
    score = 0
    for yy in y:
        if arr[yy[1]] - arr[yy[0]] == yy[2]:
            score += yy[3]
    return score

scores = []
for t in itertools.combinations_with_replacement(range(1, 11), x[0]):
    scores.append(get_score(t))

print(max(scores))

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 20000
temperature = 100
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    A = np.sort(A)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.9999

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
import itertools

def c165(n, m, q, adli):

    s = ""
    ans = 0
    for i in range(1, m+1):
        s += str(i)
    li = list(itertools.combinations_with_replacement(s, n))
    for i in li:
        count = 0
        for a, b, c, d in adli:
            if int(i[b-1]) - int(i[a-1]) == c:
                count += d
        ans = max(ans, count)

    return ans

def main():
    n, m, q = map(int, input().split())
    adli = [list(map(int, input().split()))for i in range(q)]
    print(c165(n, m, q, adli))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 30000
temperature = 150
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.999

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int, input().split())
q_list = []
final_score = 0
for i in range(Q):
    q_list.append(list(map(int, input().split())))
for A in itertools.combinations_with_replacement(range(1,11), N):
    score = 0
    for q in q_list:
        if A[q[1]-1] - A[q[0]-1] == q[2]:
            score += q[3]
        final_score = max([final_score, score])
print(final_score)

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 20000
temperature = 200
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.999

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
import itertools

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

nums = []
for _ in range(q):
    nums.append(list(map(int, input().split())))

total = 0

l = [1,2,3,4,5,6,7,8,9,10,11]
for v in itertools.combinations_with_replacement(l, n):
    scores = []
    for a,b,c,d in nums:
        if v[b-1]-v[a-1] == c:
            scores.append(d)
        else:
            scores.append(0)
    if total < sum(scores):
        total = sum(scores)

print(total)

Submission status: Wrong Answer

Solution code:
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])
  
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums = list(itertools.combinations_with_replacement(num, 10))
scores = []
for row in nums:
  score = 0
  i = 0
  while i < Q:
    if row[l[i][1]-1]-row[l[i][0]-1] == l[i][2]:
      score += l[i][3]
    i += 1
  scores.append(score)
  
print(max(scores))

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 10000
temperature = 100
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.999

print(np.int(calc_energy(A)))

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
a=[]
b=[]
c=[]
d=[]
for i in range(Q):
  ta,tb,tc,td=map(int,input().split())
  a+=[ta]
  b+=[tb]
  c+=[tc]
  d+=[td]
maxs=0
A=[0 for i in range(10)]
for i in range(10):
  for j in range(i,10):
    for k in range(j,10):
      for l in range(k,10):
        for m in range(l,10):
          for n in range(m,10):
            for o in range(n,10):
              for p in range(o,10):
                for q in range(p,10):
                  for r in range(q,10):
                    A[0]=i
                    A[1]=j
                    A[2]=k
                    A[3]=l
                    A[4]=m
                    A[5]=n
                    A[6]=o
                    A[7]=p
                    A[8]=q
                    A[9]=r
                    su=0
                    for z in range(Q):
                      if A[b[z]-1]-A[a[z]-1]==c[z]:
                        su+=d[z]
                    if su>maxs:
                      maxs=su
print(maxs)

Submission status: Wrong Answer

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

a_l = [1] * n
ans = 0
while True:
    if a_l == [m]*n:
        break
    ans_tmp = 0
    for a, b, c, d in abcd:
        if a_l[b-1] - a_l[a-1] == c:
            ans_tmp += d
    ans = max(ans,ans_tmp)
    for i in range(1,n+1):
        a_l[-i] += 1
        if a_l[-i] == m+1:
            a_l[-i] = 1
            continue
        else:
            break
    for i in range(1,n):
        while a_l[i-1] > a_l[i]:
            a_l[i] += 1

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,m,q = list(map(int,input().split()))
abcd =  [list(map(int,input().split())) for i in range(q)]


def dfs(x,y,z,que):
    ret = 0
    if x == 0:
        for a,b,c,d in abcd:
            if que[b-1] - que[a-1]==c:
                ret+=d
        return ret
    for i in range(y,z+1):
        ret = max(ret,dfs(x-1,i,z,que + [i]))
    return ret

print(dfs(n,1,10,[]))

Submission status: Wrong Answer

Solution code:
import numpy as np
#from numba import jit

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

A = np.sort(np.random.randint(1,m+1,n))

#@jit
def calc_energy(A):
    energy = 0.
    for i in range(q):
        if (A[b[i]-1] - A[a[i]-1]) == c[i]:
            energy += d[i]
    return energy


T = 10000
temperature = 100
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)
    
    idx = np.random.randint(0, n)
    A[idx] = np.random.randint(1, m+1)
    energy = calc_energy(A)
    
    if np.random.rand() < (energy - energy_prev)/temperature:
        pass
    else:
        A = np.copy(A_prev)
    temperature *= 0.999

print(calc_energy(A))

Submission status: Wrong Answer

Solution code:
import itertools

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

lis=[i for i  in range(1,m+1)]
lis=list(itertools.combinations_with_replacement(lis,n))
box=[0 for j in range(len(lis))]

print(lis)

for i in range(q):
    que=list(map(int,input().split()))
    for j in range(len(lis)):
        if lis[j][que[1]-1]-lis[j][que[0]-1]==que[2]:
            box[j]+=que[3] 
print(max(box))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N, M, Q = map(int, input().split())
a_list = [list(map(int, input().split())) for i in range(Q)]

temp_list = []

max_count = 0

dp_dict = {}

def solve(i, j):
    global max_count
    if i == N:
        temp_count = 0
        for m in a_list:
            if j[m[1] - 1] - j[m[0] - 1] == m[2]:
                temp_count += m[3]
        if max_count < temp_count:
            max_count = temp_count
        return

    a = 0
    if len(j) > 0:
        a = j[-1]
    else:
        a = 1
    for k in range(a, M + 1):
        if M - k >= N - (i + 1):
            solve(i + 1, j + [k])

solve(0, [])
print(max_count)

Submission status: Wrong Answer

Solution code:
# dfs
n_len, n_max, q = map(int, input().split())
qs = []
for i in range(q):
    qs.append(tuple(map(int, input().split())))
ans = 0
seq = [0]*n_len


def dfs(ii):
    if(ii == n_len):
        ans_tmp = calc()
        global ans
        ans = max(ans, ans_tmp)
        return
    else:
        for j in range(1, n_max+1):
            seq[ii] = j
            dfs(ii+1)


def calc():
    ans_tmp = 0
    for a, b, c, d in qs:
        if(seq[b-1]-seq[a-1] == c):
            ans_tmp += d
    return ans_tmp


dfs(0)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
R=[range(i+1,m+1) for i in range(m)]
def dfs(d,s):
    global n,m,Q,ans
    if d==n-1:
        ans_=0
        for k in Q:
            a,b,c,e=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=e
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in R[l-1]:
            dfs(d+1,s+str(i))
for i in range(1,m+1):
    dfs(0,str(i))

print(ans)

Submission status: Wrong Answer

Solution code:
n, m, q = map(int, input().split())
lst = []
for _ in range(q):
    lst_tmp = [int(i) for i in input().split()]
    lst.append(lst_tmp)

lst.sort()

ans = 0
A = list(range(1, -n, -1))
A[1] = 1
ref = [0] * (n + 1)

for a, b, c, d in lst:
    if A[b] - A[a] != c:
        if ref[b] < ref[a] + d:
            ans += ref[a] + d - ref[b]
            print('---', a, b, c, ans, ref[a] + d - ref[b], '---', ref[a], d, ref[b], ref)
            ref[b] = ref[a] + d
            A[b] = A[a] + c

for i in lst:
    print(i)
print(A)
print(ref)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for i in range(q)]
ans=0
#めっちゃfor分あるならdfs
R=[range(i+1,m+1) for i in range(m)]
def dfs(d,s):
    global n,m,Q,ans
    if d==n-1:
        ans_=0
        for k in Q:
            a,b,c,d=k
            if int(s[b-1])-int(s[a-1])==c:
                ans_+=d
        ans=max(ans,ans_)
    else:
        l=int(s[-1])
        for i in R[l-1]:
            dfs(d+1,s+str(i))
for i in range(1,m+1):
    dfs(0,str(i))

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q=map(int, input().split())
arr=[[0]*4 for i in range(q)]

for i in range(q):
    arr[i][0],arr[i][1],arr[i][2],arr[i][3]=map(int, input().split())

ans=0
for i in itertools.product([k for k in range(1,m+1)],repeat=n):
    score=0
    for j in range(q):
        tmp1=i[arr[j][1]-1]
        tmp0=i[arr[j][0]-1]
        if tmp1-tmp0==arr[j][2]:
            score+=arr[j][3]
    if ans<score:
        ans=score
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement as cwr

N, M, Q, *Z = map(int, open(0).read().split())
*Z, = zip(*[iter(Z)]*4)
print(Z)

ans = 0
for A in cwr(range(M), N):
    score = 0
    for a, b, c, d in Z:
        if A[b-1]-A[a-1] == c:
            score += d

    if score > ans:
        ans = score

print(ans)

Submission status: Wrong Answer

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

max_score = 0
for mid in itertools.combinations_with_replacement(range(m), n-1):
    if sum(mid)>m-1:
        continue
    tmp_score=0
    for i in qs:
        sum_mid = 0
        for j in range(i[0]-1,i[1]-1):
            sum_mid += mid[j]
        if sum_mid == i[2]:
            tmp_score += i[3]
    if tmp_score > max_score:
        max_score = tmp_score
        
print(max_score)

Submission status: Wrong Answer

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

p_max = 0
point =0
A = [1]*n
E = [m]*n

while A != E :

    #得点計算
    for j in range(q):
        if A[List[j][1]-1] - A[List[j][0]-1] == List[j][2]:
            point += List[j][3]


    #最大値の更新
    if p_max < point:
        p_max = point
    point = 0


    #Aの更新
    for i in reversed(range(0,n)):
        if A[i] < m:
            A[i] = A[i] + 1
            if i !=n-1:
                for k in range(i+1,n):
                    A[k] = A[i]
            break

print(p_max)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
def score_check(A,L):
    point = 0
    for a, b, c, score in L:
        temp = A[b - 1] - A[a - 1]
        if temp == c:point += score

    return point

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

num = 0
for i in permutations([i for i in range(1, m) for j in range(n)], n):
    #print(i)
    temp = score_check(i, l)
    if num < temp:num = temp
print(num)