Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
s = input().rstrip()

def split(s):
    new = [s[0]]
    for i in range(n-1):
        if s[i] != s[i+1]:
            new.append(s[i+1])
        else:
            new[-1] += s[i+1]
    return new

def transform(s, k):
    for _ in range(k):
        s = split(s)
        if len(s) == 1:
            s = ''.join(s)
            break
        idx = 1
        s[idx] = s[idx].replace('L', 'X').replace('R', 'L').replace('X', 'R')
        s = ''.join(s)
    return s


s = transform(s, k)


def count(s):
    cnt = 0
    for i in range(n):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(count(s))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
s = input().rstrip()

def count(s):
    cnt = 0
    for i in range(n):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(min(count(s)+2*k, n-1))

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()


def split(s):
    last = ''
    new = []
    for x in s:
        if last != x:
            new.append(x)
        else:
            new[-1] += x
        last = x
    return new


def transform(s, k):

    for _ in range(k):
        s = split(s)
        ls = list(map(len, s))
        idx = ls.index(min(ls))
        if idx == 0:
            idx = ls[1:].index(min(ls[1:]))+1

        # print(s, idx, ls)
        s[idx] = s[idx].replace('L', 'X').replace('R', 'L').replace('X', 'R')

        if ''.join(s) == s:
            break
        else:
            s = ''.join(s)
    # print(s)
    return s


s = transform(s, k)


def count(s):
    cnt = 0
    for i in range(len(s)):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(count(s))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
s = input().rstrip()

def count(s):
    cnt = 0
    for i in range(n):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(min(count(s)+2*k, n-1))

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()


def split(s):
    last = ''
    new = []
    for x in s:
        if last != x:
            new.append(x)
        else:
            new[-1] += x
        last = x
    return new


def transform(s, k):

    for _ in range(k):
        s = split(s)
        ls = list(map(len, s))
        idx = ls.index(min(ls))
        if idx == 0:
            idx = ls[1:].index(min(ls[1:]))+1

        # print(s, idx, ls)
        s[idx] = s[idx].replace('L', 'X').replace('R', 'L').replace('X', 'R')
        s = ''.join(s)
    # print(s)
    return s


s = transform(s, k)


def count(s):
    cnt = 0
    for i in range(len(s)):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(count(s))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
s = input().rstrip()

def count(s):
    cnt = 0
    for i in range(n):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(min(count(s)+2*k, n-1))