結果
| 問題 | 
                            No.684 Prefix Parenthesis
                             | 
                    
| コンテスト | |
| ユーザー | 
                             gew1fw
                         | 
                    
| 提出日時 | 2025-06-12 18:03:21 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 513 bytes | 
| コンパイル時間 | 381 ms | 
| コンパイル使用メモリ | 82,048 KB | 
| 実行使用メモリ | 430,592 KB | 
| 最終ジャッジ日時 | 2025-06-12 18:03:26 | 
| 合計ジャッジ時間 | 4,125 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 3 TLE * 1 -- * 27 | 
ソースコード
n = int(input())
s = input().strip()
prefixes = []
for i in range(n):
    substr = s[:i+1]
    a = substr.count('(')
    b = substr.count(')')
    prefixes.append((a - b, substr))
# Sort the prefixes in descending order of (a - b)
prefixes.sort(reverse=True, key=lambda x: x[0])
balance = 0
result = 0
for _, substr in prefixes:
    for c in substr:
        if c == '(':
            balance += 1
        else:
            if balance > 0:
                result += 2
                balance -= 1
print(result)
            
            
            
        
            
gew1fw