結果

問題 No.3185 Three Abs
ユーザー norioc
提出日時 2025-06-21 03:57:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 881 ms / 2,000 ms
コード長 1,604 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 124,092 KB
最終ジャッジ日時 2025-06-21 03:57:39
合計ジャッジ時間 24,068 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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 get_value(to: E, fm_v, v):
    if to in (E.LN, E.MN, E.RN):
        return fm_v - v

    return fm_v + v


def op(to: E, to_v, fm: E, fm_v, v):
    return max(to_v, get_value(to, fm_v, 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)
0