結果

問題 No.684 Prefix Parenthesis
ユーザー mkawa2mkawa2
提出日時 2022-01-25 11:55:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,406 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 11,016 KB
実行使用メモリ 30,852 KB
最終ジャッジ日時 2023-08-22 04:34:26
合計ジャッジ時間 5,615 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,220 KB
testcase_01 AC 14 ms
8,112 KB
testcase_02 AC 14 ms
8,040 KB
testcase_03 AC 63 ms
14,264 KB
testcase_04 AC 140 ms
19,360 KB
testcase_05 AC 158 ms
25,552 KB
testcase_06 AC 63 ms
13,400 KB
testcase_07 AC 160 ms
19,636 KB
testcase_08 AC 33 ms
9,636 KB
testcase_09 AC 35 ms
10,272 KB
testcase_10 AC 212 ms
29,048 KB
testcase_11 AC 164 ms
21,632 KB
testcase_12 AC 36 ms
10,316 KB
testcase_13 AC 201 ms
30,836 KB
testcase_14 AC 200 ms
30,080 KB
testcase_15 AC 83 ms
15,912 KB
testcase_16 AC 197 ms
28,880 KB
testcase_17 AC 111 ms
19,824 KB
testcase_18 AC 16 ms
8,516 KB
testcase_19 AC 210 ms
26,484 KB
testcase_20 AC 216 ms
25,784 KB
testcase_21 AC 233 ms
26,008 KB
testcase_22 AC 200 ms
30,784 KB
testcase_23 AC 178 ms
30,852 KB
testcase_24 AC 189 ms
24,500 KB
testcase_25 AC 194 ms
23,760 KB
testcase_26 AC 14 ms
8,008 KB
testcase_27 AC 15 ms
8,116 KB
testcase_28 AC 14 ms
8,196 KB
testcase_29 AC 230 ms
26,424 KB
testcase_30 AC 212 ms
27,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = (1 << 63)-1
inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

n = II()
s = SI()
cs = [0]
cnt = cur = 0
for c in s:
    if c == "(":
        cs.append(cs[-1]+1)
        cur += 1
    else:
        cs.append(cs[-1]-1)
    cnt += cur

mn = [inf]
for s in cs[1:]:
    mn.append(min(mn[-1], s))
# pDB(cs)
# pDB(mn)

pos, neg = [], []
for s, m in zip(cs[1:], mn[1:]):
    if s >= 0:
        pos.append((s, m))
    else:
        neg.append((s, m))
pos.sort(key=lambda x: (-x[1], -x[0]))
neg.sort(key=lambda x: (x[1]-x[0], -x[0]))

# sm = [(s, m) for s, m in zip(cs[1:], mn[1:])]
# sm.sort(key=lambda x: (-x[0], x[1]))

now = 0
# loss = 0
for s, m in pos+neg:
    d = max(0, -now-m)
    now += s+d
    # pDB(s, m, now, loss)

ans = cnt-now
print(ans*2)
0