結果

問題 No.684 Prefix Parenthesis
ユーザー gew1fw
提出日時 2025-06-12 21:37:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,126 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 95,072 KB
最終ジャッジ日時 2025-06-12 21:41:02
合計ジャッジ時間 4,158 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    S = input[1] if len(input) > 1 else ''
    
    pre_left = [0] * (N + 1)
    pre_right = [0] * (N + 1)
    
    for i in range(1, N+1):
        c = S[i-1]
        pre_left[i] = pre_left[i-1] + (1 if c == '(' else 0)
        pre_right[i] = pre_right[i-1] + (1 if c == ')' else 0)
    
    blocks = []
    for i in range(1, N+1):
        a_left = pre_left[i]
        a_right = pre_right[i]
        net = a_left - a_right
        blocks.append((-net, a_left, a_right))
    
    blocks.sort()
    blocks = [(a_left, a_right) for (net, a_left, a_right) in blocks]
    
    c_left = 0
    max_match = 0
    current_match = 0
    
    for (a_left, a_right) in blocks:
        m = min(c_left, a_right)
        current_match += m * 2
        c_left -= m
        a_right -= m
        
        excess_right = a_right
        c_left = max(c_left - excess_right, 0)
        
        c_left += a_left
        
        if current_match > max_match:
            max_match = current_match
    
    print(max_match)

if __name__ == '__main__':
    main()
0