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 sys
sys.setrecursionlimit(10 ** 9)

n,m,q = map(int,input().split())
abcd = []
for i in range(q):
  abcd.append(tuple(map(int,input().split())))

ans = 0
def dfs(l):
  global ans
  if len(l) == n:
    ans = max(check(l),ans)
  else:
    if len(l) == 0:
      mx = 1
    else:
      mx = l[-1]
    for i in range(mx,11):
      tmpl = l + [i]
      dfs(tmpl)

def check(l):
  res = 0
  for a,b,c,d in abcd:
    a -= 1
    b -= 1
    if l[b]-l[a] == c:
      res += d
  return res

dfs([])
print(ans)

Submission status: Wrong Answer

Solution code:
def judge():
    global num, a, b, c, d, q
    point = 0
    for i in range(q):
        if (num[b[i]] - num[a[i]] == c[i]):
            point += d[i]
    return point


def mk_num(i):
    global mx_p
    for j in range(num[i-1], 10):
        num[i] = j
        if (i == n):
            tmp = judge()
            if (mx_p < tmp):
                mx_p = tmp
        else:
            mk_num(i+1)


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())

num = [0] * (n+1)
mx_p = 0

mk_num(1)
print(mx_p)

Submission status: Wrong Answer

Solution code:
def judge():
    global num, a, b, c, d, q
    point = 0
    for i in range(q):
        if (num[b[i]] - num[a[i]] == c[i]):
            point += d[i]
    return point


def mk_num(i):
    global mx_p
    for j in range(num[i-1], 11):
        num[i] = j
        if (i == n):
            tmp = judge()
            if (mx_p < tmp):
                mx_p = tmp
        else:
            mk_num(i+1)


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())

num = [1] * (n+1)
mx_p = 0

mk_num(1)
print(mx_p)

Submission status: Wrong Answer

Solution code:
def judge():
    global num, a, b, c, d, q
    point = 0
    for i in range(q):
        if (num[b[i]] - num[a[i]] == c[i]):
            point += d[i]
    return point


def mk_num(i):
    global mx_p
    for j in range(num[i-1], 11):
        num[i] = j
        if (i == n):
            tmp = judge()
            if (mx_p < tmp):
                mx_p = tmp
        else:
            mk_num(i+1)


n, m, q = map(int, input().split())
a = [0]*q
b = [0]*q
c = [0]*q
d = [0] * q

num = [0] * (n+1)
mx_p = 0
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, input().split())
mk_num(1)
print(mx_p)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

# 1～Mまでの数を複数回用いて、長さNの要素の部分列返す
#（combinations：複数使用なし、combinations_with_replacement：複数使用あり）
A = np.array(list(itertools.combinations_with_replacement(range(1, M + 1), N)))

n = len(A) # 組合せ総数
score = np.zeros(n) # [0,0,...,0] :0が n個のaraay

m = map(int, read().split())
for a, b, c, d in zip(m, m, m, m):
    cond = A[:, b - 1] - A[:, a - 1] == c
    score += d * cond

print(score.max())

Submission status: Wrong Answer

Solution code:
import itertools

n,m,q=map(int, input().split())
a=[]
b=[]
c=[]
d=[]

for i in range(0,q):
    x,y,z,w=map(int, input().split())
    a.append(x)
    b.append(y)
    c.append(z)
    d.append(w)

res = 0
for A in itertools.combinations(range(1,m+1),n):
    tmp = 0
    for i in range(0,q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            tmp += d[i]
    print(A)
    print(tmp)
    if tmp > res:
        res = tmp
print(res)

Submission status: Wrong Answer

Solution code:
def dfs(A):
    global ans
    if len(A) == n+1:
        now = 0
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                now += d[i]
        ans = max(ans, now)
        return

    A.append(A[-1])
    while A[-1] <= m:
        dfs(A)
        A[-1] += 1


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)

dfs([1])
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,Q = map(int,input().rstrip().split(" "))
i = 0
K = []

A = [1] * N
A.append(M)
E = [M] * (N + 1)

q = []

karians = 0
ans = 0
for _ in range(Q):
    q.append(list(map(int,input().split(" "))))

while A != E:
    karians = 0
    for qq in q:
        if A[qq[1] -1] - A[qq[0] -1] == qq[2]:
            karians += qq[3]
    if karians > ans:
        ans = karians
    i = 0
    A[0] += 1
    while A[i] == A[i + 1] + 1:
        A[i] = 1
        A[i + 1] += 1
        i += 1
        if i == N - 1:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from itertools import combinations_with_replacement
 
n, m, q = map(int, input().split())

pt = np.array(list(combinations_with_replacement(range(1,m+1), n)))
score = np.zeros(len(pt))

for i in range(q):
    a, b, c, d =map(int, input().split())
    flg = pt[:,b-1] - pt[:,a-1] == c
    score += flg * d
#    print(score)
 
print(score.max())

Submission status: Wrong Answer

Solution code:
#dfs試す
#挙動追うのきつい
n,m,q=map(int, input().split( ))
qs=tuple(tuple(map(int, input().split( ))) for _ in range(q))
def dfs(ll):
    if len(ll)==n+1:
        sm=0
        for ai,bi,ci,di in qs:
            if ll[bi]-ll[ai]==ci:
                sm+=di 
        return sm
    res=[]
    for i in range(ll[-1],m+1):
        ll2=ll+[i]
        res.append(dfs(ll2))
    return(max(res))
l=[0]
print(dfs(l))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
import time




def make_table_min(n, m):
    Table = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    c = 10
    for i in range(100):
        rem = np.random.randint(1,m+1)
        if rem in Table:
            Table.remove(rem)
            c-=1
            if c <=n:
                return Table
    return make_table(n, m)



def make_table(n, m):
    Table = [None]*n
    for i in range(n):
        rem = np.random.randint(1,m+1)
        Table[i] = rem
    return Table

t0 = time.time()
n, m, q = map(int, input().split())
Lis = [[int(e) for e in input().split()] for _ in range(q)]

Sum_cor = []
for i in range(10000000000):
    if m >=6:
        Table = make_table(n,m)
    else:
        Table = make_table(n,m)
    Table.sort()
    d_sum = 0
    for l in Lis:
        if Table[l[1]-1]-Table[l[0]-1] == l[2]:
            d_sum+=l[3]
    Sum_cor.append(d_sum)
    t1 = time.time()
    if t1-t0 >1.8:
        print(max(Sum_cor))
        break

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def f(i, j):
    global ans
    if j == n:
        c = 0
        for ss in s:
            if t[ss[1] - 1] - t[ss[0] - 1] == ss[2]:
                c += ss[3]
        ans = max(ans, c)
        return
    for k in range(i, m + 1):
        f(k, j + 1)
        t[j] = k
    f(m, j + 1)

n, m, q = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(q)]
t = [1] * n
ans = 0
f(1, 0)
print(ans)

Submission status: Wrong Answer

Solution code:


def main():
    N, M, Q = [int(x) for x in input().split()]
    queries = [[int(x) for x in input().split()] for _ in range(Q)]

    ans = float("-inf")

    def dfs(A: list):
        nonlocal ans
        if len(A) == N:
            s = sum(d for a, b, c, d in queries if A[b-1]-A[a-1] == c)
            ans = max(ans, s)
        else:
            lo = 0 if not A else A[-1]
            for i in range(lo, M+1):
                A.append(i)
                dfs(A)
                A.pop()
    dfs([])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

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

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

for i in range(q):
    ai, bi, ci, di = list(map(int, input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

def score(A, a, b, c, d):
    s = 0
    for i in range(len(a)):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            s += d[i]
    return s

m = 0
it = itertools.combinations_with_replacement(range(1, m+1), n)
A_list = np.array(list(it))


for A in A_list:
    m = max(m, score(A,a,b,c,d))

print(m)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m, q = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for _ in range(q)]
all_pattern = []
start = ['1']

def dfs(s):
    last_s = int(s[-1])
    next_s = [str(x) for x in range(last_s, m + 1)]
    for i in next_s:
        t = s[:]
        t.append(i)
        if len(t) == n + 1:
            all_pattern.append(t[1:])
        else:
            dfs(t)
dfs(start)
ans = 0
for p in all_pattern:
    res = 0
    pattern = ''.join(p)
    for i in range(q):
        a, b, c, d = abcd[i]
        if int(pattern[b - 1]) - int(pattern[a - 1]) == c:
            res += d
    ans = max(ans, res)
#print(all_pattern)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m, q = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for _ in range(q)]
all_pattern = []
start = ['1']

def dfs(s):
    last_s = int(s[-1])
    next_s = [str(x) for x in range(last_s, m + 1)]
    for i in next_s:
        t = s[:]
        t.append(i)
        if len(t) == n + 1:
            all_pattern.append(t[1:])
        else:
            dfs(t)
dfs(start)
ans = 0
for pattern in all_pattern:
    res = 0
    pattern = ''.join(pattern)
    for i in range(q):
        a, b, c, d = abcd[i]
        if int(pattern[b - 1]) - int(pattern[a - 1]) == c:
            res += d
    ans = max(ans, res)
#print(all_pattern)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m, q = [int(x) for x in input().split()]
abcd = [[int(x) for x in input().split()] for _ in range(q)]
all_pattern = []
start = ['1']

def dfs(s):
    last_s = int(s[-1])
    next_s = [str(x) for x in range(last_s, m + 1)]
    for i in next_s:
        t = s[:]
        t.append(i)
        if len(t) == n + 1:
            all_pattern.append(t[1:])
        else:
            dfs(t)
dfs(start)
ans = 0
for pattern in all_pattern:
    pattern = ''.join(pattern)
    res = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if int(pattern[b - 1]) - int(pattern[a - 1]) == c:
            res += d
    ans = max(ans, res)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations as c 
n, m, q = map(int, input().split()) 
arr = [] 
for i in range(q): 
    l = [*map(int, input().split())] 
    arr.append(l) 
res = 0
for g in c(list(range(1, m+1)), n): 
    f = 0 
    for h in arr:
        if g[h[1]-1] - g[h[0]-1] == h[2]: 
            f += h[3] 
    res = max(res, f) 
print(res)

Submission status: Wrong Answer

Solution code:
import sys,bisect,string,math,time,functools,random
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            a,b,c=(inp+[1])[:3]
            org_inp.append(inp)
        else:
            index=0
            a,b,c=(ls[i]+[1])[:3]
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)];rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

class Tree:
    def __init__(self,inp_size=None,init=True):
        if init:
            self.stdin(inp_size)
        return

    def stdin(self,inp_size=None):
        if inp_size==None:
            self.size=int(input())
        else:
            self.size=inp_size
        self.edges,_=GI(self.size,self.size-1)
        return
    
    def listin(self,ls):
        self.size=len(ls)+1
        self.edges,_=GI(self.size,self.size-1,ls)
        return

    def __str__(self):
        return  str(self.edges)

    def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append(x)
        v=[-1]*self.size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==-1:
                    q.append(nb)
                    v[nb]=func(v[c],nb,d)
        return v

    def EulerTour(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append((-1,x))
        v=[None]*self.size
        v[x]=root_v
        et=[]
        while q:
            cb,ce=q.pop()
            et.append(ce)
            for nb,d in self.edges[ce]:
                if v[nb]==None:
                    q.append((nb,ce))
                    q.append((ce,nb))
                    v[nb]=func(v[ce],nb,d)
        vid=[[-1,-1]for i in range(self.size)]
        for i,j in enumerate(et):
            if vid[j][0]==-1:
                vid[j][0]=i
            else:
                vid[j][1]=i
        return v,et,vid
    
    def LCA_init(self,depth,et):
        self.st=SegTree(self.size*2-1,func=min,ide=inf)
        for i,j in enumerate(et):
            self.st.update(i,j)
        self.LCA_init_stat==True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            depth,et,vid=self.EulerTour(root)
            self.LCA_init(depth,et)
        return self.st.query(x,y+1)

show_flg=False
show_flg=True

p=[]
def pat(n,m,ls):
    if ls:
        M=ls[-1]
    else:
        M=0

    if n==0:
        p.append(ls[:])
        return
    
    for i in range(M,m+1):
        pat(n-1,m,ls[:]+[i])

ans=0
z=[]
N,M,q=LI()
pat(N,M,[])
for i in range(q):
    a,b,c,d=LI()
    z+=[(a,b,c,d)]

for y in p:
    
    tmp=0
    for a,b,c,d in z:
        if y[b-1]-y[a-1]==c:
            tmp+=d
    if ans<tmp:
        ans=tmp
        rt=y[:]

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement
N,M,Q = map(int,input().split())
A = []
for i in range(Q):
    b = list(map(int,input().split()))
    A.append(b)

T = range(M+1)
K = list(combinations_with_replacement(T,N))

ans = 0
for line in K:
    total = 0
    for combi in A:
        if line[combi[1]-1] - line[combi[0]-1] == combi[2]:
            total += combi[3]
    ans = max(total,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement
N,M,Q = map(int,input().split())
A = []
for i in range(Q):
    b = list(map(int,input().split()))
    A.append(b)
K = list(combinations_with_replacement([1,2,3,4,5,6,7,8,9,10],N))

ans = 0
for line in K:
    total = 0
    for combi in A:
        if line[combi[1]-1] - line[combi[0]-1] == combi[2]:
            total += combi[3]
    ans = max(total,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(500000)
List = []
def dfs(S):
    global N,M

    if len(S)==N:
        List.append(list(map(int, list(S))))
        return
    for i in range(1, M+1, 1):
        i = str(i)
        dfs(S+i)
    return

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

dfs("")
for i in range(1,M+1,1):
    L.append(i)

A = []
B = []
C = []
D = []
for _ in range(Q):
    a,b,c,d = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)

ans = 0
for l in List:
    temp = 0
    for q in range(Q):
        if (l[B[q]-1]-l[A[q]-1])==C[q]:
            temp = temp + D[q]
    ans = max(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement
N,M,Q = map(int,input().split())
A = []
for i in range(Q):
    b = list(map(int,input().split()))
    A.append(b)
K = list(combinations_with_replacement([1,2,3,4,5,6,7,8,9],N))

ans = 0
for line in K:
    total = 0
    for combi in A:
        if line[combi[1]-1] - line[combi[0]-1] == combi[2]:
            total += combi[3]
    ans = max(total,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q = [int(i) for i in input().split()]
a = []; b = []; c = []; d = []
for _ in range(q):
    l = list(map(int, input().split()))
    a.append(l[0]-1)
    b.append(l[1]-1)
    c.append(l[2])
    d.append(l[3])
    
ans = 0

def dfs(A):
    global ans
    if len(A) == n+1:
        now = 0
        print(A)
        for i in range(q):
            print(a[i],b[i],c[i],d[i])
            if A[b[i]] - A[a[i]] == c[i]: now += d[i]
        ans = max(ans, now)
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A)
        A[-1] += 1
        
dfs([1])

print(ans)

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(1,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=[a,b,c,d,e,f,g,h,i,j]
                                        count=0
                                        for k in range(Q):
                                            if S2[S[k][1]-1]-S2[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

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

abcd = sorted(abcd, key=lambda x: x[3], reverse=True)

l = np.zeros(N+1)
ans = 0
for a, b, c, d in abcd:
    if l[a] == 0 and l[b] == 0:
        l[b] = a+c
        l[a] = a
        ans += d
    elif l[a] == 0 and l[b] != 0:
        l[a] = l[b]-c
        ans += d
    elif l[a] != 0 and l[b] == 0:
        l[b] = l[a]+c
        ans += d
        
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q = [int(i) for i in input().split()]
a = []; b = []; c = []; d = []
for _ in range(q):
    l = list(map(int, input().split()))
    a.append(l[0])
    b.append(l[1])
    c.append(l[2])
    d.append(l[3])

ans = 0

def dfs(A):
    global ans
    if len(A) == n+1:
        now = 0
        print(A)
        for i in range(q):
            print(a[i],b[i],c[i],d[i])
            if A[b[i]] - A[a[i]] == c[i]: now += d[i]
        ans = max(ans, now)
        return
    A.append(A[-1])
    while A[-1] <= m:
        dfs(A)
        A[-1] += 1
        
dfs([1])

print(ans)

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 = 21000
temperature = 100
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,3)
    A[idx] = np.random.randint(1, m+1,3)
    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*0.999#temperature_init*(1/(1+t**4))

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement
from math import factorial
n,m,q=map(int,input().split())
lst=[list(map(int,input().split())) for i in range(q)]
total=[]
for a in combinations_with_replacement(range(1,m+1),n):
  ans=0
  for j in range(q):
    if int(a[lst[j][1]-1])-int(a[lst[j][0]-1])==lst[j][2]:
      ans+=lst[j][3]
  total.append(ans)
print(total)
print(max(total))

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 = 21000
temperature = 120
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,n//3)
    A[idx] = np.random.randint(1, m+1,n//3)
    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*0.999#temperature_init*(1/(1+t**4))

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

Submission status: Wrong Answer

Solution code:
import math
import itertools

n, m, q = map(int, input().split(" "))
aL = []
_max = 0

for i in range(q):
    aL.append(list(map(int, input().split(" "))))

li = []


def dfs(i, _n, p):
    if i == n:
        li.append(list(map(int, list(str(_n)))))
        return
    for j in range(p, m + 1):
        dfs(i + 1, _n * 10 + j, j)


for i in range(1, m + 1):
    dfs(1, i, i)

for t in li:
    if t[0] == 0:
        continue
    _sum = 0
    for a in aL:
        if t[a[1] - 1] - t[a[0] - 1] == a[2]:
            _sum += a[3]
    _max = max(_max, _sum)

print(_max)

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 = 24000
temperature = 120
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,n//2)
    A[idx] = np.random.randint(1, m+1,n//2)
    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*0.999#temperature_init*(1/(1+t**4))

print(np.int(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(1,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

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 = 21000
temperature = 120
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,4)
    A[idx] = np.random.randint(1, m+1,4)
    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*0.999#temperature_init*(1/(1+t**4))

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

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 = 24000
temperature = 120
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,4)
    A[idx] = np.random.randint(1, m+1,4)
    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*0.999#temperature_init*(1/(1+t**4))

print(np.int(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(1,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])
                                        
                                        count=0
                                        for k in range(Q):
                                            if S2[len(S2)-1][S[k][1]-1]-S2[len(S2)-1][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

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
for t in range(T):
    A_prev = np.copy(A)
    energy_prev = calc_energy(A_prev)

    idx = np.random.randint(0, n,3)
    A[idx] = np.random.randint(1, m+1,3)
    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*0.999#temperature_init*(1/(1+t**4))

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