結果

問題 No.684 Prefix Parenthesis
コンテスト
ユーザー lam6er
提出日時 2025-04-09 21:06:04
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 578 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 235 ms
コンパイル使用メモリ 95,076 KB
実行使用メモリ 440,656 KB
最終ジャッジ日時 2026-07-08 15:08:25
合計ジャッジ時間 4,972 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 TLE * 1 -- * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

n = int(input())
s = input().strip()

prefixes = []
for i in range(n):
    prefix = s[:i+1]
    l = prefix.count('(')
    r = len(prefix) - l
    balance = l - r
    prefixes.append((balance, prefix))

# Sort the prefixes in descending order of balance
prefixes.sort(reverse=True, key=lambda x: x[0])

current_left = 0
total_pairs = 0

for balance, prefix in prefixes:
    for c in prefix:
        if c == '(':
            current_left += 1
        else:
            if current_left > 0:
                current_left -= 1
                total_pairs += 1

print(total_pairs * 2)
0