Problem p02918 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappop, heappush

def rle(lst):
    res = []
    pre, cnt = lst[0], 1
    for i in range(1, len(lst)):
        if lst[i] == pre:
            cnt += 1
        else:
            res.append((pre, cnt))
            pre = lst[i]
            cnt = 1
    res.append((pre, cnt))
    return res

def solve():
    n, k = MI()
    S = list(input())

    R = rle(S)

    score = 0
    for r in R:
        score += r[1] - 1

    l = len(R)
    for _ in range(k):
        if l > 2:
            score += 2
            l -= 2
        elif l == 2:
            score += 1
            l -= 1
        else:
            break
    print(score)






if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product


def solve():
    n, k = MI()

    S = list(input())

    A = []
    # print(S)
    cnt = 0
    pre = ''
    for s in S:
        if pre == s:
            cnt += 1
        else:
            if cnt:
                A.append((pre, cnt))
            pre = s
            cnt = 1
    if cnt:
        A.append((pre, cnt))
    # print('A', A)

    target = 'L'
    rev = 'R'
    ans = 0
    for _ in range(2):
        B = A.copy()
        T = []
        for idx, (b, cnt) in enumerate(B):
            if b == target:
                l = 0
                r = 0
                if idx - 1 >= 0:
                    l = B[idx-1][1]
                if idx + 1 < len(B):
                    r = B[idx+1][1]

                T.append((idx, l + cnt + r, cnt))

        T = sorted(T, key=lambda x: x[1], reverse=True)
        # print('T', T)

        q = deque(T)
        for _ in range(k):
            idx, _, cnt = q.popleft()
            B[idx] = (rev, cnt)

        res = 0
        sm = 0
        pre = ''
        for (b, cnt) in B:
            if pre == b:
                sm += cnt
            else:
                if sm > 1:
                    res += sm - 1
                sm = cnt
                pre = b
        if sm > 1:
            res += sm - 1

        # print('B', B)
        # print(res)
        ans = max(res, ans)
        target = 'R'
        rev = 'L'
    print(ans)


if __name__ == '__main__':
    solve()