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())
cond = []

for qq in range(q):
    a,b,c,d = map(int, input().split())
    cond.append([a,b,c,d])

cond.sort()
#print(cond)
finalres = 0
dat = [1] * n
while dat[0] == 1:
    dat[n - 1] += 1
    for i in range(n - 1, 0, -1):
        if dat[i] > 10:
            dat[i] = dat[i - 1]
            dat[i - 1] += 1
        else:
            break
    ispass = False
    for i in range(n - 1, 0, -1):
        if dat[i] < dat[i - 1]:
            ispass = True
            break
    if ispass:
        continue

    #print("Try:", dat)

    can = True
    res = 0
    for i in range(q):
        a, b, c, d = cond[i]
        a, b = a-1, b-1
        if dat[b] - dat[a] == c:
            #print(" > match i{0} -i{1} == c gain {2}".format(b, a, d))
            res += d
    finalres = max(finalres, res)
print(finalres)

Submission status: Wrong Answer

Solution code:
import itertools

N, M, Q = list(map(int,input().split()))
numbers = [i for i in range(1, M+1)]
array = itertools.combinations(numbers, N)

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

max_score = 0
for combi in array:
    score = 0
    for _ in range(Q):
        a, b, c, d = combinations[_]
        a = a - 1
        b = b - 1
        if combi[b] - combi[a] == c:
            score += d
    if score > max_score:
        max_score = score
    
print(max_score)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement
n,m,q=map(int,input().split())
l=list(combinations_with_replacement(range(1,m+1),n))

res=0
lt=[list(map(int,input().split())) for _ in range(q)]

for j in range(len(l)):
    tmp=0
    for i in range(len(lt)):
        a,b,c,d = lt[i][0],lt[i][1],lt[i][2],lt[i][3]
        if l[j][b-1]-l[j][a-1] == c:
            tmp += d
    print(tmp)
    res=max(tmp,res)

print(res)

Submission status: Wrong Answer

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions

t = stdin.readline().rstrip().split()
N = int(t[0])
M = int(t[1])
Q = int(t[2])

data = []
for el in range(Q):
    tt = stdin.readline().rstrip().split()
    a = int(tt[0])-1
    b = int(tt[1])-1
    c = int(tt[2])
    d = int(tt[3])
    data.append([a, b, c, d])

tes = 0
for pos in range(N):
    row = [1] * N
    res = 0
    for el in data:
        if (row[el[1]] - row[el[0]]) == el[2]:
            continue
        else:
            if el[0] == pos:
                row[el[1]] = row[el[0]] + el[2]
    for el in data:
        if (row[el[1]] - row[el[0]]) == el[2]:
            res += el[3]
    else:
        if res > tes and max(row) <= M:
            tes = res
print(tes)

Submission status: Wrong Answer

Solution code:
import itertools

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

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)

List = list(itertools.combinations(L, N))
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:
n, m, q = map(int, input().split())
ps_ = [list(map(int, input().split())) for _ in range(q)]

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

score = 0
s = [0] * n
for p in ps:
    a, b, c, d = p
    if s[a] == 0 and s[b] == 0:
        s[a] = 1
        s[b] = 1 + c
        score += d
    elif s[a] == 0:
        s[a] = s[b] - c
        score += d
    elif s[b] == 0:
        s[b] = s[a] + c
        score += d
    elif s[b] == s[a] + c:
        score += d
    print(p, ":", s, ":", score)
print(score)

Submission status: Wrong Answer

Solution code:
from itertools import product


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

    mm = [_ for _ in range(1, m+1)]
    m_list = product(mm, repeat=n)

    ans = 0
    for l in m_list:
        tmp = 0
        for data in abcd:
            if l[data[1]-1] - l[data[0]-1] == data[2]:
                tmp += data[3]
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools

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

l = [i for i in range(1, m+1)]

p = itertools.product(l, repeat=n)
list_p = list(p)
cmb = []
for p in list_p:
  cmb.append(list(p))



ans = 0

# 処理系
for cm in cmb:
  tmp = 0
  for d in data:
    b_point = d[1]
    a_point = d[0]
    c_point = d[2]
    d_point = d[3]
    if cm[b_point - 1] - cm[a_point - 1] == c_point:
      tmp += d_point

  ans = max(ans, tmp)


print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q = map(int ,input().split())
lst = [list(map(int, input().split())) for _ in range(q)]
import itertools
l = [i+1 for i in range(m)]
lst2 = [com for com in itertools.combinations(l, n)]
res = 0
for com in lst2:
    score = 0
    for l in lst:
        if com[l[1]-1] - com[l[0]-1] == l[2]:
            score += l[3]
    if res < score:
        res = score 
    #print(score)
print(res)

Submission status: Wrong Answer

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions

t = stdin.readline().rstrip().split()
N = int(t[0])
M = int(t[1])
Q = int(t[2])

data = []
for el in range(Q):
    tt = stdin.readline().rstrip().split()
    a = int(tt[0])-1
    b = int(tt[1])-1
    c = int(tt[2])
    d = int(tt[3])
    data.append([a, b, c, d])

tes = -10
for pos in range(N):
    row = [1] * N
    res = 0
    for el in data:
        if (row[el[1]] - row[el[0]]) == el[2]:
            continue
        else:
            if el[0] == pos:
                row[el[1]] = row[el[0]] + el[2]
    for el in data:
        if (row[el[1]] - row[el[0]]) == el[2]:
            res += el[3]
    else:
        if res > tes and max(row) <= M:
            tes = res
print(tes)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,m,q = map(int,readline().split())
lst1 = []
for i in range(q):
    a,b,c,d = map(int,readline().split())
    lst1.append([a-1,b-1,c,d])

def func(lst):
    res = 0
    for a,b,c,d in lst1:
        if lst[b]-lst[a] == c:
            res += d
    return res
thresh = n-1
def func2(lst): #次の状態にする
    global thresh
    dig = n-1
    while True:
        if lst[dig] == m:
            if dig == thresh:
                thresh -= 1
                for i in range(thresh,n):
                    lst[i] = 2
                return lst
            else:
                dig -= 1
        else:
            lst[dig] += 1
            break
    return lst

def dfs(lst):
    ans = 0
    while True:
        if all(i==m for i in lst):break
        #print(lst)
        ans = max(ans,func(lst))
        lst = func2(lst)
    return ans

print(dfs([1]*n))

Submission status: Wrong Answer

Solution code:
a,b,n = map(int,input().split())
import math
def func(x):
    y = math.floor(a*x/b)-a*math.floor(x/b)
    return y

if b > n:
    print(func(n))
else:
    while True:
        if n % b == b-1:
            print(func(n))
            break
        else:
            n += -1

Submission status: Wrong Answer

Solution code:
import itertools
N,M,Q=map(int,input().split())
a_b_c_d=[list(map(int,input().split())) for _ in range(Q)]
nums=[i for i in range(1,M+1)]
A_s=itertools.permutations(nums,N)
ans=0
for A in A_s:
    flg=1
    for n in range(N-1):
        if A[n+1]<A[n]:
            flg=0
            break
    if flg:
        tmp=0
        for i in range(Q):
            if A[a_b_c_d[i][1]-1]-A[a_b_c_d[i][0]-1]==a_b_c_d[i][2]:
                tmp+=a_b_c_d[i][3]
        if tmp>ans:
            ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
point=0
while Q>0:
  a,b,c,d=map(int,input().split())
  l=b-a+M-N
  s=b-a
  if s<=c<=l:
    point=point+d
  Q=Q-1
print(point)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import itertools

def chkA(A, Q, a, b, c, d):
    ret=0
    for i in range(Q):
        ret += d[i] if A[b[i]-1] - A[a[i]-1] == c[i] else 0
    return ret            

def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    P=[i+1 for i in range(M+N)]
    B=[0]*N
    ans=0
    for A in itertools.combinations(P, N):
        for i in range(N):
            B[i]=A[i]-i
        ans = max(ans, chkA(B, Q, a, b, c, d))
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def next(A):
    if A[n - 1] < m:
        A[n - 1] += 1
        return A
    for i in reversed(range(n - 1)):
        if A[i] < A[i + 1]:
            A[i] += 1
            return A
    return None

A = [1] * n
max_score = 0
while A is not None:
    score = 0
    for i in range(q):
        if A[b[i] - 1] - A[a[i] - 1] == c[i]:
            score += d[i]
    if score > max_score:
        max_score = score
    A = next(A)
print(max_score)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q=map(int,input().split())
l=[]
for i in range(q):
  a=list(map(int,input().split()))
  l.append(a)
#print(l)
ans=0
aa=0
for now in itertools.combinations(range(1,m+1), n):
  aa+=1
  ansn=0
  #print(now)
  for i in l:
    if now[i[1]-1]-now[i[0]-1]==i[2]:
      ansn+=i[3]
  #print(ansn)
  if ansn>ans:
    ans=ansn
#print(aa)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q = map(int ,input().split())
lst = [list(map(int, input().split())) for _ in range(q)]
import itertools
l = [i+1 for i in range(m)]
lst2 = [com for com in itertools.combinations(l, n)]
res = 0
for com in lst2:
    score = 0
    for l in lst:
        if com[l[1]-1] - com[l[0]-1] == l[2]:
            score += l[3]
    if res < score:
        res = score 
    print(score)
print(res)

Submission status: Wrong Answer

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

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

top = 0
for base in A:
    res = 0
    for i in range(len(SU)):
        a,b,c,d = SU[i][0], SU[i][1], SU[i][2], SU[i][3]
        if base[b-1] - base[a-1] == c:
            res += d
        if res >= top:
            top = res

print(top)

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)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
q_list=[]
for i in range(q):
    a=list(map(int,input().split()))
    q_list.append(a)
import itertools
ans=0
for i in itertools.combinations_with_replacement([1,2,3,4,5,6,7,8,9,10],n):
#     i=sorted(i)
    count=0
    for a,b,c,d in q_list:
        if i[b-1]-i[a-1]==c:
            count+=d
    ans=max(count,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

def main():
    m = 100
    n, m, q = input_list()
    queries = []
    for _ in range(q):
        queries.append(input_list())

    a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ans = []
    for i in itertools.combinations_with_replacement(a, n):
        ans.append(calc(i, queries))
    print(max(ans))


def calc(i, queries):
    ans = 0
    for q in queries:
        a, b, c, d = q
        if i[b-1] - i[a-1] == c:
            ans += d
    return ans

def input_list():
    return list(map(int, input().split()))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools

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

seleted = [x for x in range(1, m+1)]
A_list = list(itertools.combinations(seleted, n))
# print(A_list)
for A in A_list:
    tmp = 0
    for q in q_list:
        if A[q[1]-1] - A[q[0]-1] == q[2]:
            tmp += q[3]
    # print(tmp)
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q=map(int,input().split())
l=[]
for i in range(q):
  a=list(map(int,input().split()))
  l.append(a)
#print(l)
ans=0
aa=0
for noww in itertools.permutations([i for i in range(1,m+1)], r=n):
  aa+=1
  ansn=0
  now=sorted(noww)
  #print(now)
  for i in l:
    if now[i[1]-1]-now[i[0]-1]==i[2]:
      ansn+=i[3]
  if ansn>ans:
    ans=ansn
#print(aa)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
q_list=[]
for i in range(q):
    a=list(map(int,input().split()))
    q_list.append(a)
import itertools
ans=0
for i in itertools.combinations_with_replacement([1,2,3,4,5,6,7,8,9,10],10):
#     i=sorted(i)
    count=0
    for a,b,c,d in q_list:
        if i[b-1]-i[a-1]==c:
            count+=d
    ans=max(count,ans)
print(ans)

Submission status: Wrong Answer

Solution code:

def next_A(A):
    rev_A = list(reversed(A))
    max_num = M
    if rev_A[0] < M:
        rev_A[0] = rev_A[0] + 1
        return list(reversed(rev_A))
    
    for i in range(N):
        if rev_A[i] < max_num:
            rev_A[i] = rev_A[i] + 1
            for j in reversed(range(i)):
                rev_A[j] = rev_A[j+1]
            return list(reversed(rev_A))
        max_num = rev_A[i]
    return [-1]



def make_A():
    A = []
    for i in range(N):
        A.append(i+1)
    return A

def solve(A):
    ans = 0
    for i in range(Q):
        if(A[b[i]] - A[a[i]] == c[i]):
            ans = ans + d[i]
    return ans




#A = []
#len(A) == N
#A[b[i]] - A[a[i]] == c[i]

N,M,Q = (-1,-1,-1)
a = []
b = []
c = []
d = []


N,M,Q = map(int,input().split())
for i in range(Q):
    A,B,C,D = map(int,input().split())
    a.append(A-1)
    b.append(B-1)
    c.append(C)
    d.append(D)
    
A = make_A()
ans = 0
while(-1 not in A):
    #print(A)
    ans = max(solve(A),ans)
    A = next_A(A)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q = map(int,input().split())
s = [list(map(int, input().split())) for _ in range(q)]
#print(s)

maxi = 0
l = [1,2,3,4,5,6,7,8,9,10]
for a in itertools.combinations_with_replacement(l, n):
	#print(a)
	pnt = 0
	for i in range(q):
		#print(s[i][1]-1,s[i][0]-1)

		if a[s[i][1]-1]-a[s[i][0]-1] == s[i][2]:
			#print(a[s[i][1]-1],a[s[i][0]-1] )
			
			pnt += s[i][3]
	#print(pnt)
	if pnt >maxi:
		maxi = pnt
		#print(a)
print(maxi)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import combinations_with_replacement, accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M, Q = MAP()
graph = [[] for _ in range(N)]
abcd = [LIST() for _ in range(Q)]
ans = 0

for x in combinations_with_replacement(range(1, 11), N):
	tmp = 0
	for a, b, c, d in abcd:
		a -= 1
		b -= 1
		if x[b]-x[a] == c:
			tmp += d
	ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    n, m, q = map(int, args[0].split())
    ABCD = [tuple(map(int, abcd.split())) for abcd in args[1:]]

    ret = 0
    stack = [(0, '')]
    while stack:
        d, s = stack.pop()
        S = s+str(d)
        if len(S) == n:
            ret = max(
                ret,
                sum([d for a, b, c, d in ABCD if int(S[b-1])-int(S[a-1]) == c])
            )

            continue

        for i in range(d, 10):
            stack.append((i, S))

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

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)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        print(a,b,c,d)
        if int(A[b-1])-int(A[a-1]) == c:
            pointA += d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, m, q = map(int, input().split())
abcd = [0] * q
for i in range(q):
    abcd[i] = list(map(int, input().split()))
abcd.sort()
ans = 0
for s in range(1 << q):
    li = [-100] * (n + 1)
    li[1] = 1
    sm = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if s >> i & 1:
            if li[a] == li[b] == -100:
                li[a] == max([1] + li[:a])
                li[b] = li[a] + c
            elif li[a] == -100:
                li[a] = li[b] - c
                if li[a] < 1:
                    break
            elif li[b] == -100:
                li[b] = li[a] + c
                if li[b] > m:
                    break
            else:
                if li[b] - li[a] != c:
                    break
            sm += d
    if not all(li[i + 1] == -100 or li[i] <= li[i + 1] for i in range(n)):
        continue
    ans = max(ans, sm)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
q_list=[]
for i in range(q):
    a=list(map(int,input().split()))
    q_list.append(a)
import itertools
ans=0
for i in itertools.combinations_with_replacement([1,2,3,4,5,6,7,8,9],10):
#     i=sorted(i)
    count=0
    for a,b,c,d in q_list:
        if i[b-1]-i[a-1]==c:
            count+=d
    ans=max(count,ans)
print(ans)

Submission status: Wrong Answer

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

import itertools
l = range(1, m+1)
sums = [0]
for v in itertools.combinations(l, n):
  v = list(v)
  sum = 0
  for a, b, c, d in tokutens:
    if v[int(b)-1] - v[int(a)-1] == int(c):
      sum+=int(d)
  sums.append(sum)

sums.sort(reverse=True)
print(sums[0])

Submission status: Wrong Answer

Solution code:
import random

a,b,n = map(int,input().split())
ans = 0

for i in range(10 ** 6):
    num = random.randint(0,n)
    ans = max(ans,(a * num)//b - a * (num//b))
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, m, q = map(int, input().split())
abcd = [0] * q
for i in range(q):
    abcd[i] = list(map(int, input().split()))
abcd.sort()
ans = 0
for s in range(1 << q):
    li = [-100] * (n + 1)
    li[1] = 1
    sm = 0
    for i in range(q):
        a, b, c, d = abcd[i]
        if s >> i & 1:
            if li[a] == li[b] == -100:
                li[a] == max([1] + li[:a])
                li[b] = li[a] + c
            elif li[a] == -100:
                li[a] = li[b] - c
                if li[a] < 1:
                    break
            elif li[b] == -100:
                li[b] = li[a] + c
                if li[b] > m:
                    break
            else:
                if li[b] - li[a] != c:
                    break
            sm += d
    if not all(li[i] <= li[i + 1] for i in range(n)):
        continue
    ans = max(ans, sm)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N, M, Q = list(map(int, input().split()))
a = []
b = []
c = []
d = []

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

l = []

for i in range(M):
    l.append(i+1)

S = []

for v in itertools.product(l, repeat=N):
    S.append(list(v))

ans = 0

for i in S:
    cou = 0

    for j in range(Q):
        if i[b[j]-1] - i[a[j]-1] == c[j]:
            cou += d[j]
    if ans < cou:
        ans = cou
    cou = 0

print(ans)

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

zLi = itertools.combinations_with_replacement(list(range(m + 1)), n)
for t in zLi:
    _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:
def f(A, i, N, reqs, M, Q):
    if i == N:
        res = 0
        for i in range(Q):
            a,b,c,d = reqs[i]
            if A[b] - A[a] == c:
                res += d
        return res
    b = 0
    for n in range(A[i-1],M+1):
        A[i] = n
        b = max(b, f(A,i+1,N,reqs,M,Q))
    return b

def main():
    N,M,Q = [int(_n) for _n in input().split()]
    reqs = []
    for i in range(Q):
        a,b,c,d = [int(_n) for _n in input().split()]
        a-=1
        b-=1
        reqs.append((a,b,c,d))
    print(f([0]*N,1,N,reqs,M,Q))
main()

Submission status: Wrong Answer

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

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

# 数列の作成
import itertools
items = list(range(1, m+1))

combs = itertools.combinations(items, n)
ans = 0
for comb in combs:
  point = 0

  for abcd in abcds:
    if (int(comb[abcd[1]-1])-int(comb[abcd[0]-1]) == abcd[2]):
      point += abcd[3]
  ans = max(ans, point)

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
nmq = input()
[n, m, q] = [int(comp) for comp in nmq.split()]
abcd = []
for i in range(q):
  temp = input()
  abcd.append([int(comp) for comp in temp.split()])
a = np.ones(n, dtype=int)
maxi = 0


def search(i, mini):
  global maxi
  global a
  if(i == n):
    temp = np.array([comp[3] for comp in abcd if (
        a[comp[1] - 1] - a[comp[0] - 1] == comp[2])]).sum()
    maxi = max(temp, maxi)
    return 0
  for j in range(mini, 11):
    a[i] = j
    search(i + 1, j)


search(0, 1)
print(maxi)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(q)]
ans=0
for i in combinations_with_replacement([i for i in range(1,11)], n):
    tmp=0
    for a,b,c,d in l:
        if i[b-1]-i[a-1]==c:
            tmp+=d
    ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from itertools import combinations
N, M, Q = map(int, input().split())
# N, M, Q = 3, 4, 3

Qi = np.zeros((Q, 4))

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


Mi = np.arange(1, M+1)
maxD = 0
for listMi in (list(combinations(Mi, N))):
    d = 0
    for i in range(Q):
        if (int(listMi[int(Qi[i][1]-1)]-listMi[int(Qi[i][0]-1)]) == int(Qi[i][2])):
            d += Qi[i][3]
    if d>maxD:
        maxD = d
print (int(maxD))

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int, input().split())
List = [list(map(int, input().split())) for i in range(Q)]
Max = 0

for a in itertools.permutations(range(1, M+1), N):
  A = list(a)
  A.sort()
  point = 0
  for i in range(Q):
    ai = List[i][0]
    bi = List[i][1]
    if 1 <= ai <= N and 1 <= bi <= N:
      if A[bi-1]-A[ai-1] == List[i][2]:
        point += List[i][3]
    if Max < point:
      Max = point

print(Max)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, m, q = map(int, input().split())
    queue = []
    for i in range(q):
        queue.append(list(map(int, input().split())))
    ans = 0
    import itertools
    l = list(itertools.combinations(list(range(1, m + 1)) * n, n))
    for i in l:
        x = 0
        for j in queue:
            if i[j[1] - 1] - i[j[0] - 1] == j[2]:
                x += j[3]
        ans = max(ans, x)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import itertools

N, M, Q = map(int, input().split())
ABCD = []
for _ in range(Q):
    ABCD.append(list(map(int, input().split())))
M_list = [i+1 for i in range(M)]
N_list = itertools.combinations(M_list, N)
max_point = 0
for n in N_list:
    point = 0
    for abcd in ABCD:
        if n[abcd[1]-1] - n[abcd[0]-1] == abcd[2]:
            point += abcd[3]
    if max_point < point:
        max_point = point
print(max_point)

Submission status: Wrong Answer

Solution code:
def make_arr(x, arr):
    if len(arr) == n:
        pattern.append(arr)
    else:
        for i in range(x, 11):
            make_arr(i, arr + [i])

n, m, q = map(int, input().split())
A = [m] * n
query = [list(map(int, input().split())) for _ in range(q)]
pattern = []

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

ans = 0
for p in pattern:
    point = 0
    for a, b, c, d in query:
        a -= 1
        b -= 1
        if c == p[b] - p[a]:
            point += d
    ans = max(ans, point)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(2**(N+M)):
    cnt = 0
    mm = bin(i)
    for j in list(str(mm)):
        if j=="1":
            cnt += 1
    if cnt != M:
        continue
    
    li = [0]
    mm2 = 1
    for j in range(N+M):
        if (i>>j)&1:
            mm2 += 1
        else:
            li.append(mm2)
    
    ttl = 0
    for j in range(Q):
        a,b,c,d = ab[j]
        if li[b]-li[a]==c:
            ttl += d
    ans = max(ans,ttl)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def rangeI(it, l, r):
    for i, e in enumerate(it):
        if l <= i < r:
            yield e
        elif l >= r:
            break

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

MOD = 10**9+7

n,m,q = get_nums_l()
abcd = []
for _ in range(q):
    s = input()
    abcd.append(list(map(int, s.split())))
#abcd = list(map(lambda s: list(map(int, s.split())), map(lambda s: s.strip(), sys.stdin.readlines())))

def solve(A):
    score = 0
    for query in abcd:
        if A[query[1]-1] - A[query[0]-1] == query[2]:
            score += query[3]
    return score


ans = 0

def dfs(A):
    global ans
    if len(A) == n:
        # log(A)
        ans = max(ans, solve(A))
    elif len(A) == 0:
        for a in range(1, 11):
            dfs([a])
    else:
        for a in range(A[-1], 11):
            dfs(A+[a])

dfs([])
print(ans)

Submission status: Wrong Answer

Solution code:
import random as rng
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)


def dump_impl(*objects):
    print(*objects, file=sys.stderr)


def dump_dummy(*objects):
    pass


dump = dump_impl if "DEBUG" in sys.argv else dump_dummy

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

dump(N, M, Q)
dump(a)
dump(b)
dump(c)
dump(d)
ans = 0
for A in it.combinations(range(1, M+1), N):
    A = list(A)
    x = [0]
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            x.append(d[i])
    dump(x, A)
    ans = max(ans, sum(x))
print(ans)