185 words
1 minutes
[NYPC] 1차 5번 게임

그냥 구현 문제
5번까지는 그래도 빨리 풀긴 하였다.
def play_game(s, k, n):
    s = list(s)
    zero, one = k, k
    # zc, oc = 0, 0
    # for i in range(n):
    #     if s[i] =='0': zc+=1
    #     else: oc += 1
    removed = set()
    zero_left, one_left = 0, 0
    for i in range(n):
        if s[i] == '0' and zero:
            zero -= 1
            zero_left = i
        elif s[i] == '1' and one:
            one -= 1
            one_left = i
    # print(len(removed), zero, one, removed)
    for i in range(n - 1, -1, -1):
        if zero and s[i] == '1':
            removed.add(i)
            zero -= 1
        if one and s[i] == '0':
            removed.add(i)
            one -= 1
        if not one and not zero: break
    result = []
    for i in range(min(zero_left, one_left)+1, n):
        if (s[i] == '1' and one_left >= i) or (s[i] == '0' and zero_left >= i) or i in removed: continue
        result.append(s[i])
    # print(len(result))
    return ''.join(result)
    
    
n, k = map(int, input().split())
s = input()
result = play_game(s, k, n)
if len(result) == 0: result='0'
print(result)

