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):
def main():
    N, K = map(int, input().split())
    S = list(input())
    base = S[0]
    flag = False
    for i in range(0, N):
        if K == 0:
            break
        if S[i] != base and (not flag):
            flag = True
            S[i] = base
        elif S[i] != base and flag:
            S[i] = base
        elif S[i] == base and flag:
            flag = False
            K -= 1
        else:
            pass
    
    ans = 0
    for i in range(N-1):
        ans += S[i] == S[i+1]
    
    print(ans)
  
if __name__ == "__main__":
    main()

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

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

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(n-1, score + 2 * k)
print(ans)

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n,k = i_map()
    s = list(input())
    a = 0
    for i,q in itertools.groupby(s):
        
        a += 1
    a -= 1
    print(n-1-max(a-2*k,0))




if __name__=="__main__":
    main()

Solution (Python):
import sys

def read_str(): return sys.stdin.readline().strip()
def read_int(): return int(sys.stdin.readline().strip())
def read_ints(): return map(int, sys.stdin.readline().strip().split())
def read_str_split(): return list(sys.stdin.readline().strip())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))

def Main():
    n, k = read_ints()
    s = read_str()
    cnt = s.count('RL') + s.count('LR') + 1
    cnt = min(n-1, n-cnt+2*k)
    print(cnt)

if __name__ == '__main__':
    Main()

Solution (Python):






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





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

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  s=S()
  ans=0
  x=s[0]
  cnt=0
  for y in s[1:]:
    if x==y:
      cnt+=1
    else:
      ans+=cnt
      cnt=0
    x=y
  ans+=cnt

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

  return ans


print(main())

Solution (Python):








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


if N==1:
  print(0)
  exit()
cnt = 0
if S[0] == 'R':
  if S[1] == 'R':
    cnt += 1
if S[-1] == 'L':
  if S[-2] == 'L':
    cnt += 1
for k in range(1,N-1):
  if S[k] == 'L':
    if S[k-1] =='L':
      cnt += 1
  else:
    if S[k+1] == 'R':
      cnt += 1

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

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline
N, K = map(int, input().split())
S = list(input().strip())

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


print(ans + min(2*K, split))

Solution (Python):
N, K = map(int, input().split())
S = input()+"."
l = 0
ans = 0

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

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

Solution (Python):

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

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

Solution (Python):
N,K = map(int,input().split())
S = list(input())
tmp = []
tmp.append(S[0])
for i in  range(1,N):
    if(S[i] != S[i-1]):
        tmp.append(S[i])
G = len(tmp)
G -= 2*K
G = max(G-1,0)

print(N-G-1)

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

cnt=0
lst=[]
for i in range(len(s)-1):
    if s[i]!=s[i+1]:
        cnt+=1
    else:
        lst.append(cnt)
        cnt=0

lst.append(cnt)

ans=n-sum(lst)-1

lst2=[]
for i in lst:
    for j in range(i//2):
        lst2.append(2)
    
    if i%2==1:
        lst2.append(1)

lst3=[]
xxxxx=lst2.count(1)

if xxxxx%2==1:
    lst3.append(1)

for i in range(xxxxx//2):
    lst3.append(2)

for i in range(lst2.count(2)):
    lst3.append(2)

lst3.sort()
lst3.reverse()

if len(lst3)>k:
    ans+=sum(lst3[:k])
else:
    ans+=sum(lst3)


print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))



N,K=mint()
S=input()
r1=0
r2=0
now=0
mark=S[0]
tmp=None
for i in range(N):
    if i<N-1 and S[i]==S[i+1]:
        now+=1
    if tmp==mark and S[i]!=mark:
        r2+=1
    tmp=S[i]
if tmp!=mark:
    r1+=1
    r2-=1
now+=2*min(K,r2)
K-=min(K,r2)
now+=min(K,r1)
print(now)

Solution (Python):
N,K = map(int,input().split())
S = input()
Hap = 0
NewS = S[0]
def chan(X):
    if X == 'R':
        return 'L'
    else:
        return 'R'
for i in range(1,len(S)):
    strS = S[i]
    strS1 = S[i-1]
    if strS != strS1:
        NewS += strS
    else:
        Hap += 1
while len(NewS) > 1 and K != 0:
    Hap += 2
    NewS = chan(NewS[-1]) + NewS[2:-2] + chan(NewS[0]) 
    K -= 1
if Hap > N-1:
    Hap = N-1
print(Hap)

Solution (Python):
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = S[0]
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
answer -= 1
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 >= K:
  print(answer+K*2)
else:
  print(answer+(r_cnt//2+1)*2-1)

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

rl=s.count('RL')
if s[0]=='L':
    start=1
else:
    start=0
if s[-1]=='R':
    end=1
else:
    end=0
a=rl-k
if a>=1:
    ans=a*2+start+end
elif a==0:
    ans=start+end
elif a<=-1:
    ans=1
print(n-ans)

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

Solution (Python):
from itertools import *
N,K = map(int,input().split())
S = input()
A = []

for i,j in groupby(S):
  A+=[len(list(j))-1]

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

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

x=0
y=0
ans=n







for i in range(n-1):
    if s[i:i+2]=="RL":
        x+=1
        
        
    if s[i:i+2]=="LR":
        y+=1

x=max(x-k,0)
y=max(y-k,0)
ans=ans-x-y-1





print(ans)

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

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


for i in range(K):
  face2face-=1
  back2back-=1
face2face=max(face2face,0)
back2back=max(back2back,0)
  
print(N-face2face-back2back-1)

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

def runLengthEncoding(s:str) -> list:
    """
    ランレングス圧縮します。リスト内に(文字,個数)の形でカウントを収納して
    Returnします。見ての通りO(N)です。
    """
    l = []
    N = len(s)
    count = 1
    char = s[0]
    for i in range(1, N):
        if char != s[i]:
            tmp = (char, count)
            l.append(tmp)

            char = s[i]
            count = 1

        elif char == s[i]:
            count += 1

    l.append((char, count))

    return l


lre = runLengthEncoding(S)

if len(lre) == 2:
    print(N-1)
    exit()

else:
    n = len(lre) // 2
    ans = None
    if K >= n:
        ans = N-1
    else:
        groups = len(lre) - 2*K

        ans = (N-1) - groups + 1

    print(ans)

Solution (Python):
N,K = map(int,input().split())
S = input()
if N == 1:
    print(0)
    exit()

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

ans += min(K,cnt//2) * 2

if K > cnt//2 and S[0] != S[-1]:
    ans += 1

print(ans)

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

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

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

  
if __name__=='__main__':
  main()

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

now=S[0]
a=-1
for s in S:
    a+=now==s
    now=s
a+=K*2
ans=min(N-1,a)
print(ans)

Solution (Python):
from collections import deque

def main():
  n,k=map(int,input().split())
  s=input()
  
  t=[]
  t.append(s[0])
  x=s[0]
  for i in range(1,n):
    if x!=s[i]:
      t.append(s[i])
      x=s[i]
  
  
  j=0
  t=deque(t)
  while j<k:
    if len(t)<=2:
      print(n-1)
      exit(0)
    t.popleft()
    t.popleft()
    j+=1
    
  print(n-1-(len(t)-1))

  
if __name__=='__main__':
  main()

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)

from itertools import product, accumulate, combinations, product







INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def run():
    N, K = map(int, sysread().split())
    S = input()
    c = S[0]
    checkpoints = 0
    endpoints = 0
    for s in S:
        if c == 'L':
            if s == 'R':
                c = 'R'
                endpoints += 1
            else:
                continue
        elif c == 'R':
            if s == 'L':
                checkpoints += 1
                c = 'L'
            else:
                continue
    
    ans = N - 2 * checkpoints
    halfpoint = 0
    edge = 0
    if S[0] == 'R':
        edge += 1
    else:
        halfpoint += 1

    if S[-1] == 'L':
        edge += 1
    else:
        halfpoint += 1

    ans -= halfpoint
    val = min([checkpoints, endpoints, K])
    checkpoints -= val
    endpoints -= val
    K -= val
    ans += 2 * val

    if K > 0:
        if checkpoints > 0:
            if edge > 0:
                val = min([edge, checkpoints, K])
                K -= val
                edge -= K
                checkpoints -= K
                ans += val
    if K > 0:
        if halfpoint > 0 and endpoints > 0:
            val = min([K, halfpoint, endpoints])
            K -= val
            halfpoint -= val
            endpoints -= val
            ans += val

    print(ans)

if __name__ == "__main__":
    run()

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

s_list=[]
for i in s:
    if i=="L":s_list.append(0)
    else:s_list.append(1)
start_num=s_list[0]
i=0
while k>0:
    if i>=n:break
    if s_list[i]!=start_num:
        k-=1
        while i<n and s_list[i]!=start_num:
            s_list[i]=start_num
            i+=1
    i+=1
ans=0
for i in range(n):
    if i+1<n and s_list[i]==1 and s_list[i+1]==1:
        ans+=1
    if i-1>=0 and s_list[i]==0 and s_list[i-1]==0:
        ans+=1
print(ans)

Solution (Python):
n, k = 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())
S = list(input())

cnt = 0
init_s = S[0]
pre = '_'
for i, s in enumerate(S):
    if s != init_s:
        if pre != s:
            K -= 1
        if K == -1:
            break
        S[i] = init_s
    pre = s
ans = 0
pre = '_'
for s in S:
    ans += pre == s
    pre = s
print(ans)

Solution (Python):
from sys import exit
n, k = map(int, input().split())
s = list(input())
if n == 1:
    print(0)
    exit()

L_record = []
R_record = []

if s[0] == "L":
    flg = "R"
else:
    flg = "L"

for i, char in enumerate(s):
    if char == "L" and flg == "R":
        L_record.append([i, 1])
        flg = "L"
    elif char == "L" and flg == "L":
        L_record[-1][1] += 1
    elif char == "R" and flg == "L":
        R_record.append([i, 1])
        flg = "R"
    elif char == "R" and flg == "R":
        R_record[-1][1] += 1

L_record.sort(key=lambda x: x[0])
R_record.sort(key=lambda x: x[0])


if L_record != []:
    if L_record[-1][0] + L_record[-1][1] == n:
        record = L_record.pop()
        L_record = [record] + L_record
if R_record != []:
    if R_record[-1][0] + R_record[-1][1] == n:
        record = R_record.pop()
        R_record = [record] + R_record


point = 0
for i, char in enumerate(s):
    
    if char == "L" and i-1 >= 0:
        if s[i-1] == "L":
            point += 1
            
    if char == "R" and i+1 < n:
        if s[i+1] == "R":
            point += 1
            



point_l = point
for i in range(k):
    if L_record != []:
        idx, num = L_record.pop()
        if idx == 0:
            point_l += 1
        elif idx + num == n:
            point_l += 1
        else:
            point_l += 2

point_r = point
for i in range(k):
    if R_record != []:
        idx, num = R_record.pop()
        if idx == 0:
            point_r += 1
        elif idx + num == n:
            point_r += 1
        else:
            point_r += 2
print(min(max(point_r, point_l), n-1))

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

import copy


cnt = 0
tmp_s = copy.deepcopy(s)
flag = 1
for i, j in enumerate(s):
    if j == 'L':
        tmp_s[i] = 'R'
        if flag:
            cnt += 1
            flag = 0
    else:
        tmp_s[i] = 'R'
        flag = 1

    if cnt >= k and flag:
        break

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

ans = cnt

cnt = 0
tmp_s = copy.deepcopy(s)
flag = 1
for i in range(n-1, -1, -1):
    j = s[i]
    if j == 'L':
        tmp_s[i] = 'R'
        if flag:
            cnt += 1
            flag = 0
    else:
        tmp_s[i] = 'R'
        flag = 1

    if cnt >= k and flag:
        break

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

ans = max(ans, cnt)


cnt = 0
tmp_s = copy.deepcopy(s)
flag = 1
for i, j in enumerate(s):
    if j == 'R':
        tmp_s[i] = 'L'
        if flag:
            cnt += 1
            flag = 0
    else:
        tmp_s[i] = 'L'
        flag = 1

    if cnt >= k and flag:
        break

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

ans = max(ans, cnt)


cnt = 0
tmp_s = copy.deepcopy(s)
flag = 1
for i in range(n-1, -1, -1):
    j = s[i]
    if j == 'R':
        tmp_s[i] = 'L'
        if flag:
            cnt += 1
            flag = 0
    else:
        tmp_s[i] = 'L'
        flag = 1

    if cnt >= k and flag:
        break

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

ans = max(ans, cnt)

print(ans)

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

t = S[0]
c = 0
for i in range(N):
    if S[i] != t:
        c += 1
        t = S[i]

if c <= 2 * K:
    print(N - 1)
else:
    a = c - 2 * K
    print(N - a - 1)

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

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

a = 0
for i in range(n-1):
    if s[i] != s[i+1]:
        a += 1
print(n-1-max(a-2*k,0))

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

first = s[0]
second = 'R' if first=='L' else 'L'

i = 0
while k > 0 and i < n:
    if s[i] ==second:
        while i < n and  s[i] == second:
            s[i] = first
            i += 1
        k -= 1
    else:
        i += 1



ans = 0

def happy_check(i):
    global ans
    if i < n-1 and s[i] == 'R' and s[i+1] == 'R':
        ans += 1
    elif 0 < i and s[i] == 'L' and s[i-1] == 'L':
        ans += 1

for i in range(n):
    happy_check(i)

print(ans)

Solution (Python):
from collections import deque

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

q = deque()

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

q.append(cnt + 1)

for _ in range(K):
    if not q:
        break
    a = q.popleft()
    b = q.popleft() if q else 0
    c = q.popleft() if q else 0
    q.appendleft(a+b+c)

print(sum(q) - len(q))

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

ans  = 0
tran = 0

for i in range(n-1):
    chk = s[i] + s[i+1]

    if chk == 'LR' or chk == 'RL':
        tran += 1
    else:
        ans += 1

if k*2 >= tran:
    print(n-1)
else:
    print(ans+(k*2))

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


if __name__ == "__main__":
    main()

Solution (Python):








def solve(N, K, S):
    sachi = 0
    L_cluster, R_cluster = (0, 1) if S[0] == 'R' else (1, 0)
    for i in range(N):
        if S[i] == 'R' and i != N - 1 and S[i + 1] == 'R':
            sachi += 1
        if S[i] == 'L' and i != 0 and S[i - 1] == 'L':
            sachi += 1
        if i > 0 and S[i] != S[i - 1]:
            if S[i] == 'R':
                R_cluster += 1
            else:
                L_cluster += 1

    ans = 0
    reverse_cluster = min(L_cluster, R_cluster)
    if K >= reverse_cluster:
        ans = N - 1
    else:
        ans = sachi + K * 2

    print(ans)


if __name__ == '__main__':
    
    N, K = map(int, input().split())
    S = input()
    
    
    solve(N, K, S)

Solution (Python):
N,K=list(map(int,input().split()))
let=input()
can=0
for i in range(N-1):
   if let[i]!=let[i+1]:
      can+=1
if can<=K*2:
   print(N-1)
else:
   can-=K*2
   print(N-can-1)

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

rl = 0
end = 0
if s[0] == "L":
    end += 1
if s[-1] == "R":
    end += 1
for i in range(n-1):
    if s[i] == "R" and s[i+1] == "L":
        rl += 1

ans = n - 2*rl - end

if end == 0:
    if k >= rl:
        ans += 2*rl
        ans -= 1
    else:
        ans += 2*k
elif end == 1:
    if k >= rl:
        ans += 2*rl
    else:
        ans += 2*k
elif end == 2:
    if k == rl:
        ans += 2*rl
    elif k >= rl+1:
        ans += 2*rl
        ans += 1
    else:
        ans += 2*k
print(ans)

Solution (Python):
def cin():  return list(map(int,input().split()))

N, K = cin()
S = input()

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

Solution (Python):

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    S=input()
    
    ans=0
    now="A"
    for i in range(N):
        if S[i]==now:
            ans+=1
        now=S[i]
    
    ans=(min(N-1,ans+K*2))
    print(ans)
        

main()

Solution (Python):

import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, K = MI()
    original = input()
    num_happy = 0

    for i in range(N - 1):
        if original[i] == original[i+1]:
            num_happy += 1

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


main()

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

status = s[0]
cnt = 1
sa = []
for i in range(1, n):
    if s[i] != status:
        sa.append(status * cnt)
        cnt = 0
        status = s[i]
    cnt += 1
sa.append(status * cnt)

for i in range(1, min(k*2, len(sa)), 2):
    sa[i] = ("R" if sa[i][0] == "L" else "L") * len(sa[i])

sa = ''.join(sa)

ans = 0
for i in range(n):
    if sa[i] == "L":
        if i > 0: 
            if sa[i-1] == "L": ans += 1
    else:
        if i < n-1:
            if sa[i+1] == "R": ans += 1

print(ans)

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

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

for i in range(K):
    A=min(N-1,A+2)

print(A)

Solution (Python):
n,k=map(int,input().split())
a=list(input())
count=0
for i in range(n):
    if a[i]=="L" and i-1>=0 and a[i-1]=="L":
        count+=1
    elif a[i]=="R" and i+1<=n-1 and a[i+1]=="R":
        count+=1
print(min(n-1,count+k*2))

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,k = list(map(int, input().split()))
s = input()
v = 0
l = [[s[0],1]]
for c in s[1:]:
    if l[-1][0]!=c:
        l.append([c,1])
    else:
        l[-1][1] += 1
vs = []
if len(l)==1:
    ans = n-1
else:
    num = 0
    for i in range(n):
        if (i==0 and s[i]=="L") or (i==n-1 and s[i]=="R") or (0<=i<=n-2 and s[i]=="R" and s[i+1]=="L") or (1<=i and s[i]=="L" and s[i-1]=="R"):
            num += 1
    vals = []
    for i in range(len(l)):
        if l[i][0]=="L":
            if i in (0, len(l)-1):
                vals.append(1)
            else:
                vals.append(2)
    vals.sort()
    ans = n - (num - sum(vals[-k:]))
    
    vals = []
    for i in range(len(l)):
        if l[i][0]=="R":
            if i in (0, len(l)-1):
                vals.append(1)
            else:
                vals.append(2)
    vals.sort()
    ans = max(ans, n - (num - sum(vals[-k:])))
    
print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
pre = s[0]
rs = s[0]
first = s[0]

if s[0] == "R":
    second = "L"
else:
    second = "R"

for i in s[1:]:
    if i == second and k > 0:
        rs = rs + first
        pre = i
    elif i == first and pre == second:
        k -= 1
        rs = rs + i
        pre = i
    elif k == 0:
        rs = rs + i
    elif i == first and pre == first:
        rs = rs + i

pre = "0"
ans = 0

for i in rs:
    if i == pre:
        ans += 1
    pre = i

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
pre = s[0]
ans = s[0]
first = s[0]

if s[0] == "R":
    second = "L"
else:
    second = "R"

for i in s[1:]:
    if i == second and k > 0:
        ans = ans + first
        pre = i
    elif i == first and pre == second:
        k -= 1
        ans = ans + i
        pre = i
    elif k == 0:
        ans = ans + i
    elif i == first and pre == first:
        ans = ans + i

pre = "0"
cnt = 0

for i in ans:
    if i == pre:
        cnt += 1
    pre = i

print(cnt)

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

S = input()

cnt = 0
b = S[0]

for i in range(1,N):
  if b != S[i]:
    cnt += 1
  b = S[i]
  
if cnt % 2 == 1:
  if K > cnt//2:
    print(N-1)
  else:
    print(N-2*(cnt//2+1)+2*K)
else:
  if K >= cnt//2:
    print(N-1)
  else:
    print(N-cnt+2*K-1)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

ans = 0
for i in range(1,N):
    if S[i-1] == "R" and S[i] == "L":
        ans += 2

if S[0] == "L":
    ans += 1
if S[N-1] == "R":
    ans += 1

print(N - max(1, ans - 2*K))

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

ans = []
c = 1

for i in range(1,n):
    if S[i-1] == S[i]:
        c += 1
    else:
        ans.append(c)
        c = 1
ans.append(c)

if len(ans) // 2  <= k:
    print(n-1)
else:
    d = len(ans) - 2*k
    print(n-d)

Solution (Python):
(str_len,cng_cnt) = input().split(" ")
str_input = list(input())

str_len= int(str_len)
cng_cnt= int(cng_cnt)
tmp1=""
cnt=0
cnt2=0
for st in str_input:
    if(tmp1 == st ):
      cnt = cnt+1
    else:
      cnt2= cnt2 + 1
      
    tmp1=st

if (cng_cnt < cnt2-1 ):
	c_up = cng_cnt * 2 
else:
	c_up= (cnt2 * 2) + (cng_cnt - cnt2)
ans =cnt + c_up

if(ans >= str_len):
  ans =str_len-1

print(ans)

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    '''
    RL...LL 0
    RL...RR 0
    RL...RL 0

    RL...LR +2
    LR...RL +2

    RL      +1

    13 3
    LRRLRLRRLRLLR
    '''
    N, K = read_ints()
    S = input()
    score = 0
    for i in range(N-1):
        if S[i] == S[i+1]:
            score += 1
    return min(score+2*K, N-1)


if __name__ == '__main__':
    print(solve())

Solution (Python):
n,k = map(int,input().split())
s = input()
edge,inside,cnt,ix = 0,0,0,0
if s[0]=='R' or s[-1]=='R':
  edge += 1
temp = ''
for i in range(n):
  if temp=='R' and s[i]=='L':
    inside += 1
  elif temp==s[i]:
    cnt += 1
  temp = s[i]
if inside>=k:
  print(min(k*2+cnt,n-1))
else:
  print(min(inside*2+edge+cnt,n-1))

Solution (Python):
n,k = map(int,input().split())
s = input()
edge,inside,cnt,ix = 0,0,0,0
if s[0]=='R' or s[-1]=='R':
  edge += 1
temp = ''

s += 'L'
for i in range(n+1):
  if i!=n:
    if temp=='R' and s[i]=='L':
      inside += 1
    elif temp==s[i]:
      cnt += 1
  temp = s[i]

if inside>=k:
  print(min(k*2+cnt,n-1))
else:
  print(min(inside*2+edge+cnt,n-1))

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,k=mp()
s=list(input())
ans=[]
c=1
for i in range(1,n):
    if s[i]==s[i-1]:
        c+=1
    else:
        ans.append(c)
        c=1
ans.append(c)
if len(ans)//2<=k:
    print(n-1)
else:
    print(n-len(ans)+2*k)

Solution (Python):
import sys

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

lcnt=-1
rcnt=-1

rr=[0]*n
ll=[0]*n
sim=""
for si in s:
    if si=="R" and sim=="R":
        rr[rcnt]+=1
    elif si=="R" and sim!="R":
        rcnt+=1
        rr[rcnt]=1
    if si=="L" and sim=="L":
        ll[lcnt]+=1
    elif si=="L" and sim!="L":
        lcnt+=1
        ll[lcnt]=1
    sim=si
        
ll=[i for i in ll if i!=0]
rr=[i for i in rr if i!=0]



minlen=min(len(ll),len(rr))
if minlen<=k:
    print(n-1)
    sys.exit()
    
for i in range(k):
    ll[i]+=rr[i]
    rr[i]=0

for i in range(len(ll)-1):
    if i<len(rr) and ll[i]!=0 and rr[i]==0 :
        ll[i+1]+=ll[i]
        ll[i]=0

icnt=0
for i in range(len(ll)):
    icnt+=max(ll[i]-1,0)
for i in range(len(rr)):
    icnt+=max(rr[i]-1,0)
    
print(icnt)

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

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,n-1-cnt+2*k))

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

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

ans = n - 1 - max(a - 2 * k, 0)
print(ans)