結果

問題 No.684 Prefix Parenthesis
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-16 09:28:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 556 bytes
コンパイル時間 745 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 88,576 KB
最終ジャッジ日時 2024-05-05 12:05:47
合計ジャッジ時間 4,523 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,584 KB
testcase_01 AC 36 ms
51,968 KB
testcase_02 AC 36 ms
51,968 KB
testcase_03 AC 87 ms
78,464 KB
testcase_04 AC 136 ms
84,224 KB
testcase_05 AC 139 ms
84,864 KB
testcase_06 AC 93 ms
78,336 KB
testcase_07 WA -
testcase_08 AC 62 ms
70,656 KB
testcase_09 WA -
testcase_10 AC 174 ms
88,064 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 69 ms
76,800 KB
testcase_14 AC 67 ms
76,160 KB
testcase_15 AC 56 ms
67,456 KB
testcase_16 AC 48 ms
64,768 KB
testcase_17 AC 45 ms
62,336 KB
testcase_18 AC 40 ms
57,728 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 178 ms
87,680 KB
testcase_22 AC 46 ms
58,368 KB
testcase_23 AC 47 ms
57,984 KB
testcase_24 AC 48 ms
64,768 KB
testcase_25 AC 123 ms
86,016 KB
testcase_26 AC 40 ms
51,968 KB
testcase_27 AC 40 ms
51,584 KB
testcase_28 AC 40 ms
51,712 KB
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
S = input()

R = 0  # (((
LR = []  # ))((
L = 0  # ))

ans = 0

add = 0
cntL = 0
cntR = 0
for s in S:
    if s == ")":
        if cntR:
            cntR -= 1
            add += 2
        else:
            cntL += 1
    elif s == "(":
        cntR += 1

    ans += add

    if cntL and cntR:
        LR.append((cntL, cntR))
    elif cntL:
        L += cntL
    elif cntR:
        R += cntR


LR.sort(key=lambda x: (x[0], -x[1]))
for l, r in LR:
    d = min(R, l)
    ans += 2 * d
    R -= d
    R += r
d = min(R, L)
ans += 2 * d

print(ans)
0