結果
| 問題 | 
                            No.684 Prefix Parenthesis
                             | 
                    
| コンテスト | |
| ユーザー | 
                             lam6er
                         | 
                    
| 提出日時 | 2025-04-09 21:06:04 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 578 bytes | 
| コンパイル時間 | 174 ms | 
| コンパイル使用メモリ | 82,356 KB | 
| 実行使用メモリ | 432,812 KB | 
| 最終ジャッジ日時 | 2025-04-09 21:07:59 | 
| 合計ジャッジ時間 | 3,822 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 3 TLE * 1 -- * 27 | 
ソースコード
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)
            
            
            
        
            
lam6er