結果

問題 No.3185 Three Abs
ユーザー 蜜蜂
提出日時 2025-06-14 02:02:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 590 bytes
コンパイル時間 496 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 107,692 KB
最終ジャッジ日時 2025-06-14 02:02:49
合計ジャッジ時間 9,594 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline

def solve():
  n = int(input())
  a = list(map(int, input().split()))
  inf = int(1e18)
  dp = [-inf] * 6
  dp[0] = dp[1] = 0
  for i in range(n):
    ndp = [-inf] * 6
    for j in range(3):
      if j < 2:
        ndp[2 * j + 2] = max(dp[2 * j], dp[2 * j + 1]) + a[i]
        ndp[2 * j + 3] = max(dp[2 * j], dp[2 * j + 1]) - a[i]
      ndp[2 * j] = max(ndp[2 * j], dp[2 * j] + a[i])
      ndp[2 * j + 1] = max(ndp[2 * j + 1], dp[2 * j + 1] - a[i])
    dp, ndp = ndp, dp
  print(max(dp[4], dp[5]))

t = int(input())
for i in range(t):
  solve()
0