Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()

happiness = 0

li = []
lamp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        lamp += 1
    else:
        li.append(lamp)
        lamp = 1
li.append(lamp)

flag = False
num = 1
while k != 0:
    if len(li) == 2 and k > 0:
        print(n-1)
        quit()
    for i in range(1, len(li)-1):
        if li[i] == num:
            a = li[i-1] + li[i] + li[i+1]
            li[i-1:i+2] = [a]
            k -= 1
            flag = True
            break
        flag = False
    if flag == False:
        num += 1
    if len(li) == 2:
        break

for i in li:
    happiness += i-1

print(happiness)



Fixed submission:
n, k = map(int, input().split())
s = input()

s = list(s)

li = []
que = []
for i in range(n-1):
    if s[i] != s[i+1]:
        li.append(i)
li.append(n-1)
if len(li) == 1:
    print(n-1)
    exit()

for i in range(0,len(li)-1, 2):
    if k > 0:
        char = s[li[i]]
        a = s[li[i]+1:li[i+1]+1]
        s[li[i]+1:li[i+1]+1] = char * (li[i+1] - li[i])
    k -= 1

happiness = 0

for i in range(n):
    if i == 0:
        if s[i] == 'R' and s[i+1] == 'R':
            happiness +=1
    elif i == n-1:
        if s[i] == 'L' and s[i-1] == 'L':
            happiness += 1
    else:
        if s[i] == 'R' and s[i + 1] == 'R':
            happiness += 1
        elif s[i] == 'L' and s[i - 1] == 'L':
            happiness += 1

print(happiness)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()
turn = 0

happiness = 0


def exchange(num, loc):
    global s
    tmp = s[loc:loc+num+1]
    tmp = tmp[::-1]
    tmp = ['L' if x == 'R' else 'R' for x in tmp]
    s[loc:loc+num+1] = tmp

for i in range(n):
    if i == 0:
        if s[i] == 'R' and s[i+1] == 'R':
            happiness += 1
    elif i == n-1:
        if s[i] == 'L' and s[i-1] == 'L':
            happiness += 1
    else:
        if s[i] == 'R' and s[i+1] == 'R':
            happiness += 1
        elif s[i] == 'L' and s[i-1] == 'L':
            happiness += 1

for i in range(n-1):
    if s[i] == 'R' and s[i+1] == 'L' or s[i] == 'L' and s[i+1] == 'R':
        turn += 1


s = 'X' + s + 'X'
s = list(s)
lamp = -1
while k != 0:
    lamp += 1
    if turn == 1:
        print(n-1)
        exit()
    for j in range(2, n-lamp-1):
        if s[j] == 'R' and s[j - 1] != 'R' and s[j + lamp] == 'R' and s[j + lamp + 1] != 'R':
            happiness += 2
            exchange(lamp, j)
            k -= 1
            turn -= 2
            break
        elif s[j] == 'L' and s[j - 1] != 'L' and s[j + lamp] == 'L' and s[j + lamp + 1] != 'R':
            happiness += 2
            exchange(lamp, j)
            k -= 1
            turn -= 2
            break
    if turn == 0:
        break

print(happiness)


Fixed submission:
n, k = map(int, input().split())
s = input()

s = list(s)

li = []
que = []
for i in range(n-1):
    if s[i] != s[i+1]:
        li.append(i)
li.append(n-1)
if len(li) == 1:
    print(n-1)
    exit()

for i in range(0,len(li)-1, 2):
    if k > 0:
        char = s[li[i]]
        a = s[li[i]+1:li[i+1]+1]
        s[li[i]+1:li[i+1]+1] = char * (li[i+1] - li[i])
    k -= 1

happiness = 0

for i in range(n):
    if i == 0:
        if s[i] == 'R' and s[i+1] == 'R':
            happiness +=1
    elif i == n-1:
        if s[i] == 'L' and s[i-1] == 'L':
            happiness += 1
    else:
        if s[i] == 'R' and s[i + 1] == 'R':
            happiness += 1
        elif s[i] == 'L' and s[i - 1] == 'L':
            happiness += 1

print(happiness)