結果

問題 No.921 ずんだアロー
ユーザー gew1fw
提出日時 2025-06-12 20:04:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 772 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 89,684 KB
最終ジャッジ日時 2025-06-12 20:10:04
合計ジャッジ時間 2,582 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
a = list(map(int, input().split()))

# Create blocks of consecutive same values
blocks = []
if not a:
    print(0)
    exit()
current_val = a[0]
current_len = 1
for num in a[1:]:
    if num == current_val:
        current_len += 1
    else:
        blocks.append(current_len)
        current_val = num
        current_len = 1
blocks.append(current_len)

# Dynamic programming to find maximum sum of non-adjacent blocks
if len(blocks) == 0:
    print(0)
else:
    prev0 = 0  # previous state: not taking the block
    prev1 = blocks[0]  # previous state: taking the block
    for i in range(1, len(blocks)):
        new_prev0 = max(prev0, prev1)
        new_prev1 = prev0 + blocks[i]
        prev0, prev1 = new_prev0, new_prev1
    print(max(prev0, prev1))
0