結果

問題 No.684 Prefix Parenthesis
ユーザー mkawa2mkawa2
提出日時 2022-01-25 11:13:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,422 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 105,252 KB
最終ジャッジ日時 2023-08-22 03:47:21
合計ジャッジ時間 6,504 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,296 KB
testcase_01 AC 73 ms
71,136 KB
testcase_02 AC 75 ms
71,016 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 178 ms
90,052 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 179 ms
92,100 KB
testcase_12 AC 102 ms
77,736 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 177 ms
96,680 KB
testcase_17 AC 144 ms
89,752 KB
testcase_18 AC 84 ms
76,352 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 134 ms
105,252 KB
testcase_23 AC 132 ms
105,244 KB
testcase_24 AC 170 ms
96,540 KB
testcase_25 AC 165 ms
91,584 KB
testcase_26 AC 72 ms
71,152 KB
testcase_27 AC 74 ms
71,304 KB
testcase_28 AC 74 ms
71,172 KB
testcase_29 WA -
testcase_30 AC 226 ms
91,748 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[1]))

# 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