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:
#c
import itertools

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

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

# n=3
# m=4
# q=3
# ls1 = [1, 3, 3, 100]
# ls2 = [1, 2, 2, 10]
# ls3 = [1, 1, 0, 1000]
# lslist = [ls1, ls2, ls3]

acomn = list(itertools.combinations_with_replacement(list(range(m)), n))

def tensu(ls, a):
    if a[ls[1]-1] - a[ls[0]-1] == ls[2]:
        return ls[3]
    else:
        return 0

def tensu2(lslist, a):
    return sum(list(map(lambda x: tensu(x, a), lslist)))

print(max(list(map(lambda a: tensu2(lslist, a), acomn))))

Submission status: Wrong Answer

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

al=list()

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


Sum=0
Summax=0
cnt=0
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): 
                                        cnt+=1
                                        Al=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        Sum=0
                                        for i in range(Q): 
                                            if(Al[al[i][1]-1]-Al[al[i][0]-1]==al[i][2]):
                                                Sum+=al[i][3]
                                        if(Summax<Sum):
                                            Summax=Sum

print(Summax)    
print(cnt)

Submission status: Wrong Answer

Solution code:
# import time
import itertools
N,M,Q = map(int,input().split())

# t1 = time.time()

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

# 列挙by product
pd = itertools.product(list(range(1,M+1)),repeat=N)


highscore = 0

for cand in pd:
    AA = list(cand)
#     print(AA)
    score = 0
    for q in Qs:
        a,b,c,d = q
        if AA[b-1] - AA[a-1] == c:
            score += d
    if score > highscore:
        highscore = score
        
print(highscore)
# t2 = time.time()
# print(t2-t1)

Submission status: Wrong Answer

Solution code:
import itertools

n, m, q = [int(x) for x in input().split()]
 
tmp = []
for i in range(q):
    a, b, c, d = [int(x) for x in input().split()]
    tmp.append([a, b, c, d])

t = list(itertools.combinations_with_replacement(range(1, m + 1), n))

score = 0
for _t in t:
    _s = 0
    for a, b, c, d in tmp:
        if len(_t) <= a or len(_t) <= b:
            continue
        if c == _t[b - 1] - _t[a - 1]:
            _s += d
    score = max(_s, score)
print(score)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

N,M,Q=map(int, sys.stdin.readline().split())
X=[ map(int, sys.stdin.readline().split()) for _ in range(Q) ]

qq=deque()

for m in range(1,M+1):
    #BFS
    q=deque()
    q.append([m])
    flag=True
    while flag:
        fro=q.popleft()
        last=fro[-1]
        for to in range(last,M+1):
            temp=copy.deepcopy(fro)
            temp.append(to)
            if N<len(temp):
                flag=False
                break
            q.append(temp)
    qq+=q

ans=float("-inf")
for L in qq:
    temp_ans=0
    for a,b,c,d in X:
        if L[b-1]-L[a-1]==c:
            temp_ans+=d
    ans=max(ans,temp_ans)
print ans

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n,m,q=l_in()

abcd=[]
for _ in range(q):
    abcd.append(l_in())

s = []

from functools import reduce

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

def search(s, current):
    tmp = 0
    for d in range(current, m+1):
        s.append(d)
        if len(s) == n:
            tmp = max(tmp, check())
        else:
            tmp = max(tmp, search(s, d))
        s.pop()

    return tmp


print(search(s,0))

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

N,M,Q = map(int, input().split())		# 5 7 2

X=[]
for i in range(Q):
	a,b,c,d = map(int, input().split())		# 5 7 2
	a-=1
	b-=1
	X.append([a,b,c,d])



def score(X,A):
	iReturn=0
	for q in range(Q):
		a = X[q][0]
		b = X[q][1]
		c = X[q][2]
		d = X[q][3]
		
		if A[b]-A[a]==c: iReturn+=d
	
	return iReturn

ans=0
for i0 in range(0,11):
	for i1 in range(i0,11):
		for i2 in range(i1,11):
			for i3 in range(i2,11):
				for i4 in range(i3,11):
					for i5 in range(i4,11):
						for i6 in range(i5,11):
							for i7 in range(i6,11):
								for i8 in range(i7,11):
									for i9 in range(i8,11):
										A=[i0,i1,i2,i3,i4,i5,i6,i7,i8,i9]
										ans = max(ans,score(X,A))

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
N,M,Q=map(int,input().split())
A = []
B= []
C =[]
D=[]
Answer = 0
for i in range(1,Q+1):
    a,b,c,d=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)
ListC = list(itertools.product(range(1,M+1),repeat=N))
for j in range(1,len(ListC)+1):
    List = ListC[j-1]
    An = 0
    for k in range(1,Q+1):
        if List[B[k-1]-1] - List[A[k-1]-1] == C[k-1]:
            An += D[k-1]
        if Answer < An:
            Answer = An
print(Answer)

Submission status: Wrong Answer

Solution code:
N,M,Q = map(int,input().split(" "))
rule_list = []
for i in range(Q):
        rule_list.append(list(map(int,input().split(" "))))
basic_list = [0]*N
pattern_list = []
def mklist(temp,temp2):
    global pattern_list
    global basic_list
    if temp>0:
        for j in range(temp2,N):
            basic_list[j] +=1
            pattern_list.append([*basic_list])
            mklist(temp-1,j)
            basic_list[j] -=1

temp = M-1
mklist(temp,0)
max_d = 0
for i in pattern_list:
    sum_d = 0
    for j in rule_list:
        a = j[0]
        b = j[1]
        c = j[2]
        d = j[3]
        dis = 0
        for k in range(a-1,b-1):
            dis += i[k]
        if dis == c:
            sum_d += d
    max_d = max(max_d,sum_d)

print(max_d)

Submission status: Wrong Answer

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

mids = [i for i in range(m)]
max_ans = 0
for mid in itertools.combinations_with_replacement(mids, n-1):
    if sum(mid)>m-1:
        continue
#     print(mid)
    sum_ans=0
    for i in qs:
        sum_mid = 0
        for j in range(i[0]-1,i[1]-1):
            sum_mid += mid[j]
        if sum_mid == i[2]:
            sum_ans += i[3]
    if sum_ans > max_ans:
        max_ans = sum_ans
        
print(max_ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
import copy

n,m,q=map(int,input().split())
x=[list(map(int,input().split())) for i in range(q)]
#x.sort(key=lambda x:x[3],reverse=True)

def calc(lst):
  sum=0
  #print(lst)
  for a,b,c,d in x:
    if(lst[b]-lst[a]==c):
      sum+=d
      #print(a,b,c,d,sum)
  return sum
      
def dfs(n,lst,i):
  ans=0
  lst.append(i)
  if(n==0):
    ans=calc(lst)
  else:
    for j in range(i,m+1):
      tmp=copy.copy(lst)
      ans=max(ans,dfs(n-1,tmp,j))
  return ans


print(dfs(n+1,[],0))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n,m,q=l_in()

abcd=[]
for _ in range(q):
    abcd.append(l_in())

abcd.sort(reverse=True)

s = set()
res = 0

from collections import deque


s = []

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


def search(i, current):
    tmp = 0
    for d in range(current, m+1):
        s.append(d)
        if i == n-1:        
            tmp = max(tmp, check())
        else:
            tmp = max(tmp, search(i+1, d))
        s.pop()

    return tmp


print(search(0,0))

Submission status: Wrong Answer

Solution code:
import itertools
def generateA(N, M):
    m = range(1, M+1)
    A = [1 for i in range(N)]
    lastA = [M for i in range(N)]
    while True:
        yield A
        if A == lastA:
            break
        for i in range(N):
            if A[N-1-i] == M:
                continue
            A[N-1-i] += 1
            for j in range(N-1-i+1, N):
                A[j] = 1
            break

def point(A, L):
    result = 0
    for (a, b, c, d) in L:
        if A[b-1] - A[a-1] == c:
            result += d

    return result

def summary(N, M, L):
    As = generateA(N, M)

    results = []
    for A in As:
        print(A)
        results.append(point(A,L))
    return results

def main():
    N, M ,Q = map(int, input().split())
    L = []
    for i in range(Q):
        a, b, c, d = map(int, input().split())
        L.append((a,b,c,d))
    results = summary(N,M,L)

    print(max(results))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
for case in combinations([i for i in range(1,n+m+1)],n):
    now = 0
    A = [x-i for i,x in enumerate(case)]
    for i in range(q):
        a,b,c,d = abcd[i]
        if A[b-1]-A[a-1] == c:
            now += d
    ans = max(now,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # import sysすること！再帰の深さ制限を緩くする
n, m, q = list(map(int, input().split()))
data = []
for i in range(q):
    a, b, c, d = list(map(int, input().split()))
    data.append([a-1, b-1, c, d])


def dfs(i, t):
    if i == n:
        score = 0
        for a, b, c, d in data:
            if t[b] - t[a] == c:
                score += d
        global ans
        ans = max(ans, score)
        return
    if i == 0:
        for j in range(m+1):
            dfs(i+1, [j])
    else:
        for j in range(t[-1], m+1):
            dfs(i+1, t+[j])


ans = 0
dfs(0, [])
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys
import itertools

n,m,q=map(int,input().split())
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
e=[0]*m
for i in range(q): 
  a[i],b[i],c[i],d[i]=map(int,input().split())
for j in range(m):
  e[j]=j+1
total=0
maxnum=0
num=1<<len(e)
for current in itertools.product(e, repeat = n):
  if(len(current)==n):
    total=0
    #print(current)
    for i in range(q):
      if current[b[i]-1]-current[a[i]-1]==c[i]:
        total+=d[i]
    #print(total)
    maxnum=max(maxnum,total)
print(maxnum)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
A = [1] * N
ans = 0

for a, b, c, d in l:
    if A[b-1] - A[a-1] == c:
        ans += d
count = 1
if M > 1:
    while True:
        count += 1
        A[-1] += 1
        ans_ = 0
        for a, b, c, d in l:
            if A[b-1] - A[a-1] == c:
                ans_ += d
        ans = max(ans, ans_)

        if A[-1] == M + 1:
            for i in range(1, N):
                A[-(i+1)] += 1
                for k in range(N-(i+1), N):
                    A[k] = A[-(i+1)]
                if A[-(i+1)] != M + 1:
                    break
        #print(A)
        if A[0] == M:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(Q)]
A = [1] * N
ans = 0

for a, b, c, d in l:
    if A[b-1] - A[a-1] == c:
        ans += d
count = 1
if M > 1:
    while True:
        count += 1
        A[-1] += 1
        ans_ = 0
        for a, b, c, d in l:
            if A[b-1] - A[a-1] == c:
                ans_ += d
        ans = max(ans, ans_)

        if A[-1] == M + 1:
            for i in range(1, N):
                A[-(i+1)] += 1
                for k in range(N-(i+1), N):
                    A[k] = A[-(i+1)]
                if A[-(i+1)] != M + 1:
                    break
        #print(A)
        if A[0] == M:
            break
print(ans)
print(count)

Submission status: Wrong Answer

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

n=0
for i in range(N):
    n+=10**i
m=0
for i in range(N):
    m+=M*10**i
ans=0
for j in range(n,m+1):
    J=str(j)
    total=0
    for k in range(Q):
        if int(J[S[k][1]-1])-int(J[S[k][0]-1])==S[k][2]:
            total+=S[k][3]
    if total>ans:
        ans=total

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
from itertools import combinations_with_replacement

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

def main():
    N, M, Q = map(int, input().split())
    abcd = [list(map(int, input().split())) for _ in range(Q)]
    ans = 0
    for pattern in combinations_with_replacement(range(1, 11), N):
        ans = max(ans, check(abcd, pattern))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    from math import floor
    a,b,n = readInts()

    p = b-1 if b-1 < n else n-1
    #print(p)
    print(floor(a*p/b) - a*floor(p/b))


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

N,M,Q = map(int, input().split())		# 5 7 2

X=[]
for i in range(Q):
	a,b,c,d = map(int, input().split())		# 5 7 2
	a-=1
	b-=1
	X.append([a,b,c,d])


A=[0]*10

def score(X,A):
	iReturn=0
	for q in range(Q):
		a = X[q][0]
		b = X[q][1]
		c = X[q][2]
		d = X[q][3]
		
		if A[b]-A[a]==c: iReturn+=d
	
	return iReturn

ans=0
for i0 in range(1,11):
	for i1 in range(i0,11):
		for i2 in range(i1,11):
			for i3 in range(i2,11):
				for i4 in range(i3,11):
					for i5 in range(i4,11):
						for i6 in range(i5,11):
							for i7 in range(i6,11):
								for i8 in range(i7,11):
									for i9 in range(i8,11):
										A=[i0,i1,i2,i3,i4,i5,i6,i7,i8,i9]
										ans = max(ans,score(X,A))

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product
 
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
 
A = [0, 1]
combnation = list(product(A, repeat=Q))
abcd.sort()
 
ans = 0
for comb in combnation:
    print(comb)
    comb_list = [abcd[i] for i, k in enumerate(comb) if k==1]
    check = True
    A = [0 for i in range(N+1)]
    amount = 0
    for a, b, c, d in comb_list:
        amount += d
        if A[a] == 0 and A[b] == 0:
            if A[a] == 0 and A[b] == 0:
                A[a] = 1
                A[b] = 1 + c
    
        elif A[a] == 0 and A[b] != 0:
            if A[b] - c >= 1:
                A[a] = A[b] - c
            else:
                check = False
                break
    
        elif A[a] != 0 and A[b] == 0:
            if c + A[a] <= M:
                A[b] = A[a] + c
            else:
                check = False
                break
        else:
            if A[b] - A[a] != c:
                check = False
                break
    
    if check and amount > ans:
        ans = amount
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
a=[0 for i in range(Q)]
b=[0 for i in range(Q)]
c=[0 for i in range(Q)]
d=[0 for i in range(Q)]
A=[0 for i in range(N)]
def swap(a,b,c):
    kari=a[b]
    a[b]=a[c]
    a[c]=kari
    return a
for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int, input().split())
for i in range(0,N):
    if(N==1):
        break
        for j in range(1,N-i):
            if d[j]>d[j-1]:
                d=swap(d,j,j-1)
                a=swap(a,j,j-1)
                b=swap(b,j,j-1)
                c=swap(c,j,j-1)
sum=0
recode={}
for i in range(0,Q):
    if a[i] in recode.keys() and b[i] in recode.keys():
        continue
    else:
        recode[a[i]]=1
        recode[b[i]]=1
        sum+=d[i]
print(sum)

Submission status: Wrong Answer

Solution code:
import itertools
def generateA(N, M):
    m = range(1, M+1)
    A = [1 for i in range(N)]
    lastA = [M for i in range(N)]
    while True:
        yield A
        if A == lastA:
            break
        for i in range(N):
            if A[N-1-i] == M:
                continue
            A[N-1-i] += 1
            break

def point(A, L):
    result = 0
    for (a, b, c, d) in L:
        if A[b-1] - A[a-1] == c:
            result += d

    return result

def summary(N, M, L):
    As = generateA(N, M)

    results = []
    for A in As:
        results.append(point(A,L))
    return results

def main():
    N, M ,Q = map(int, input().split())
    L = []
    for i in range(Q):
        a, b, c, d = map(int, input().split())
        L.append((a,b,c,d))
    results = summary(N,M,L)

    print(max(results))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

from itertools import product

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

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

kumiawase = list(product(range(1, m + 1), repeat=n))

ans = 0
for kumi in kumiawase:
    A = [0] + list(kumi)
    temp = 0
    for abcd in ABCD:
        a, b, c, d = abcd
        if A[b] - A[a] == c:
            temp += d
    ans = max(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
#import collections
# c = collections.Counter()
#import itertools as it
#import math
#import numpy as np
#import sys 
#sys.exit()
 
#  = input()
#k  = int(input())
n,m,q  = map(int, input().split())
'''
#  = list(map(int, input().split()))
'''
'''
n = input()
a = [int(input()) for i in range(n)]
'''

#n,m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(q)]

print(n,m,q)
print(a)
#-------------------------------#
A=[]
for i in range(1,m+1):
    A.append(i)
print(A)

Submission status: Wrong Answer

Solution code:
n,m,s=map(int,input().split())
a=[]
q=[[0]]
while q:
  x=q.pop()
  if len(x)<=n:
    for j in range(x[-1],m+1):
      c=x+[j]
      q.append(c)
  else:
    a.append(x)
li=[tuple(map(int,input().split())) for _ in  range(s)]

ans=0
for A in a:
  s=0
  for i,j,k,l in li:
    if A[j]-A[i]==k:
      s+=l
  ans=max(ans,s)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,q = list(map(int,input().split()))
a = [[]]*q
for i in range(q):
    a[i] = list(map(int,input().split()))
ans = 0
def calc(x):
    cnt = 0
    for j in range(q):
        if x[a[j][1]-1] - x[a[j][0]-1] == a[j][2]:
            cnt += a[j][3]
    return cnt
for k in range(10*n):
    l = [0]*n
    b = str(k).zfill(n)
    for o in range(n):
        if o == 0:
            l[o] = 1
        else:
            l[o] = int(b[o])
            if l[o] < l[o-1]:
                break
    else:
        if l[n-1] <= m:
            if ans < calc(l):
                ans = calc(l)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
N, M, Q = map(int,input().split())
ABCD = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for s in itertools.combinations_with_replacement(range(1,N+1),N):
    temp = 0
    for i in range(Q):
        if s[ABCD[i][1]-1] - s[ABCD[i][0]-1] == ABCD[i][2]:
            temp += ABCD[i][3]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

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

p_max = 0
point =0
A = [1]*n
E = [m]*n

while A != E :

    #得点計算
    for j in range(q):
        if A[List[j][1]-1] - A[List[j][0]-1] == List[j][2]:
            point += List[j][3]
  #      print(j,point)

    #最大値の更新
    if p_max < point:
        p_max = point
    point = 0

    #Aの更新
    for i in reversed(range(0,n)):
        if A[i] < m:
            A[i] = A[i] + 1
            break
        elif A[i-1] == m:
            A[i] = m
        else:
            A[i] = A[i-1] + 1 
#    print(A)
print(p_max)

Submission status: Wrong Answer

Solution code:
N, M, Q = map(int, input().split())
a=[0 for i in range(Q)]
b=[0 for i in range(Q)]
c=[0 for i in range(Q)]
d=[0 for i in range(Q)]
A=[0 for i in range(N)]
def swap(a,b,c):
    kari=a[b]
    a[b]=a[c]
    a[c]=kari
    return a
for i in range(Q):
    a[i],b[i],c[i],d[i]=map(int, input().split())
for i in range(0,N):
    if(N==1):
        break
        for j in range(1,N-i):
            if d[j]<d[j-1]:
                d=swap(d,j,j-1)
                a=swap(a,j,j-1)
                b=swap(b,j,j-1)
                c=swap(c,j,j-1)
sum=0
recode={}
for i in range(0,Q):
    if a[i] in recode.keys() and b[i] in recode.keys():
        continue
    else:
        recode[a[i]]=1
        recode[b[i]]=1
        sum+=d[i]
print(sum)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
from itertools import *


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    l = [i for i in range(1,M+1)]
    A = []
    if N==M:
        A.append(l)
    else:
        for v in combinations_with_replacement(l, N):
            if sorted(v) not in A:
                A.append(sorted(v))

    ans = [0] * len(A)

    for i in range(len(A)):
        #print(A[i])
        for j in range(Q):
            #print(a[j],b[j],c[j],d[j],A[i][b[j]-1]-A[i][a[j]-1], c[j], d[j])
            #print(A[i][b[j]-1]-A[i][a[j]-1], c[j])
            if (A[i][b[j]-1]-A[i][a[j]-1])==c[j]:
                ans[i] += d[j]
        #print(ans[i])
    #print(ans)
    print(max(ans))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
A,B,N = map(int, input().split())
if B > N:
    res = (A*N//B)-A*(N//B)
else:
    N = N - N%B - 1
    res = (A*N//B)-A*(N//B)
print(res)

Submission status: Wrong Answer

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

p_max = 0
point =0
A = [1]*n
E = [m]*n

while A != E :

    #得点計算
    for j in range(q):
        if A[List[j][1]-1] - A[List[j][0]-1] == List[j][2]:
            point += List[j][3]
  #      print(j,point)

    #最大値の更新
    if p_max < point:
        p_max = point
    point = 0

    #Aの更新
    for i in reversed(range(0,n)):
        if A[i] < m:
            A[i] = A[i] + 1
            break
        elif A[i-1] == m:
            A[i] = m
        else:
            A[i] = A[i-1] + 1 
    print(A)
print(p_max)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
from itertools import *


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    l = [i for i in range(1,M+1)]
    A = []
    if N==M:
        A.append(l)
    else:
        for v in combinations_with_replacement(l, N):
            if sorted(v) not in A:
                A.append(sorted(v))

    ans = [0] * len(A)

    for i in range(len(A)):
        print(A[i])
        for j in range(Q):
            #print(a[j],b[j],c[j],d[j],A[i][b[j]-1]-A[i][a[j]-1], c[j], d[j])
            #print(A[i][b[j]-1]-A[i][a[j]-1], c[j])
            if (A[i][b[j]-1]-A[i][a[j]-1])==c[j]:
                ans[i] += d[j]
        #print(ans[i])
    #print(ans)
    print(max(ans))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
A,B,N = map(int, input().split())
if B > N:
    res = A*N//B-A*(N//B)
else:
    N = N - N%B - 1
    res = A*N//B-A*(N//B)
print(res)

Submission status: Wrong Answer

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

n=0
for i in range(N):
    n+=10**i
m=0
for i in range(N):
    m+=M*10**i
ans=0
for j in range(n,m+1):
    J=str(j)
    Y=[]
    for k in range(Q):
        if int(J[S[k][1]-1])-int(J[S[k][0]-1])==S[k][2]:
            Y.append((S[k][3]))
    C=sum(Y)
    if C>ans:
        ans=C

print(ans)

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 itertools

A = []
M = []
for i in range(1, m+1):
    M.append(i)
for team in itertools.combinations(M, n):
    A.append(team)
    
B = [0]*len(A)
for i in range(q):
    a = abcd[i][0]
    b = abcd[i][1]
    c = abcd[i][2]
    d = abcd[i][3]
    for j in range(len(A)):
        if A[j][b-1] - A[j][a-1] == c:
            B[j] += d

print(B)

Submission status: Wrong Answer

Solution code:
import sys
import itertools


sys.setrecursionlimit(10 ** 7)

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

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


ans = 0
for lst in itertools.combinations_with_replacement(range(1, N + 1), N):
    s = 0
    for a, b, c, d in ABCD:
        if (lst[b - 1] - lst[a - 1]) == c:
            s += d

    ans = max(ans, s)

print(ans)

Submission status: Wrong Answer

Solution code:
A,B,N=map(int,input().split())
x=min(B-1,N)
print(int(A*x/B)-A*int(x/B))

Submission status: Wrong Answer

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
import itertools

N, M, Q = MAP()
a, b, c, d = [], [], [], []
for _ in range(Q):
    ai, bi, ci, di = MAP()
    a.append(ai-1); b.append(bi-1); c.append(ci); d.append(di)


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

ans = 0
for A in itertools.combinations_with_replacement(range(1, 11), N):
    ans = max(ans, solve(A))

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

n, m, q = [int(x) for x in input().split()]
 
tmp = []
for i in range(q):
    a, b, c, d = [int(x) for x in input().split()]
    tmp.append([a, b, c, d])

t = list(itertools.combinations_with_replacement(range(1, m + 1), m))

score = 0
for _t in t:
    _s = 0
    for a, b, c, d in tmp:
        if len(_t) <= a or len(_t) <= b:
            continue
        if c == _t[b - 1] - _t[a - 1]:
            _s += d
    score = max(_s, score)
print(score)

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 = [1] * N
point = 0
points = [0] * N
for i in range(Q):
  if A[b[i]-1] == 1:
    A[b[i]-1] = A[a[i]-1] + c[i]
    point += d[i]
    points[b[i]-1] += d[i]
  else:
    if points[b[i]-1] <= d[i]:
      A[b[i]-1] = A[a[i]-1] + c[i]
      point += d[i]
      point -= points[b[i]-1]
    
print(point)