結果

問題 No.684 Prefix Parenthesis
ユーザー mkawa2mkawa2
提出日時 2022-01-25 11:16:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,422 bytes
コンパイル時間 492 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 30,880 KB
最終ジャッジ日時 2023-08-22 03:50:04
合計ジャッジ時間 5,985 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,040 KB
testcase_01 AC 17 ms
8,124 KB
testcase_02 AC 16 ms
8,228 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 169 ms
19,764 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 170 ms
21,880 KB
testcase_12 AC 42 ms
10,224 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 210 ms
28,904 KB
testcase_17 AC 126 ms
19,964 KB
testcase_18 AC 19 ms
8,652 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 218 ms
30,808 KB
testcase_23 AC 211 ms
30,856 KB
testcase_24 AC 214 ms
24,608 KB
testcase_25 AC 216 ms
23,192 KB
testcase_26 AC 16 ms
8,068 KB
testcase_27 AC 17 ms
8,064 KB
testcase_28 AC 16 ms
8,164 KB
testcase_29 WA -
testcase_30 AC 245 ms
27,176 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:
    loss += max(0, -now-m)
    now += s
    # pDB(s, m, now, loss)

ans = max(0, min(cnt, n*(n+1)//2-cnt-loss))
print(ans*2)
0