Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))

now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append(cum)
        now = popped
        cum = 1
count.append(cum)

if len(count) // 2 <= K:
    print(N-1)
else:
    m = 0
    for i in range(len(count)-2*K):
        if i == 0:
            c = sum(count[0:2*K+1]) - 1
            d = sum([j-1 for j in count[2*K+1:]])
        elif i == len(count) - 2*K + 1:
            c = sum(count[-(2*K+1):])
            d = sum([j-1 for j in count[0:-(2*K+1)]])
        else:
            c = sum(count[i:2*K+1+i]) - 1
            d1 = sum([j-1 for j in count[0:i]])
            d2 = sum([j-1 for j in count[2*K+1+i:]])
            d = d1 + d2
        m = max(m,c+d)
    print(m)

Fixed submission:
from collections import deque
import numpy as np
N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))

now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append(cum)
        now = popped
        cum = 1
count.append(cum)
points = [i-1 for i in count]
cs1 = np.array(count).cumsum()
cs2 = np.array(points).cumsum()

if len(count) // 2 <= K:
    print(N-1)
else:
    m = 0
    for i in range(len(count)-2*K):
        if i == 0:
            c = cs1[2*K] - 1
            d = cs2[-1] - cs2[2*K]
        elif i == len(count) - 2*K + 1:
            c = cs1[-1] - cs1[-2*K-2] - 1
            d = cs2[-2*K-2]
        else:
            c = cs1[2*K+i] - cs1[i-1] - 1
            d = cs2[i-1] + (cs2[-1]-cs2[2*K+i])
        m = max(m,c+d)
    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))

now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append([now,cum])
        now = popped
        cum = 1
count.append([now,cum])

a = [x[0] for x in count]
if min(a.count('L'),a.count('R')) <= K:
    print(N-1)
else:
    b = [x[1] for x in count]
    m = 0
    for i in range(len(b)-2*K):
        if i == 0:
            c = sum(b[0:2*K+1]) - 1
            d = sum([j-1 for j in b[2*K+1:]])
        elif i == len(b) - 2*K + 1:
            c = sum(b[-(2*K+1):])
            d = sum([j-1 for j in b[0:-(2*K+1)]])
        else:
            c = sum(b[i:2*K+1+i]) - 1
            d1 = sum([j-1 for j in b[0:i]])
            d2 = sum([j-1 for j in b[2*K+1+i:]])
            d = d1 + d2
        m = max(m,c+d)
    print(m)

Fixed submission:
from collections import deque
import numpy as np
N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))

now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append(cum)
        now = popped
        cum = 1
count.append(cum)
points = [i-1 for i in count]
cs1 = np.array(count).cumsum()
cs2 = np.array(points).cumsum()

if len(count) // 2 <= K:
    print(N-1)
else:
    m = 0
    for i in range(len(count)-2*K):
        if i == 0:
            c = cs1[2*K] - 1
            d = cs2[-1] - cs2[2*K]
        elif i == len(count) - 2*K + 1:
            c = cs1[-1] - cs1[-2*K-2] - 1
            d = cs2[-2*K-2]
        else:
            c = cs1[2*K+i] - cs1[i-1] - 1
            d = cs2[i-1] + (cs2[-1]-cs2[2*K+i])
        m = max(m,c+d)
    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))

now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append([now,cum])
        now = popped
        cum = 1
count.append([now,cum])

a = [x[0] for x in count]
if min(a.count('L'),a.count('R')) <= K:
    print(N-1)
else:
    b = [x[1] for x in count]
    m = 0
    for i in range(len(b)-2*K):
        if i == 0:
            c = sum(b[0:2*K+1]) - 1
            d = sum([j-1 for j in b[2*K+1:]])
        elif i == len(b) - 2*K + 1:
            c = sum(b[-(2*K+1):])
            d = sum([j-1 for j in b[0:-(2*K+1)]])
        else:
            c = sum(b[i:2*K+1+i]) - 1
            d1 = sum([j-1 for j in b[0:i]])
            d2 = sum([j-1 for j in b[2*K+1+i:]])
            d = d1 + d2
        m = max(m,c+d)
    print(m)

Fixed submission:
from collections import deque
import numpy as np
N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))

now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append(cum)
        now = popped
        cum = 1
count.append(cum)
points = [i-1 for i in count]
cs1 = np.array(count).cumsum()
cs2 = np.array(points).cumsum()

if len(count) // 2 <= K:
    print(N-1)
else:
    m = 0
    for i in range(len(count)-2*K):
        if i == 0:
            c = cs1[2*K] - 1
            d = cs2[-1] - cs2[2*K]
        elif i == len(count) - 2*K + 1:
            c = cs1[-1] - cs1[-2*K-2] - 1
            d = cs2[-2*K-2]
        else:
            c = cs1[2*K+i] - cs1[i-1] - 1
            d = cs2[i-1] + (cs2[-1]-cs2[2*K+i])
        m = max(m,c+d)
    print(m)