結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,628 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 72 ms
71,288 KB
testcase_05 AC 69 ms
71,328 KB
testcase_06 AC 74 ms
71,632 KB
testcase_07 AC 74 ms
71,448 KB
testcase_08 AC 71 ms
71,488 KB
testcase_09 AC 70 ms
71,416 KB
testcase_10 AC 72 ms
71,384 KB
testcase_11 AC 208 ms
79,192 KB
testcase_12 AC 98 ms
77,168 KB
testcase_13 AC 110 ms
78,420 KB
testcase_14 AC 104 ms
77,944 KB
testcase_15 AC 100 ms
77,320 KB
testcase_16 AC 103 ms
77,916 KB
testcase_17 AC 106 ms
78,196 KB
testcase_18 AC 109 ms
77,848 KB
testcase_19 AC 97 ms
77,424 KB
testcase_20 AC 101 ms
77,988 KB
testcase_21 AC 112 ms
77,644 KB
testcase_22 AC 109 ms
78,492 KB
testcase_23 AC 100 ms
77,700 KB
testcase_24 AC 101 ms
77,752 KB
testcase_25 AC 100 ms
77,732 KB
testcase_26 AC 98 ms
77,456 KB
testcase_27 AC 104 ms
77,772 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 105 ms
78,232 KB
testcase_31 AC 104 ms
77,952 KB
testcase_32 AC 114 ms
78,348 KB
testcase_33 AC 102 ms
77,768 KB
testcase_34 WA -
testcase_35 AC 107 ms
78,068 KB
testcase_36 AC 105 ms
77,904 KB
testcase_37 AC 104 ms
77,756 KB
testcase_38 AC 99 ms
77,304 KB
testcase_39 WA -
testcase_40 AC 105 ms
77,736 KB
testcase_41 AC 104 ms
77,916 KB
testcase_42 AC 121 ms
78,460 KB
testcase_43 AC 100 ms
77,164 KB
testcase_44 AC 114 ms
78,308 KB
testcase_45 AC 100 ms
77,276 KB
testcase_46 AC 107 ms
77,712 KB
testcase_47 AC 98 ms
77,252 KB
testcase_48 AC 97 ms
77,340 KB
testcase_49 AC 100 ms
77,212 KB
testcase_50 AC 100 ms
77,264 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 AC 166 ms
78,380 KB
testcase_54 WA -
testcase_55 AC 162 ms
78,812 KB
testcase_56 AC 118 ms
77,912 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 196 ms
79,156 KB
testcase_62 AC 201 ms
78,892 KB
testcase_63 AC 198 ms
79,364 KB
testcase_64 AC 206 ms
79,036 KB
testcase_65 AC 203 ms
79,056 KB
testcase_66 AC 203 ms
78,920 KB
testcase_67 AC 207 ms
79,296 KB
testcase_68 AC 215 ms
79,124 KB
testcase_69 AC 236 ms
79,136 KB
testcase_70 AC 199 ms
79,592 KB
testcase_71 AC 170 ms
78,616 KB
testcase_72 AC 169 ms
79,072 KB
権限があれば一括ダウンロードができます

ソースコード

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 + 1, 50)):
			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 + 1, 50)):
			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