結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー nephrologistnephrologist
提出日時 2021-01-29 21:44:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 962 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 140,724 KB
最終ジャッジ日時 2023-09-09 15:06:09
合計ジャッジ時間 6,263 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,200 KB
testcase_01 AC 68 ms
71,452 KB
testcase_02 AC 497 ms
87,324 KB
testcase_03 AC 228 ms
80,380 KB
testcase_04 AC 285 ms
81,844 KB
testcase_05 AC 284 ms
140,564 KB
testcase_06 AC 286 ms
140,380 KB
testcase_07 AC 251 ms
140,240 KB
testcase_08 AC 278 ms
140,236 KB
testcase_09 AC 322 ms
140,504 KB
testcase_10 AC 320 ms
140,504 KB
testcase_11 AC 326 ms
140,464 KB
testcase_12 AC 322 ms
140,644 KB
testcase_13 AC 323 ms
140,524 KB
testcase_14 AC 329 ms
140,724 KB
testcase_15 AC 330 ms
140,384 KB
権限があれば一括ダウンロードができます

ソースコード

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