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
import math
import itertools

def main():
    input = sys.stdin.readline
    N, M, Q = map(int, input().split())
    A = list(itertools.combinations(range(1, M+1), N))
    l = []
    m = 0
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        l.append([a, b, c, d])
    for a_list in A:
        tmp = 0
        for i in l:
            if a_list[i[1] -1] - a_list[i[0] - 1] == i[2]:
                tmp += i[3]
        if tmp > m:
            m = tmp
    return m


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

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

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

import itertools
seq = list(range(1,m+1))
lis_p = list(itertools.combinations(seq, n))

ans = 0
ans_max = 0

for li_p in lis_p:
    for li in lis:
        if li_p[li[1]-1] - li_p[li[0]-1] == li[2]:
            ans += li[3]
    #print(li_p, ans)
    if ans > ans_max:
        ans_max = ans
    ans = 0

print(ans_max)

Submission status: Wrong Answer

Solution code:

import itertools

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

def solve(N, M, constraints):
    max_so_far = 0
    for seq in itertools.combinations(range(1, M + 1), N):
        #print("Processing sequence: ", seq)
        curr_score = 0
        for const in constraints:
            a,b,c,d = const

            if seq[b-1] - seq[a-1] == c:
                curr_score += d

        max_so_far = max(max_so_far, curr_score)

    return max_so_far


N, M, Q = iln()
constraints = []
for i in range(Q):
    constraint = tuple(iln())
    constraints.append(constraint)

print(solve(N, M, constraints))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A=[list(map(int,input().split())) for _ in range(Q)]
num=0
for i in A:
  if i[1]-i[0]==i[2] and i[3]>0:
    num+=i[3]
    
print(num)

Submission status: Wrong Answer

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

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

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

ans = 0
for i in range(2**M+1):
    num = 0
    tmp = []
    if(bin(i).count("1") != N):
        continue
    for j in range(M+1):
        if((i>>j)&1):
            tmp.append(j+1)
    for k in range(len(sA)):
        if(tmp[sA[k][1]-1]-tmp[sA[k][0]-1] == sA[k][2]):
            num += sA[k][3]
    ans = max(ans, num)

print(ans)

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
s=[list(map(int, input().split())) for i in range(Q)]
def ju(N,a,l):
    k=[]
    if N==1:
        for i in range(a,M+1):
            k+=[l+[i]]
    else:
        for i in range(a,M+1):
            k+=ju(N-1,i,l+[i])
    return k
l=ju(N,0,[])
a=0
for i in l:
    t=0
    for j in s:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            t+=j[3]
    if t>a:
        a=t
print(a)

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:
        ans = max(ans, solve(A))
    elif len(A) == 0:
        for a in range(11):
            dfs([a])
    else:
        for a in range(A[-1], 11):
            dfs(A+[a])

dfs([])
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
def nasu(s, l):
  n = N - 2
  T = []
  for i in range(s, l + 1):
    T.append(i)
  L = list(itertools.combinations_with_replacement(T, n))
  ans = 0
  for i in L:
    P = [s] + list(i) + [l]
    ans = max(ans, honya(P))
  return ans

def honya(L):
  ans = 0
  for a, b, c, d in abcd:
    a -= 1
    b -= 1
    if L[b] - L[a] == c:
      ans += d
  return ans

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

ans = 0
for i in range(1, 11):
  for j in range(i, 11):
    ans = max(ans, nasu(i, j))

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

ans = 0

nums = range(1,M+1)
arrs = itertools.combinations(nums, N)

for a in arrs:
  _sum = 0
  a = list(a)
  for b in arr:
    if a[b[1]-1]-a[b[0]-1]==b[2]:
      _sum += b[3]
  ans = max(ans,_sum)
  
print(ans)

Submission status: Wrong Answer

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

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


def ge(num, keta):
    temp = num // (10**(N - keta - 1))
    return temp % 10


ans = 0
for i in range(999999999):
    cnt = 0
    dame = False
    for kaku in range(N - 1):
        if ge(i, kaku) > ge(i, kaku + 1):
            dame = True

    if dame:
        continue

    if i >= (10**(N - 1)):
        break
    for j in range(Q):
        if ge(i, query[j][1] - 1) - ge(i, query[j][0] - 1) == query[j][2]:
            cnt += query[j][3]

    ans = max(ans, cnt)

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.combinations(l, N):
    S.append(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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools 
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N,M,Q=nm()
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]=nm()

A_lb=[i+1 for i in range(M)]
A_cmb=list(itertools.combinations_with_replacement(A_lb,N))

sumd=0
sumd_max=0
for alist in A_cmb:
    if(sumd>sumd_max):
        sumd_max=sumd
    sumd=0
    for i in range(Q):
        if(alist[b[i]-1]-alist[a[i]-1]==c[i]):
            sumd+=d[i]

print(sumd_max)

Submission status: Wrong Answer

Solution code:
N,M,Q=map(int,input().split())
s=[list(map(int, input().split())) for i in range(Q)]
def ju(N,a,l):
    k=[]
    if N==1:
        for i in range(a,11):
            k+=[l+[i]]
    else:
        for i in range(a,11):
            k+=ju(N-1,i,l+[i])
    return k
l=ju(N,0,[])
a=0
for i in l:
    t=0
    for j in s:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            t+=j[3]
    if t>a:
        a=t
print(a)

Submission status: Wrong Answer

Solution code:
n, m, q =  list(map(int, input().split()))
aa = []
bb =[]
cc = []
dd =[]
for i in range(q):
    a,b,c,d =  list(map(int, input().split()))
    aa.append(a)
    bb.append(b)
    cc.append(c)
    dd.append(d)

def ev(A):
    s = 0
    for i in range(q):
        if A[bb[i] - 1] - A[aa[i] - 1] == cc[i]:
            s += dd[i]
    return s

A = [0] * n
s = ev(A)
s_pre = s
while True:
    for i in range(1, n + 1):
        for k in range(q):
            if aa[k] == i:
                tmp = A[bb[k] - 1]
                if cc[k] + A[aa[k] - 1] > m:
                    continue
                if cc[k] + A[aa[k] - 1] < max(A[:bb[k] - 1]):
                    continue
                A[bb[k] - 1] = cc[k] + A[aa[k] - 1]
                ss = ev(A)
                if ss > s:
                    s = ss
                else:
                    A[bb[k] - 1] = tmp
    if s_pre == s:
        break
    else:
        s_pre = s
print(s)

Submission status: Wrong Answer

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

aq = [None] * q
bq = [None] * q
cq = [None] * q
dq = [None] * q
for i in range(q):
    aq[i], bq[i], cq[i], dq[i] = list(map(int, input().split(' ')))

result = 0

def dfs(i, ary):
    global result
    if i == n:
        val = 0
        for i in range(q):
            if ary[bq[i] - 1] - ary[aq[i] - 1] == cq[i]:
                val += dq[i]
        if result < val:
            result = val
    else:
        if ary == []:
            for c in range(0, m + 1):
                dfs(i + 1, [c])
        else:
            l = ary[-1]
            for c in range(l, m + 1):
                dfs(i + 1, ary + [c])

dfs(0, [])

print(result)

Submission status: Wrong Answer

Solution code:
import itertools
n, m, q = map(int, input().split())
ls = []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    ls.append((a, b, c, d))

def calc(x, ls):
    s = sum([d for a, b, c, d in ls if x[b - 1] - x[a - 1] == c])
    return s

ans = 0
cands = range(1, m)
c = itertools.combinations_with_replacement(cands, n - 1)
for x in c:
    x = list(x) + [m]
    ans = max(ans, calc(x, ls))
print(ans)

Submission status: Wrong Answer

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

from itertools import combinations_with_replacement

cands = list(combinations_with_replacement(range(1,m+1), n))
print(cands)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

def main():
    n,m,q = (int(x) for x in input().split())
    A = list(combinations(range(1,m+1),n))
    query = [[int(x) for x in input().split()] for _ in range(q)]
    pmax = 0

    for i in A:
        Ai = list(i)
        # print('b', b)
        p = 0
        for j in query:
            if Ai[j[1]-1] - Ai[j[0]-1] == j[2]:
                p += j[3]
        pmax = max(p,pmax)
    print(pmax)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int, input().split())
P = [[int(x) for x in input().split()] for _ in range(Q)]

li = [x for x in range(1, M+1)]
Com = itertools.combinations(li, N)

ans=0
for com in Com:
    reg=0
    for a,b,c,d in P:
        if com[b-1] - com[a-1] == c:
            reg += d
    ans = max(reg, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, q =  list(map(int, input().split()))
aa = []
bb =[]
cc = []
dd =[]
for i in range(q):
    a,b,c,d =  list(map(int, input().split()))
    aa.append(a)
    bb.append(b)
    cc.append(c)
    dd.append(d)

def ev(A):
    s = 0
    for i in range(q):
        if A[bb[i] - 1] - A[aa[i] - 1] == cc[i]:
            s += dd[i]
    return s

A = [0] * n
s = ev(A)
s_pre = s
while True:
    for i in range(1, n + 1):
        for k in range(q):
            if aa[k] == i:
                tmp = A[bb[k] - 1]
                if cc[k] + A[aa[k] - 1] > m:
                    continue
                A[bb[k] - 1] = cc[k] + A[aa[k] - 1]
                ss = ev(A)
                if ss > s:
                    s = ss
                else:
                    A[bb[k] - 1] = tmp
    if s_pre == s:
        break
    else:
        s_pre = s
print(s)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
N,M,Q=map(int,input().split())
a=[]
tmp=0
A=[1,2,3,4,5,6,7,8,9,10]
P=list(permutations(A, N))
li=[list(map(int,input().split())) for _ in range(Q)]
for p in P:
  for l in range(Q):
    if p[li[l][1]-1]-p[li[l][0]-1]==li[l][2]:
      tmp+=li[l][3]
  a.append(tmp)
  tmp=0
print(max(a))

Submission status: Wrong Answer

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

aq = [None] * q
bq = [None] * q
cq = [None] * q
dq = [None] * q
for i in range(q):
    aq[i], bq[i], cq[i], dq[i] = list(map(int, input().split(' ')))

result = 0

def dfs(i, ary):
    global result
    if i == n:
        val = 0
        for i in range(q):
            if ary[bq[i] - 1] - ary[aq[i] - 1] == cq[i]:
                val += dq[i]
        if result < val:
            result = val
    else:
        if ary == []:
            for c in range(0, 11):
                dfs(i + 1, ary + [c])
        else:
            l = ary[-1]
            for c in range(int(l), 11):
                dfs(i + 1, ary + [c])

dfs(0, [])

print(result)

Submission status: Wrong Answer

Solution code:
import sys

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

a = []
b = []
c = []
d = []
A = []
for i in range(Q):
    temp_a, temp_b, temp_c, temp_d = map(int, sys.stdin.readline().split())
    a.append(temp_a)
    b.append(temp_b)
    c.append(temp_c)
    d.append(temp_d)
flag = 0

for i in range(N):
    A.append(1)

max = 0
for i in range(N):
    for j in range(1, M+1):
        A[N-i-1] = j
        #print(A)
        sum = 0
        for k in range(Q):
            if A[b[k]-1] - A[a[k]-1] == c[k]:
               sum += d[k]
        if sum > max:
            max = sum

print(max)

Submission status: Wrong Answer

Solution code:
n, m, q =  list(map(int, input().split()))
aa = []
bb =[]
cc = []
dd =[]
for i in range(q):
    a,b,c,d =  list(map(int, input().split()))
    aa.append(a)
    bb.append(b)
    cc.append(c)
    dd.append(d)

def ev(A):
    s = 0
    for i in range(q):
        if A[bb[i] - 1] - A[aa[i] - 1] == cc[i]:
            s += dd[i]
    return s

A = [0] * n
s = ev(A)
s_pre = s
while True:
    for i in range(1, n + 1):
        for k in range(q):
            if aa[k] == i:
                tmp = A[bb[k] - 1]
                A[bb[k] - 1] = cc[k] + A[aa[k] - 1]
                ss = ev(A)
                if ss > s:
                    s = ss
                else:
                    A[bb[k] - 1] = tmp
    if s_pre == s:
        break
    else:
        s_pre = s
print(s)

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

def func2(lst): #次の状態にする
    dig = n-1
    while True:
        if lst[dig] == m:
            pass
        else:
            lst[dig] += 1
            break
        dig -= 1
    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:
import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
A_itr = itertools.combinations(range(1, M+1), N)
max_score = 0
for A in A_itr:
    score = 0
    for j_abcd in abcd:
        if A[j_abcd[1]-1]-A[j_abcd[0]-1] == j_abcd[2]:
            score += j_abcd[3]
    max_score = max(max_score, score)
print(max_score)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
N,M,Q = map(int ,input().split())
ls = [0]*Q
for i in range(Q):
  a,b,c,d = map(int, input().split())
  ls[i] = (a,b,c,d)
def f(l,cnt):
  if cnt>0:
    j = l[-1]
    ans  = 0
    for i in range(j,M+1):
      ans = max(f(l+[i],cnt-1),ans)
    return ans
  else:
    m  = 0
    for a,b,c,d in ls:
      if l[b-1]-l[a-1]==c:
        m += d
    return m
ans  = 0
for i in range(M+1):
  ans = max(ans,f([i],N-1))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int, input().split())
a = [None for _ in range(Q)]
b = [None for _ in range(Q)]
c = [None for _ in range(Q)]
d = [None for _ in range(Q)]
for i in range(Q):
    a[i], b[i], c[i], d[i] = map(int, input().split())

ans = 0
elements = list(range(1, M+1))
for combination in itertools.combinations_with_replacement(elements, N):
    score = 0
    for i in range(Q):
        print(combination, b[i], a[i])
        if combination[b[i]-1] - combination[a[i]-1] == c[i]:
            score += d[i]
    ans = max(score, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
def solve(arr, abcd, Q):
    ans = 0
    for i in range(Q):
        if arr[abcd[i][1]-1]-arr[abcd[i][0]-1] == abcd[i][2]:
            ans += abcd[i][3]
    return ans


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

ans = 0
for i1 in range(1, M+1):
    for i2 in range(1, M+1):
        if i1 > i2:
            continue

        for i3 in range(1, M+1):
            if i2 > i3:
                continue
            if N == 3:
                tmp = [i1, i2, i3]
                tmp_ans = solve(tmp, abcd, Q)
                ans = max(tmp_ans, ans)

            for i4 in range(1, M+1):
                if i3 > i4:
                    continue
                if N == 4:
                    tmp = [i1, i2, i3, i4]
                    tmp_ans = solve(tmp, abcd, Q)
                    ans = max(tmp_ans, ans)

                for i5 in range(1, M+1):
                    if i4 > i5:
                        continue
                    if N == 5:
                        tmp = [i1, i2, i3, i4, i5]
                        tmp_ans = solve(tmp, abcd, Q)
                        ans = max(tmp_ans, ans)

                    for i6 in range(1, M+1):
                        if i5 > i6:
                            continue
                        if N == 6:
                            tmp = [i1, i2, i3, i4, i5, i6]
                            tmp_ans = solve(tmp, abcd, Q)
                            ans = max(tmp_ans, ans)

                        for i7 in range(1, M+1):
                            if i6 > i7:
                                continue
                            if N == 7:
                                tmp = [i1, i2, i3, i4, i5, i6, i7]
                                tmp_ans = solve(tmp, abcd, Q)
                                ans = max(tmp_ans, ans)

                            for i8 in range(1, M+1):
                                if i7 > i8:
                                    continue
                                if N == 8:
                                    tmp = [i1, i2, i3, i4, i5, i6, i7, i8]
                                    tmp_ans = solve(tmp, abcd, Q)
                                    ans = max(tmp_ans, ans)

                                for i9 in range(1, M+1):
                                    if i8 > i9:
                                        continue
                                    if N == 9:
                                        tmp = [i1, i2, i3, i4,
                                               i5, i6, i7, i8, i9]
                                        tmp_ans = solve(tmp, abcd, Q)
                                        ans = max(tmp_ans, ans)

                                    for i10 in range(1, M+1):
                                        if i9 > i10:
                                            continue
                                        if N == 10:
                                            tmp = [i1, i2, i3, i4,
                                                   i5, i6, i7, i8, i9, i10]
                                            tmp_ans = solve(tmp, abcd, Q)
                                            ans = max(tmp_ans, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, q =  list(map(int, input().split()))
aa = []
bb =[]
cc = []
dd =[]
for i in range(q):
    a,b,c,d =  list(map(int, input().split()))
    aa.append(a)
    bb.append(b)
    cc.append(c)
    dd.append(d)

def ev(A):
    s = 0
    for i in range(q):
        if A[bb[i] - 1] - A[aa[i] - 1] == cc[i]:
            s += dd[i]
    return s

A = [0] * n
s = ev(A)
for i in range(1, n + 1):
    for k in range(q):
        if aa[k] == i:
            tmp = A[bb[k] - 1]
            A[bb[k] - 1] = cc[k] + A[aa[k] - 1]
            ss = ev(A)
            if ss > s:
                s = ss
            else:
                A[bb[k] - 1] = tmp
print(s)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
N,M,Q = map(int ,input().split())
ls = [0]*Q
for i in range(Q):
  a,b,c,d = map(int, input().split())
  ls[i] = (a,b,c,d)
def f(l,cnt):
  if cnt>0:
    j = l[-1]
    ans  = -1
    for i in range(j,M+1):
      ans = max(f(l+[i],cnt-1),ans)
    return ans
  else:
    m  = 0
    for a,b,c,d in ls:
      if l[b-1]-l[a-1]==c:
        m += d
    return m
ans  = -1
for i in range(M+1):
  ans = max(ans,f([i],N-1))
print(ans)

Submission status: Wrong Answer

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

A=[list(map(int,input().split())) for _ in range(Q)]
num=0
for i in A:
  if i[1]-i[0]==i[2]:
    num+=i[3]
    
print(num)

Submission status: Wrong Answer

Solution code:
from itertools import (
    accumulate,  # 累積和のイテレータ
    groupby,  # 値をkey,同じ値の集まりをgroup
    permutations,  # 順列
    combinations,  # 組み合わせ
    product,  # 重複あり順列（直積, 深いネストにも使える）
    combinations_with_replacement,  # 重複あり組み合わせ
)

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

ans = 0
for v in combinations_with_replacement(range(0, M+1), N):
    score = 0
    for i in range(Q):
        joken = q[i]
        if v[joken[1]-1] - v[joken[0]-1] == joken[2]:
            score += joken[3]
    ans = max(ans, score)
    #print(v, ': ', score)

print(ans)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
X=[0]*Q
for i in range(Q):
    X[i] = list(map(int, input().split()))
from itertools import combinations_with_replacement
def solve(N,M,Q,X):
    ans = 0
    for c in combinations_with_replacement(range(1,11), N-1):
        A = [1]+list(c)
        score = 0
        for x in X:
            if A[x[1]-1]-A[x[0]-1]==x[2]:
                score += x[3]
        ans = max(ans,score)
    return ans
print(solve(N,M,Q,X))

Submission status: Wrong Answer

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

def Increase(A, index, M):
	if A[index] == M:
		Increase(A, index-1, M)
		A[index] = A[index-1]
	else:
		A[index] += 1

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)

maximumScore = 0

A = [1]*n

while min(A) != m:
	score = Score(A, a, b, c, d)
	maximumScore = max(maximumScore, score)
	Increase(A, n-1, m)

print(maximumScore)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement

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

query = tuple(tuple(map(int, input().split())) for _ in range(Q))

ans = 0
for comb in combinations_with_replacement(range(1, M+1), N):
    print(comb)
    score = 0
    for a, b, c, d in query:
        if comb[b-1] - comb[a-1] == c:
            score += d
    ans = max(ans, score)
print(ans)

Submission status: Wrong Answer

Solution code:
a, b, n = map(int, input().split())
 
ans = 0
for i in range(n):
  t = int(a * i / b) - a * int(i/b)
  if t > ans:
    ans = t

print(int(ans))

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
Mat = [list(map(int, list(input().split()))) for _ in range(Q)]
s = int(''.join(['1'] * N))
ans = 0
for A in range(s, 10 ** N):
    A = str(A)
    buff = 0
    for i in range(Q):
        a = Mat[i][0]
        b = Mat[i][1]
        c = Mat[i][2]
        d = Mat[i][3]
        if int(A[b - 1]) - int(A[a - 1]) == c:
            buff += d
    ans = max(ans, buff)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(q)]
L=[]
for i in range(1,11):
  if n==1:
    L.append((i))
  else:
    for i2 in range(i,11):
      if n==2:
        L.append((i,i2))
      else:
        for i3 in range(i2,11):
          if n==3:
            L.append((i,i2,i3))
          else:
            for i4 in range(i3,11):
              if n==4:
                L.append((i,i2,i3,i4))
              else:
                for i5 in range(i4,11):
                  if n==5:
                    L.append((i,i2,i3,i4,i5))
                  else:
                    for i6 in range(i5,11):
                      if n==6:
                        L.append((i,i2,i3,i4,i5,i6))
                      else:
                        for i7 in range(i6,11):
                          if n==7:
                            L.append((i,i2,i3,i4,i5,i6,i7))
                          else:
                            for i8 in range(i7,11):
                              if n==8:
                                L.append((i,i2,i3,i4,i5,i6,i7,i8))
                              else:
                                for i9 in range(i8,11):
                                  if n==9:
                                    L.append((i,i2,i3,i4,i5,i6,i7,i8,i9))
                                  else:
                                    for i10 in range(i9,11):
                                      L.append((i,i2,i3,i4,i5,i6,i7,i8,i9,i10))
ans=[]
for i in range(len(L)):
  ct=0
  for j in range(q):
    if L[i][l[j][1]-1]-L[i][l[j][0]-1]==l[j][2]:
      ct+=l[j][3]
  ans.append(ct)
print(max(ans))

Submission status: Wrong Answer

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

As = [0] * N

def next(ls):
    if ls[-1] < M:
        ls[-1] += 1
    else:
        for i, elem in reversed(list(enumerate(ls))):
            if elem < M:
                for j in range(i, N):
                    ls[j] = elem + 1
                break
        else:
            return False
    return True

cond = True
max_score = 0
while cond:
    score = 0
    for a, b, c, d in ts:
        if As[b-1] - As[a-1] == c:
            score += d
    max_score = max(max_score, score)
    cond = next(As)

print(max_score)

Submission status: Wrong Answer

Solution code:
import sys

if __name__ == '__main__':
    # k = int(sys.stdin.readline())
    n, m, q = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
    lst = []
    for i in range(q):
        a,b,c,d = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
        lst += [[a-1,b-1,c,d]]
    # lst = sorted(lst, key=lambda l:l[0])
    lst = sorted(lst, key=lambda l:-l[3])

    score = 0
    seq = [1 for i in range(n)]
    seq[-1] = 0
    for i in range((n+1)*(m+1)):
        tmpscore = 0
        seq[-1] += 1
        for j in reversed(range(len(seq))):
            if seq[j] == m + 1:
                seq[j-1] += 1
        for j in range(len(seq)):
            if seq[j] == m+1:
                seq[j] = seq[j-1]
        # print(seq)
        # seq完成
        for a, b, c, d in lst:
            if seq[b] - seq[a] == c:
                tmpscore += d
        if tmpscore > score:
            score = tmpscore
    print(score)