結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー nephrologist
提出日時 2021-01-29 21:44:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 473 ms / 2,000 ms
コード長 962 bytes
コンパイル時間 325 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 139,652 KB
最終ジャッジ日時 2024-06-27 07:57:44
合計ジャッジ時間 5,136 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline
t = int(input())


def check(a, b, c):
    if a == b or b == c or c == a:
        return False
    if b > c and b > a:
        return True
    if b < c and b < a:
        return True
    return False


def calc(A):
    dp = [[0] * (3) for _ in range(n - 1)]
    for i in range(n - 2):
        ni = i + 1
        a, b, c = A[i], A[i + 1], A[i + 2]
        for j in range(3):
            if check(a, b, c):
                nj = max(0, j - 1)
                dp[ni][nj] = max(dp[ni][nj], dp[i][j])
                if j == 0:
                    nj = 2
                    dp[ni][nj] = max(dp[ni][nj], dp[i][j] + a)
            else:
                nj = max(0, j - 1)
                dp[ni][nj] = max(dp[ni][nj], dp[i][j])
    return max(dp[-1])


for _ in range(t):
    n = int(input())
    A = list(map(int, input().split()))
    A2 = A[1:] + A[:1]
    A3 = A[2:] + A[:2]
    print(max(calc(A), calc(A2), calc(A3)))

0