結果

問題 No.921 ずんだアロー
ユーザー lam6er
提出日時 2025-04-16 00:36:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 677 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 81,408 KB
実行使用メモリ 89,788 KB
最終ジャッジ日時 2025-04-16 00:40:50
合計ジャッジ時間 2,287 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

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

if n == 0:
    print(0)
    exit()

# Segment the array into blocks of consecutive same values
blocks = []
current_val = a[0]
count = 1
for val in a[1:]:
    if val == current_val:
        count += 1
    else:
        blocks.append(count)
        current_val = val
        count = 1
blocks.append(count)

# Dynamic programming to find the maximum sum of non-adjacent blocks
prev_not_take = 0
prev_take = 0

for length in blocks:
    current_not_take = max(prev_not_take, prev_take)
    current_take = prev_not_take + length
    prev_not_take, prev_take = current_not_take, current_take

print(max(prev_not_take, prev_take))
0