結果
問題 |
No.505 カードの数式2
|
ユーザー |
![]() |
提出日時 | 2025-06-21 16:06:55 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,702 bytes |
コンパイル時間 | 548 ms |
コンパイル使用メモリ | 82,224 KB |
実行使用メモリ | 63,560 KB |
最終ジャッジ日時 | 2025-06-21 16:06:59 |
合計ジャッジ時間 | 3,202 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 WA * 12 |
ソースコード
from collections.abc import Iterable from enum import IntEnum, auto from functools import cache class E(IntEnum): S = 0 ADD = auto() SUB = auto() MUL = auto() DIV = auto() def nexts(self): match self: case E.S: return [E.ADD] case E.ADD: return [E.ADD, E.SUB, E.MUL, E.DIV] case E.SUB: return [E.ADD, E.SUB, E.MUL, E.DIV] case E.MUL: return [E.ADD, E.SUB, E.MUL, E.DIV] case E.DIV: return [E.ADD, E.SUB, E.MUL, E.DIV] assert False @staticmethod @cache def states(): res = [] for fm in E: for to in fm.nexts(): res.append((fm, to)) return res def state_dp(xs: Iterable, op, e, init: dict): dp = [e] * len(E) for k, v in init.items(): dp[k] = v for x in xs: pp = [e] * len(E) dp, pp = pp, dp for fm, to in E.states(): if not is_valid(to, pp[fm], x): continue dp[to] = op(to, dp[to], fm, pp[fm], x) return dp def is_valid(to: E, fm_v, v) -> bool: if fm_v == -INF: return False if to == E.DIV and v == 0: return False return True def get_value(to: E, fm_v, v): match to: case E.ADD: return fm_v + v case E.SUB: return fm_v - v case E.MUL: return fm_v * v case E.DIV: return fm_v // v assert False def op(to: E, to_v, fm: E, fm_v, v): return max(to_v, get_value(to, fm_v, v)) INF = 1 << 60 N = int(input()) A = list(map(int, input().split())) dp = state_dp(A, op, -INF, {E.S: 0}) ans = max(dp[x] for x in [E.ADD, E.SUB, E.MUL, E.DIV]) print(ans)