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 copy

def cnt(a, m, i):
    if a[i] < m:
        a[i] += 1
    else:
        a = cnt(a, m, i-1)
        a[i] = copy.deepcopy(a[i-1])
    return a

def check(a, ab):
    score = 0
    for i in range(q):
        ai, bi = ab[i][0], ab[i][1]
        if a[bi] - a[ai] == ab[i][2]:
            score += ab[i][3]
    return score
  
n, m, q = map(int,input().split())
ab = []
for i in range(q):
    ab.append(list(map(int,input().split())))

a = [1] * (n+1)
ans = 0
while a[0] == 1:
    x = check(a, ab)
    ans = max(ans, x)
    # print(a, ans)
    a = cnt(a, m, -1)
print(ans)

Submission status: Runtime Error

Solution code:
import copy

def cnt(a, m, i):
    if a[i] < m:
        a[i] += 1
    else:
        a = cnt(a, m, i-1)
        a[i] = copy.deepcopy(a[i-1])
    return a

def check(a, ab):
    score = 0
    for i in range(q):
        ai, bi = ab[i][0], ab[i][1]
        if a[bi] - a[ai] == ab[i][2]:
            score += ab[i][3]
    return score
  
n, m, q = map(int,input().split())
ab = []
for i in range(q):
    ab.append(list(map(int,input().split())))
ab = sorted(ab, key=lambda x:(x[3]), reverse=True)
# print(ab)
a = [1] * (n+1)
ans = 0
while a[0] == 1:
    x = check(a, ab)
    ans = max(ans, x)
    # print(a, ans)
    a = cnt(a, m, -1)
print(ans)

Submission status: Runtime Error

Solution code:
n,m,q = map(int,input().split())
arr = [list(map(int,input().split())) for x in range(q)]
ans = 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(i4,m+1):
        for i5 in range(i4,m+1):
          for i6 in range(i5,m+1):
            for i7 in range(6,m+1):
              for i8 in range(i7,m+1):
                for i9 in range(i8,m+1):
                  for i10 in range(i9,m+1):
                    tmp = 0
                    arrs = [0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
                    for a,b,c,d in arr:
                      if arrs[b] - arrs[a] == c:
                        tmp += d
                    ans = max(ans,tmp)
print(ans)

Submission status: Runtime Error

Solution code:
# abc165_c.py
# https://atcoder.jp/contests/abc165/tasks/abc165_c

# C - Many Requirements /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# 正整数 N, M , Q と、4 つの整数の組 ( ai , bi , ci , di ) Q組が与えられます。
# 以下の条件を満たす数列 Aを考えます。
#     Aは、長さ Nの正整数列である。
#     1≤A1≤A2≤⋯≤AN≤M
# この数列の得点を、以下のように定めます。
#     Abi−Aai=ciを満たすような i についての、 di の総和 (そのような i が存在しないときは 0)
# Aの得点の最大値を求めてください。

# 制約
#     入力は全て整数
#     2≤N≤10
#     1≤M≤10
#     1≤Q≤50
#     1≤ai<bi≤N( i=1,2,...,Q)
#     0≤ci≤M−1( i=1,2,...,Q)
#     (ai,bi,ci)≠(aj,bj,cj)( i≠jのとき)
#     1≤di≤105( i=1,2,...,Q)

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N M Q
# a1 b1 c1 d1
# :
# aQ bQ cQ dQ

# 出力
# Aの得点の最大値を出力せよ。

# 入力例 1
# 3 4 3
# 1 3 3 100
# 1 2 2 10
# 2 3 2 10

# 出力例 1
# 110

# A={1,3,4}のとき、この数列の得点は 110 となります。
# この条件の下では 110 より高い得点を持つ数列は存在しませんから、答えは 110です。

# 入力例 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

# 出力例 2
# 357500

# 入力例 3
# 10 10 1
# 1 10 9 1

# 出力例 3
# 1


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    # N = int(lines[0])
    N, M, Q = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    valueses = list()
    for i in range(Q):
        valueses.append(list(map(int, lines[i+1].split())))

    # ☆あとでちゃんと考えます...
    # e.g.1
    patterns = list()
    for x1 in range(1, 5):
        for x2 in range(1, 5):
            for x3 in range(1, 5):
                patterns.append((x1, x2, x3))
    ma = 0
    for pattern in patterns:
        flag = False
        su = 0
        for values in valueses:
            a, b, c, d = values
            if pattern[b-1] - pattern[a-1] == c:
                su += d
        if ma < su:
            ma = su

    return [ma]


# 引数を取得
def get_input_lines():
    line = input()
    N, M, Q = list(map(int, line.split()))
    lines = list()
    lines.append(line)
    for _ in range(Q):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['3 4 3', '1 3 3 100', '1 2 2 10', '2 3 2 10']
        lines_export = [110]
    if pattern == 2:
        lines_input = ['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']
        lines_export = [357500]
    if pattern == 3:
        lines_input = ['10 10 1', '1 10 9 1']
        lines_export = [1]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

def dfs(A):
    if len(A)==n:
        return score(A)
    ans = 0
    prev_last = A[-1] if len(A)>0 else 0
    for v in range(prev_last, m):
        A.append(v)
        ans = max(ans, dfs(A))
        A.pop()
    return ans

n, m, q = map(int, readline().split())
A,B,C,D = [0]*q, [0]*q, [0]*q, [0]*q
for i in range(q):
    a,b,C[i],D[i] = map(int, readline().split())
    A[i] = a-1
    B[i] = b-1

print(dfs([]))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

class Combination_with_repetition():
    def getPattern(self, arr :list, r :int) -> list:
        self._selected=[]
        self._all_pattern=[]
        
        self._make(arr,r)
        return self._all_pattern
        
    def _make(self, arr :list, r :int):
        if r == 0:
            self._all_pattern.append(self._selected[:])
            return 
        
        for i in range(len(arr)):
            self._selected.append( arr[i] )
            self._make(arr[i:],r-1)
            
            self._selected.pop()  # Backtracking

#-----
N,M,Q = list(map(int, input().rstrip().split()))
abcd_list = [list(map(int, input().rstrip().split())) for i in range(Q)]

#-----
comb_rep = Combination_with_repetition()
pattern = comb_rep.getPattern( list(range(1, M+1)) , M)


max_sum = 0

for pat in pattern:
    tmp_sum = 0
    
    for a,b,c,d in abcd_list:
        if ( pat[b-1] - pat[a-1] ) == c:
            tmp_sum += d
    
    max_sum = max(max_sum, tmp_sum)

print(max_sum)

Submission status: Runtime Error

Solution code:
N, M, Q = map(int, input().split())
a = [0 for _ in range(Q)]
b = [0 for _ in range(Q)]
c = [0 for _ in range(Q)]
d = [0 for _ in range(Q)]
for k in range(Q):
  a[k], b[k], c[k], d[k] = map(int, input().split())
A = [1 for _ in range(N)]

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

def next(A):
  k = 1
  while k < N+1:
    if A[-k] < M:
      A[-k] += 1
      return A
      break
    else:
      for j in range(1, k+1):
        A[-j] = A[-k-1] + 1
      k += 1
      
ans = max(ans, point([M for _ in range(N)]))
while A[0] < M:
  ans = max(ans, point(A))
  A = next(A)
print(ans)

Submission status: Runtime Error

Solution code:
N,M,Q = gets.split.map(&:to_i)
a = []
b = []
c = []
d = []
Q.times do |i|
  a[i], b[i], c[i], d[i] = gets.split.map(&:to_i)
  a[i] -= 1
  b[i] -= 1
end

def score(array,a,b,c,d)
  tmp = 0
  for ai,bi,ci,di in a.zip(b,c,d)
    tmp += di if array[bi] - array[ai] == ci
  end
  tmp
end

def dfs(array,a,b,c,d)
  if array.length == N
    return score(array,a,b,c,d)
  end
  result = 0
  array.length > 0 ? (prev_last = array[-1]) : (prev_last = 0)
  for v in prev_last..M
    array.push(v)
    result = [result, dfs(array,a,b,c,d)].max
    array.pop
  end
  result
end

puts dfs(Array.new,a,b,c,d)

Submission status: Runtime Error

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

t = [1]*(n+1)
ans = 0
while t[0] == 1:
    s = 0
    for i in range(q):
        if t[l[i][1]]-t[l[i][0]] == l[i][2]:
            s += l[i][3]
    ans = max(ans, s)
    k = n
    while t[k] == m and n >= 1:
        k -= 1
    t[k] += 1
    for i in range(k+1, n+1):
        t[i] = t[i-1]

print(ans)

Submission status: Runtime Error

Solution code:
#import itertools

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

abcd = [input() for _ in range(q)]

#x = itertools.product(range(1,m+1),repeat=n)
x = []
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):
        x.append([i,j,k,l])
tmp = 0
ans = 0
for xi in x:
  tmp = 0
  for i in abcd:
    a,b,c,d = map(int,i.split())
    if xi[b-1] - xi[a-1] == c:
      tmp += d
  ans = max(ans,tmp)

print(ans)

Submission status: Runtime Error

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

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

C=[]
score = 0
max = 0
for i in range(1,2**(M+1)):
    flg=[0,0,0,0,0,0,0,0,0,0]
    C.clear()
    for j in range(1,M+1):
        if ( i & (1 << (j-1)) ) != 0:
            C.append(j)

    if len(C) < N:
        continue
    score = 0
    for j in range(N):
        if C[A[j][1]-1] - C[A[j][0]-1] == A[j][2]:
            score += A[j][3]
    if max < score and score > 0:
        max = score

print(max)

Submission status: Runtime Error

Solution code:
# O(n^M) -> TLE
from itertools import product
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
arr = list(range(1, M+1))
ans = 0
for x in product(arr, repeat=N):
    cur = 0
    for y in ls:
        if x[y[1]-1] - x[y[0]-1] == y[2]:
            cur += y[3]
    ans = max(ans, cur)
print(ans)

# DFS
from collections import deque
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
arr = list(range(1, M+1))
ans = 0
for i in range(1, M+1):
    st = deque([])
    st.append((i, str(i)))
    while st:
        p, l = st.pop()
        if len(l) == N:
            cur = 0
            for y in ls:
                if int(l[y[1]-1]) - int(l[y[0]-1]) == y[2]:
                    cur += y[3]
            ans = max(ans, cur)
        else:
            for j in range(p, M+1):
                st.append((j, l + str(j)))
print(ans)

# 数列生成
from itertools import combinations
N, M, Q = map(int, input().split())
ls = []
for i in range(Q):
    ls.append([int(j) for j in input().split()])
V = [[] for i in range(N+1)]
for i in range(1, M+1):
    V[1].append([i])

for i in range(1, N):
    for v in V[i]:
        b = v[-1]
        for x in range(b, M+1):
            V[i+1].append(v + [x])
ans = 0
for x in V[-1]:
    cur = 0
    for z in ls:
        if x[z[1]-1] - x[z[0]-1] == z[2]:
            cur += z[3]
    ans = max(ans, cur)
print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <iomanip>
#include <vector>
#include <cstring>
#include <queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uld = unsigned long long;
using P = pair<int,int>;
const ll mod = 1e9+7;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
template<class T> inline bool chmax(T &a,T& b){if(a < b){a = b; return true;} else return false;}
template<class T> inline bool chmin(T &a,T& b){if(a > b){a = b; return true;} else return false;}
int n,m,q;
vector<int> a,b,c,d;
int score(vector<int> &A){
    int res = 0;
    for(int i=0;i<q;i++){
        if(A[b[i]] - A[a[i]] == c[i]) res += d[i];
    }
    return res;
}
int dfs(vector<int> &A){
    if(A.size() == n){
        return score(A);
    }
    int res = 0;
    int p = 0;
    if(!A.empty()) p = A.back();
    for(int v=p;v<m;v++){
        A.push_back(v);
        res = max(res,dfs(A));
        A.pop_back();
    }
    return res;
}
int main(){
    cin >> n >> m >> q;
    a.resize(q); b.resize(q); c.resize(q); d.resize(q);
    rep(i,q){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        --a[i]; --b[i];
    }
    vector<int> A;
    cout << dfs(A) << endl;
}

Submission status: Runtime Error

Solution code:
def resolve():
	from collections import deque
	N, M, Q = map(int, input().split())
	A = [list(map(int, input().split())) for i in range(Q)]
	
	def calc(x): # type(x) == str型
		ans = 0
		for i in A:
			a, b, c, d = i[0], i[1], i[2], i[3]
			if int(s[b-1]) - int(s[a-1]) == c:
				ans += d
		return ans
		
	q = deque([1])
	ans = 0
	while q:
		tmp = q.popleft()
		if len(tmp) == N:
			ans = max(ans, calc(tmp))
		else:
			for i in range(int(tmp[-1]), M+1):
				q.append(tmp+str(i))

	print(ans)
	
resolve()

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

Submission status: Runtime Error

Solution code:
import itertools
N, M, Q = map(int, input().split())
a = [int(input()) for _ in range(Q)]
b = [int(input()) for _ in range(Q)]
c = [int(input()) for _ in range(Q)]
d = [int(input()) for _ in range(Q)]
for i in range (Q):
     a[i], b[i], c[i], d[i] = map(int, input().split())
ans = 0
for A in combinations_with_replacement(range(1, M+1), N):
    tmp = 0
    for i in range(Q):
        if A[b[i]-1] - A[a[i]-1] == c[i]:
            tmp += d[i]
    ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n,m,q = map(int, input().split())
lis = [1 for i in range(n)]
ords = []
cost = []
dp = [-1 for i in range(200)]
ans = 0
for i in range(q):
    o = list(map(int, input().split()))
    ords.append(o[:n])
    cost.append(o[-1])
def checker(p, arr, ans):
    if(dp[p] == ans):
        return dp[p]
    for i in range(n-p-1, -1, -1):
        for c in range(1, m+1):
            arr[p] = c
            temp = 0
            ans = checker(p+1, arr, ans)
            for i in range(q):
                if(arr[ords[i][1]-1]-arr[ords[i][0]-1]==ords[i][2]):
                    temp+=cost[i]
            if(temp > ans):
                ans = temp
            dp[p] = ans
    return ans
print(checker(0, lis, 0))

Submission status: Runtime Error

Solution code:
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n,m,q = map(int, input().split())
lis = [1 for i in range(n)]
ords = []
cost = []
dp = [-1 for i in range(200)]
ans = 0
for i in range(q):
    o = list(map(int, input().split()))
    ords.append(o[:n])
    cost.append(o[-1])
def checker(p, arr, ans):
    if(dp[p] == ans):
        return dp[p]
    for i in range(n-p-1, -1, -1):
        for c in range(m):
            arr[p] = c
            temp = 0
            ans = checker(p+1, arr, ans)
            for i in range(q):
                if(arr[ords[i][1]-1]-arr[ords[i][0]-1]==ords[i][2]):
                    temp+=cost[i]
            if(temp > ans):
                ans = temp
            dp[p] = ans
    return ans
print(checker(0, lis, 0))

Submission status: Runtime Error

Solution code:
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n,m,q = map(int, input().split())
lis = [1 for i in range(n)]
ords = []
cost = []
dp = [200 for i in range(200)]
ans = 0
for i in range(q):
    o = list(map(int, input().split()))
    ords.append(o[:n])
    cost.append(o[-1])
def checker(p, arr, ans):
    if(dp[p]!= 200):
        return dp[p]
    for i in range(n-p-1, -1, -1):
        for c in range(m):
            arr[p] = c
            temp = 0
            ans = checker(p+1, arr, ans)
            for i in range(q):
                if(arr[ords[i][1]-1]-arr[ords[i][0]-1]==ords[i][2]):
                    temp+=cost[i]
            if(temp > ans):
                ans = temp
            dp[p] = ans
    return ans
print(checker(0, lis, 0))

Submission status: Runtime Error

Solution code:
N,M,Q=map(int,input().split())
L=[]
for i in range(Q):
  L.append(list(map(int,input().split())))
  L[i][0]-=1
  L[i][1]-=1

import itertools
L=list(range(1,M+1))
A=list(itertools.combinations_with_replacement(L,N))
ans=0
for i in A:
  tmp=0
  for a,b,c,d in L:
    if (i[b]-i[a])==c:
      tmp+=d
  ans=max(ans,tmp)
print(ans)

Submission status: Runtime Error

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

As = []

def make(A):
  global As, N, M
  if len(A) == N+1:
    As.append(A)
  elif:
    for i in range(A[len(A)-1], M+1):
      A.append(i)

make([1])

max = 0
for A in As:
  score = 0
  for req in reqs:
    if A[req[1]] - A[req[0]] == req[2]:
      score += req[3]
  max = max(score, max)

print(max)

Submission status: Runtime Error

Solution code:
import sys
def basen(x,n):
    if (int(x/n)):return basen(int(x/n),n)+str(x%n)
    return str(x%n)
def f(s,n):
    L=['0']*(n-len(s))+list(s)
    return list(map(lambda x:int(x)+1,L))
def main():
    N,M,Q=tuple(map(int,sys.stdin.readline().split()))
    R,res=[],0
    for _ in [0]*Q:
        a,b,c,d=tuple(map(int,sys.stdin.readline().split()))
        R.append((a-1,b-1,c,d))
    for i in range(N*M):
        A,y=f(basen(i,M),N),0
        for a,b,c,d in R:y=y+d if A[b]-A[a]==c else y
        if res<y:res=y
    print(res)
if __name__=='__main__':main()

Submission status: Runtime Error

Solution code:
import itertools
n, m, q = list(map(int,input().split()))
sets = [list(map(int,input().split())) for i in range(q)]
mx = 0
nums = list(range(1,m+1))
for h in itertools.combinations_with_replacement(nums,n):
    mxtemp = 0
    for j in range(q):
        a = sets[q][0]
        b = sets[q][1]
        if list(h)[b-1] - list(h)[a-1] == sets[q][2]:
            mxtemp += sets[q][3]
    if mxtemp > mx:
        mx = mxtemp
print(mx)

Submission status: Runtime Error

Solution code:
import itertools
n, m, q = list(map(input().split()))
sets = [list(map(int,input().split())) for i in range(q)]
mx = 0
nums = list(range(1,n+1))
for h in itertools.combinations_with_replacement(nums,n):
    mxtemp = 0
    for j in range(q):
        a = sets[q][0]
        b = sets[q][1]
        if list(h)[b-1] - list(h)[a-1] == sets[q][2]:
            mxtemp += sets[q][3]
    if mxtemp > mx:
        mx = mxtemp
print(mx)

Submission status: Runtime Error

Solution code:
import itertools
n, m, q = list(map(input().split()))
sets = [list(map(int,input().split())) for i in range(q)]
mx = 0
nums = list(range(1,11))
for h in itertools.combinations_with_replacement(nums,10):
    mxtemp = 0
    for j in range(q):
        a = sets[q][0]
        b = sets[q][1]
        if list(h)[b-1] - list(h)[a-1] == sets[q][2]:
            mxtemp += sets[q][3]
        if mxtemp > mx:
            mx = mxtemp
print(mx)

Submission status: Runtime Error

Solution code:
import intertools
N, M, Q = map(int, input().split())
abcd = []
ans = 0
for i in range(Q):
  abcd.append(input())
for X in intertools.conbinations_with_replacemant(range(1, M + 1), N):
  tmp = 0
  for z in abcd:
    a, b, c, d = map(int, z.split())
    if X[b - 1] - X[a - 1] == c:
      tmp += d
  ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
N, M, Q = map(int, input().split())
abcd = []
ans = 0
for i in range(Q):
  abcd.append(input())
for X in intertools.conbinations_with_replacemant(range(1, M + 1), N):
  tmp = 0
  for z in abcd:
    a, b, c, d = map(int, z.split())
    if X[b - 1] - X[a - 1] == c:
      tmp += d
  ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations
n,m,q=map(int,input().split())
aa=[]
bb=[]
cc=[]
dd=[]
for i in range(q):
  a,b,c,d=map(int,input().split())
  aa.append(a-1)
  bb.append(b-1)
  cc.append(c)
  dd.append(d)
sumd=[]
#print(list(combinations(range(1,m+1),n)))
for Alis in combinations(range(1,m+1),n):
  
  di=0
  
  for i in range(q):
    if cc[i]==(Alis[bb[i]]-Alis[aa[i]]):
      di+=dd[i]
      
  sumd.append(di)
print(max(sumd))

Submission status: Runtime Error

Solution code:
for i in range(q):
  a,b,c,d=map(int,input().split())
  aa.append(a-1)
  bb.append(b-1)
  cc.append(c)
  dd.append(d)
sumd=[]
#print(list(combinations(range(1,m+1),n)))
for Alis in combinations(range(1,m+1),n):
  
  di=0
  
  for i in range(q):
    if cc[i]==(Alis[bb[i]]-Alis[aa[i]]):
      di+=dd[i]
      
  sumd.append(di)
print(max(sumd))

Submission status: Runtime Error

Solution code:
N,M,Q = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(Q)]
def dfs(s):
    if len(s) == N:
        return[s]
    result = []
    if s == '':
        num = 0
    else:
        num = int(s[-1])
    for i in range(num,M+1):
        result.extend(dfs(s+str(i)))
    return result
ans = 0
S = dfs('')
for s in S:
    C = [int(i)+1 for i in s]
    B = 0
    for a,b,c,d in A:
        if C[b-1] - C[a-1] == c:
            B += d
    ans = max(ans,B)
print(ans)

Submission status: Runtime Error

Solution code:
def next_array(array):#辞書式に次の配列を与える関数
  s = -1
  for x in array[::-1]:
    if x < M:
      break
    else:
      s -= 1
    #Mより小さい最後の項を取得
  
  if s >= -N:
    array[s] += 1#その項を１増やす
    if s < -1:
      for i in range(s+1,0):#以降の項を全て同じ値に
        array[i] = array[s]
        
  return array
 
def score(array, rules):#点数計算
  scr = 0
  for rule in rules:
    if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):
      scr += rule[3]
      
  return scr

# input
N, M, Q = map(int, input().split())
rules = []
for i in range(Q):
  a, b, c, d = map(int, input().split())
  rules.append([a, b, c, d])
    
array　= [1] * N
max_scr = 0
 
while True:
  max_scr = max(max_scr, score(array,rules)) 
  if array[0] == M:#全探索完了
    break 
    array = next_array(array)#次の配列へ
    
print(max_scr)

Submission status: Runtime Error

Solution code:
def next_array(array):#辞書式に次の配列を与える関数
	s = -1
	for x in array[::-1]:
		if x < M:
			break
		else:
			s -= 1
    #Mより小さい最後の項を取得
    
    if s >= -N:
    	array[s] += 1#その項を１増やす
    	if s < -1:
      		for i in range(s+1,0):#以降の項を全て同じ値に
      			array[i] = array[s]
        
	return array
 
def score(array, rules):#点数計算
	scr = 0
    for rule in rules:
		if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):
    		scr += rule[3]
    return scr
 
 
# input
N, M, Q = map(int, input().split())
rules = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    rules.append([a, b, c, d])
    
array　= [1] * N
max_scr = 0
 
while True:
	max_scr = max(max_scr, score(array,rules)) 
	if array[0] == M:#全探索完了
		break 
	array = next_array(array)#次の配列へ
    
print(max_scr)

Submission status: Runtime Error

Solution code:
def next_array(array):#辞書式に次の配列を与える関数
	s = -1
	for x in array[::-1]:
		if x < M:
			break
		else:
			s -= 1
    #Mより小さい最後の項を取得
    
    if s >= -N:
    	array[s] += 1#その項を１増やす
    	if s < -1:
      		for i in range(s+1,0):#以降の項を全て同じ値に
      			array[i] = array[s]
        
	return array
 
def score(array, rules):#点数計算
	scr = 0
	for rule in rules:
    	if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):
        	scr += rule[3]
    return scr
 
 
# input
N, M, Q = map(int, input().split())
rules = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    rules.append([a, b, c, d])
    
array　= [1] * N
max_scr = 0
 
while True:
	max_scr = max(max_scr, score(array,rules)) 
	if array[0] == M:#全探索完了
		break 
	array = next_array(array)#次の配列へ
    
print(max_scr)

Submission status: Runtime Error

Solution code:
def next_array(array):#辞書式に次の配列を与える関数
	s = -1
	for x in array[::-1]:
    	if x < M:
        	break
        else:
        	s -= 1
    #Mより小さい最後の項を取得
    
    if s >= -N:
    	array[s] += 1#その項を１増やす
    	if s < -1:
      		for i in range(s+1,0):#以降の項を全て同じ値に
      			array[i] = array[s]
        
	return array
 
def score(array, rules):#点数計算
	scr = 0
	for rule in rules:
    	if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):
        	scr += rule[3]
    return scr
 
 
# input
N, M, Q = map(int, input().split())
rules = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    rules.append([a, b, c, d])
    
array　= [1] * N
max_scr = 0
 
while True:
	max_scr = max(max_scr, score(array,rules)) 
	if array[0] == M:#全探索完了
		break 
	array = next_array(array)#次の配列へ
    
print(max_scr)

Submission status: Runtime Error

Solution code:
def next_array(array):#辞書式に次の配列を与える関数
	s = -1
	for x in array[::-1]:
    	if x < M:
        	break
        else:
        	s -= 1
    #Mより小さい最後の項を取得
    
    if s >= -N:
      array[s] += 1#その項を１増やす
      if s < -1:
      	for i in range(s+1,0)#以降の項を全て同じ値に
      		array[i] = array[s]
        
	return array
 
def score(array, rules):#点数計算
	scr = 0
	for rule in rules:
    	if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):
        	scr += rule[3]
    return scr
 
 
# input
N, M, Q = map(int, input().split())
rules = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    rules.append([a, b, c, d])
    
array　= [1] * N
max_scr = 0
 
while True:
  max_scr = max(max_scr, score(array,rules)) 
  if array[0] == M:#全探索完了
    break 
  array = next_array(array)#次の配列へ

Submission status: Runtime Error

Solution code:
def next_array(array)#辞書式に次の配列を与える関数
	s = -1
	for x in array[::-1]:
    	if x < M:
        	break
        else:
        	s -= 1
    #Mより小さい最後の項を取得
    
    if s >= -N:#その項を１増やす
      array[s] += 1
      for i in range(s,0)#以降の項を全て同じ値に
      	array[i] = array[s]
        
	return array

def score(array, rules)#点数計算
	scr = 0
	for rule in rules:
    	if (array[rule[1]-1] - array[rule[0]-1] == rule[2]):
        	scr += rule[3]
    return scr


# input
N, M, Q = map(int, input().split())
rules = []
for i in range(Q):
    a, b, c, d = map(int, input().split())
    rules.append([a, b, c, d])
    
array　= [1] * N
max_scr = 0

while True:
  max_scr = max(max_scr, score(array,rules)) 
  if array[0] == M:#全探索完了
    break 
  array = next_array(array)#次の配列へ

Submission status: Runtime Error

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

q = []
def dp(arg):
    global N
    if len(arg) == N:
        q.append(arg)
    else:
        tmp = 0 if len(arg) == 0 else arg[-1]
        for i in range(arg[-1], M + 1):
            dp(arg + [i])

dp([])

ans = 0
for A_tmp in q:
    ans_tmp = 0
    for j, (aj, bj, cj, dj) in enumerate(abcd):
        if A_tmp[bj - 1] - A_tmp[aj - 1] == cj:
            ans_tmp += dj
    #
    ans = max(ans, ans_tmp)

print(ans)

Submission status: Runtime Error

Solution code:
import itertools

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

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

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    if a[0]<= a[1] and a[1] <= a[2]:
        A_list.append(a)

score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i][0], Q_list[i][1], Q_list[i][2], Q_list[i][3]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    score_list.append(score)

print(max(score_list))

Submission status: Runtime Error

Solution code:

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


s=[[1]]

#長さNまで
import copy
while True:
    n = []
    for s1 in s:
        for append in range(s1[-1], M+1):
            n1 = copy.copy(s1)
            n1.append(append)
            n.append(n1)
    if len(n[0]) > M:
        break
    s = n
#for s1 in s:
#    print(s1)
iidp = [list(map(int,input().split())) for q in range(Q)]

al = 0
for s1 in s:
    a = 0
    for i1, i2, d, p in iidp:
        if s1[i2-1] - s1[i1-1] == d:
            a += p
    al = max(a,al)

print(al)

Submission status: Runtime Error

Solution code:

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


s=[[1]]

#長さNまで
import copy
while True:
    n = []
    for s1 in s:
        for append in range(s1[-1], M+1):
            n1 = copy.copy(s1)
            n1.append(append)
            n.append(n1)
    if len(n[0]) > M:
        break
    s = n

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

al = []
for s1 in s:
    a = 0
    for i1, i2, d, p in iidp:
        if s1[i2-1] - s1[i1-1] == d:
            a += p
    al.append(a)

print(max(al))

Submission status: Runtime Error

Solution code:
from itertools import permutations

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[i] -= 1
    b[i] -= 1


arr = [0]*(n+m)
for j in range(m):
    arr[j] = 1

ans = 0

for vec in permutations(arr):
    zero_cnt = 0
    score = 0
    A = []
    for k in range(n+m):
        if vec[k] == 0:
            zero_cnt += 1
        else:
            A.append(zero_cnt)

    for l in range(q):
        if A[b[l]] - A[a[l]] == c[l]:
            score += d[l]

    ans = max(ans,score)


print(ans)