Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

N, K = [int(x) for x in input().split()]
S = input()

dict = defaultdict(list)

L_num = S.count('L')
R_num = S.count('R')

ans = S
skip = -1
for i in range(N):
    if i <= skip:
        continue
    if S[i] == 'R':
        count = 0
        for j in range(i + 1, N):
            if S[j] == 'R':
                count += 1
            else:
                break
        dict[count].append(i)
        skip = count + i
count = 0
for i in range(K + 10):
    max_key = max(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        max_key = max(dict.keys())
    target_index = dict[max_key].pop()
    if target_index == 0 or target_index == N - 1:
        continue
    ans = ans[0:target_index] + "L" * (max_key + 1) + ans[target_index + max_key + 1:]
    count += 1
    if count == K:
        break

score = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score += 1

print(score)




Fixed submission:
from collections import defaultdict

N, K = [int(x) for x in input().split()]
S = input()

ans = S
target_char = S[0]
replacing = False
for i in range(N):
    if S[i] != target_char:
        ans = ans[0:i] + target_char + ans[i + 1:]
        replacing = True
    else:
        if replacing:
            K -= 1
            replacing = False
    if K == 0:
        break

score = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score += 1

print(score)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

N, K = [int(x) for x in input().split()]
S = input()

dict = defaultdict(list)

L_num = S.count('L')
R_num = S.count('R')

ans = S
skip = -1
for i in range(N):
    if i <= skip:
        continue
    if S[i] == 'R':
        count = 0
        for j in range(i + 1, N):
            if S[j] == 'R':
                count += 1
            else:
                break
        dict[count].append(i)
        i += count
for i in range(K):
    max_key = max(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        if len(dict.keys()) == 0:
            break
        max_key = max(dict.keys())
    target_index = dict[max_key].pop()
    ans = ans[0:target_index] + "L" * (max_key + 1) + ans[target_index + max_key + 1:]

score1 = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score1 += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score1 += 1

dict.clear()
dict = defaultdict(list)
ans = S
skip = -1
for i in range(N):
    if i <= skip:
        continue
    if S[i] == 'L':
        count = 0
        for j in range(i + 1, N):
            if S[j] == 'L':
                count += 1
            else:
                break
        dict[count].append(i)
        skip = i + count
for i in range(K):
    max_key = max(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        if len(dict.keys()) == 0:
            break
        max_key = max(dict.keys())
    target_index = dict[max_key].pop()
    ans = ans[0:target_index] + "R" * (max_key + 1) + ans[target_index + max_key + 1:]

score2 = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score2 += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score2 += 1

ans = S
skip = -1
for i in range(N):
    if i <= skip:
        continue
    if S[i] == 'R':
        count = 0
        for j in range(i + 1, N):
            if S[j] == 'R':
                count += 1
            else:
                break
        dict[count].append(i)
        i += count
for i in range(K):
    max_key = min(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        if len(dict.keys()) == 0:
            break
        max_key = min(dict.keys())
    target_index = dict[max_key].pop()
    ans = ans[0:target_index] + "L" * (max_key + 1) + ans[target_index + max_key + 1:]

score3 = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score3 += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score3 += 1

dict.clear()
dict = defaultdict(list)
ans = S
skip = -1
for i in range(N):
    if i <= skip:
        continue
    if S[i] == 'L':
        count = 0
        for j in range(i + 1, N):
            if S[j] == 'L':
                count += 1
            else:
                break
        dict[count].append(i)
        skip = i + count
for i in range(K):
    max_key = min(dict.keys())
    if len(dict[max_key]) == 0:
        dict.pop(max_key)
        if len(dict.keys()) == 0:
            break
        max_key = min(dict.keys())
    target_index = dict[max_key].pop()
    ans = ans[0:target_index] + "R" * (max_key + 1) + ans[target_index + max_key + 1:]

score4 = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score4 += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score4 += 1


print(max(score1, score2, score3, score4))




Fixed submission:
from collections import defaultdict

N, K = [int(x) for x in input().split()]
S = input()

ans = S
target_char = S[0]
replacing = False
for i in range(N):
    if S[i] != target_char:
        ans = ans[0:i] + target_char + ans[i + 1:]
        replacing = True
    else:
        if replacing:
            K -= 1
            replacing = False
    if K == 0:
        break

score = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score += 1

print(score)