結果
問題 |
No.684 Prefix Parenthesis
|
ユーザー |
![]() |
提出日時 | 2025-06-12 12:52:25 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 513 bytes |
コンパイル時間 | 515 ms |
コンパイル使用メモリ | 82,384 KB |
実行使用メモリ | 432,740 KB |
最終ジャッジ日時 | 2025-06-12 12:53:11 |
合計ジャッジ時間 | 3,966 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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)