Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy
import re
n, k = [int(i) for i in sys.stdin.readline().split()]
s = input()
def _reverse(text):
    res = text[::-1]
    res = ["R" if i == "L" else "L" for i in res]
    res = "".join(res)
    return res
"""
for i in range(k):
    left = s.find("R")
    left = 0 if left == -1 else left
    right = s.rfind("R") + 1

    s = s[:left] + _reverse(s[left:right]) + s[right:]
    #s = copy.deepcopy(s_new)
cnt = sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)])
s = copy.deepcopy(s_origi)
"""
for i in range(k):
    left = s.find("LR") + 1
    left = 0 if left == -1 else left
    right = s.rfind("RL")
    right = n if right == -1 else right + 1

    s = s[:left] + _reverse(s[left:right]) + s[right:]
    #s = copy.deepcopy(s_new)
cnt = sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)])
print(cnt)

Fixed submission:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
flg = (s[-1] == "R") + (s[0] == "R")
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg > 0:
        print(cnt + (len(red_ls) - flg) * 2 + flg)
    else:
        print(cnt + len(red_ls) * 2)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy
import re
n, k = [int(i) for i in sys.stdin.readline().split()]
s = input()
s_origi = copy.deepcopy(s)
def _reverse(text):
    res = text[::-1]
    res = ["R" if i == "L" else "L" for i in res]
    res = "".join(res)
    return res
for i in range(k):
    left = s.find("R")
    left = 0 if left == -1 else left
    right = s.rfind("R") + 1

    s = s[:left] + _reverse(s[left:right]) + s[right:]
    #s = copy.deepcopy(s_new)
cnt = sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)])
s = copy.deepcopy(s_origi)
for i in range(k):
    left = s.find("LR") + 1
    left = 0 if left == -1 else left
    right = s.rfind("RL") + 1
    if left > right:
        break

    s = s[:left] + _reverse(s[left:right]) + s[right:]
    #s = copy.deepcopy(s_new)
cnt = max(cnt, sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)]))
print(cnt)

Fixed submission:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
flg = (s[-1] == "R") + (s[0] == "R")
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg > 0:
        print(cnt + (len(red_ls) - flg) * 2 + flg)
    else:
        print(cnt + len(red_ls) * 2)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy
import re
n, k = [int(i) for i in sys.stdin.readline().split()]
s = input()
s_origi = copy.deepcopy(s)
def _reverse(text):
    res = text[::-1]
    res = ["R" if i == "L" else "L" for i in res]
    res = "".join(res)
    return res
for i in range(k):
    left = s.find("R")
    left = 0 if left == -1 else left
    right = s.rfind("R") + 1

    s = s[:left] + _reverse(s[left:right]) + s[right:]
    #s = copy.deepcopy(s_new)
cnt = sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)])
s = copy.deepcopy(s_origi)
for i in range(k):
    left = s.find("LR") + 1
    left = 0 if left == -1 else left
    right = s.rfind("RL") + 1
    if left > right:
        break

    s = s[:left] + _reverse(s[left:right]) + s[right:]
    #s = copy.deepcopy(s_new)
cnt = max(cnt, sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)]))
print(cnt)

Fixed submission:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
flg = (s[-1] == "R") + (s[0] == "R")
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg > 0:
        print(cnt + (len(red_ls) - flg) * 2 + flg)
    else:
        print(cnt + len(red_ls) * 2)