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: Runtime Error

Solution code:
import itertools
n, m, q = map(int, input().split())
A = itertools.product(range(1, m + 1), repeat=m)
quads = [[int(i) for i in input().split()] for j in range(q)]
max_p = 0
for a in A:
    p = 0
    for quad in quads:
        if a[quad[1] - 1] - a[quad[0] - 1] == quad[2]:
            p += quad[3]
    if max_p < p:
        max_p = p
print(max_p)

Submission status: Runtime Error

Solution code:
# https://www.youtube.com/watch?v=C5_NnCp1CRI&feature=youtu.be

import sys
sys.stdin = open('input.txt')
import itertools

# C++
# https://atcoder.jp/contests/abc165/submissions/12656990
# Python
# https://atcoder.jp/contests/abc165/submissions/12832942
# https://atcoder.jp/contests/abc165/submissions/12832027
def dfs(A):
    """
    dfs([1]) で使用。index 1 からが本来求めたい数列になる
    """
    global ans
    if len(A) == n+1:
        # print(A)
        now = 0
        for j in range(q):
            if A[b[j]] - A[a[j]] == c[j]:
                now += d[j]
        ans = max(ans, now)
        return
    else:
        # 以下 2 行いずれの場合も、既存リストが mutate されるのでうまく再帰できない
        # A.append(A[-1])
        # A += [A[-1]]
        # 以下は変数の再定義なのでリストは mutate されない
        A = A + [A[-1]]
        # こちらも mutate しない例
        # A = A[:]
        # A.append(A[-1])
        while A[-1] <= m:
            # print(A)
            dfs(A)
            A[-1] += 1

# https://atcoder.jp/contests/abc165/submissions/12679398
# AC
def dfs2(i):
    global ans
    for j in range(num[i-1], m):
        num[i] = j
        if i == n:
            print(num)
            now = 0
            for k in range(q):
                if num[b[k]] - num[a[k]] == c[k]:
                    now += d[k]
            ans = max(ans, now)
        else:
            dfs2(i+1)

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

    global ans
    it = [i for i in range(m)]
    for A in itertools.combinations_with_replacement(it, n):
        update_ans(A)


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

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

dfs([1])

# num = [0] * (n+1)
# dfs2(1)

# solve_combinations_with_replacement(n, m)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import math
import scipy
import itertools

def score(arr):
    hoge = 0
    for ai,bi,ci,di in zip(a, b, c, d):
        if arr[bi-1]-arr[ai-1] == ci:
            hoge += di
    return hoge

def dfs(arr):
    if len(arr) == n:
        return score(arr)
    last = arr[-1] if len(arr) > 0 else 0
    res = 0
    for i in range(last, m+1):
        a.append(i)
        res = max(res, dfs(arr))
        a.pop()
    return res

def solve():
    global a, b, c, d, n, m, q
    n, m, q = map(int, input().split())
    a, b, c, d = [0]*q, [0]*q, [0]*q, [0]*q
    for i in range(q):
        a[i], b[i], c[i], d[i] = map(int, input().split())
    print(dfs([]))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import itertools

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

# 重複組み合わせ: nHr = n+r-1Cr を考える
ans = 0
for comb in itertools.combinations_with_replacement([i for i in range(1, N+2)], M):
    temp = 0
    for abcd in ABCD:
        a, b, c, d = abcd
        if comb[b-1] - comb[a-1] == c:
            temp += d
    if temp >= ans:
        ans = temp

    # print("comb: {}, temp: {}, ans: {}".format(comb, temp, ans))

print(ans)

Submission status: Runtime Error

Solution code:
N,M,Q = map(int, input().split())
ABCD = [list(map(int, input().split())) for i in range(Q)]
score=0
for i in range(1, M+1):
  dfs([i])
print(score)


def dfs(l):
    # 末端まで探索したか判定
    if judge_end(l,N):
        global score
        
        tmp_score = calc_score(ABCD,l)
        score = max(tmp_score, score)
        return
    else:
        # 現時点から選べる選択肢の列挙と探索
        nexts = listup_next(l[-1], M)
        for x in nexts:
            dfs(num_list+[x])


def judge_end(l,N):
    return len(l)==N


def calc_score(ABCD, l):
    s=0
    for a,b,c,d in ABCD:
      if l[b-1] - l[a-1] ==c:
        s+=d
    return s


def listup_next(m, M):
    return range(m,M+1)

Submission status: Runtime Error

Solution code:
import itertools


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

a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

A = [i for i in range(1, M+1)] # [1, 2, 3, 4, ... M]

A_pattern = list(itertools.combinations_with_replacement(A, M)) # 10^5

ans = 0

for a_pattern in A_pattern: # 数列全部調べる
    ansi = 0
    for i in range(Q): # 得点になるかどうか調べる

        if a_pattern[b[i]-1] - a_pattern[a[i]-1] == c[i]: # A[bi] - A[ai] == ci -> ansi += di
            ansi += d[i]

    ans = max(ans, ansi)


print(ans)

Submission status: Runtime Error

Solution code:
import itertools


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

a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

A = [i for i in range(1, M+1)] # [1, 2, 3, 4, ... M]

A_pattern = list(itertools.combinations_with_replacement(A, M)) # 10^5

ans = 0

for a_pattern in A_pattern: # 数列全部調べる
    ansi = 0
    for i in range(Q): # 得点になるかどうか調べる
        if a_pattern[b[i]] - a_pattern[a[i]] == c[i]: # A[bi] - A[ai] == ci -> ansi += di
            ansi += d[i]

    ans = max(ans, ansi)


print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations_with_replacement
import numpy as np

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

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

ans = 0

# 可能な数列Aの全探索、そのままやとめちゃ多いからなんか工夫する？
# そもそもAの全探索方がいまいちわからん...

A = np.array(list(itertools.combinations_with_replacement(range(1, M + 1), N)))
scores = np.zeros(len(A))
for g in G:
    a, b, c, d = g
    cond = A[:, b - 1] - A[:, a - 1] == c
    scores += d * cond

print(scores.max())

Submission status: Runtime Error

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())
import itertools
for A in itertools.combinations_with_replacement(range(1,M+1),N):
    ans=0
    if i in range(Q):
        s=0
        if A[b[i]]-A[a[i]]==c[i]:
           s+=d[i]
            ans=max(ans,s)
print(ans)

Submission status: Runtime Error

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())
import itertools
for A in itertools.combinations_with_replacement(range(1,M+1),N):
    ans=0
    if i in range(Q):
        s=0
        A[b[i]]-A[a[i]]=c[i]
        s+=d[i]
        ans=max(ans,s)
print(ans)

Submission status: Runtime Error

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

mm = [0]*m

for k in range(1,m+1,1):
    mm[k-1]=k

A = list(itertools.combinations_with_replacement(mm, n))
su = 0
ans = 0

for x in range(len(A)):
    l=A[x]
    su = 0
    for h in range(q):
        if l[b[h]-1]-l[a[h]-1] == c[h]:
            su += d[h]

    if su > ans:
        ans = su

print(str(ans))

Submission status: Runtime Error

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

a=[]
b=[]
c=[]
d=[]

max_= 0

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


for i in range(q):
  aa,bb,cc,dd = map(int, input().split())
  a.append(aa)
  b.append(bb)
  c.append(cc)
  d.append(dd)
  
dfs([1])

print(max_)

Submission status: Runtime Error

Solution code:
import itertools
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())
ans = 0
for A in itertools.combinations_with_replacement(range(1,m+1),n):
    cnt = 0
    for i in range(q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            cnt += d[i]
    ans = max(ans,tmp)
print(ans)

Submission status: Runtime Error

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(n):
    a[i],b[i],c[i],d[i] = map(int,input().split())
    a[i] -= 1
    b[i] -= 1
A = [0]*n
ans = 0
def dfs(h):
    global ans
    if h == n-1:
        cnt = 0
        for i in range(q):
            if A[b[i]] - A[a[i]] == c[i]:
                cnt += d[i]
        ans = max(ans,cnt)
    else:
        for i in range(A[h],m+1):
            A[h+1] = i
            dfs(h+1)
    return

for i in range(1,m+1):
    A[0] = i
    dfs(0)
print(ans)

Submission status: Runtime Error

Solution code:
import itertools
N,M,Q=list(map(int,input().split()))
src=[list(map(int,input().split())) for q in range(Q)]
ML=[n+1 for n in range(M)]
X=list(itertools.combinations_with_replacement(ML,N))
L=[]
for x in X:
  a=0
  for s in src:
    if x[s[1]] - x[s[0]] == s[2]:
      a+=s[3]
  L.append(a)
print(max(L))

Submission status: Runtime Error

Solution code:
import math
import itertools
n,m,q=map(int,input().split())
a=list()
b=list()
c=list()
d=list()
for i in range(q):
    x,y,z,w=map(int,input().split())
    a.append(x)
    b.append(y)
    c.append(z)
    d.append(w)
ans=0
a=0
l=[i for i in range(1,m+1)]
for v in itertools.combinations_with_replacement(l,n):
    for i in range(q):
        if v[b[i]-1]-v[a[i]-1]==c[i]:
            a+=d[i]
    if a>ans:
        ans=a
print(a)

Submission status: Runtime Error

Solution code:
B = {}#辞書作成
N = int(input())
A = list(map(int, input().split()))


for i in range(N):
    B[i+1] = 0
#print(B)
for j in A:
    B[j] += 1
#print(B)
for k in B.values():
    print(k)

Submission status: Runtime Error

Solution code:
import itertools
N,M,Q = map(int, input().split())
cand = list(itertools.combinations_with_replacement(N+M-1,N))
fs = [list(map(int, input().split())) for i in range(Q)]

def calc(fs,As):
  count = 0
  for a,b,c,d in fs:
    if As[b-1] - As[a-1] == a:
      count += d
	return count
ans = 0
for As in cand:
  ans = max(ans,calc(fs,As))

print(ans)

Submission status: Runtime Error

Solution code:
N,M,Q=map(int,input().split())
v=[[] for _ in range(Q)]
o=0
for i in range(Q):
    v[i]=list(map(int,input().split()))
l=list(itertools.combinations(range(M+N), N))
for x in l:
    s=0
    a=[]
    for i in range(N):
        a.append(x[i]-i)
    for i in range(Q):
        if a[l[i][2]]-a[l[i][1]]-l[i][3]==0:
            s+=l[i][4]
    if s>o:
        o=s
print(o)

Submission status: Runtime Error

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

A = [1]*N
def recur(A=A, index=0, lim=M):
    if index>len(A)-2:
        return
    while A[index] <= lim-1:
        A[index] += 1
        yield A[::-1]
        yield from recur(A[:], index+1, A[index])
    return

def get_point(A):
    return sum((A[b[1]-1]-A[b[0]-1]==b[2])*b[3] for b in B)

print(max(get_point(A), *(get_point(i) for i in recur())))

Submission status: Runtime Error

Solution code:
# 重複順列　19C10
from itertools import combinations_with_replacement as cw

n,m,q = map(int, input().split())
a_candidates = cw([i+1 for i in range(m)],m)
fs = [list(map(int, input().split())) for i in range(q)]

def calc(fs,As):
    cnt = 0
    for a,b,c,d in fs:
        a -= 1
        b -= 1
        if As[b]-As[a] == c: cnt += d
    return cnt

ans = 0
for As in list(a_candidates):
    ans = max(ans, calc(fs,As))

print(ans)

Submission status: Runtime Error

Solution code:
# 重複順列　19C10
from itertools import combinations_with_replacement as cw

n,m,q = map(int, input().split())
a_candidates = cw([i+1 for i in range(m)],m)
fs = [list(map(int, input().split())) for i in range(q)]

def calc(fs,As):
    cnt = 0
    for a,b,c,d in fs:
        if As[b]-As[a] == c: cnt += d
    return cnt

ans = 0
for As in list(a_candidates):
    ans = max(ans, calc(fs,As))

print(ans)

Submission status: Runtime Error

Solution code:
N,M,Q = map(int,input().split())
li = []
counter = 0
n = []
for s in range(Q):
  li.append(list(map(int,input().strip())))

for i in range(10**(N-1),10**N):
  k = str(i)
  for q in range(Q):
    if k[li[q][1]] -  k[li[q][0]] ==  k[li[q][2]]:
      counter +=  k[li[q][3]]
    n.append(counter)
    counter = 0
print(max(n))

Submission status: Runtime Error

Solution code:
N,M,Q = map(int,input().split())
li = []
counter = 0
n = []
for s in range(Q):
  li.append(list(map(int,input().strip())))

for i in range(10**(N-1),10**N):
  k = str(i)
  for q in range(Q):
    if k[li[q][1]] -  k[li[q][0]] ==  k[li[q][2]]:
      counter +=  k[li[q][3]]
    n.append(counter)
    counter = 0
print(max)

Submission status: Runtime Error

Solution code:
import itertools

N, M, Q = [int(i) for i in input().split()]
pos_A = sorted([i for i in range(1, M+1)] * N)
comb_A = list(itertools.combinations(pos_A, N))

for i in range(Q):
    a, b, c, d  = [int(i) for i in input().split()]
    for j, k in enumerate(comb_A):
        if k[b-1] - k[a-1] == c:
            score[j] += d
print(max(score))

Submission status: Runtime Error

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

def check():
    res = 0
    for i in range(q):
        ra, rb, rc, rd = req[i]
        if a[rb - 1] - a[ra - 1] == rc:
            res += rd
    return res

def set_a(idx: int, au: int):
    ans = 0
    for i in range(au, m + 1):
        a[idx] = i
        if idx + 1 < n:
            ans = max(set_a(idx + 1, i), ans)
        else:
            ans = max(check(), ans)
    return ans

print(set_a(0, 1))

Submission status: Runtime Error

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

def check():
    res = 0
    for i in range(q):
        ra, rb, rc, rd = req[i]
        if a[rb - 1] - a[ra - 1] == rc:
            res += rd
    return res

def set_a(idx: int, au: int):
    ans = 0
    for i in range(au, m + 1):
        a[idx] = i
        if idx + 1 < n:
            ans = max(set_a(idx + 1, i), ans)
        else:
            ans = max(check(), ans)
    return ans

print(set_a(0, 0))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
h = list(map(int,input().split()))
 
H = [0]*n
for p in range(n):
  H[p] = []
 
 
for _ in range(m):
  a,b = map(int,input().split())
 
  H[a-1].append(h[b-1])
  H[b-1].append(h[a-1])

ans = 0
i = 0
 
for one in H:
 
  i += 1
  if one == []:
    ans += 1
  else:
    for k in one:
      if k >= h[i-1]:
        break
    else:
      ans += 1
        
print(ans)

Submission status: Runtime Error

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

H = [0]*n
for p in range(n):
  H[p] = []


for _ in range(m):
  a,b = map(int,input().split())

  H[a-1].append(h[b-1])
  H[b-1].append(h[a-1])

ans = 0
i = 0

for one in H:

  i += 1
  if one == []:
    ans += 1
  else:
    for k in one:
      if k >= h[i-1]:
        break
    else:
      ans += 1
        
print(ans)

Submission status: Runtime Error

Solution code:
import itertools
n, m, q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(q)]
Max = 0
for A in itertools.combinations_with_replacement(range(1, m + 1) n):
    #score = sum([d for a, b, c, d in abcd if C[b - 1] - C[a - 1] == c])
    score = 0
    for ai, bi, ci, di in abcd:
        if A[bi - 1] - A[ai - 1] == ci:
            score += di
    Max = max(Max, score)
    #print(C)
print(Max)

Submission status: Runtime Error

Solution code:
from itertools import combinations_with_replacement

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


def calc(l):
    score = 0
    for a, b, c, d in cond:
        if l[b] - l[a] == c:
            score += d
    return score


ans = 0
l = [0] * n
l[0] = 1


def dfs(l_len):
    if l_len == n:
        ans = max(ans, calc(l))
        return

    for i in range(l[l_len - 1], m + 1):
        l[l_len] = i
        dfs(l_len + 1)


dfs(0)
print(ans)

Submission status: Runtime Error

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

K= [list(map(int, input().split())) for i in range(Q)]#二次配列

ans = 0
for A in itertools.combinations_with_replacement([i for i in range(1, M + 1)], N):
    ans1 = 0
    for i in range(Q):
        a, b, c, d = K[i]　#各数字に配列の値を代入する
        if (A[b - 1] - A[a - 1] == c):
            ans1 += d
        ans = max(ans, ans1)
print(ans)

Submission status: Runtime Error

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] = [int(j) for j in input().split()]

matrix = []
for i in range(1,m+1):
    for j in range(i,m+1):
        for k in range(j,m+1):
            for l in range(k,m+1):
                matrix.append([i,j,k,l])

possible_ans = []
for i in range(len(matrix)):
    ans = 0
    for j in range(q):
        if matrix[i][b[j]-1] - matrix[i][a[j]-1] == c[j]:
            ans += d[j]
    possible_ans.append(ans)

print(max(possible_ans))

Submission status: Runtime Error

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] = [int(j) for j in input().split()]

matrix = []
for i in range(1,m+1):
    for j in range(i,m+1):
        for k in range(j,m+1):
            for l in range(k,m+1):
                matrix.append([i,j,k,l])

possible_ans = []
for i in range(len(matrix)):
    ans = 0
    for j in range(q):)
        if matrix[i][b[j]-1] - matrix[i][a[j]-1] == c[j]:
            ans += d[j]
    possible_ans.append(ans)

print(max(possible_ans))

Submission status: Runtime Error

Solution code:
mport itertools
def main():
    N, M, Q = map(int, input().split())
    abcd = []
    for _ in range(Q):
        a, b, c, d = map(int, input().split())
        abcd.append((a-1, b-1, c, d))
    score = 0
    for ones in list(itertools.combinations([i for i in range(N+M-1)], M-1)):
        temp = [0] * (N + M -1)
        for idx in ones:
            temp[idx] = 1
        A = [0] * N
        idx = 0
        sum_ = 0
        for i in temp:
            if i == 0:
                A[idx] = sum_ + 1
                idx += 1
            else:
                sum_ += 1
        score_temp = get_score(A, abcd)
        if score_temp > score:
            score = score_temp
    print(score)
            

def get_score(A, abcd):
    score = 0
    for a, b, c, d in abcd:
        if A[b] - A[a] == c:
            score += d
    return score

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
n, m, q = map(int, input().split())
alis, blis, clis, dlis = [], [], [], []
for _ in range(q):
    a, b, c, d = map(int, input().split())
    alis.append(a)
    blis.append(b)
    clis.append(c)
    dlis.append(d)
sys.setrecursionlimit(10**9)
Alist = [] 
def dfs(A):
    if len(A)==(n+1):
        Alist.append(A)
        return
    for i in range(A[-1], m+1):
        A.append(i)
        dfs(A)

dfs([1])

ans = 0
for A in Alist:
    score = 0
    for i in range(q):
        if A[blis[i]] - A[alis[i]] == clis[i]:
            score += dlis[i]
    ans = max(ans, score)

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def main():
    N,M=map(int,input().split())
    a=1
    b=N-1
    for i in range(1,M+1):
        print(a,b)
        a+=1
        b-=1
if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np

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

a = range(1,M+1)
arr = np.asarray(list(itertools.combinations_with_replacement(a,M))) #重複組み合わせ
#print(arr)

sc = np.zeros(arr.shape[0],dtype=int)
#print(sc)

for i in range(Q):
  abcd = list(map(int, input().split()))
  f = (arr[:,abcd[1]-1]-arr[:,abcd[0]-1])==abcd[2]
  sc += abcd[3]*f 
#
print(sc.max())

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np

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

a = range(1,M+1)
arr = np.asarray(list(itertools.combinations_with_replacement(a,M))) #重複組み合わせ
#print(arr)

sc = np.zeros(arr.shape[0],dtype=int)
#print(sc)

for i in range(Q):
  abcd = list(map(int, input().split()))
  f = (arr[:,abcd[1]]-arr[:,abcd[0]])==abcd[2]
  sc += abcd[3]*f 
#
print(sc.max())

Submission status: Runtime Error

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

#左側にあるボールの数 + 1
A = [1]*(M-1)
B = [0]*N
lis = A + B
max_result = 0

import itertools
 
permutations_lis = itertools.permutations(lis)
#全ての場合のリスト

for one_case in permutations_lis:
  A = [0]*N
  q = 1
  i = 0
  for j in one_case:
    if j == 0:
      A[i] = q
      i += 1
    else:
      q += 1
    #点数の計算
    count = 0
    for k in range(Q):
      a,b,c,d = data[k][0],data[k][1],data[k][2],data[k][3]
      if A[b]-A[a] == c:
        count += d
    max_result = max(count,max_resilt)
    
print(max_result)

Submission status: Runtime Error

Solution code:
N ,M ,Q = map(int,input().split())
l = [list(map(int,input().split())) for i in range(Q)]
ans=[]
score = 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):
                                        A = [i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                                        for a, b, c, d in l:
                                            if A[b+1] - A[a+1] == c:
                                                score += d
                                            else:
                                                pass
                                        ans.append(score)
                                        score = 0
print(max(ans))

Submission status: Runtime Error

Solution code:
from itertools import combinations_with_replacement

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

ans = 0
for A in combinations_with_replacement(range(1, M + 1), min(N, M)):
    tmp = 0
    for a, b, c, d in L:
        if A[b - 1] - A[a - 1] == c:
            tmp += d

    if tmp > ans:
        ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int n, m, q;
vector<int> a, b, c, d;
int ans;

void dfs(vector<int> A) {
    if (A.size() == n+1) {
        int now = 0;
        rep(i,q) {
            if (A[b[i]] - A[a[i]] == c[i]) now += d[i];
        }
        ans = max(ans, now);
        return ;
    }

    A.push_back(A.back());
    while(A.back() <= m) {
        dfs(A);
        A.back()++;
    }
}

int main() {
    cin >> n >> m >> q;
    a = b = c = d = vector<int>(q);

    rep(i,q) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    dfs(vector<int>(1,1));
    cout << ans << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
import itertools

n, m, q = map(int, input().split())
abcd = [tuple(map(int, input().split())) for _ in range(Q)]
ans = 0
for com in itertools.combinations_with_replacement(range(1, M + 1), N):
    score = sum([d for a, b, c, d in abcd if com[b-1] - com[a-1] == c])
    if ans < score:
        ans = score
print(ans)

Submission status: Runtime Error

Solution code:
N, M, Q = list(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] = list(map(int, input().split()))
li = [1]*12
ans = 0

def countup(l, n, m):
  l[n] += 1
  if l[n] > m:
    l[n] -= 1
    countup(l, n-1, m)
  return

while(li[0]==1):
  pt = 0
  for i in range(Q):
    if (li[b[i]]-li[a[i]]==c[i]):
      pt += d[i]
  ans = max(ans, pt)
  countup(li, N, M)

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
n,m,q = map(int,input().split())
a = []
b = []
c = []
d = []
for i in range(1,q+1):
    ai,bi,ci,di = map(int,input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)
AList =  list(itertools.combinations(range(1,m+1), n))
PointList = []
for array in AList:
    point = 0
    for j in range(q):
        if array[b[j]-1] - array[a[j]-1] == c[j]:
            point = point + d[j]
    PointList.append(point)
print(max(PointList))

Submission status: Runtime Error

Solution code:
import itertools
N,M,Q = map(int,input().split())
L = [list(map(int,input().split())) for _ in range(Q)]
data = list(range(1,M+1))
A=list(itertools.combinations_with_replacement(data,N))
max_score = 0

for Ai in A:
  x = 0
  for a,b,c,d in L:
    if Ai[b] - Ai[a] == c:
      x += d
  max_score = max(max_score,x)

print(max_score)

Submission status: Runtime Error

Solution code:
import itertools
N,M,Q = map(int,input().split())
L = [list(map(int,input().split())) for _ in range(Q)]
date = list(range(1,M+1))
A=list(itertools.combinations_with_replacement(data,N))
max_score = 0

for Ai in A:
  x = 0
  for a,b,c,d in L:
    if Ai[b] - Ai[a] == c:
      x += d
  max_score = max(max_score,x)
  
print(max_score)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
n, m, q  = map(int, input().split())
abcd  = [tuple(map(int, input().split())) for i in range(q)]

ans = 0

def check(A):
    global ans
    max_ = 0

    # 点数計算
    for (a, b, c, d) in abcd:
        if A[b-1] - A[a-1] == c:
            max_ += d

    # 最大値更新
    ans = max(ans, max_)

def suretu(A=[], st=1):
    # 再帰的に数字を1つずつ足してって数列生成
    for i in range(st, 11):
        if len(A) == m: # 数列完成
            check(A)
            return
        else:
            suretu(A + [i], i)

    return ans

print(suretu())

Submission status: Runtime Error

Solution code:
import itertools

#19 choose 9
#92378

def main():
    N, M, Q = map(int, input().split())
    Q = []
    final_score = 0
    for i in range(Q):
        Q.append(list(map(int, input().split())))
    for A in itertools.combinations_with_replacement(range(1, M+1), N):
        score = 0
        for q in Q:
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                score += q[3]
            final_score = max([final_score, score])
    print(final_score)
main()

Submission status: Runtime Error

Solution code:
import itertools

#19 choose 9
#92378

def main():
    N, M, Q = map(int, input().split())
    Q = []
    final_score = 0
    for i in range(Q):
        Q.append(list(map(int, input().split())))
    for A in itertools.combinations_with_replacement(range(1, M+1), N):
        score = 0
        for q in Q:
            if A[q[1]-1] - A[q[0]-1] == q[2]:
                score += q[3]
    out = max([final_score, score])
    print(out)
main()

Submission status: Runtime Error

Solution code:
import itertools
 
def check(lis, a):
    return sum((i[3] for i in a if lis[i[1]-1]-lis[i[0]-1]==i[2]))
   
n, m, q = map(int, input().split())
a = ((int(i) for i in input().split(" ")) for j in range(q))
ans = 0
for i in itertools.combinations_with_replacement(range(m), n):
    ans = max(ans, check(i, a))
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import itertools;n,m,q,*l=map(int,open(0).read().split());print(max(sum(d for a in[l[j*4:j*4+4]for j in range(q)]if i[b-1]-i[a-1]==c)for i in itertools.combinations_with_replacement(range(m),n)))

Submission status: Runtime Error

Solution code:
import itertools;n,m,q,*l=map(int,open(0).read().split());print(max(sum(d for a,b,c,d in l[::4]if i[b-1]-i[a-1]==c)for i in itertools.combinations_with_replacement(range(m),n)))

Submission status: Runtime Error

Solution code:
import numpy as np
import math
n, m, q = map(int,input().split())
max = 0
j = []
for i in range(q):
    aa,b,c,d = map(int,input().split())
    j.append([aa,b,c,d])

a = [0 for k in range(11)]
for i1 in range(1,11):
    a[1] = i1
    for i2 in range(i1,11):
        a[2] = i2
        for i3 in range(i2,11):
            a[3] = i3
            for i4 in range(i3,11):
                a[4] = i4
                for i5 in range(i4,11):
                    a[5] = i5
                    for i6 in range(i5,11):
                        a[6] = i6
                        for i7 in range(i6,11):
                            a[7] = i7
                                for i8 in range(i7,11):
                                a[8] = i8
                                    for i9 in range(i8,11):
                                    a[9] = i9
                                        for i10 in range(i9,11):
                                        a[10] = i10
                                        s = 0
                                        for i in j:
                                            if a[i[1]]-a[i[0]] = i[2]:
                                                s += i[3]
                                        if max < s:
                                            max = s

print(max)