Problem:
 Score : 400 points    Problem Statement There are N people standing in a queue from west to east.  Given is a string S of length N representing the directions of the people.
The i -th person from the west is facing west if the i -th character of S is L , and east if that character of S is R .  A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.  You can perform the following operation any number of times between 0 and K (inclusive):  Operation: Choose integers l and r such that 1 \leq l \leq r \leq N , and rotate by 180 degrees the part of the queue: the l -th, (l+1) -th, ... , r -th persons. That is, for each i = 0, 1, ..., r-l , the (l + i) -th person from the west will stand the (r - i) -th from the west after the operation, facing east if he/she is facing west now, and vice versa.  What is the maximum possible number of happy people you can have?      Constraints  N is an integer satisfying 1 \leq N \leq 10^5 .  K is an integer satisfying 1 \leq K \leq 10^5 .  |S| = N  Each character of S is L or R .         Input Input is given from Standard Input in the following format:  N  K  S       Output Print the maximum possible number of happy people after at most K operations.        Sample Input 1 6 1
LRLRRL      Sample Output 1 3  If we choose (l, r) = (2, 5) , we have LLLRLL , where the 2 -nd, 3 -rd, and 6 -th persons from the west are happy.       Sample Input 2 13 3
LRRLRLRRLRLLR      Sample Output 2 9       Sample Input 3 10 1
LLLLLRRRRR      Sample Output 3 9       Sample Input 4 9 2
RRRLRLRLL      Sample Output 4 7

Solution (Python):
n,k = map(int,input().split())
s = list(input())
def ranl(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append(cnt)
            cnt = 1
            ini = lst[i]
    ans.append(cnt)
    return ans
s = ranl(s)
if len(s)//2 <= k:
    print(n-1)
else:
    cnt = 0
    cnt += sum(s[0:2*k+1]) - 1
    for i in range(2*k+1, len(s)):
        cnt += s[i]-1
    print(cnt)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

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()

Solution (Python):
import sys
import numpy as np
input = lambda: sys.stdin.readline().rstrip()
INF = 10**9 + 1


def solve():
    N, K = map(int, input().split())
    S = np.array(list(input()), dtype='str')

    if N == 1:
        print(0)
        exit()

    ri = INF
    kc = 0
    fs = S[0]
    if fs == 'R':
        nfs = 'L'
    else:
        nfs = 'R'

    for i in range(N):
        if S[i] == nfs:
            ri = min(ri, i)
        elif S[i] == fs and ri != INF:
            S[ri:i] = fs
            ri = INF
            kc += 1
            if kc == K:
                break
    else:
        if ri != INF and S[-1] == nfs:
            S[ri:N] = fs

    

    happy = 0
    for i in range(N - 1):
        if S[i] == S[i + 1]:
            happy += 1

    print(happy)


if __name__ == '__main__':
    solve()

Solution (Python):

import sys
input = sys.stdin.readline









N,K = map(int,input().split())
S = input().rstrip()


cnt = 0
for i in range(N-1):
    if S[i]==S[i+1]:
        cnt += 1


cnt += 2*K


print(min(N-1,cnt))

Solution (Python):
N,K=map(int, input().split())
S=input()
comp=[]
tmp=""

for i in range(N):
  if tmp=="":
    tmp=S[i]
    cnt=1
  else:
    if S[i]==tmp:
      cnt +=1
      
  if S[i]!=tmp:
    comp.append([tmp,cnt])
    tmp=S[i]
    cnt=1
  if i==N-1:
    comp.append([tmp,cnt])
  
ans=0  
for run in comp:
  ans += run[1]-1
ans += K*2
ans = min(ans,N-1)
print(ans)

Solution (Python):
(N,K) = map(int,input().split())
S = input()
dan = 1

for i in range(N-1):
    if S[i] != S[i+1]:
        dan += 1

if 2*K >= (dan-1):
    print(N-1)
else:
    print(N-dan+2*K)

Solution (Python):
n,k,s=open(0).read().split()
n,k=int(n),int(k)
d=0
for i in range(1,n):
  if not s[i]==s[i-1]:
    d+=1
print(n-1-max(d-2*k,0))

Solution (Python):
n , k = map(int,input().split())
s = input()
cou = 0
for i in range(1,n):
    if s[i-1] != s[i]:
        cou += 1
print(n-1-max(0,cou-2*k))

Solution (Python):
"""
1度の操作で最大+2できる
隣り合う箇所はN-1
LないしRが連続している箇所をグループとして見ると、求める数は
  N-1-隣り合うグループが異なっている場所の個数
= N-1-(グループの個数-1)
"""
import sys
sys.setrecursionlimit(10**6)
n, k = map(int, input().split())
s = input()

score = 0
for i in range(n-1):
    if s[i] == s[i+1]: score += 1

ans = min(score+2*k, n-1)
print(ans)

Solution (Python):
n,k = map(int, raw_input().split())
score,d,cd = 0,None,0
s = raw_input()
for i,l in enumerate(s):
	if d != l:
		cd +=1
		d = l
	if i and s[i-1] == s[i]:  score += 1


if cd > 2:
	t = min(k,((cd - 2)+1)/2)
	k -= t
	score += 2 * t 
	cd -= 2 * t
if cd == 2 and k:
	score +=1
	cd -= 1
print score

Solution (Python):
n,k = map(int, raw_input().split())
s = raw_input()
cd = 0
d = None
for l in s:
	if d != l:
		cd +=1
		d = l
score = 0
for u,v in zip(s,s[1:]):
	if u == v:
		score +=1

if cd > 2:
	t = min(k,((cd - 2)+1)/2)
	k -= t
	score += 2 * t 
	cd -= 2 * t
if cd == 2 and k:
	score +=1
	cd -= 1
print score

Solution (Python):
n,k = map(int, raw_input().split())
s = raw_input()
cd = 0
d = None
for l in s:
	if d != l:
		cd +=1
		d = l
score = 0
for u,v in zip(s,s[1:]):
	if u == v:
		score +=1

while(k and cd > 1):

	if cd > 2:
		score += 2 
		cd -= 2
	elif cd == 2:
		score +=1
		cd -= 1
	k -=1
print score

Solution (Python):
N, K = map(int, input().split())
S = list(input())

diff = []
ans = 0
for i in range(N-1):
  if S[i] != S[i+1]:
    diff += [i]
  else:
    ans += 1

print(min(N-1, ans+2*min(K, len(diff))))

Solution (Python):
n, k = map(int, input().split())
s = [x for x in input()]

start = s[0]
current = 1
prev = None
utility = 0
while k != 0 and current < n:
  if s[current] != start:
    if not prev:
      prev = current
    else:
      utility += 1
  elif prev:
    if current == n:
      utility += 1
    else:
      utility += 2
    k = k-1
    prev = None
  else:
    utility += 1
  current += 1

if prev:
  if current == n and k > 0:
    utility +=  1
  if start == 'L':
    prev = 'R'
  else:
    prev = 'L'
else:
  if start == 'R':
    prev = 'R'
  else:
    prev = 'L'
while current < n:
  if s[current] == prev:
    utility += 1
  prev = s[current]
  current += 1
print(utility)

Solution (Python):
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, K = rl()
S = rs()

import itertools
g = list(itertools.groupby(S))

print(N-max(len(g)-1-2*K, 0)-1)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N,K = IL()
S = S()

count = 0
nokori = 0
prev = None
for s in S:
  if prev != s:
    nokori += 1
  else:
    count += 1
  prev = s
  
print(count+min(nokori-1,K*2))

Solution (Python):
n, k = map(int, input().split())
s = input()
r = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        r += 1
print(min(r + 2 * k, n - 1))

Solution (Python):
n,k=map(int,input().split())
s=input()
un=0
for i in range(1,len(s)):
  if s[i-1]!=s[i]:
    un+=1
print((n-1)-max(0,un-2*k))

Solution (Python):
N, K = map(int, input().split())
S = input()

ans = 0
for i in range(1, N):
    if S[i] == S[i-1]:
        ans += 1

ans = min(N-1, ans + 2*K)

print(ans)

Solution (Python):

import sys
from itertools import groupby


def runLengthEncode(S: str):
    grouped = groupby(S)
    res = []
    for k, v in grouped:
        res.append((k, str(len(list(v)))))
    return res

def runLengthdecode(L: "list[tuple]"):
    res = ""
    for c, n in L:
        res += c * int(n)
    return res


def main():
    N, K = map(int, input().split())
    S = input()
    compressed = runLengthEncode(S)

    
    for k in range(K):
        i = 2 * k + 1   
        if i >= len(compressed):
            break
        compressed[i] = ('L', compressed[i][1]) if compressed[i][0] == "R" else ('R', compressed[i][1])

    reCompressed = runLengthEncode(runLengthdecode(compressed))
    ans = N - len(reCompressed)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, k = map(int, input().split())
    s = input()

    かわいそうな人たち = s.count('LR') + s.count('RL')
    t = min(かわいそうな人たち, k)
    u = n - (かわいそうな人たち + 1) + 2 * t
    print(min(n - 1, u))


if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
s=input()
from itertools import groupby as gb
var=[len(list(b)) for a,b in gb(s)]
ans=n-len(var)
G=len(var)
if s[0]==s[-1]:
    t=G//2
    h=G-G//2+2
else:
    t,h=G-G//2,G//2+2

if k>min(t,h):
    print(n-1)
elif k==min(t,h):
    print(n-1)
else:
    print(ans+2*k)

Solution (Python):
n, k = map(int, input().split())
L=list(input())
cnt=0
if L[0]=='R' and L[1]=='R':
	cnt+=1 

for i in range(1,n-1):
	if L[i]=='R' and L[i+1]=='R':
		cnt+=1
	if L[i]=='L' and L[i-1]=='L':
		cnt+=1

if L[n-1]=='L' and L[n-2]=='L':
	cnt+=1

print(min(cnt+2*k,n-1))

Solution (Python):
n, k = map(int, input().split())
s = "X" + input() + "X"
t = [s[i] == s[i + [1, -1][s[i] == "L"]] for i in range(1, n + 1)]
print(min(sum(t) + k * 2, n - 1))

Solution (Python):
n,k = map(int,input().split())

B = input()
happy = 0
for i in range(n):
  if B[i]=="L" and i != 0 :
    if B[i-1] == "L":
      happy +=1
  if B[i]=="R" and i != n-1 :
    if B[i+1] == "R":
      happy +=1
print(min(happy+2*k, n-1))

Solution (Python):
n,k=map(int,input().split())
S=input()
c=0
d=0
for i in range(n-1):
    if S[i]==S[i+1]:
        c+=1
    if S[i]!=S[i+1]:
        d+=1
d=(d+1)//2
print(c+2*d-1 if (d<=k and S[0]!=S[-1]) else c+2*min(d,k))

Solution (Python):
N, K = [int(_c) for _c in input().split(" ")]
S_orig = input()
point_list = []
for target, non_target in zip(["L", "R"], ["R", "L"]):
    S = list(S_orig)

    start_idx_list = []
    before_char = None
    for i, c in enumerate(S):
        if i == 0:
            start_idx_list.append(i)
            before_char = c
        else:
            if before_char == non_target and c == target:
                start_idx_list.append(i)
            before_char = c
    start_idx_list.append(len(start_idx_list) - 1)
    if len(start_idx_list) - 1 <= K:
        point_list.append(len(S) - 1)
        continue

    longest = 0
    start_idx = -1
    end_idx = -1
    for i in range(len(start_idx_list) - K):
        cur_length = start_idx_list[i + K] - start_idx_list[i] + 1
        if cur_length > longest:
            start_idx = start_idx_list[i]
            end_idx = start_idx_list[i + K]
            longest = cur_length

    for _idx in range(start_idx, end_idx):
        S[_idx] = target
    S = ["N"] + S + ["N"]

    point = 0
    for idx in range(1, N + 1):
        if S[idx] == "L":
            if S[idx + 1] == "L":
                point += 1
        if S[idx] == "R":
            if S[idx - 1] == "R":
                point += 1

    point_list.append(point)

print(max(point_list))

Solution (Python):
N, K = map(int, input().split())
S = input()

cnt = 0
for s, s_ in zip(S, S[1:]):
    if s == s_:
        cnt += 1

unhappy = N - 1 - cnt

print(cnt + min(unhappy, 2 * K))

Solution (Python):

N, K = map(int, input().split())
S = list(input()+"X")

temp = S[0]
cnt = 1
c_cnt = 1
ans = 0

for i in range(1, N+1):
  if temp == S[i]:
    cnt += 1
  else:
    ans += cnt - 1
    cnt = 1
    c_cnt += 1
    temp = S[i]
  


c_cnt -= 1

if c_cnt//2 <= K:
  if c_cnt%2 == 0:
    ans += (c_cnt//2) * 2 - 1
  else:
    ans += (c_cnt//2) * 2
else:
  ans += K * 2

print(ans)

Solution (Python):
N,K = map(int,input().split())
S = list(input())
i=1
count =0
owari = False
while i < N:
    if S[i]!=S[0]:
        for j in range(i,N,1):
            if S[j]!=S[0]:
                S[j]=S[0]
            else:
                i = j
                break
            if j==N-1:
                i=N
        count+=1
        if count >= K:
            owari = True
    i+=1
    if owari:
        break
score=0
for i in range(0,N,1):
    if S[i]=="R":
        if i < N-1 and S[i+1]==S[i]:
            score+=1
    else:
        if i > 0 and S[i-1]==S[i]:
            score+=1
print(score)

Solution (Python):
n, k = map(int, input().split())
s = input()
pre = "O"
cnt = 0
for i in range(n):
    now = s[i]
    if now != pre:
        cnt += 1
    pre = now
print(min((n - cnt + 2 * k), n - 1))

Solution (Python):
N,K=map(int,input().split())
S=input()

H=0
cnt=0
for i in range(N):
    if i>0 and S[i]==S[i-1]=="L":
        H+=1
    elif i<N-1 and S[i]==S[i+1]=="R":
        H+=1
    if i<N-1 and S[i]!=S[i+1]:
        cnt+=1
print(H+min(cnt,K*2))

Solution (Python):
N, K = map(int, input().split())
S = input()
changeLR = []
changeRL = []
happy = 0
for i in range(N):
    if i == 0:
        before = S[0]
    else:
        tmp = S[i]
        if tmp == 'L' and before == 'R':
            changeLR.append(i)
        elif tmp == 'R' and before == 'L':
            changeRL.append(i)
        else:
            happy += 1
        before = tmp
koukan = min(len(changeLR), len(changeRL))
if K <= koukan:
    happy += 2 * K
elif K > koukan:
    if len(changeLR) != len(changeRL):
        happy += 2 * koukan + 1
    else:
        happy += 2 * koukan
print(happy)

Solution (Python):
n,k=map(int,input().split())
s=input()
c=0
for i in range(1,n):
  if s[i]==s[i-1]:
    c+=1
c+=2*k
print(min(n-1,c))

Solution (Python):
N, K = map(int, input().split())
S = input()

def calc_happiness(S):
    cnt = 0
    for i in range(1, len(S)):
        if S[i-1] == S[i]:
            cnt += 1
    return cnt

print(min(N-1, calc_happiness(S)+ K*2))

Solution (Python):
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product









import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

    N, K = map(int, input().split())
    S = list(input())
    gr = groupby(S)
    cnt = 0
    happy = 0
    for i in range(N-1):
        if S[i] == S[i+1]:
            happy += 1
    for key, group in gr:  
        """ex.
        0: [0, 0, 0]
        1: [1, 1]
        0: [0, 0, 0]
        1: [1, 1]
        0: [0]
        1: [1]
        """
        cnt += 1
    cut = cnt-1
    while K > 0:
        if cut == 0:
            break
        elif cut >= 2:
            happy += 2
            cut -= 2
        else:
            happy += 1
            cut -= 1
        K -= 1

    print(happy)

resolve()

Solution (Python):
n,k=map(int,input().split())
s = list(input())
cnt=0
now=s[0]
for i in range(1,n):
    if now==s[i]:continue
    else:
        cnt+=1
        now=s[i]

ans=n-1-cnt


print(min(n-1,ans+2*k))

Solution (Python):
N, K = map(int, input().split())
S = input()

happy = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        happy += 1

m = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        m += 1

if K <= m:
    happy += 2*K
else:
    if m > 1:
        happy += 2*m
    if S[0] != S[N-1]:
        happy += 1
print(happy)

Solution (Python):
import sys


import math
import collections



import fractions

import copy
import heapq
import decimal

import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n, k = ns()
    s = list(input())

    cnt = 0
    happy = 0
    for i in range(1, n):
        if s[i - 1] != s[i]:
            cnt += 1
        else:
            happy += 1

    kaisu = (cnt + 1) // 2
    tmp = -1 if cnt % 2 else 0

    ans = min(2 * kaisu + tmp, 2 * k)

    print(ans + happy)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
S = input()

def calc_happiness(S):
    cnt = 0
    for i in range(1, len(S)):
        if S[i-1] == S[i]:
            cnt += 1
    return cnt

print(min(N-1, calc_happiness(S)+ K*2))

Solution (Python):
n,k = map(int,input().split())
s = input()

cnt = 0
for i in range(len(s)):
	if s[i] == 'L':
		if i == 0:
			continue
		if s[i-1] == 'L' :
			cnt += 1
	else:
		if i == len(s) - 1:
			continue
		if s[i+1] == 'R' :
			cnt += 1

print(min(n-1,cnt + k*2))

Solution (Python):
n, k = map(int, input().split())
s = input()
score = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        score += 1
print(min(score + 2*k, n - 1))

Solution (Python):
n, k = map(int, input().split())
s = input()
score = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        score += 1
print(min(score + 2*k, len(s) - 1))

Solution (Python):
N, K = map(int, input().split())
S = input()
x, y = 0, 0
if S[0] == "L":
    y += 1
if S[-1] == "R":
    y += 1
x = S.count("RL")
if x:
    if x > K:
        x -= K
        K = 0
    else:
        K -= x
        x = 0
if K > 0:
    y = max(1, y - K)
print(N - 2 * x - y)

Solution (Python):
n, k = map(int, input().split())
s = str(input())

start = s[0]
end = s[-1]

count = 0
flag = 0
answer = 0
for i in range(1,n):
    if s[i-1] == start and s[i] != start and flag == 0:
        flag += 1
    elif s[i-1] != start and s[i] == start and flag == 1:
        flag = 0
        count += 1
    if s[i-1] == s[i]:
        answer += 1

answer += min(count,k)*2
if k > count:
    answer += flag

print(answer)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7











N, K = getNM()
s = input()
cnt = 0
for i in range(N - 1):
    if s[i] == s[i + 1]:
        cnt += 1
print(min(cnt + 2 * K, N - 1))

Solution (Python):
n, k = map(int,input().split())
s = input()
s += '
lis = []
cnt = 0
for i in range(n):
    cnt += 1
    if s[i] != s[i+1]:
        lis.append(cnt)
        cnt = 0

val = 0
ans = 0
for i in range(len(lis)):
    if i < 2*k+1:
        val += lis[i]
    else:
        ans += lis[i] - 1
ans += val - 1
print(ans)

Solution (Python):
N, K = list(map(int, input().split()))

S = input()
ans = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        ans += 1
print(min(ans+ 2*K, N-1))

Solution (Python):
N, K = map(int, input().split())
Ss = input().rstrip()

num = Ss.count('LR') + Ss.count('RL')
num -= 2*K
if num < 0:
    num = 0

ans = N - (num+1)
print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()

l = []
t = []
st = s[0]
for c in s[1:]:
    if not st or st[-1] == c:
        st += c
    else:
        l.append(len(st))
        t.append(st)
        st = c
l.append(len(st))
t.append(st)

print(len(''.join(t[:k*2])) + sum(map(lambda x: x-1, l[k*2:] if l[k*2:] else [0])))

Solution (Python):
n,k = map(int,input().split())
s = list(input())
change = 0
for i in range(n-1):
  if s[i] != s[i+1]:
    change += 1
happy = n-change-1
print(min(n-1,happy+min(k,change)*2))

Solution (Python):
n,k = map(int,input().split())
s = input()

score=0
for i in range(n-1):
    if s[i] == s[i+1]:
        score+=1

ans=min(score+2*k,n-1)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
s = read().rstrip().decode()
ans = n - 1
for bf, af in zip(s, s[1:]):
    if bf != af:
        ans -= 1
print(min(n - 1, ans + k * 2))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    s=input()
    cunt=0
    for i in range(n-1):
        if s[i]!=s[i+1]:
            cunt+=1
    print(n-1-cunt+min(2*k,cunt))

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    s=input()
    cunt=0
    for i in range(n-1):
        if s[i]!=s[i+1]:
            cunt+=1
    print(n-1-cunt+min(2*k,cunt))

if __name__=='__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
s = list(str(input()))
unhappy = 0
for i in range(n - 1):
    if s[i] != s[i + 1]:
        unhappy += 1
print(n - unhappy - 1 + min(2 * k, unhappy))

Solution (Python):


N, K = map(int, input().split())
S = str(input())

score = 0
for i in range(N - 1):
    if S[i] == S[i + 1]:
        score += 1

print(min(score + K * 2, N - 1))

Solution (Python):
N,K=map(int,input().split())
S=input()



trans=0
for i in range(N-1):
  if S[i]!=S[i+1]:
    trans += 1
trans_new = max(trans - 2*K,0)
print(N-trans_new-1)

Solution (Python):
N,K = map(int, input().split())
S = list(input())

ans = 0

for i in range(1,N):
    if S[i-1]==S[i]:
        ans += 1

if ans + 2*K <= N-1:
    print(ans + 2*K)
else:
    print(N-1)

Solution (Python):
n,k  = map(int,input().split())
S = input()
s = list(S)
ans = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        ans += 1
ans += 2*k
print(min(ans,n-1))

Solution (Python):
N, K = map(int, input().split())
S = input()

cnt = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    cnt += 1

print(min(N-1, cnt + 2*K))

Solution (Python):
n,k = map(int, input().split())
s = str(input())

cnt = s.count('RL')
ans = 0

for i in range(0,n-1):
    if s[i] != s[i+1]:
        cnt += 1
    else:
        ans += 1


if cnt >= k:
    ans += 2*k
else:
    ans += 2*cnt
    if k-cnt>=2:
        if s[-1] == 'L' and s[0] == 'L':
            ans += 2
        elif s[-1] == 'L' or s[0] == 'L':
            ans += 1
    else:
        if s[-1] == 'L' or s[0] == 'L':
            ans += 1       


print(min(ans,n-1))

Solution (Python):
N, K = map(int, input().split(' '))
S = input()
happy = sum([1 for i in range(N - 1) if S[i] == S[i + 1]])
print(min(N - 1, happy + 2 * K))

Solution (Python):
N, K = map(int, input().split())
S = list(input())

count = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        count += 1

print(min(count+2*K, len(S)-1))

Solution (Python):
n, k = map(int, input().split())
s = list(input())
v = s[0]
cnt = 0
a = []
for t in s:
    if t == v:
        cnt += 1
    else:
        a.append(cnt)
        cnt = 1
        v = t
a.append(cnt)
la = len(a)
b, c = [0] * (la + 1), [0] * (la + 1)
for i in range(1, la + 1):
    b[i] = b[i - 1] + a[i - 1]
    c[i] = b[i] - i
l = 2 * k + 1
if la < l:
    print(n - 1)
    exit()
ans = 0
i, j = l, 0
while True:
    ans = max(ans, b[i] - b[j] + c[la] + c[j] - c[i] - 1)
    i += 1
    j += 1
    if i > la:
        print(ans)
        exit()

Solution (Python):
N,K=list(map(int,input().split()))
S=input()
cnt=0

for i in range(N-1):
    if S[i]==S[i+1]:
        cnt+=1

print(min(cnt+2*K,N-1))

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    num = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            num += 1
    print(min(num + 2*K, N - 1))


if __name__ == '__main__':
    main()

Solution (Python):


n, k = list(map(int, input().split()))
s = input()
group = 1
for c, d in zip(s, s[1:]):
    if c != d:
        group += 1
print(n - max(1, group - 2 * k))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce


import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n, k = Is()
s = S()

contl = 0
contr = 0
sandl = 0
sandr = 0


for i in range(1, n):
    if s[i-1] == s[i] == "L":
        contl += 1
    if s[i-1] == s[i] == "R":
        contr += 1

ans = min(n-1, contl+contr+k*2)
print(ans)

Solution (Python):
inf = 10**15
mod = 10**9+7
n, k = map(int, input().split())
s = input()
count = 0
flag = False
for c in s:
    if c == 'R':
        if flag == False:
            flag=True
    else:
        if flag == True:
            count+=1
            flag=False
if s[-1]=='R' and count > 0:
    count+=1

if k >= count:
    print(n-1)
elif k == count-1:
    if s[0] == 'R' and s[-1] == 'R':
        print(n-1)
    elif s[0] == 'R' or s[-1] == 'R':
        print(n-2)
    else:
        print(n-3)
else:
    if s[0] == 'R' and s[-1] == 'R':
        print(n - 3 - (count - 2 - k) * 2)
    elif s[0] == 'R' or s[-1] == 'R':
        print(n - 2 - (count - 1 - k) * 2)
    else:
        print(n - 1 - (count - k) * 2)

Solution (Python):
n, k = map(int, input().split())
s = input()
d = 0
for c1, c2 in zip(s, s[1:]):
    if c1 == 'R' and c2 == 'L':
        d += 1
e = 0
if s[0] == 'L':
    e += 1
if s[-1] == 'R':
    e += 1
k, d = max(k - d, 0), max(0, d - k)
if k > 0:
    e = 1
ans = max(0, n - d * 2 - e)
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
happy = 0
for n in range(N):
    if S[n] == "L" and n > 0 and S[n - 1] == "L":
        happy += 1
    elif S[n] == "R" and n < N - 1 and S[n + 1] == "R":
        happy += 1
print(min(happy + 2 * K, N - 1))

Solution (Python):
n, k = map(int, input().split())
s = input()

target = "R" if s[0] == "L" else "L"
s = "N" + s + "N"
happy_count = 0
position = []
for i in range(1, len(s)-1):
    if s[i] == "R" and s[i+1] == "R":
        happy_count += 1
    elif s[i] == "L" and s[i-1] == "L":
        happy_count += 1
    if s[i] == target and s[i+1] != target:
        position.append(i)

for i in range(len(position)):
    if k == 0:
        break
    if position[i] == n:
        happy_count += 1
    else:
        happy_count += 2
    k -= 1

print(happy_count)

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()

    count = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count +=1 

    ans = min(count + K*2, N-1)
    print(ans)
    
main()

Solution (Python):
n, k = map(int, input().split())
s = input()
point = 0
tmp = 'N'
for i in s:
    if i == tmp: point += 1
    tmp = i
rl = s.count('RL')
lr = s.count('LR')
print(point + min(2 * k, rl + lr))

Solution (Python):
N, K = map(int, input().split())
S = input()

def solve(S):
    ans = 0
    switch = 0
    for i in range(1,N):
        if S[i]!=S[i-1]:
            switch += 1
    switch = max(0,switch-K*2)
    ans = N-switch-1
    return ans
print(solve(S))