結果

問題 No.3017 交互浴
ユーザー u_kun
提出日時 2025-02-03 12:23:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 2,834 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 107,128 KB
最終ジャッジ日時 2025-02-03 12:24:09
合計ジャッジ時間 19,738 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int, input().split()))

ans = 0  # 水色のマスの個数

LIFO = []
LIFO.append(["B", A[0]])
ans += A[0]

print(ans)

for i in range(1, N):
    new_L = A[i]
    if i % 2 == 0:
        new_color = "B"
    else:
        new_color = "G"
    
    del_cnt = 0 # 末端から削除したマスの個数
    
    # 末端からnew_L個を削除する (そもそもnew_L個無い可能性にも注意!)
    while True:
        
        if not LIFO:
            break
        
        end_color, end_L = LIFO.pop()
        
        if del_cnt + end_L == new_L:
            del_cnt += end_L
            
            if end_color == "B":
                ans -= end_L
            
            break
        
        elif del_cnt + end_L < new_L:
            del_cnt += end_L
            
            if end_color == "B":
                ans -= end_L
        
        elif del_cnt + end_L > new_L:  # 消しすぎ
            LIFO.append([end_color, end_L - (new_L - del_cnt)])
            
            if end_color == "B":
                ans -= (new_L - del_cnt)
            
            del_cnt += (new_L - del_cnt)
            break
            
        
    
    # 新しいものを末端に追加
    if LIFO:
        if LIFO[-1][0] == new_color:
            LIFO[-1][1] += new_L
        else:
            LIFO.append([new_color, new_L])
    else:
        LIFO.append([new_color, new_L])
    
    if new_color == "B":
        ans += new_L
    
    print(ans)
0