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=(int(i) for i in input().split())
S=input()

p=0
m=0
r=1
r_list=[]
bit=True
ans=0
if S[0]=="R":
    m+=1
if S[0]=="L":
    p+=1
for i in range(N-1):
    if S[i]=="R" and S[i+1]=="L":
        p+=1
        if bit==True:
            r+=1
    elif S[i]=="L" and S[i+1]=="R":
        m+=1
        if bit==True:
            r+=1
    else:
        r_list.append(r)
        bit=False
        r=0
        ans+=1
if S[N-1]=="R":
    m+=1
    if bit==True:
        r+=1
        r_list.append(r)
if S[N-1]=="L":
    p+=1
    if bit==True:
        r+=1
        r_list.append(r)
t=min(p,m)
ans+=min(t,K)*2

if p+m==2:
    if max(r_list)==p+m:
        if t<=K:
            ans-=1
if p+m==3:
    if max(r_list)==p+m:
        if t<=K:
            ans-=1
if p+m==4:
    if max(r_list)==p+m:
        ans-=1
    if max(r_list)==p+m-1:
        ans-=1

if ans>=N:
    ans=N-1
if ans<0:
    ans=0
print(ans)

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

Solution (Python):
N, K = list(map(int, input().split()))
s = input()
cnt = 0
for be, af in zip(s[:-1], s[1:]):
  if be == af:
    cnt += 1
print(min(cnt + 2*K, N-1))

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

face = n - 1 - s.count('LR') - s.count('RL')
ans = min(n - 1, face + 2 * k)
print(ans)

Solution (Python):
N,K = [int(x) for x in input().split()]
S = list(input())
point = N - 1
count = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        point -= 1
        count += 1
if K >= count/2:
    print(N-1)
elif K >= int(count/2):
    print(N-2)
else:
    print(point+2*K)

Solution (Python):
n_k_str = input()
n_k_list = n_k_str.split(" ")
N = int(n_k_list[0])
K = int(n_k_list[1])
S = list(input())

pre_s = S.pop(0)
pre_index = -1
result = 0
for i,s in enumerate(S):
    if s != pre_s:
        temp = i - pre_index - 1
        result += temp
        pre_index = i
    pre_s = s

temp = len(S) - pre_index - 1
result += temp

result += K*2
if result >= N:
    result = N - 1

print(result)

Solution (Python):
N,K=map(int,input().split())
S=input()
def turn(s):
    if s=="L":
        return("R")
    else:
        return("L")
   
score=0
k_count=0
change_flag=False
S2=list(S)
first_s=S[0]
for i in range(1,N):
    
    if k_count<K:
        if S2[i] != first_s:        
            S2[i]=turn(S2[i])
        if S[i-1]!=S[i] and S[i-1]!=first_s:
            k_count+=1
    if S2[i-1]==S2[i]:
        score+=1
print(score)

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

which = s[0]
init_sum = 0
for i in range(1, n):
  if s[i] == which:
    init_sum += 1
  else:
    which = s[i]
    
print(min(init_sum + 2*k, n-1))

Solution (Python):
import sys,bisect as bs,collections as cl
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

n,k = m()

a = list(input())
aa = s(a)

al = []
ppp = [0]
ans = sum(map(lambda x:x[1]-1,aa))
for i in range(len(aa)-1):
    ppp.append(ppp[-1]+aa[i][1])

if len(aa) != 1:
    al.append([aa[0][1],0,1])
    al.append([aa[-1][1],len(aa)-1,1])

for i in range(1,len(aa)-1):
    al.append([aa[i][1],i,2])
al.sort(key = lambda x:x[2],reverse = True)
on = [True for i in range(n)]
oon = [0 for i in range(len(aa))]
al = cl.deque(al)
co = min(len(aa),k)
while co:
    if len(al) == 0:
        break
    an = al.popleft()
    co -= 1
    if an[2] == 1:
        if an[1] == 0:
            if not oon[1] == -1:
                oon[0] = -1
                oon[1] = 1
                ans += 1
            else:
                co += 1
        else:
            if not oon[-2] == -1:
                oon[-1] = -1
                oon[-2] = 1
                ans += 1
            else:
                co += 1
    else:
        if not (oon[an[1]-1] == -1 or oon[an[1]+1]):
            oon[an[1]-1] = 1
            oon[an[1]+1] = 1
            oon[an[1]] = -1
            ans += 2
        else:
            co += 1

print(ans)

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

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

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

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

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

if hap+2*k >= n-1:
    hap = n-1

else:
    hap = hap + 2*k

print(hap)

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

Solution (Python):
N, K = map(int, input().split())
S = str(input())
check = 0
ans = 0
need_cng = 0
for i in range(0,N-1):
    if (S[i] != S[i+1]):
        check = check +1
if(check%2 ==0):
    need_cng = check/2
else:
    need_cng = check//2+1

if (need_cng > K):
    ans = N - (check - 2*K) -1
else:
    ans = N-1
print(ans)

Solution (Python):
N, K = [int(i) for i in input().split(' ')]
s_list = input()

last_s = None
happy = 0
for s in s_list:
    if s == last_s:
        happy += 1
    last_s = s
print(min(happy + 2 * K, N - 1))

Solution (Python):
N, K = [int(nk) for nk in input().split()]
S = input()

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

mx_turn =  howmany_LR_or_RL // 2
mx_turn_include_0N = mx_turn + 1
if howmany_LR_or_RL % 2 == 0:
  if K <= mx_turn:
    happy += K * 2
  else: 
    happy += (mx_turn_include_0N - 2) * 2
    happy += 2

else: 
  if K <= mx_turn:
    happy += K * 2
  else: 
    happy += mx_turn * 2
    happy += 1

print(happy)

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

happy = 0

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

happy += K*2

if happy >= N:
  happy = N-1

print(happy)

Solution (Python):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()

lr = 0
rl = 0
cnt = 0
for i in range(N):
    if i >= 1:
        if S[i] == S[i - 1] == 'L':
            cnt += 1
        if S[i] == 'R' and S[i - 1] == 'L':
            lr += 1
        if S[i] == 'L' and S[i - 1] == 'R':
            rl += 1
    if i <= N - 2:
        if S[i] == S[i + 1] == 'R':
            cnt += 1

op = min(lr, rl, K)
print(cnt + op * 2 + min(max(lr, rl) - op, (K - op)))

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

Solution (Python):
n,k = map(int,input().split())
S = input()
S += '+'
L = []
cur = S[0]
cnt = 1
for i in range(1,n+1):
    if cur == S[i]:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
        cur = S[i]
if len(L)%2 == 0:
    if len(L)//2 <= k:
        print(n-1)
    else:
        ans = 0
        for i in range(2*k+1):
            ans += L[i]
        for i in range(2*k+1,len(L)):
            ans += L[i]-1
        print(ans-1)
else:
    if (len(L)-1)//2 <= k:
        print(n-1)
    else:
        ans = 0
        for i in range(2*k+1):
            ans += L[i]
        for i in range(2*k+1,len(L)):
            ans += L[i]-1
        print(ans-1)

Solution (Python):
while True:
    try:
        n, k = map(int, input().split())
        if n <= 2:
            print(n-1)
            continue
        lst = []
        ans = 0
        string = input()
        temp = string[0]
        count = 1
        for c in string[1:]:
            if c == temp:
                count += 1
            else:
                temp = c
                lst.append(count)
                ans += count-1
                count = 1
        lst.append(count)
        ans += count-1
        ans += min(k*2, len(lst)-1)
        print(ans)
    except:
        break

Solution (Python):
N, K = map(int, input().split())
S = str(input())
max = N-1
cnt = 0
if N == 1:
    print(0)
    exit()
    
if (S[0] == "R") & (S[1] == "R"):
    cnt += 1
if (S[N-1] =="L") & (S[N-2]=="L"):
    cnt += 1
for i in range(1, N-1):
    if S[i] == "L":
        if S[i-1] == "L":
            cnt += 1
    if S[i] == "R":
        if S[i+1] == "R":
            cnt += 1

if (cnt + K*2) > max:
    print(max)
else:
    print(cnt + K*2)

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

if len(s) == 1:
    print(0)
    sys.exit()
ans = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ans += 1
print(min(len(s)-1, ans+2*k))

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

temp = s[0]
flag = False
count = 0

for i in range(1,len(s)):
    this = s[i]
    if flag == False:
        if not(temp == this):
            flag = True
        else:
            count += 1
    else:
        if not(temp == this):
            if k>0:
                count += 2
                k -= 1
                flag = False
        else:
            count += 1
    
    
    temp = this
    
    
else:
    if flag == True and k>0:
        count += 1
            
print(count)

Solution (Python):
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''

gi = lambda : list(map(int,input().split()))
n, m = gi()
s = list(input())
ans = 0
for k in range(1, n):
	if s[k] == s[k - 1]:
		ans += 1
ss = [s[0]]
k = 1
l = [1, 0]
cur = 1
while k < n:
	while k < n and s[k] == s[k - 1]:
		k += 1
	if k == n:
		break;
	l[cur % 2] += 1
	cur += 1
	ss.append(s[k])
	k += 1
x, y = l
x *= 2
y *= 2
x -= 1
if ss[-1] == ss[0]:
	x -= 1
else:
	y -= 1
ans += min(min(x, y), m * 2)
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
b = []
shio = 1
for i in range(n):
  if i == n-1:
    b.append(shio)
    break
  if s[i] == s[i+1]:
    shio += 1
    continue
  else:
    b.append(shio)
    shio = 1
ans = 0
for i in b:
  if i >= 2:
    ans += i-1
if (len(b)//2) - k <=0:
  ans = len(s)-1
if (len(b)//2) - k >= 1:
  ans += (k*2)
print(ans)

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

def change():
    check = S[0]
    k = 1
    while S[k] == check:
        k += 1
    i = 0
    while S[k + i] != check:
        S[k + i] = check
        i += 1

group = 1
old = S[0]
happy_num = 0
for i in range(1, N):
    if old == S[i]:
        group += 1
        if i == N - 1:
            happy_num += group - 1
    else:
        happy_num += group - 1
        old = S[i]
        group = 1



old = S[0]
border = 0
for i in range(1, N):
    if old != S[i]:
        border += 1
        old = S[i]


changed = 2 * K
if changed < border:
    print(happy_num + 2 * K)
else:
    print(N - 1)

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

for i in range(1, len(S)):
  if SS[-1]!=S[i]:
    SS.append(S[i])

if len(SS)==1:
  print(N-1)
  exit()
  
if len(SS)==2:
  if K>0:
    print(N-1)
    exit()
  else:
    print(N-2)
    exit()
  
for i in range(1, len(SS)-1):
  if SS[i]!=SS[i-1] and SS[i]!=SS[i+1] and K>0:
    SS[i]=SS[i-1]
    K-=1
  
if SS[0]!=SS[1] and K>0:
  S[0]=S[1]
  K-=1
  
if SS[-1]!=SS[-2] and K>0:
  SS[-1]=SS[-2]
  K-=1
  
unhappy=0
if (SS[0]=='L') or (SS[0]=='R' and SS[1]=='L'):
  unhappy+=1
  
for i in range(1, len(SS)-1):
  if (SS[i]=='L' and SS[i-1]=='R') or (SS[i]=='R' and SS[i+1]=='L'):
    unhappy+=1
    
if (SS[-1]=='R') or (SS[-1]=='L' and SS[-2]=='R'):
  unhappy+=1
  
happy=N-unhappy
print(happy)

Solution (Python):
n, B = list(input().split())
n, B=int(n), int(B)
xs =input()
b = None
c = 1
r=0
for x in xs:
    if b==None:
        b=x
        continue
    if x == b:
        c += 1
        continue
    r+=(c-1)
    c = 1
    b=x
r+=(c-1)
print(min(r+(B*2), n-1))

Solution (Python):
N, K = list(map(int, input().split()))
S = [1 if x=='R' else -1 for x in input()]
cnt = 0
rule = S[0]
f = False
for i in range(1, N):
    if S[i] != rule:
        f = True
        S[i] *= -1
    else:
        if f:
            f = False
            cnt += 1
    if cnt >= K:
        break

S.insert(0, 1)
S.append(-1)
ans = 0
for i in range(N):
    x = S[i+1]
    y = S[i+1+x]
    if x == y:
        ans += 1

print(ans)

Solution (Python):
from collections import deque


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

tmp = deque()
cnt = 1
s += '-'
for i in range(n):
    if s[i] == s[i + 1]:
        cnt += 1
    else:
        tmp.append(cnt)
        cnt = 1

for i in range(k):
    if len(tmp) == 1:
        break
    elif len(tmp) == 2:
        tmp = [tmp[0] + tmp[1]]
    else:
        hoge = tmp[0] + tmp[1] + tmp[2]
        tmp.popleft()
        tmp.popleft()
        tmp.popleft()
        tmp.appendleft(hoge)

if len(tmp) == 1:
    print(tmp[0] - 1)
else:
    total = 0
    for j in tmp:
        if j > 1:
            total += j - 1
    print(total)

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

l = -1
r = -1

k = 0
while k < K:
    for i in range(1, N):
        
        if S[i] != S[i-1]:
            if l < 0:
                l = i
            else:
                
                r = i
                for s in range(l, r):
                    if S[s] == "L":
                        S[s] = "R"
                    else:
                        S[s] = "L"
                l = -1
                r = -1
                k += 1
                if k == K:
                    break
    l = -1
    r = -1
    
    
    
    
    
    
    
    
    
    
    if l < 0:
        break
ans = 0

if len(set(S)) == 2 and k < K:
    ans += 1
for i in range(1, N):
    if S[i] == S[i-1]:
        ans += 1

print(ans)

Solution (Python):
N,K = map(int,input().split())
S = input()
fortune = 0
nums = 0
count = 0
for i in range(N):
    if i != N-1 and S[i] == S[i+1]:
        count += 1
    else:
        fortune += count
        count = 0
for i in range(N):
    if i != N-1 and S[i] != S[i+1]:
        nums +=1
if nums%2 ==0 and nums//2 <= K:
    print(fortune + nums)
elif nums%2 == 0 and K <= nums//2:
    print(fortune + K*2)
elif nums%2 ==1 and (nums-1)//2+1 <= K:
    print(fortune + nums)
elif nums%2 ==1 and K <= (nums-1)//2+1:
    print(fortune + K*2)

Solution (Python):
N ,K =map(int,input().split())
S ="R" +input() +"L"
coun = 0
foo = 0
for i in range(N+1):
    if S[i:i+2] =="RL":
        coun += 1
        if i==0 or i==N:
            foo  +=1
if K >= coun:
    print(N-1)
elif coun -K <= foo:
    print(N-(coun-K))
else:
    print(N-2*(coun-K) +foo)

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

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

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

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

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

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

leftflag = 0
if s[0] == "L":
  leftflag = 1
  
rightflag = 0
if s[-1] == "R":
  rightflag = 1
    
rlnum = 0

for i in range(len(s)-1):
  if s[i] == "R" and s[i+1] == "L":
    rlnum += 1

rlnum_final = rlnum
rightflag_final = rightflag
leftflag_final = leftflag
    
if k <= rlnum - 1:
  rlnum_final = rlnum - k
  
if k == rlnum :
  rlnum_final = 0
  if rightflag == 0 and leftflag == 0:
    rightflag_final = 1
    
if k >= rlnum + 1:
  rlnum_final = 0
  rightflag_final = 1
  leftflag_final = 0

print(n - (2*rlnum_final + rightflag_final + leftflag_final))

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

sn = [1]
for i in range(n-1):
  if S[i] == S[i+1]:
    sn[-1] += 1
  else:
    sn.append(1)
    
for i in range(k):
  temp = 0
  if len(sn) > 2:
    for j in range(3):
      temp += sn.pop()
    sn.append(temp)
  elif len(sn) == 2:
    sn[0] += sn.pop()
  else:
    break
    
ans = sum(sn)-len(sn)
print(ans)

Solution (Python):
import re

n,k= map(int,(input().split()))
b= input()
LRn=len(re.findall('LR',b))
RLn=len(re.findall('RL',b))

if min(LRn,RLn)>=k:
    ans=k*2
elif min(LRn,RLn)<=k and max(LRn,RLn)>=k:
    ans=k+min(LRn,RLn)
else:
    ans=LRn+RLn

a= list(b)
for i in range(len(a)-1):
    if a[i]==a[i+1]:
        ans +=1

print(ans)

Solution (Python):
import sys
inputs = sys.stdin.readlines()

N, K = map(int, inputs[0].split())
S = inputs[1].strip()

from collections import deque

count = 1
A = deque()
for i in range(len(S)):
  try:
    if S[i] == S[i+1]:
      count += 1
    else:
      A.append(count)
      count = 1
  except:
    A.append(count)

for _ in range(K):
  if len(A) >= 3:
    A.append(A.pop() + A.pop() + A.pop())
  elif len(A) == 2:
    A.append(A.pop() + A.pop())
  else:
    break

print(sum(A)-len(A))

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

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

while count > 0 and K > 0:
    if count == 1:
        count -= 1
        ans += 1
        K -= 1
    else:
        K -= 1
        count -= 2
        ans += 2

print(ans)

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

cnt = 0
s = S[0]
flag = False
for i in range(n):
    if flag and S[i] == s:
        cnt += 1
        flag = False
    if S[i] != s:
        S[i] = s
        flag = True
    if cnt == k:
        break

ans = 0
for i in range(n-1):
    if S[i] == S[i+1]:
        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
tmp=0
m=0
omake=0
for i in range(n-1):
    if s[i]!=s[i+1]:
        tmp+=1
        if tmp%2==0:
            m+=1
        elif i==n-2:
            omake+=1
    elif tmp>0 and i==n-2 and tmp%2==1:
        omake+=1

if m<k:
    print(ans+2*min(m,k)+omake)
else:
    print(ans+2*min(m,k))

Solution (Python):
N,K = list(map(int,input().split()))
S = input()
Now = S[0]
switch = 0
happiness = 0
for i in range(N-1):
  if(S[i+1] == Now):
    happiness += 1
  if(S[i+1] != Now):
    Now = S[i+1]
    switch += 1
number = 0
while((switch > 1) and (number < K)):
  number += 1
  happiness += 2
  switch -= 2
if((switch == 1) and (number < K)):
  happiness += 1
print(happiness)

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

base = 0
odd = 0

pre_char = 'N'
for index, char in enumerate(s):
    if index > 0:
        if pre_char == char:
            base += 1
        else:
            odd += 1
    pre_char = char








output = base + 2 * k
if output > len(s) - 1:
    output = len(s) - 1

print(output)

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
if k*2 <= n-1-happy:
    print(happy + k * 2)
else:
    print(n-1)

Solution (Python):
N , K  = map(int,input().split())
S = [0 if s == "L" else 1 for s in input()]
def main():
    rep = 1
    pre = S[0]
    for s in S[1:]:
        if s != pre:
            rep += 1
        pre = s
    print(N - (rep - 2*K) if rep - 2*K > 0 else N - 1)
    return
main()

Solution (Python):
import sys
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':ct+=1
if ct<=k-1:
  print(n-1)
  sys.exit()
ans=n-ct*2
ans+=2*k
if s[0]=='L':
  ans-=1
if s[n-1]=='R':
  ans-=1
print(ans)

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

def search(s):
    ret = 0
    for i in range(len(s)):
        if s[i] == 'R' and i != n - 1:
            if s[i + 1] == 'R':
                ret += 1
        if s[i] == 'L' and i != 0:
            if s[i - 1] == 'L':
                ret += 1
    return ret

ans = search(s)
print(min(ans + 2 * k, len(s) - 1))

Solution (Python):
from sys import setrecursionlimit as SRL, stdin

SRL(10 ** 7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())

n, k = rrd()

s = list(map(str, rd().strip()))

cnt = 0
pre = 'i'
for x in s:
    if x != pre:
        cnt += 1
    pre = x

print(n - max(1, cnt - 2 * k))

Solution (Python):
from itertools import groupby

n, k = map(int, input().split())
print(n - max(1, sum(1 for _ in groupby(input())) - 2 * k))

Solution (Python):
import math
import copy


n, k = [int(i) for i in input().split()]





s = input()




kotae = 0

for i in range(n) :
    if (i == 0) :
        if (n == 1) :
            break
        if (s[i] == "R" and s[i+1] =="R") :
            kotae += 1
    elif (i == n-1) :
        if(s[i] == "L" and s[i-1] == "L") :
            kotae += 1
    else :
        if (s[i] == "L" and s[i - 1] == "L") :
            kotae += 1
        elif (s[i] == "R" and s[i+1] == "R") :
            kotae += 1

j = 0



for i in range(1, n) :
    if (n == 1) :
        break
    if (s[i] != s[i-1]) :
        j += 1

kotae += min(2*k,j)


print(kotae)

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
s = input()
chg = 0
for i in range(n-1):
    if s[i] != s[i+1]:
        chg += 1
if chg <= 2*k:
    print(n-1)
else:
    chg -= 2*k
    print(n-1-chg)

Solution (Python):


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

for i in range(n):
    if s[i] == "L" and i == 0:
        kfk.append(0)
    elif s[i] == "L" and i != 0:
        if s[i-1] == "L":
            kfk.append(1)
        else:
            kfk.append(0)
    elif s[i] == "R" and i == (n-1):
        kfk.append(0)
    elif s[i] == "R" and i != (n-1):
        if s[i+1] == "R":
            kfk.append(1)
        else:
            kfk.append(0)

ans = sum(kfk) + (2 * k)
if ans < (n-1):
    print(ans)
else:
    print(n-1)

Solution (Python):
N , K = [int(i) for i in input().split()]
S = input()

S = 'R' + S + 'L'

a = 0

for i in range(N+1):
    if S[i] == 'R' and S[i+1] == 'L':
        a += 2

if S[1] == 'L':
    a -= 1

if S[-2] == 'R':
    a -= 1

ans = N - a + (2*K)

if ans > N-1:
    ans = N-1
    
print(ans)

Solution (Python):
from itertools import permutations

def score(S):
    S0 = [' '] + S + [' ']
    score = 0
    s = [0]*len(S0)
    for i in range(1, len(S)+1):
        c = S0[i]
        if c == 'L':
            s[i] += 1 if S0[i-1] == 'L' else 0
        else:
            s[i] += 1 if S0[i+1] == 'R' else 0
    return sum(s)

def main2():
    N, K = map(int, input().split())
    S = list(input().strip())
    m = score(S)
    
    print(min(m + 2 * K, N-1))

def main():
    N, K = map(int, input().split())
    S = list(input().strip())
    print(S, score(S), "initial")
    m = score(S)
    for ps in permutations([(i, j) for i in range(N) for j in range(i, N)], K):
        S_ = list(S)
        for p in ps:
            apply(S_, p)
        s = score(S_) 
        if m < s:
            mp = ps
            m = s
    S_ = list(S)
    for p in mp:
        apply(S_, p)
        print(S_, score(S_), p)
    print(m, mp)

def apply(S, lr):
    l, r = lr
    for i in range(r-l+1):
        if l+i > r-i:
            break
        S[l+i], S[r-i] = S[r-i], S[l+i]
        S[l+i] = 'R' if S[l+i] == 'L' else 'L'
        if l+i != r-i:
            S[r-i] = 'R' if S[r-i] == 'L' else 'L'

if 1==0:
    S = list("LLLLL")
    apply(S, (0, 4))
    print(S)

main2()

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

s = s+"a" 

lst = [i for i in s]

ans = 0
memo1 = 0
memo2 = 0
for i in range(n):
    if lst[i] == lst[i+1]:
        ans += 1
    elif lst[i] == "L" and lst[i+1] == "R":
        memo1 += 1
    elif lst[i] == "R" and lst[i+1] == "L":
        memo2 += 1
        
ans += 2*min(k,memo1,memo2)
num = min(k,memo1,memo2)

jdg = False
jdg2 = False

if memo1 >= num+1 and lst[n-1] == "R" and num < k:
    ans += 1
    jdg = True
if jdg:
    if memo2 >= num+2 and lst[0] == "L" and num < k+1:
        ans += 1
        jdg2 = True
else:
    if memo2 >= num+1 and lst[0] == "L" and num < k:
        ans += 1
        jdg2 = True
        
if not(jdg):
    if memo2 >= num+1 and lst[n-1] == "L" and num < k:
        ans += 1
        jdg = True
    if jdg:
        if memo1 >= num+2 and lst[0] == "R" and num < k+1:
            ans += 1
            jdg2 = True
    else:
        if memo1 >= num+1 and lst[0] == "R" and num < k:
            ans += 1
            jdg2 = True
    
elif not(jdg2):
    if memo2 >= num+1 and lst[n-1] == "L" and num < k:
        ans += 1
        jdg = True
    if jdg:
        if memo1 >= num+2 and lst[0] == "R" and num < k+1:
            ans += 1
            jdg2 = True
    else:
        if memo1 >= num+1 and lst[0] == "R" and num < k:
            ans += 1
            jdg2 = True
    
    

print(ans)

Solution (Python):


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

happiness = 0
unhappiness = 0

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

if S[0] == 'R':
    if S[0] == S[1]:
        happiness += 1
    else:
        unhappiness += 1
else:
    unhappiness += 1


for i in range(1, N - 1):
    c = S[i]
    if c == 'R':
        nex = S[i + 1]
    elif c == 'L':
        nex = S[i - 1]

    if c == nex:
        happiness += 1
    else:
        unhappiness += 1
    

if S[-1] == 'L':
    if S[-1] == S[-2]:
        happiness += 1
    else:
        unhappiness += 1
else:
    unhappiness += 1




moves = min(unhappiness // 2, K)
plus_happiness = unhappiness % 2 if K - moves > 0 else 0
ans = 2 * moves + happiness + plus_happiness

print(min(ans, N-1))

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

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

if c == 1:
    print(n-1)
    exit()


t = -1
left = s[0]
q = -1
for i in range(1,n):
    if s[i] != left:
        t += 1
        left = s[i]
    if t == 1 and k > 0:
        k -= 1
        ans += 2
        t = -1

if t == 0 and k > 0:
    ans += 1
print(ans)

Solution (Python):


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

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

    x = (N-1-t) + min(t//2, K)*2

    if t%2 == 1 and t//2 < K:
        x += 1

    print(x)



if __name__ == '__main__':
    main()

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

LR = S.count("LR")
RL = S.count("RL")
RRLL = N-1-LR-RL

if min(LR,RL) != 0: 
    if K <= min(LR,RL):
        print(RRLL+K*2)       
    if K > min(LR,RL):
        if min(LR,RL) != max(LR,RL):
            print(RRLL+min(LR,RL)*2+1)
        if min(LR,RL) == max(LR,RL):
            print(RRLL+min(LR,RL)*2)
if min (LR,RL) == 0:
    if max(LR,RL) !=0:
        print(RRLL+1)
    if max(LR,RL) ==0:
        print(RRLL)

Solution (Python):
N,K = map(int,input().split())
S = input()
switch = 0
ans = 0
for i in range(1,len(S)):
    if S[i] != S[i-1]:
        switch += 1
    else:
        ans += 1
for i in range(K):
    if switch == 0:
        break
    elif switch == 1:
        switch -= 1
        ans += 1
    else:
        switch -= 2
        ans += 2
print(ans)

Solution (Python):
from collections import deque
import sys
n,k=map(int,input().split())
s=input()
buf=s[0]
a = deque([]) 
count=0
ans=0
for i in range(n):
    if buf==s[i]:
        count+=1
    else:
        a.append(count)
        count=1
        buf=s[i]
    if i==n-1:
        a.append(count)
l=len(a)
c=0
for i in range(l):
    ans+=a[i]-1
for i in range(l//2):
    if c==k:
        break
    if 2*i+1!=l-1:
        ans+=2
        c+=1

if c<k:
    ans+=k-c
    ans=min(n-1,ans)
print(ans)

Solution (Python):
def mapping(string):
    return 1 if string == "L" else -1


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

flips = []
for i in range(n - 1):
    if s[i] != s[i + 1]:
        flips.append(i + 1)

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

Solution (Python):

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

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


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




rcheck = 0
lcheck = 0
if S[0] == 'R':
    rcheck += 1
else:
    lcheck += 1
if S[-1] == 'R':
    rcheck += 1
else:
    lcheck += 1

ans = saisyo


if nr - K >= 2:
    ans += 2*K
elif nr - K == 1:
    if rcheck == 0:
        ans += 2*K
    elif rcheck >= 1:
        ans += 2*K - 1
elif nr - K == 0:
    if rcheck == 0:
        ans += 2*K
    elif rcheck >= 1:
        ans += 2*K - 1
    elif rcheck == 2:
        ans += 2*K - 2
else:
    ans += 2*nr - rcheck

rans = ans 
ans = saisyo


if nl - K >= 2:
    ans += 2*K
elif nl - K == 1:
    if lcheck == 0:
        ans += 2*K
    elif lcheck >= 1:
        ans += 2*K 
    elif lcheck >= 1:
        ans += 2*K - 1
elif nl - K == 0:
    if lcheck == 0:
        ans += 2*K
    elif lcheck >= 1:
        ans += 2*K - 1
    elif lcheck == 2:
        ans += 2*K - 2
else:
    ans += 2*nl - lcheck

lans = ans

ans = max(rans, lans)
print(ans)

Solution (Python):
a = list(map(int,input().split()))
s = list(input())
kou = 0
LR = 0
RL = 0
for (i,j) in zip(s[:-1],s[1:]):
    if i == j:
        kou += 1
    elif i == 'L' and j == 'R':
        LR += 1
    else:
        RL += 1
kou += min(2*a[1],LR+RL)
print(kou)