結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー 👑 SPD_9X2
提出日時 2025-05-17 00:37:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 1,167 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 93,400 KB
最終ジャッジ日時 2025-05-17 00:38:13
合計ジャッジ時間 5,921 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #


import sys

N,K = map(int,input().split())
S = input()

ans = "Yes"

h = 0
for c in S:
    if c == "(":
        h += 1
    else:
        h -= 1

    if h < 0:
        ans = "No"

if h != 0:
    ans = "No"

if ans == "No":
    print ("No")
    sys.exit()



stk = []

ok = set()

for i in range(len(S)):

    if S[i] == "(":
        stk.append( i )

    elif S[i] == ")":
        stk.pop()
        if len(stk) > 0 and S[i+1] == "(":
            ok.add(stk[-1])

ans = []
num = 0

for i in range(len(S)):

    ans.append(S[i])
    if i == len(S)-1:
        continue

    if S[i] == "(" and S[i+1] == ")":
        ans.append("1+1")
        num += 2
    elif S[i] == "(" and S[i+1] == "(" and (i not in ok):
        ans.append("1+")
        num += 1
    elif S[i] == ")" and S[i+1] == "(":
        ans.append("+")

while num < K:
    ans.append("+1")
    num += 1

if num > K:
    print ("No")
else:

    nh = 0
    ANS = "".join(ans)
    ans = "No"
    for c in ANS:
        if c == "(":
            nh += 1
        elif c == ")":
            nh -= 1
        elif c == "+" and nh == 0:
            ans = "Yes"

    print (ans)
    if ans == "Yes":
        print (ANS)
0