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 = map(int, input().split())
#下記の書き方応用できそうだ
arr = [list(map(int, input().split())) for _ in range(Q)]

ans = 0

"""
下記は問題の条件式をみて判断できる1<=M<=10
"""
for i1 in range(1, M+1):
    for i2 in range(i1, M+1):
        for i3 in range(i2, M+1):
            for i4 in range(i3, M+1):
                for i5 in range(i4, M+1):
                    for i6 in range(i5, M+1):
                        for i7 in range(i6, M+1):
                            for i8 in range(i7, M+1):
                                for i9 in range(i8, M+1):
                                    for i10 in range(i9, M+1):
                                        tmp = 0
                                        #添字の順番を合わせるために一つ目に0を代入している感じ。
                                        arrs = [0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
                                        for a, b, c, d in arr:
                                            if arrs[b] - arrs[a] == c:
                                                tmp += b
                                        ans = max(ans, tmp)
                            
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N,M,Q = map(int,input().split())
s = [] 
score = 0
b_2 = []
max_score  = 0

for _ in range(Q):
  s.append(list(input().split()))

for b in itertools.product(range(M+1), repeat=N):
  b_2.append(list(b))

for b in b_2:
  for x in range(Q):
    if b[int(s[x][1])-1]-b[int(s[x][0])-1] == int(s[x][2]):
      score += int(s[x][3])
    if max_score<= score:
      max_score = score
  score = 0
print(max_score)

Submission status: Wrong Answer

Solution code:
import itertools

N,M,Q = map(int,input().split())
s = [] 
score = 0
max_score  = 0

for _ in range(Q):
  s.append(list(input().split()))

for b in itertools.product(range(M+1), repeat=N):
  for x in range(Q):
    if b[int(s[x][1])-1]-b[int(s[x][0])-1] == int(s[x][2]):
      score += int(s[x][3])
    if max_score<= score:
      max_score = score
  score = 0
print(max_score)

Submission status: Wrong Answer

Solution code:
def search(v, n, m, seq, scs):
    if len(seq) == n:
        ans = 0
        for r in scs:
            if seq[r[1]-1] - seq[r[0]-1] == r[2]:
                ans += r[3]
        return ans
    ans = 0
    for i in range(v, m+1):
        tmp = search(v+1, n, m, seq + [i], scs)
        ans = max(ans, tmp)
    return ans

def resolve():
    n, m, q = map(int, input().split())
    scs = [list(map(int, input().split())) for _ in range(q)]
    v = search(1, n, m, [], scs)
    print(v)
resolve()

Submission status: Wrong Answer

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

import copy

# 全探索？？

#pypy3なら通りそう

ans = [0]
arr = [0]*N


def rec(index, score, arr, ans):
    # print(arr)
    if index > Q-1:
        ans[0] = max(score, ans[0])
        return
    # 何もしない
    score1 = score
    score2 = score



    if (arr[abcd[index][1]-1] - arr[abcd[index][0]-1] == abcd[index][2]) and arr[abcd[index][1]-1]!= 0 and arr[abcd[index][0]-1]!=0:
        # print("a")
        rec(index+1, score1 + abcd[index][-1], arr, ans)
    else:
        rec(index+1, score1, arr, ans)

        

        # 追加する
    for i in range(M-abcd[index][2]):
        # print("a",M-abcd[index][2])
        if (arr[abcd[index][0]-1] == 0 or arr[abcd[index][1]-1] == 0):
            if arr[abcd[index][0]-1] !=0 and arr[abcd[index][0]-1] !=i + 1:continue
            if arr[abcd[index][1]-1] !=0 and arr[abcd[index][1]-1] !=i + 1 + abcd[index][2]:continue
            # arr1 = arr
            arr1 = copy.deepcopy(arr)
            arr1[abcd[index][0]-1] = i + 1
            arr1[abcd[index][1]-1] = i + 1 + abcd[index][2]
            # print("a",abcd[index][2])
            rec(index+1, score2 + abcd[index][-1], arr1, ans)


rec(0, 0, arr, ans)

print(ans[0])

Submission status: Wrong Answer

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

import copy

# 全探索？？

#pypy3なら通りそう

ans = [0]
arr = [0]*N


def rec(index, score, arr, ans):
    # print(arr)
    if index > Q-1:
        ans[0] = max(score, ans[0])
        return
    # 何もしない
    score1 = score
    score2 = score

    rec(index+1, score1, copy.deepcopy(arr), ans)

    if (arr[abcd[index][1]-1] - arr[abcd[index][0]-1] == abcd[index][2]) and arr[abcd[index][1]-1]!= 0 and arr[abcd[index][0]-1]!=0:
        # print("a")
        rec(index+1, score1 + abcd[index][-1], copy.deepcopy(arr), ans)
    else:
        

        # 追加する
        for i in range(M-abcd[index][2]):
            # print("a",M-abcd[index][2])
            if (arr[abcd[index][0]-1] == 0 or arr[abcd[index][1]-1] == 0):
                if arr[abcd[index][0]-1] !=0 and arr[abcd[index][0]-1] !=i + 1:continue
                if arr[abcd[index][1]-1] !=0 and arr[abcd[index][1]-1] !=i + 1 + abcd[index][2]:continue
                # arr1 = arr
                arr1 = copy.deepcopy(arr)
                arr1[abcd[index][0]-1] = i + 1
                arr1[abcd[index][1]-1] = i + 1 + abcd[index][2]
                # print("a",abcd[index][2])
                rec(index+1, score2 + abcd[index][-1], arr1, ans)


rec(0, 0, arr, ans)

print(ans[0])

Submission status: Wrong Answer

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

import copy

# 全探索？？

ans = [0]
arr = [0]*N


def rec(index, score, arr, ans):
    # print(arr)
    if index > Q-1:
        ans[0] = max(score, ans[0])
        return
    # 何もしない
    score1 = score
    score2 = score

    rec(index+1, score1, arr, ans)

    if (arr[abcd[index][1]-1] - arr[abcd[index][0]-1] == abcd[index][2]) and arr[abcd[index][1]-1]!= 0 and arr[abcd[index][0]-1]!=0:
        # print("a")
        rec(index+1, score1 + abcd[index][-1], arr, ans)
    else:
        

        # 追加する
        for i in range(M-abcd[index][2]):
            # print("a",M-abcd[index][2])
            if (arr[abcd[index][0]-1] == 0 or arr[abcd[index][1]-1] == 0):
                if arr[abcd[index][0]-1] !=0 and arr[abcd[index][0]-1] !=i + 1:continue
                if arr[abcd[index][1]-1] !=0 and arr[abcd[index][1]-1] !=i + 1 + abcd[index][2]:continue
                # arr1 = arr
                arr1 = copy.deepcopy(arr)
                arr1[abcd[index][0]-1] = i + 1
                arr1[abcd[index][1]-1] = i + 1 + abcd[index][2]
                # print("a",abcd[index][2])
                rec(index+1, score2 + abcd[index][-1], arr1, ans)


rec(0, 0, arr, ans)

print(ans[0])

Submission status: Wrong Answer

Solution code:
import itertools
#1~Mと仕切りN枚を適宜並べ、「左にあるかずの個数」を考える。
#1~N+Mの中からN個選ぶ
N,M,Q = [int(hoge) for hoge in input().split()]
L = list(itertools.combinations(range(1,M+N+1),N))
abcd = [[int(hoge) for hoge in input().split()] for q in range(Q)]

curmax = 0
for l in L:
    suretu = []
    score = 0
    for n,k in enumerate(l):#数列を実際に生成
        #(1,2,5,6,7,9)とか出てくるので…
        suretu.append(k-n-1)
    for a,b,c,d in abcd:
        if suretu[b-1] - suretu[a-1] == c:
            score += d
    curmax = max(curmax,score)
print(curmax)

Submission status: Wrong Answer

Solution code:
import itertools

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

for i in range(Q):
  abcd=[list(map(int,input().split()))]
 #重複組み合わせ

result=0
for A in itertools.combinations_with_replacement(range(1,M+1),N):
  t=0
  for a,b,c,d in abcd:
    if A[b-1]-A[a-1]==c:
      t+=d
  result=max(result,t)
print(result)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement


def main():
    n_ints, max_int, n_queries = map(int, input().split())

    queries = (list(map(int, input().split())) for _ in range(n_queries))

    possible_sequences = combinations_with_replacement(range(1, max_int+1), n_ints)

    max_score = 0
    for sequence in possible_sequences:
        score = 0

        for i, q in enumerate(queries):
            a, b, c, d = q
            if sequence[b-1] - sequence[a-1] == c:
                score += d

        max_score = max(max_score, score)

    print(max_score)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement


def main():
    n_ints, max_int, n_queries = map(int, input().split())

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

    possible_sequences = combinations_with_replacement(range(1, max_int+1), n_ints)

    max_score = 0
    for sequence in possible_sequences:
        score = 0

        for i, q in enumerate(queries):
            a, b, c, d = q
            if sequence[a-1] - sequence[b-1] == c:
                score += d

        max_score = max(max_score, score)

    print(max_score)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import combinations, product
n, m, q = [int(i) for i in sys.stdin.readline().split()]
ls = []
for i in range(q):
    a, b, c, d = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    ls.append([a, b, c, d])

def check_ls(ls, A):
    cnt = 0
    for a, b, c, d in ls:
        if A[b] - A[a] == c:
            cnt += d
    return cnt

_best = 0
que = [str(i) for i in list(range(1,1+m))]
res = []
def dfs(last, cur):
    flg = len(cur) < n - 1
    for i in range(last, 1+m):
        if flg:
            dfs(i, cur + str(i))
        else:
            res.append(cur + str(i))
dfs(0, "")
for i in res:
    i = [int(j) for j in i]
    _best = max(_best, check_ls(ls, i))
print(_best)

Submission status: Wrong Answer

Solution code:
N,M,Q = map(int,input().split())
List = [list(map(int, input().split())) for i in range(Q)]
ListA = [1]*(N+1)
ListB = []
S = 0


for a in range(N):
  for ListA[N-a] in range(ListA[N-1-a], M+1):
    for i in range (Q):
      if ListA[List[i][1]]-ListA[List[i][0]] == List[i][2]:
        S += List[i][3]
    ListB.append(S)
    S = 0
  
print(max(ListB))

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
a=[0]*Q
b=[0]*Q
c=[0]*Q
d=[0]*Q

for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int,input().split())
    a[i]-=1
    b[i]-=1    #Aのa[i],b[i]番目を指定したいから-1する。

def score(A):
    一つ一つの点数=0
    for ai,bi,ci,di in zip(a,b,c,d):
        if A[bi]-A[ai]==ci:
            一つ一つの点数+=di
    return 一つ一つの点数

def dfs(A):
    if len(A)==N:
        return score(A)
    暫定の点数=0
    数列=A[-1] if len(A)>0 else 0
    for v in range(数列,M+1):
        A.append(v)
        暫定の点数=max(暫定の点数,dfs(A))    #再帰関数を呼び出しながら暫定1位の点数を更新。
        A.pop()
    return 暫定の点数

print(dfs([]))

Submission status: Wrong Answer

Solution code:
import itertools


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

    A, B, C, D = [], [], [], []
    for _ in range(q):
        a, b, c, d = map(int, input().split())
        A.append(a)
        B.append(b)
        C.append(c)
        D.append(d)
    if m == 1:
        return 0
    ans = 0
    length = m+n-1
    for item in itertools.combinations(range(length), m-1):
        ptn = []
        num = 1
        p = 0
        for i in range(length):
            if i == item[p]:
                num += 1
                p += 1
                p = min(m-2, p)
                continue
            ptn.append(num)
        tmp = 0
        for k in range(q):
            if (ptn[B[k]-1] - ptn[A[k]-1]) == C[k]:
                tmp += D[k]

        ans = max(tmp, ans)
    return ans

print(main())

Submission status: Wrong Answer

Solution code:
from sys import stdin
import itertools
N,M,Q=list(map(int, (stdin.readline().strip().split())))
num_list=[]
ans_list=[]
score=[]
def judge(ans_list,num):
    count_list=[0]*N
    if (N*(N-1))//2==len(ans_list):return False
    sa=num[2]
    if len(ans_list)!=0:
        min=[ans_list[0][0],ans_list[0][2]]
        max=[ans_list[0][1],ans_list[0][2]]
    else:
        min=[10,0]
        max=[0,0]
    for a,b,c,d in ans_list:
        count_list[a-1]=1
        count_list[b-1]=1
        sa+=c
        if sa>M+1:return False
        if a==num[0] and b==num[1]:return False
        if a==num[0] and b<num[1] and c>num[2]:return False
        if a==num[0] and b>num[1] and c<num[2]:return False
        if b==num[1] and a<num[0] and c<num[2]:return False
        if b==num[1] and a>num[0] and c>num[2]:return False
        if min[0]>=a:min=[a,c+min[1]]
        if max[0]<=b:max=[b,c+max[1]]
    if min[0]==num[0] and max[0]==num[1]:
        c=count_list[num[0]-1:num[1]-1]
        if c.count(0)==0:
            if min[1]+max[1]!=num[2]:return False
        else:
            if min[1] + max[1] > num[2]: return False


    return True
def tansaku(ans_list,num_list,i):


    if i<len(num_list):
        a_list = ans_list.copy()
        b_list = ans_list.copy()
        if judge(ans_list,num_list[i]):
            b_list.append(num_list[i])
            tansaku(b_list,num_list,i+1)
        tansaku(a_list,num_list,i+1)
    else:
        ans=0
        for a,b,c,d  in ans_list:
            ans+=d
        score.append(ans)

for _ in range(Q):
    a,b,c,d=list(map(int, (stdin.readline().strip().split())))
    num_list.append([a,b,c,d])
tansaku(ans_list,num_list,0)
print(max(score))

Submission status: Wrong Answer

Solution code:
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**4)
from sys import stdin, stdout
import bisect            #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def modinv(n,p):
    return pow(n,p-2,p)
def cin():
    return map(int,sin().split())
def ain():                           #takes array as input
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
import math  
def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x 
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    if n not in d:
        return v
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            v=dfs(i,d,v,c+1)
            # print(f)
    return v
f=[]
import itertools
def printCombination(arr, n, r): 
    data = [0]*r; 
    combinationUtil(arr, data, 0,  
                    n - 1, 0, r); 

def combinationUtil(arr, data, start,  
                    end, index, r):
    global f   
    if (index == r): 
        ff=[]
        for j in range(r):
            ff.append(data[j]) 
        f.append(ff) 
        return; 
 
    i = start;  
    while(i <= end and end + 1 >= r - index): 
        data[index] = arr[i]; 
        combinationUtil(arr, data, i ,  
                        end, index + 1, r); 
        i += 1; 
"""*******************************************************"""
def main():
    n,m,q=cin()
    a=[]
    b=[]
    for _ in range(q):
        u,v,w,x=cin()
        b.append((u-1,v-1,w,x))
    for i in range(m):
        a.append(i+1)
    an=0
    printCombination(a,m,n)
    for c in f:
        # print(c)
        ans=0
        for i in b:
            if c[i[1]]-c[i[0]]==i[2]:
                ans+=i[3]
        # print(ans)
        an=max(ans,an)
    print(an)

######## Python 2 and 3 footer by Pajenegod and c1729
 
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
 
# So on cf, use PyPy2 for best string performance.
 
py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
 
import os, sys
from io import IOBase, BytesIO
 
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
 
# Cout implemented in Python
import sys
class ostream:
    def __lshift__(self,a):
        sys.stdout.write(str(a))
        return self
cout = ostream()
endl = '\n'
 
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
    conv = ord if py2 else lambda x:x
    A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
    try:
        while True:
            if s[i] >= b'R' [0]:
                numb = 10 * numb + conv(s[i]) - 48
            elif s[i] == b'-' [0]: sign = -1
            elif s[i] != b'\r' [0]:
                A.append(sign*numb)
                numb = zero; sign = 1
            i += 1
    except:pass
    if s and s[-1] >= b'R' [0]:
        A.append(sign*numb)
    return A
 
# threading.Thread(target=main).start()
if __name__== "__main__":
  main()

Submission status: Wrong Answer

Solution code:
n, m, q = list(map(int, input().split(" ")))
Q = [list(map(int, input().split(" "))) for i in range(q)]
Q = [[i[0] - 1, i[1] - 1, i[2], i[3]] for i in Q]

def calc(arr: list):
    return sum([i[3] for i in Q if arr[i[1]] - arr[i[0]] == i[2]])

def get_array(arr: list, num):
    if len(arr) == n:
        return calc(arr)
    return max([get_array(arr + [i], i) for i in range(num, m + 1)])

print(get_array([], 0))

Submission status: Wrong Answer

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


def dfs(x):
    if len(x) == N + 1:
        res = 0
        for a, b, c, d in X:
            if x[b] - x[a] == c:
                res += d
        return res

    res = 0
    x.append(x[-1])
    while x[-1] <= M:
        x[-1] += 1
        res = max(res, dfs(x))
    x.pop()

    return res

print(dfs([1]))

Submission status: Wrong Answer

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


def dfs(x):
    if len(x) == N + 1:
        res = 0
        for a, b, c, d in X:
            if x[b] - x[a] == c:
                res += d
        return res

    res = 0
    for i in range(1, 11):
        x.append(i)
        res = max(res, dfs(x))
        x.pop()

    return res

print(dfs([1]))

Submission status: Wrong Answer

Solution code:

def isContradicting(matrix):
    seq = [0]*n
    seq[0] = 1
    for j in range(len(matrix)):
        if seq[matrix[j][1]-1] == 0:
            seq[matrix[j][1]-1] = seq[matrix[j][0]-1] + matrix[j][2]
        elif seq[matrix[j][1]-1] != seq[matrix[j][0]-1] + matrix[j][2]:
            return True
    
    return False

import random

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

intMatrix = []

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

point = 0

seq = [0]*n
seq[0] = 1

intMatrix.sort(key=lambda x: x[3], reverse=True)

for i in range(100):
    intMatrixTmp = []
    point_tmp = 0
    for j in range(len(intMatrix)):
        intMatrixTmp.append(intMatrix[j])

        if isContradicting(intMatrixTmp) or (bool(random.getrandbits(1)) and j < 3):
            intMatrixTmp.pop()
        else:
            point_tmp += intMatrix[j][3]
    if point < point_tmp:
        point = point_tmp


print(point)

Submission status: Wrong Answer

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

intMatrix = []

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

point = 0

seq = [0]*n
seq[0] = 1

intMatrix.sort(key=lambda x: x[3], reverse=True)

for i in range(3):
    seq = [0]*n
    seq[0] = 1
    point_tmp = 0
    for j in range(len(intMatrix)):
        if j < i:
            continue
        if seq[intMatrix[j][1]-1] == 0:
            seq[intMatrix[j][1]-1] = seq[intMatrix[j][0]-1] + intMatrix[j][2]
        if seq[intMatrix[j][1]-1] == seq[intMatrix[j][0]-1] + intMatrix[j][2]:
            point_tmp += intMatrix[j][3]
    if point_tmp > point:
        point = point_tmp

print(point)

Submission status: Wrong Answer

Solution code:
import itertools

n, m, q = map(int, input().split())
a=[]
for i in range(q):
    a.append(list(map(int, input().split())))
 
l = list(range(1,m+1))
big_a = itertools.combinations_with_replacement(l, n)
list_a = list(big_a)
#print(list_a)
s = []
o = []
  
for j in range(len(list_a)):
    nowa = list_a[j]
    z = 0
    while z <= q-1:
        p = a[z]
        aa = int(p[0])
        bb = int(p[1])
        cc = int(p[2])
        dd = int(p[3])
        #a = int(aa)
        #b = int(bb)
        #c = int(cc)
        #d = int(dd)
       
        s_b = int(nowa[bb-1])
        s_a = int(nowa[aa-1])
        if s_b - s_a == cc:
            o.append(dd)
            #print(o)
        else:
            pass
        s.append(sum(o))
        z += 1
 
 
#print(s)
print(max(s))

Submission status: Wrong Answer

Solution code:
import itertools
 
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
 
l = list(range(1,m+1))
big_a = itertools.combinations_with_replacement(l, n)
list_a = list(big_a)
#print(list_a)
s = []
o = []

for j in range(len(list_a)):
    nowa = list_a[j]
    z = 0
    while z <= q-1:
        p = abcd[z]
        aa = int(p[0])
        bb = int(p[1])
        cc = int(p[2])
        dd = int(p[3])
        #a = int(aa)
        #b = int(bb)
        #c = int(cc)
        #d = int(dd)
       
        s_b = int(nowa[bb-1])
        s_a = int(nowa[aa-1])
        if s_b - s_a == cc:
            o.append(dd)
            #print(o)
        else:
            pass
        s.append(sum(o))
        z += 1
 
 
#print(s)
print(max(s))

Submission status: Wrong Answer

Solution code:
import itertools
 
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
 
l = list(range(1,m+1))
big_a = itertools.combinations_with_replacement(l, n)
list_a = list(big_a)
#print(list_a)
s = []
   

z = 0
o = []
for j in range(len(list_a)):
    nowa = list_a[j]
    while z <= q-1:
        p = abcd[z]
        aa = int(p[0])
        bb = int(p[1])
        cc = int(p[2])
        dd = int(p[3])
        #a = int(aa)
        #b = int(bb)
        #c = int(cc)
        #d = int(dd)
       
        s_b = int(nowa[bb-1])
        s_a = int(nowa[aa-1])
        if s_b - s_a == cc:
            o.append(dd)
            #print(o)
        else:
            pass
        s.append(sum(o))
        z += 1
 
 
#print(s)
print(max(s))

Submission status: Wrong Answer

Solution code:
n,m,q = [int(i) for i in input().split()]
p = []
for i in range(q):
  p.append([int(i) for i in input().split()])
dmax = 0
a = [1 for i in range(n)]
while True:
  d = 0
  for i in range(q):
    if a[p[i][1]-1]-a[p[i][0]-1] == p[i][2]:
      d += p[i][3]
  if d > dmax:
    dmax = d
  if a[0] == 10:
    break
  c = 1
  l = a.pop()
  while l==10:
    l = a.pop()
    c += 1
  for i in range(c):
    a.append(l+1)
    
print(dmax)

Submission status: Wrong Answer

Solution code:
n,m,q = [int(i) for i in input().split()]
p = []
for i in range(q):
  p.append([int(i) for i in input().split()])
dmax = 0
a = [1 for i in range(n)]
while not a[0]==10:
  c = 1
  l = a.pop()
  while l==10:
    l = a.pop()
    c += 1
  for i in range(c):
    a.append(l+1)
  d = 0
  for i in range(q):
    if a[p[i][1]-1]-a[p[i][0]-1] == p[i][2]:
      d += p[i][3]
  if d > dmax:
    dmax = d
    
print(dmax)

Submission status: Wrong Answer

Solution code:
n, m, q = 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] = map(int, input().split())

a = [1]
ans = 0
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

dfs(a)
print(ans)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import queue
import math

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

A,B,C,D = [],[],[],[]
seq = ""

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

q = queue.Queue()
ion = 1
order = 1
point = 0
ans = 0

for i in range(1,m+1):
    q.put([i])

while order < n:
    ion = q.get()
    order = len(ion)
    top = ion[0]
    if order == n:
        q.put(ion)
        break
    for i in range(1,top + 1):
        nice = ion.copy()
        nice.reverse()
        nice.append(i)
        nice.reverse()
        q.put(nice)

while not q.empty():
    point = 0
    tre = q.get()
    print(tre)
    for i in range(Q):
        a = A[i] - 1
        b = B[i] - 1
        c = C[i]
        d = D[i]
        a1 = int(tre[a])
        a2 = int(tre[b])
        a3 = a2 - a1
        if a3 == c:
            point += d
    ans = max(point,ans)

print('{}'.format(ans))

Submission status: Wrong Answer

Solution code:
n,m,q = map(int,input().split())
a,b,c,d = [],[],[],[]
ans = 0
A = [1]

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)

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

    for i in range(A[-1], m+1):
        A.append(i)
        dfs(A)
        print(A)
        A.pop() #最後の要素を削除
dfs(A)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

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

ancer = 0
ancerL = []
    

for k in range(1,M+1):
    p = [k]
        
    
    for i in range(k,M+1):
        while not len(p) == N-1:
            p += [i]
        
        if len(p) == N-1:
            for j in range(i,M+1):
                s = p + [j]
                # print(s)
                ancer = 0
                for t in range(len(q)):
                    if (s[q[t][1]-1] - s[q[t][0]-1]) == q[t][2]:
                        ancer += q[t][3]
        
                ancerL.append(ancer)
                
            p.pop(-1)
            
        
    
print(max(ancerL))

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
A=[0]*Q
B=[0]*Q
C=[0]*Q
D=[0]*Q
for i in range(Q):
    a,b,c,d=map(int,input().split())
    A[i]=a-1
    B[i]=b-1
    C[i]=c
    D[i]=d
smax=0
def dfs(L):
    global smax
    # 終端条件 --- N 重ループまで回したら処理して打ち切り
    if len(L) == N:
        s=0
        for i in range(Q):
            if L[B[i]] - L[A[i]] == C[i]:
                s+=D[i]
        if s>smax:
            smax=s
        return
    for v in range(1,M+1):
        L.append(v)
        dfs(L)
        L.pop()

dfs([])#L=[]のリストとなている
print(smax)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
A=[]
for i in range(q):
  a[i],b[i],c[i],d[i]=map(int,input().split())
def score(A):
  sum=0
  for (ai,bi,ci,di) in zip(a,b,c,d):
    if A[bi-1]-A[ai-1]==ci:
      sum+=di
  return sum
def fbs(A):
  if len(A)==n:
    return score(A)
  if len(A)>0:
    pre_last=A[-1]
  else:
    pre_last=0
  res=0
  for v in range(pre_last,m+1):
    A.append(v)
    res=max(res,fbs(A))
    A.pop()
  return res

print(fbs(A))

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
A=[]
for i in range(q):
  a[i],b[i],c[i],d[i]=map(int,input().split())
def score(A):
  sum=0
  for (ai,bi,ci,di) in zip(a,b,c,d):
    if A[ai-1]-A[bi-1]==ci:
      sum+=di
  return sum
def fbs(A):
  if len(A)==n:
    return score(A)
  res=0
  for v in range(1,m+1):
    A.append(v)
    res=max(res,fbs(A))
    A.pop()
  return res

print(fbs(A))

Submission status: Wrong Answer

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

moji=""
for i in range(1,m+1):
  i=str(i)
  moji+=i  
  
kouho=list(it.combinations_with_replacement(moji,n))

lis=[]
for i in range(q):
  lis.append(list(map(int,input().split())))
  
keep=0
for i in range(0,len(kouho)):
  attack=kouho[i]
  point=0
  for j in range(0,q):
    a=lis[j][0]
    b=lis[j][1]
    c=lis[j][2]
    d=lis[j][3]
    if int(attack[b-1])-int(attack[a-1])==c:
      point+=d
  if point>keep:
    keep=point
print(keep)