Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

largest = 0
largest_left_index = -1

sum_list = list()

count = 0
for i in range(N):
    if S[i] == "L":
        if count <= 0:
            count = count - 1
        else:
            sum_list.append(count)
            count = -1
    elif S[i] == "R":
        if count >= 0:
            count = count + 1
        else:
            sum_list.append(-count)
            count = 1
else:
    sum_list.append(count) if count > 0 else sum_list.append(-count)

length = len(sum_list)
current_max = 0
current_max_index = 0
for j in range(length):
    current_sum = sum_list[j]
    if j < length - 1:
        current_sum = current_sum + sum_list[j+1]
    if j < length - 2:
        current_sum = current_sum + sum_list[j+2]

    if current_sum > current_max:
        current_max = current_sum
        current_max_index = j

if current_max_index < length - 2:
    sum_list.pop(current_max_index + 2)
if current_max_index < length - 1:
    sum_list.pop(current_max_index + 1)
sum_list.pop(current_max_index)
sum_list.insert(current_max_index, current_max)

for i in range(K-1):
    left = sum_list[current_max_index]
    if current_max_index > 1:
        left = left + sum_list[current_max_index - 1]
    if current_max_index > 2:
        left = left + sum_list[current_max_index - 2]

    right = sum_list[current_max_index]
    if current_max_index < len(sum_list) - 1:
        right = right + sum_list[current_max_index + 1]
    if current_max_index < len(sum_list) - 2:
        right = right + sum_list[current_max_index + 2]

    if left >= right:
        sum_list.pop(current_max_index)
        sum_list.insert(current_max_index, left)
        if current_max_index > 1:
            sum_list.pop(current_max_index - 1)
        if current_max_index > 2:
            sum_list.pop(current_max_index - 2)
    else:
        length = len(sum_list)
        if current_max_index < length - 2:
            sum_list.pop(current_max_index + 2)
        if current_max_index < length - 1:
            sum_list.pop(current_max_index + 1)
        sum_list.pop(current_max_index)
        sum_list.insert(current_max_index, right)

res = 0
for i in range(len(sum_list)):
    res = res + sum_list[i] - 1

print(res)

Fixed submission:
N, K = map(int, input().split())
S = input()

largest = 0
largest_left_index = -1

sum_list = list()

count = 0
for i in range(N):
    if S[i] == "L":
        if count <= 0:
            count = count - 1
        else:
            sum_list.append(count)
            count = -1
    elif S[i] == "R":
        if count >= 0:
            count = count + 1
        else:
            sum_list.append(-count)
            count = 1
else:
    sum_list.append(count) if count > 0 else sum_list.append(-count)

happines = 0
for i in range(len(sum_list)):
    happines = happines + sum_list[i] - 1

length = len(sum_list)

for i in range(K):
    if length > 2:
        happines = happines + 2
        length = length - 2
    elif length == 2:
        happines = happines + 1
        length = length - 1

print(happines)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = input()

largest = 0
largest_left_index = -1

sum_list = list()

count = 0
for i in range(N):
    if S[i] == "L":
        if count <= 0:
            count = count - 1
        else:
            sum_list.append(count)
            count = -1
    elif S[i] == "R":
        if count >= 0:
            count = count + 1
        else:
            sum_list.append(-count)
            count = 1
else:
    sum_list.append(count) if count > 0 else sum_list.append(-count)

for i in range(K):
    length = len(sum_list)
    current_max = 0
    current_max_index = 0
    for j in range(length):
        current_sum = sum_list[j]
        if j < length - 1:
            current_sum = current_sum + sum_list[j+1]
        if j < length - 2:
            current_sum = current_sum + sum_list[j+2]

        if current_sum > current_max:
            current_max = current_sum
            current_max_index = j

    if current_max_index < length - 2:
        sum_list.pop(current_max_index + 2)
    if current_max_index < length - 1:
        sum_list.pop(current_max_index + 1)
    sum_list.pop(current_max_index)
    sum_list.insert(current_max_index, current_max)

res = 0
for i in range(len(sum_list)):
    res = res + sum_list[i] - 1

print(res)

Fixed submission:
N, K = map(int, input().split())
S = input()

largest = 0
largest_left_index = -1

sum_list = list()

count = 0
for i in range(N):
    if S[i] == "L":
        if count <= 0:
            count = count - 1
        else:
            sum_list.append(count)
            count = -1
    elif S[i] == "R":
        if count >= 0:
            count = count + 1
        else:
            sum_list.append(-count)
            count = 1
else:
    sum_list.append(count) if count > 0 else sum_list.append(-count)

happines = 0
for i in range(len(sum_list)):
    happines = happines + sum_list[i] - 1

length = len(sum_list)

for i in range(K):
    if length > 2:
        happines = happines + 2
        length = length - 2
    elif length == 2:
        happines = happines + 1
        length = length - 1

print(happines)