結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー hato336
提出日時 2025-05-16 22:33:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,187 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 81,972 KB
実行使用メモリ 131,552 KB
最終ジャッジ日時 2025-05-17 00:33:15
合計ジャッジ時間 7,067 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k = map(int,input().split())
s = list(input().rstrip())
x = 0
for i in s:
    x += 1 if i == '(' else -1
    if x < 0:
        print('No')
        exit()
if x != 0:
    exit(print('No'))
st = []
ans = 0
a = [[] for i in range(n)]
t = set()
for i in range(n):
    x = s[i]
    if st and st[-1][0] == '(' and x == ')':
        j = st[-1][1]
        if j + 1 == i:
            ans += 2
            a[j] = list("(1+1)")
        elif j + 3 == i:
            ans += 1
            a[j] = list("(")
            a[i] = list("+1)")
        else:
            ans += 0
            a[j] = list("(")
            a[i] = list(")")
        t.add((j,i))
        st.pop()
    else:
        st.append((x,i))
for j,i in t:
    if (j+1,i-1) in t:
        if a[i] == list(")"):
            a[i] = list("+1)")
            ans += 1
p = 0
if (0,n-1) in t:
    ans += 1
    p = 1

if ans <= k:
    print('Yes')
    

    b = []
    for i in a:
        if i and i[0] == '(' and len(b) > 0 and b[-1] == ')':
            b.append('+')
        b.extend(i)
    if p:
        b.extend(list("+1"))
    #ans+=1
    while ans < k:
        ans += 1
        b.extend(list("+1"))
    print(''.join(b))
else:
    print('No')
0