結果

問題 No.2423 Merge Stones
ユーザー miya145592
提出日時 2023-08-12 15:41:12
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,349 bytes
コンパイル時間 397 ms
コンパイル使用メモリ 82,380 KB
実行使用メモリ 449,540 KB
最終ジャッジ日時 2024-11-20 03:52:00
合計ジャッジ時間 290,281 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 15 TLE * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')

def dfs(a, c):
    if (a, c) in memo:
        return memo[a, c]
    n = len(a)
    if n==1:
        memo[a, c] = max(a)
        return memo[a, c]
    ret = max(a)
    for i in range(n):
        if abs(c[i]-c[(i+1)%n])<=K:
            na = []
            nc = []
            idx = -1
            for j in range(n):
                if j==i:
                    idx = len(nc)
                    na.append(a[i]+a[(i+1)%n])
                    nc.append(c[i])
                elif j==(i+1)%n:
                    pass
                else:
                    na.append(a[j])
                    nc.append(c[j])
            if (tuple(na), tuple(nc)) not in memo:
                memo[(tuple(na), tuple(nc))] = dfs(tuple(na), tuple(nc))
            ret1 = memo[(tuple(na), tuple(nc))]
            ret = max(ret, ret1)

            nc[idx] = c[(i+1)%n]
            if (tuple(na), tuple(nc)) not in memo:
                memo[(tuple(na), tuple(nc))] = dfs(tuple(na), tuple(nc))
            ret2 = memo[(tuple(na), tuple(nc))]
            ret = max(ret, ret2)
    memo[(a, c)] = ret
    return memo[(a, c)]

N, K = map(int, input().split())
A = tuple(map(int, input().split()))
C = tuple(map(int, input().split()))

memo = dict()
ans = dfs(A, C)
print(ans)
0