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:

def main():
    N, M, Q = map(int, input().split())  # 入力

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

    A = [1]*N

    flg = True

    s = 0
    ans = None

    while flg:
        # print(A)
        t_s = rtn_score(A, kumi)
        if t_s > s:
            s = t_s
            ans = A
        for i in range(N):
            if A[N - 1 - i] < M:
                A[N - 1 - i] += 1
                break
            else:
                if A[N - 1 - i - 1] < M:
                    A[N - 1 - i - 1] += 1
                    A[N - 1 - i] = A[N - 1 - i - 1]
                    break
            if i == N - 1:  # last
                flg = False

    print(s)


def rtn_score(A, kumi):
    tmp_s = 0
    for k in kumi:
        # print(k)
        # print(k[1])
        if A[k[1]-1] - A[k[0]-1] == k[2]:
            tmp_s += k[3]
    return tmp_s


if __name__ == "__main__":
    main()

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

answer = 0

nums=[]
for i in range(1, M+1):
    nums.append(i)

for balls in itertools.combinations_with_replacement(nums, N):
    score = 0
    for i in range(Q):
        test = list(balls)
        print(test)
        if test[abcd[i][0]-1] + abcd[i][2] == test[abcd[i][1]-1]:
            score += abcd[i][3]
    if score > answer:
        answer = score

print(answer)

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

# abcd.sort(key=lambda x:x[3],reverse=True)
# for x in abcd:
#     print("")
#     for v in x:
#         print(v,end=" ")
arr = [i for i in range(1,11)]

ans = 0
for per in itertools.combinations_with_replacement(arr,n):

    tmp = 0
    for x in abcd:
        diff = per[x[1]-1] - per[x[0]-1]
        if(diff == x[2]):
            tmp += x[3]
    # print(per,end=" ")
    # print(tmp)
    ans = max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q = map(int,input().split())
A = [1]*n
a,b,c,d = [],[],[],[]
for _ in range(q):
    ta,tb,tc,td = map(int,input().split())
    a.append(ta)
    b.append(tb)
    c.append(tc)
    d.append(td)

def solve(A):
    ans = 0 
    while True:
        temp = 0
        for i in range(q):
            if A[b[i]-1] - A[a[i]-1] == c[i]:
                temp += d[i]
        if ans < temp:
            ans = temp
        if A.count(m) == n:
            break
        else:
            for i in range(1,n+1):
                if A[-1*i] != m:
                    A[-1*i] += 1
                    break
                else:
                    A[-1*i] = 1
    return ans
print(solve(A))

Submission status: Wrong Answer

Solution code:
import itertools

n, m, q = map(int, input().split())
abcd = []
a_pre = [] 


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

lis = [i for i in range(1,m+1)]
for pair in itertools.combinations(lis, n):
  a_pre.append(list(pair))

sum = 0
for i in range(len(a_pre)):
  sum_pre = 0
  for j in range(len(abcd)):
    if (a_pre[i][abcd[j][1] - 1] - a_pre[i][abcd[j][0] - 1]) == abcd[j][2]:
      sum_pre += abcd[j][3]
  if sum_pre > sum:
    sum = sum_pre

print(sum)

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_with_replacement(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:
# 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 = [(i, '') for i in range(10)]
    while stack:
        t, s = stack.pop()
        S = s+str(t)
        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(t, 10):
            stack.append((i, S))

    return str(ret)


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

Submission status: Wrong Answer

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


dp = [[[i]] for i in range(11)]
for i in range(9):
    dp2 = [[] for _ in range(11)]
    for k in range(1, 11):
        res = []
        for j in range(10, k-1, -1):
            for h in dp[j]:
                res.append([k]+h)
        dp2[k] = res
    dp = dp2[:]
        

candi = []
for i in range(1, 11):
    candi.extend(dp[i])
N, M, Q = map(int, readline().split())
ABCD = [list(map(int, readline().split())) for  _ in range(Q)]

ans = 0
for k in candi:
    res = 0
    for a, b, c, d in ABCD:
        if k[b-1] - k[a-1] == c:
            res += d
    ans = max(res, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N, M, Q = map(int, input().split())
values = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    values.append({'a': a, 'b': b, 'c': c, 'd': d})

intlist = [i for i in range(1, M+1)]
maxvalue = 0
for A in itertools.combinations(intlist, N):
    A = sorted(A)
    total = 0
    for i in range(Q):
        if A[values[i]['b']-1] - A[values[i]['a']-1] == values[i]['c']:
            total += values[i]['d']
    if maxvalue < total:
        maxvalue = total

print(maxvalue)

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int, input().split())
l = []
for _ in range(Q):
    l.append(tuple(map(int, input().split())))
k = 1
for i in l:
  if i[2] == 0:
    k += 1
c = list(itertools.combinations(list(range(1, M+1))*k, N))

max = 0
for i in c:
    i = list(i)
    i.sort()
    sum = 0
    for j in l:
        if i[j[1]-1] - i[j[0]-1] == j[2]:
            sum += j[3]
    if max < sum:
        max = sum
        
print(max)

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.permutations(a, n):
        ans.append(calc(i, queries))
    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
    #print(i, ans)
    return ans

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


if __name__ == '__main__':
    main()

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]
            if el[1] == pos:
                row[el[0]] = row[el[1]] - 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 and min(row) >= 1:
            tes = res
print(tes)

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, -1, -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 as it

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

for v in list(it.product(range(1,m+1),repeat=n)):
    num = 0
    for w in l:
        x,y,z = w[0]-1,w[1]-1,w[2]
        if v[y] - v[x] == z:
            num += w[3]
    if num > ans:
        ans = num
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations_with_replacement

n, m, q = list(map(int,input().split()))
A = [list(map(int,input().split())) for i in range(q)]
def keisan(tup):
    ans_keisan = 0
    print(tup)
    for j in A:
        if tup[j[1]-1] - tup[j[0]-1] == j[2]:
            ans_keisan += j[3]
    return ans_keisan
ans = 0
for i in combinations_with_replacement(range(m),n):
    ans = max(keisan(i), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    n, m, q = map(int, input().split())
    l_abcd = []
    for _ in range(q):
        abcd = tuple(map(int, input().split()))
        l_abcd.append(abcd)
    l_abcd = sorted(l_abcd, key=lambda x: x[3], reverse=True)  # d の降順でソート
    r = calc_max(0, [0] * (n + 1), m, l_abcd)
    print(r)


def calc_max(s, la, m, l_abcd):
    if len(l_abcd) < 1:
        return s

    a, b, c, d = l_abcd[0]

    if la[a] == la[b] == 0:
        s_ = calc_max(s, la, m, l_abcd[1:])
        i = 1
        while i + c <= m:
            la_ = list(la)
            la_[a] = i
            la_[b] = i + c
            s_ = max(s_, calc_max(s + d, la_, m, l_abcd[1:]))
            i += 1
        return s_
    elif la[a] != 0 and la[b] == 0 and la[a] + c <= m:
        la_ = list(la)
        la_[b] = la_[a] + c
        return max(calc_max(s + d, la_, m, l_abcd[1:]),
                   calc_max(s, la, m, l_abcd[1:]))
    elif la[a] == 0 and la[b] != 0 and 1 <= la[b] - c:
        la_ = list(la)
        la_[a] = la_[b] - c
        return max(calc_max(s + d, la_, m, l_abcd[1:]),
                   calc_max(s, la, m, l_abcd[1:]))
    elif la[b] - la[a] == c:
        return calc_max(s + d, la, m, l_abcd[1:])
    else:
        return calc_max(s, la, m, l_abcd[1:])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def dfs(nn,anni,vmax):
    if nn==n:
        qsum=0
        for j in range(q):
#            print(j,abcd[j][1],abcd[j][0])
            if a[abcd[j][1]-1]-a[abcd[j][0]-1]==abcd[j][2]:
                qsum=qsum+abcd[j][3]
#        print(qsum,a)
        return qsum
    a[nn]=anni
#    vmax=0
    for i in range(9):
        if a[nn]+i<=m:
            v=dfs(nn+1,a[nn]+i,vmax)
            vmax=max(v,vmax)
    return vmax

vmax=0
a=[0]*n
print(dfs(0,1,vmax))

Submission status: Wrong Answer

Solution code:
s = input().split()
n = s[2]

s1 = [input() for i in range(int(n))]
t = 0
for el in s1:
    l=el.split()
    gap=int(l[1])-int(l[0])
    if gap == int(l[2]):
        t += int(l[3])

print(t)

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]
            if el[1] == pos:
                row[el[0]] = row[el[1]] - 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:

#長さN
#各値はM以下
n,m,q=map(int,input().split())

#初期値が0の辞書
from collections import defaultdict
tree = defaultdict(lambda: set([])) 
point = defaultdict(lambda: 0) 


for _ in range(q):
    a,b,c,d=map(int,input().split())
    tree[a-1].add(b-1)
    tree[b-1].add(a-1)
    point[(a-1,b-1)]=[c,d]
    point[(b-1,a-1)]=[-c,d]

group=[]
checked=[0]*n

#print(point)

i=0

while i<n:
    if checked[i]==0:
        checked[i]=1
        temp=[i]
        temp_group=[i]
        while temp!=[]:
            key=temp.pop()
            for item in tree[key]:
                if checked[item]==0:
                    checked[item]=1
                    temp.append(item)
                    temp_group.append(item)
        
        group.append(temp_group)
    i+=1

result=0

for union in group:
    max_union_point=0
    for item in union:
        #itemが正しいと仮定する
        Ai=[0]*n
        Ai[item]=1
        temp=[item]
        union_point=0
        while temp!=[]:
            key=temp.pop()
            for item2 in tree[key]:
                if Ai[item2]==0:
                    
                    Ai[item2]=Ai[key]+point[(key,item2)][0]
                    union_point+=point[(key,item2)][1]
                else:
                    if Ai[item2]==Ai[key]+point[(key,item2)][0]:
                         union_point+=point[(key,item2)][1]
            max_union_point=max(max_union_point,union_point)
    result+=max_union_point

print(result)

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)]
#print(ABCD)
ALIST = list(itertools.combinations([i for i in range(1, M+1)], N))

maxtokuten = 0
for A in ALIST:
    tokuten = 0
    for a, b, c, d in ABCD:
        if A[b-1] - A[a-1] == c:
            tokuten += d
        else:
            continue
#    print(tokuten, A)
    maxtokuten = max(maxtokuten, tokuten)

print(maxtokuten)

Submission status: Wrong Answer

Solution code:
import itertools

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

m_li = []
for i in range(1,m+1):
    m_li.append(i)

li = []
mx = 0

for i in range(q):
    a,b,c,d = map(int,input().split())
    li.append((a,b,c,d))

for v in itertools.combinations(m_li,n):
    buf = 0
    for i in range(q):
        a = li[i][0]
        b = li[i][1]
        c = li[i][2]
        d = li[i][3]
        if v[b-1] - v[a-1] == c:
            buf += d
    if buf >= mx:
        mx = buf

print(mx)

Submission status: Wrong Answer

Solution code:
import sys
import math
input = lambda:sys.stdin.readline().strip()
n,m,q = map(int,input().split())
score = 0
def calc_score(arr):
    val = 0
    for i in qs:
        a,b,c,d = i 
        if arr[b-1]-arr[a-1]==c:
            val+=d
    return val
def f(i,ne,arr):
    if i==n:
        global score 
        score = max(score,calc_score(arr))
        return 
    for j in range(ne,m+1):
        arr = arr+[j]
        f(i+1,j,arr)
        arr.pop()
qs = []
for i in range(q):
    qs.append(list(map(int,input().split())))
f(0,0,[])
print(score)

Submission status: Wrong Answer

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

ans =0
    
if n == 2:
    for a in range(1, m+1):
        for b in range(a, m+1):
            A = [a, b]
            ans_ = 0
            for res in abcd:
                if A[res[1]] - A[res[0]] == res[2]: ans += res[3]
            ans = max(ans, ans_)
            
elif n == 3:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                A = [a, b, c]
                ans_ = 0
                for res in abcd:
                    if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                ans = max(ans, ans_)      
                
elif n == 4:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    A = [a, b, c, d]
                    ans_ = 0
                    for res in abcd:
                        if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                    ans = max(ans, ans_)
                    
elif n == 5:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        A = [a, b, c, d, e]
                        ans_ = 0
                        for res in abcd:
                            if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                        ans = max(ans, ans_)
                        
elif n == 6:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            A = [a, b, c, d, e, f]
                            ans_ = 0
                            for res in abcd:
                                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                            ans = max(ans, ans_)
                            
elif n == 7:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                A = [a, b, c, d, e, f, g]
                                ans_ = 0
                                for res in abcd:
                                    if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                ans = max(ans, ans_)
                                
elif n == 8:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                for h in range(g, m+1):
                                    A = [a, b, c, d, e, f, g, h]
                                    ans_ = 0
                                    for res in abcd:
                                        if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                    ans = max(ans, ans_)
                                    
elif n == 9:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                for h in range(g, m+1):
                                    for i in range(h, m+1):
                                        A = [a, b, c, d, e, f, g, h, i]
                                        ans_ = 0
                                        for res in abcd:
                                            if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                        ans = max(ans, ans_)
                
elif n == 10:
    for a in range(1, m+1):
        for b in range(a, m+1):
            for c in range(b, m+1):
                for d in range(c, m+1):
                    for e in range(d, m+1):
                        for f in range(e, m+1):
                            for g in range(f, m+1):
                                for h in range(g, m+1):
                                    for i in range(h, m+1):
                                        for j in range(i, m+1):
                                            A = [a, b, c, d, e, f, g, h, i, j]
                                            ans_ = 0
                                            for res in abcd:
                                                if A[res[1]] - A[res[0]] == res[2]: ans_ += res[3]
                                            ans = max(ans, ans_)
                                                    
print(ans)

Submission status: Wrong Answer

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

#abcd.sort(key=lambda  x: x[2], reverse=True)
sum_d = 0

for i in range(2**q):
    A = [1]*n
    tmp = 0
    for j in range(q):
        if i & (1<<j):
            A[abcd[j][1]-1] = A[abcd[j][0]-1] + abcd[j][2]
            tmp += abcd[j][3]
    A_sorted = sorted(A)
    if A_sorted == A and max(A)<=m:
        if sum_d < tmp:
            sum_d = tmp
            #print(A)
            #print(tmp)
    
print(sum_d)

Submission status: Wrong Answer

Solution code:
N,M,Q = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(Q)]
L.sort(key=lambda x: x[0])
ans = 0
for bit in range(1 << Q):
    sumnum = 0
    flag = True
    A = [0]*(N+1)
    s = set()
    for i in range(Q):
        if bit & (1 << i):
            if not L[i][0]-1 in s:
                s.add(L[i][0]-1)
            if not L[i][1]-1 in s:
                s.add(L[i][1]-1)
                A[L[i][1]-1] = A[L[i][0]-1]+L[i][2]
            if L[i][1]-1 in s:
                if A[L[i][1]-1] - A[L[i][0]-1] != L[i][2]:
                    flag = False
                    break
                else:
                    sumnum += L[i][3]
    
    if flag:
        ans = max(ans, sumnum)

print(ans)

Submission status: Wrong Answer

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

        



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




old = []
for i in range(1,11):
    old.append([i])
for j in range(9):
    new = []
    for e in old:
        for i in range(e[-1],11):
            new.append(e+[i])
    old = new

mx = 0
for n in new:
    score = 0
    for a,b,c,d in query:
        if (n[b] - n[a] == c):
            score += d
    mx = max(score,mx)
print (mx)

Submission status: Wrong Answer

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

a=[]
b=[]
c=[]
d=[]
for i in range(0,q):
    ay,by,cy,dy=map(int,input().split())
    a.append(ay)
    b.append(by)
    c.append(cy)
    d.append(dy)
s=0
smax=0
for i1 in range(0,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):
                                        s=0
                                        na=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for j in range(0,q):
                                            if na[b[j]-1]-na[a[j]-1]==c[j]:
                                                s+=d[j]
                                        if s>smax:
                                            smax=s


print(smax)

Submission status: Wrong Answer

Solution code:
import itertools
n,m,q=map(int,input().split())
A,B,C,D=[],[],[],[]
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)
ans=0

L=[i+1 for i in range(m)]
for l in itertools.product(L, repeat=n):
    s=0
    for j in range(q):
        if int(l[B[j]-1])-int(l[A[j]-1])==C[j]:
            s+=D[j]
    ans=max(ans,s)
    if ans==sum(D):
        print(ans)
        exit()
print(ans)

Submission status: Wrong Answer

Solution code:
# import math
# from collections import Counter

# N = int(input()) # 1行, 単数
# arg = list(map(int, input().split())) # 1行, 配列
# D = Counter(arg) # 出現数カウント

# print('Yes')
# print('No')
# print()
arg = list(map(int, input().split())) # 1行, 配列
N = arg[0]
M = arg[1]
Q = arg[2]

listA=[]
maxim = 0

while True:
  try:
    listA.append(list(map(int,input().split())))
  except:
    break;

A = [1] * N
As = []
for i in list(range(N-1,-1,-1)):
  while A[i] < M:
    A[i] = A[i] + 1
    As.append(list(A))


for item in As:
  letMaxim = 0
  for l in listA:
    if item[l[1]-1] - item[l[0]-1] == l[2]:
      letMaxim = letMaxim + l[3]
  if maxim < letMaxim:
    maxim = letMaxim


print(maxim)

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
b=[]
c=[]
out=0
for i in range(a[2]):
  b.append(list(map(int,input().split())))
for i in range(a[0]):
  c.append(1)
for i in range(a[0]):
  for j in range(a[1]):
    d=0
    for k in range(a[2]):
      #print('i:'+str(c[b[k][1]-1])+' j:'+str(c[b[k][1]-1])+' k:'+str(b[k][2]))
      #print(c)
      if c[b[k][1]-1]-c[b[k][0]-1]==b[k][2]:
        d+=b[k][3]
    if d>out:
      out=d
    c[a[0]-1-i]=j+1
print(out)

Submission status: Wrong Answer

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

ans=0
A=[1]*(N+1)
while True:
    tmp=0
    for a,b,c,d in ABCD:
        if A[b]-A[a]==c: tmp+=d
    ans = max(ans, tmp)
    up=1
    for i in range(N,0,-1):
        A[i]+=up
        if A[i]>M:
            up=1
            A[i]=M
        else:
            up=0
            break
    if up==1: break
print(ans)

Submission status: Wrong Answer

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


A = []
B = []
C = []
D = []
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)

import itertools
ANS = []
for i in list(itertools.combinations_with_replacement([i for i in range(1, 11)], n)):
    L = list(i)
    t = 0
    for i in range(q):
        if L[B[i] - 1] - L[A[i] - 1] == C[i]:
            t += D[i]
    ANS.append(t)

print(max(ANS))

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
lists = [list(map(int, input().split())) for i in range(Q)]
maximum = 0
A = [1]*N
count = 0

for l in range(1, N+1):
    for i in range(1, M): #実質M-1回
        A[-l] = A[-l] + 1
        for k in range(0, Q): #実質Q回 
            if A[lists[k][1]-1] - A[lists[k][0]-1] == lists[k][2]:
                count += lists[k][3]
        maximum = max(maximum,count)
        count = 0

print(maximum)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations_with_replacement
#from fractions import gcd
from math import gcd
from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)
inf = float("inf")

def input():
    return sys.stdin.readline()[:-1]

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

    A = list(range(m))
    ans = 0
    comb = list(combinations_with_replacement(A, n))
    for c in comb:
        tmp_ans = 0
        for i in range(q):
            if c[abcd[i][1]-1] - c[abcd[i][0]-1] == abcd[i][2]:
                tmp_ans += abcd[i][3]
                print(tmp_ans)
        ans = max(ans, tmp_ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = input().split()
N = int(N)
M = int(M)
Q = int(Q)
Num = [0 for i in range(Q)]
for i in range(Q):
  Num[i] = list(map(int,input().split()))
num = list(range(1, M+1))
A = []
for v in itertools.combinations(num, N):
  A.append(list(v))
score = 0
for w in A:
  scoreX = 0
  for j in range(Q):
    b = Num[j][1]
    a = Num[j][0]
    if Num[j][2] == w[b-1]-w[a-1]:
      scoreX += Num[j][3]
  if scoreX > score:
    score = scoreX
print(str(score))

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(1, 11):
    make_arr(i, [])

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)