結果

問題 No.2423 Merge Stones
ユーザー Seed57_cashSeed57_cash
提出日時 2023-07-10 12:37:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,272 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 87,264 KB
実行使用メモリ 82,548 KB
最終ジャッジ日時 2023-10-10 01:10:25
合計ジャッジ時間 13,927 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,636 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 71 ms
71,416 KB
testcase_05 AC 72 ms
71,252 KB
testcase_06 AC 75 ms
71,576 KB
testcase_07 AC 72 ms
71,588 KB
testcase_08 AC 72 ms
71,376 KB
testcase_09 AC 70 ms
71,556 KB
testcase_10 AC 70 ms
71,400 KB
testcase_11 RE -
testcase_12 AC 97 ms
77,304 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 99 ms
77,532 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 AC 96 ms
77,396 KB
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 AC 94 ms
77,092 KB
testcase_50 AC 97 ms
77,228 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 AC 156 ms
78,552 KB
testcase_54 WA -
testcase_55 AC 151 ms
78,716 KB
testcase_56 AC 105 ms
78,088 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
testcase_70 RE -
testcase_71 RE -
testcase_72 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 最後にとる石について考える
# その色がどこからきたのかを考えると、その石は動かなかった扱いができる
# 出発点を左右に動かす
# 新しい石に出会ったときに、貪欲に獲得する。自分は合成可能なこれまでの石のうち、一番最近のものになる


n, k = map(int, input().split())
a_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))
sum_a = sum(a_list)

res_left = [0] * n
res_right = [0] * n

# iから出発(左端の想定)
for i in range(n):
	# 右向きに進める
	# 現時点の蓄積
	r = a_list[i]
	# 各時点でなれる色と一番最近出会った時点
	colors = [[n + 1] * 50 for _ in range(n)]
	colors[0][c_list[i] - 1] = 0
	for j in range(1, n):
		new_color = c_list[(i + j) % n] - 1
		# 全探索
		# 現時点でi+jと合成可能な色のうち、一番新しい時点で出会ったものを探す
		min_j = n + 1
		for c in range(max(new_color - k, 0), min(new_color + k, 50) + 1):
			min_j = min(colors[j - 1][c], min_j)
		
		# 無理な場合
		if min_j == n + 1:
			break
	
		# 見つかった場合
		# copy
		for c in range(50):
			colors[j][c] = colors[min_j][c]
		colors[j][new_color] = j
		
		res_left[(i + j) % n] = max(res_left[(i + j) % n], r)
		r += a_list[(i + j) % n]
		
	# 左向きに進める
	# 現時点の蓄積
	r = a_list[i]
	# 各時点でなれる色と一番最近出会った時点
	colors = [[n + 1] * 50 for _ in range(n)]
	colors[0][c_list[i] - 1] = 0
	for j in range(1, n):
		new_color = c_list[(i - j) % n] - 1
		# 全探索
		# 現時点でi-jと合成可能な色のうち、一番新しい時点で出会ったものを探す
		min_j = n + 1
		for c in range(max(new_color - k, 0), min(new_color + k, 50) + 1):
			min_j = min(colors[j - 1][c], min_j)
		
		# 無理な場合
		if min_j == n + 1:
			break
	
		# 見つかった場合
		# copy
		for c in range(50):
			colors[j][c] = colors[min_j][c]
		colors[j][new_color] = j
		
		res_right[(i - j) % n] = max(res_right[(i - j) % n], r)
		r += a_list[(i - j) % n]
	
# 1周していたら過剰になるので、全部の合計にする
res = [0] * n
for i in range(n):
	res[i] = min(res_left[i] + res_right[i] + a_list[i], sum_a)

print(max(res))
0