結果
| 問題 | No.258 回転寿司(2) |
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2026-02-24 23:20:43 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 128 ms / 2,000 ms |
| コード長 | 1,545 bytes |
| 記録 | |
| コンパイル時間 | 340 ms |
| コンパイル使用メモリ | 78,168 KB |
| 実行使用メモリ | 75,380 KB |
| 最終ジャッジ日時 | 2026-02-24 23:21:08 |
| 合計ジャッジ時間 | 23,412 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 67 |
ソースコード
from enum import Enum, auto
from collections import defaultdict
from itertools import product
def to_list(cons_cell: tuple) -> list:
if len(cons_cell) == 0: return []
res = []
x = cons_cell
while x is not None:
a, x = x
res.append(a)
return res
class E(Enum):
EAT = auto()
NOT_EAT = auto()
INF = 1 << 62
N = int(input())
V = list(map(int, input().split()))
def is_valid(prev_state, curr_state) -> bool:
if prev_state == E.EAT and curr_state == E.EAT:
return False
return True
def recur_dp(p, dp):
if p == N:
ma = max(dp.values())
for s in E:
if dp[s] == ma:
return s, ma, None
assert False
pp = defaultdict(lambda: -INF)
dp, pp = pp, dp
for fm, to in product(E, repeat=2):
if pp[fm] == -INF: continue
if not is_valid(fm, to): continue
val = pp[fm] + V[p] if to == E.EAT else pp[fm]
dp[to] = max(dp[to], val)
next_state, max_v, path = recur_dp(p+1, dp)
# 復元
for fm in E:
if not is_valid(fm, next_state): continue
match next_state:
case E.EAT:
if pp[fm] + V[p] == max_v:
return fm, pp[fm], (p, path)
case E.NOT_EAT:
if pp[fm] == max_v:
return fm, max_v, path
assert False
dp = defaultdict(lambda: -INF)
dp[E.NOT_EAT] = 0
_, _, path = recur_dp(0, dp)
inds = to_list(path)
print(sum([V[p] for p in inds]))
print(*[p+1 for p in inds])
norioc