結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー norioc
提出日時 2025-05-16 22:19:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,137 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 82,560 KB
最終ジャッジ日時 2025-05-17 00:30:30
合計ジャッジ時間 5,067 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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


def is_balanced(s: str) -> bool:
    d = 0
    for c in S:
        if c == '(':
            d += 1
        elif c == ')':
            if d == 0:
                return False
            d -= 1

    return d == 0


def solve(k: int, s: str):
    if not is_balanced(s):
        print('No')
        return

    st = []
    for c in S:
        if c == '(':
            if len(st) == 0:
                st.append(c)
            elif st[-1] == ')':
                st.append('+')
                st.append(c)
            elif st[-1] == '(':
                st.append('1+')
                st.append(c)
            else:
                assert False
        elif c == ')':
            if len(st) == 0:
                return False
            elif st[-1] == '(':
                st.append('1+1')
                st.append(c)
            elif st[-1] == ')':
                st.append(c)
            else:
                assert False

    t = ''.join(st)
    cnt = t.count('1')
    if cnt <= K:
        print('Yes')
        print(t + ('+1' * (K - cnt)))
    else:
        print('No')


solve(K, S)
0