結果
| 問題 |
No.3185 Three Abs
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2025-06-21 04:44:11 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 831 ms / 2,000 ms |
| コード長 | 1,533 bytes |
| コンパイル時間 | 582 ms |
| コンパイル使用メモリ | 82,228 KB |
| 実行使用メモリ | 123,980 KB |
| 最終ジャッジ日時 | 2025-06-21 04:44:35 |
| 合計ジャッジ時間 | 23,133 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 30 |
ソースコード
from collections.abc import Iterable
from enum import IntEnum, auto
from functools import cache
class E(IntEnum):
S = 0
LP = auto()
LN = auto()
MP = auto()
MN = auto()
RP = auto()
RN = auto()
def nexts(self):
match self:
case E.S: return [E.LP, E.LN]
case E.LP: return [E.LP, E.MP, E.MN]
case E.LN: return [E.LN, E.MP, E.MN]
case E.MP: return [E.MP, E.RP, E.RN]
case E.MN: return [E.MN, E.RP, E.RN]
case E.RP: return [E.RP]
case E.RN: return [E.RN]
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:
return True
def op(to: E, to_v, fm: E, fm_v, v):
sgn = -1 if to in (E.LN, E.MN, E.RN) else 1
return max(to_v, fm_v + sgn * v)
def solve():
N = int(input())
A = list(map(int, input().split()))
dp = state_dp(A, op, -INF, {E.S: 0})
return max(dp[E.RP], dp[E.RN])
INF = 1 << 60
T = int(input())
for _ in range(T):
ans = solve()
print(ans)
norioc