Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]
r = 0
for i in range(K):
    l = r
    f = True
    prev = S[0]
    for j in range(l, N):
        current = S[j]
        if current != prev:
            l = j
            f = False
            break
        prev = current
    if f:
        break

    r = N - 1
    prev = S[l]
    for j in range(l+1, N):
        current = S[j]
        if current != prev:
            r = j - 1
            break
        prev = current
    left = S[:l]
    target = S[l:r+1]
    right = S[r+1:]
    target.reverse
    target = [-x for x in target]
    S = left + target + right

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)

Fixed submission:
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]
cnt = 0
rule = S[0]
f = False
for i in range(1, N):
    if S[i] != rule:
        f = True
        S[i] *= -1
    else:
        if f:
            f = False
            cnt += 1
    if cnt >= K:
        break

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]

for i in range(K):
    l = -1
    prev = S[0]
    for j in range(1, N):
        current = S[j]
        if current != prev:
            l = j
            break
        prev = current
    if l == -1:
        break

    r = N - 1
    prev = S[l]
    for j in range(l+1, N):
        current = S[j]
        if current != prev:
            r = j - 1
            break
        prev = current
    left = S[:l]
    target = S[l:r+1]
    right = S[r+1:]
    target.reverse
    target = [-x for x in target]
    S = left + target + right

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)

Fixed submission:
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]
cnt = 0
rule = S[0]
f = False
for i in range(1, N):
    if S[i] != rule:
        f = True
        S[i] *= -1
    else:
        if f:
            f = False
            cnt += 1
    if cnt >= K:
        break

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]

for i in range(K):
    l = -1
    for j in range(1, N):
        if S[j] != S[j-1]:
            l = j
            break
    if l == -1:
        break

    r = N - 1
    for j in range(l, N-1):
        if S[j] != S[j+1]:
            r = j
            break
    
    temp = [-S[l + i] for i in range(r - l + 1)]
    temp.reverse()
    for i in range(r - l + 1):
        S[l + i] = temp[i]

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)

Fixed submission:
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]
cnt = 0
rule = S[0]
f = False
for i in range(1, N):
    if S[i] != rule:
        f = True
        S[i] *= -1
    else:
        if f:
            f = False
            cnt += 1
    if cnt >= K:
        break

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)