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

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

cnt.append(tmp)
bn = 1 + 2 * K
if len(cnt) <= bn:
    print(N - 1)
    exit(0)
ans = sum(cnt) - (len(cnt) - bn + 1)
print(ans)

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

def count_diff(a,b):
    cnt = 0
    for c1,c2 in zip(a, b):
        if c1 != c2:
            cnt += 1
    return cnt

s2 = s[1:]
cnt = count_diff(s[:-1], s2)
init = n - cnt - 1

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

print(ans)

Solution (Python):
from itertools import*
n,k=map(int,input().split())
l=sorted([len(list(g)) for k,g in groupby(input())])[::-1]
t=min(2*k,len(l))
a=sum(l[:t])+sum(x-1 for x in l[t:])
print(a-(a==n))

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

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

Solution (Python):
from itertools import groupby
n,k=map(int,input().split())
l=sorted([len(list(g)) for k,g in groupby(input())])[::-1]
ans=sum(l[:min(2*k,len(l))])+sum(x-1 for x in l[min(2*k,len(l)):])
print(ans-(ans==n))

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

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

cur = S[0]
A = []
st = 0
tmp = is_ok[0]
for i in range(1, N):
    if S[i] == cur:
        tmp += is_ok[i]
    else:
        A.append((tmp, st, i, cur))
        st = i
        tmp = is_ok[i]
        cur = S[i]
A.append((tmp, st, N, cur))

LR = {'L': [], 'R': []}
for cnt, st, end, d in A:
    if st == 0 and end == N:
        pass
    elif st == 0 or end == N:
        LR[d].append(1)
    else:
        LR[d].append(2)

LR['L'].sort(reverse=True)
LR['R'].sort(reverse=True)
ans = sum(is_ok)





ans += max(sum(LR['L'][:K]), sum(LR['R'][:K]))
print(ans)

Solution (Python):
from itertools import groupby, accumulate

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

L = [len(tuple(v)) for k, v in groupby(S)]

A = [0] + list(accumulate(L))
M = len(A)

if M <= 2 * K + 1:
    print(max(0, A[-1] - A[0] - 1))
    quit()

D = 2 * K + 1
ans = 0
for i in range(M - D):
    l = max(0, A[i] - A[0] - i)
    c = max(0, A[i + D] - A[i] - 1)
    r = max(0, A[-1] - A[i + D] - (M - 1 - i - D))

    ans = max(ans, l + c + r)

print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
cnt1 = 0
cnt2 = 0
if len(S) == 1:
    print(0)
    exit()
p = 0
if 'L' == S[0]:
    for i in range(N):
        if 'L' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            break
else:
    for i in range(N):
        if 'R' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            break
if p == N:
    ind = N-1
ans = [1]
for i in range(ind, N):
    if S[i] == 'L':
        if cnt2:
            ans.append(2)
            cnt2 = 0
        cnt1 += 1
    else:
        if cnt1:
            ans.append(2)
            cnt1 = 0
        cnt2 += 1
if cnt1:
    ans.append(1)
else:
    ans.append(1)

ans.sort()
ans.reverse()

sums = 0
for i in range(N-1):
    if 'LL' == S[i:i+2]:
        sums += 1
    elif 'RR' == S[i:i+2]:
        sums += 1

if K >= len(ans):
    print(len(S) - 1)
else:
    if len(ans) == 0:
        print(len(S) - 1)
    if sums + sum(ans[:K]) >= len(S):
        print(len(S) - 1)
    else:
        print(sums + sum(ans[:K]))

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

res = 0
pre = None
add = {2: 0}
add[1] = 0 if S[0] == S[-1] else 1
for s in S:
    if pre is None:
        pass
    elif pre == s:
        res += 1
    elif pre == "R":
        add[2] += 1

    pre = s

if add[2] > K:
    res += 2 * K
    print(res)
else:
    res += 2 * add[2]
    K -= add[2]
    if add[1] == 1 and S[-1] == "L":
        res -= 2
    if K > 0:
        res += add[1]
    print(res)

Solution (Python):

def count(S):
    ans = 0
    for i in range(len(S)):
        if S[i] == "L" and i != 0 and S[i - 1] == "L":
            ans += 1
        if S[i] == "R" and i != len(S) - 1 and S[i + 1] == "R":
            ans += 1

    return ans


def count2(S):
    ans = 0
    for i in range(len(S) - 1):
        if S[i] != S[i + 1]:
            ans += 1

    return ans


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

first = count(S)
rest = count2(S)

ans = first + min(rest, K * 2)

print(ans)

Solution (Python):
def count_happiness(S):
    happiness = 0
    for n in range(N):
        if(S[n] == "L" and n > 0 and S[n] == S[n-1]):
            happiness += 1
        elif(S[n] == "R" and n < N -1 and S[n] == S[n+1]):
            happiness += 1
    return happiness

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

LR_RL_unhappy_pair = 0
LR_unhappy = 0
RL_unhappy = 0
for n in range(N-1):
    if(S[n] == "R" and S[n+1] == "L"):
        RL_unhappy += 1
    if(S[n] == "L" and S[n+1] == "R"):
        LR_unhappy += 1

change_pair_count  = min(K, min(RL_unhappy, LR_unhappy))
change_single_count = 0
if(K > change_pair_count):
    remain = K - change_pair_count
    changable = abs(RL_unhappy  - LR_unhappy)
    change_single_count = min(remain, changable) 
print(count_happiness(S) + 2 * change_pair_count + change_single_count)

Solution (Python):
n,k = map(int,input().split())
S = input()+'K'
S_c = []
cnt = 0
for i in range(n):
  if S[i+1]==S[i]:
    cnt += 1
  else:
    cnt += 1
    S_c.append(cnt)
    cnt = 0
    

if 2*k+1 <= len(S_c):
  ans = sum(S_c[:2*k+1])-1

  for i in range(2*k+1,len(S_c)):
    ans += S_c[i]-1
  print(ans)
else:
  print(sum(S_c)-1)

Solution (Python):

import sys;
import string;
sys.setrecursionlimit(20000) 

import copy
import math
import itertools

from functools import lru_cache


PPP = 1000000007












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























s = input()









import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def ton(n, k, x):
  s = ""
  while n > 0:
    s = str(n%k) + s
    n //= k
  return s





def getEnergy():
  for y in b:
    yield y

def doit():
  total = 0
  prev = " "
  for x in list(s):
    if prev == x:
      total += 1
    prev = x
  total += k*2
  if total >= n-1:
    return n-1
  return total

def yn():
  if doit():
    return "Yes"
  else:
    return "No"




try:
  
  print(doit())
  
except Exception as e:
  print("e", e, file=sys.stderr)

Solution (Python):
import math

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

happiness = 0
unh = 0

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

if K < math.ceil(unh/2):
    happiness += 2*K
else:
    happiness = N-1

print(happiness)

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

c_lr = 0
c_rl = 0

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

ans = N - 1 - c_lr - c_rl

for i in range(0, K):
    if c_lr > 0 and c_rl > 0:
        c_rl -= 1
        c_lr -= 1
        ans += 2
    elif c_lr > 0:
        c_lr -= 1
        ans += 1
    elif c_rl > 0:
        c_rl -= 1
        ans += 1

print(ans)

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

d = []
p = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        p += 1
        
    else:
        d.append(p)
        p = 0
d.append(p)
res = sum(d)

n = len(d)


for i in range(k):
    if n >= 3:
        res += 2
        n -= 2
    elif n == 2:
        res += 1
        n -= 1
    else:
        break
print(res)

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

Solution (Python):
n,k=map(int, input().split())
s=list(input())
lrcnt=0
rlcnt=0
lrch=0
rlch=0
ans=0

for i in range(len(s)-1):
  if s[i]=='L' and s[i+1]=='R':
    lrcnt+=2
  if s[i]=='L' and s[i+1]=='L':
    ans+=1
  if s[i]=='R' and s[i+1]=='L':
    rlcnt+=2
  if s[i]=='R' and s[i+1]=='R':
    ans+=1
if s[0]=='R':
  lrch+=1
else:
  rlch+=1
if s[-1]=='L':
  lrch+=1
else:
  rlch+=1

if min(lrcnt,rlcnt)>=k*2:
  ans+=k*2
else:
  tmp=min(lrcnt,rlcnt)
  ans+=tmp
  k-=tmp//2
  lrcnt-=tmp
  rlcnt-=tmp

  if k>=2:
    if lrcnt>0 and rlch>0:
      ans+=1
    if rlcnt>0 and lrch>0:
      ans+=1
  else:
    if lrcnt>0 and rlch>0:
      ans+=1
    else:
      if rlcnt>0 and lrch>0:
        ans+=1
  
print(ans)

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


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

if cut>2*K:
    print(N-(cut-2*K))
else:
    print(N-1)

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 sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

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

cur = S[0]
A = []
st = 0
tmp = is_ok[0]
for i in range(1, N):
    if S[i] == cur:
        tmp += is_ok[i]
    else:
        A.append((tmp, st, i, cur))
        st = i
        tmp = is_ok[i]
        cur = S[i]
A.append((tmp, st, N, cur))

LR = {'L': [], 'R': []}
for i in range(len(A)):
    cnt, st, end, d = A[i]
    if st == 0 or end == N:
        LR[d].append(1)
    else:
        LR[d].append(2)

LR['L'].sort(reverse=True)
LR['R'].sort(reverse=True)
ans = sum(is_ok)

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

ans += max(sum(LR['L'][:K]), sum(LR['R'][:K]))
print(ans)

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

Solution (Python):
def solve():
  import sys
  import math

  sys.setrecursionlimit(10**7)
  readline=sys.stdin.readline

  n,k = map(int,input().split())
  s = input()
  
  from itertools import groupby
  
  ans = 0
  for _,g in groupby(list(s)):
    l = len(list(g))
    if l > 1:
      ans += l-1

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

if __name__ == '__main__':
  solve()

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

cnt = 0

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



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

print(result)

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

result = 0
unmatch_count = 0

pre = ""

init_flag = 1
for LR in S:
    if init_flag == 1:
        pre = LR
        init_flag = 0
        continue
    if pre == LR:
        result += 1
    pre = LR

unmatch_count = n - result - 1
if k*2 < unmatch_count:
    result += k*2
else:
    result += unmatch_count
print(result)

Solution (Python):
from itertools import groupby

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

Solution (Python):
N,K = map(int,input().split())
S = input()
sum = 0
dis = 0
for i in range(N-1):
    if S[i]== S[i+1]:
        sum+=1
    else:
        dis+=1
l = (dis+1)//2
if (S[0]!=S[-1]) and (l<=K):
    sum-=1
print(sum+2*min(K,l))

Solution (Python):
n, k =map(int, input().split()) 
s = input()
count = 0
even =0
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
    else:
        even +=1

for i in range(k):
        if even >= 2:
            count +=2
            even -= 2
        elif even ==1:
            count+=1
            even -=2
print(str(count))

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

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

p, q = 0, 0
p_list, q_list = [], []
for i in range(n):
    if s[i] == mode:
        p += 1
        if i == n-1:
            p_list.append(p)
        if q != 0:
            q_list.append(q)
        q = 0
    else:
        q += 1
        if i == n-1:
            q_list.append(q)
        if p != 0:
            p_list.append(p)
        p = 0

ans_list = [sum(p_list[:k])+ sum(q_list[:k+1])] + p_list[k:] + q_list[k+1:]

ans = 0
for i in ans_list:
    ans += i-1
print(ans)

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

Solution (Python):



















def solve(N, K, S):
    ans = 0
    last = S[0]
    lr = 0
    rl = 0
    for s in S[1:]:
        if last != s:
            if last == 'L':
                lr += 1
            else:
                rl += 1
        else:
            ans += 1
        last = s

    n = min(lr, rl)
    if n >= K:
        ans += K*2
    else:
        ans += n*2
        if lr != rl:
            ans += 1

    return ans


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

    ans = solve(N, K, S)

    print(ans)

Solution (Python):
from itertools import groupby

n, k = map(int, input().split())
print(n - max(1, len(list(groupby(input()))) - 2 * k))

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

idx = 0
dirs = []
while idx < N:
    start = idx
    dirs.append([0, 1][S[idx] == "R"])
    while idx < N and S[idx] == S[start]:
        idx += 1
    intervals.append(idx - start)

assert(sum(intervals) == N)

def calc_ans(result_dirs):
    ans = N
    if result_dirs[0] == 0:
        ans -= 1
    for i in range(len(result_dirs)-1):
        now_dir, next_dir = result_dirs[i], result_dirs[i+1]
        if now_dir == 1 and next_dir == 0:
            ans -= 2
    if result_dirs[-1] == 1:
        ans -= 1
    return ans


rest = K
result_dirs = [dirs[0]]
for now_dir in dirs[1:]:
    if rest > 0 and now_dir == 1:
        result_dirs.append(0)
        rest -= 1
    else:
        result_dirs.append(now_dir)
if rest > 0 and dirs[0] == 1:
    result_dirs[0] = 0
left_ans = calc_ans(result_dirs)

rest = K
result_dirs = [dirs[0]]
for now_dir in dirs[1:]:
    if rest > 0 and now_dir == 0:
        result_dirs.append(1)
        rest -= 1
    else:
        result_dirs.append(now_dir)
if rest > 0 and dirs[0] == 0:
    result_dirs[0] = 1
right_ans = calc_ans(result_dirs)
print(max(left_ans, right_ans))

Solution (Python):
n, k = map(int, input().split())
s = list(input().strip())
count = 0
for i in range(len(s)):
    if (i == 0 and s[i] == 'L') or (i == len(s) - 1 and s[i] == 'R'):
        pass
    elif (s[i] == 'R' and s[i + 1] == 'R') or (s[i] == 'L' and s[i - 1] == 'L'):
        count += 1

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

print(count)

Solution (Python):
n, k = [int(h) for h in input().split()]
s_s = list(input())

count = 0
for i in range(n):
  d = s_s[i]
  if d == 'L':
    if i > 0 and s_s[i - 1] == d:
      count += 1
  else:
    if i < n - 1 and s_s[i + 1] == d:
      count += 1

count += 2 * k

ans = min(n - 1, count)
      
print(ans)

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

cnt = 0
for i in range(len(lr)-1):
    if lr[i] == "R":
        if lr[i+1] == "R":
            cnt+=1

for i in range(len(lr)-1,0,-1):

    if lr[i] == "L":
        if lr[i-1]=="L":
            cnt+= 1
            
cnt += 2*K
print(min(N-1,cnt))

Solution (Python):
def run_length_compress(S):
  l = []
  left = 0
  for right in range(1,len(S)):
    if right != len(S) - 1:
      if S[right] != S[right - 1]:
        l.append([S[right - 1], right - left])
        left = right
      else:
        pass
    else:
      if S[right] != S[right - 1]:
        l.append([S[right - 1], right - left])
        l.append([S[right], 1])
      else:
        l.append([S[right], right - left + 1])
  return l

def main():
  n,k = map(int,input().split())
  s = input()
  l = run_length_compress(s)
  if len(l) > 1:
    count = 0
    for i in range(1, len(l), 2):
      l[i][0] = l[i-1][0]
      count += 1
      if count >= k: break
  t = ''
  for j in range(len(l)):
    t += l[j][0]*l[j][1]
  
  res = 0
  for k in range(1,n):
    if t[k-1] == t[k]:
      res += 1
  print(res)
  
if __name__ == "__main__":
  main()

Solution (Python):
N,K=map(int,input().split())
S=input()
initial=S[0]
S01=[]
if initial=='L':
    for s in S:
        if s=='L':
            S01.append(0)
        else:
            S01.append(1)
else:
    for s in S:
        if s=='R':
            S01.append(0)
        else:
            S01.append(1)
            
count=0
T01=[0]
flag=0
for n in range(N-1):
    left=S01[n]
    right=S01[n+1]
    if left==0 and right==1:
        flag=1
    if left==1 and right==0:
        flag=0
        count+=1
    
    if flag==0:
        T01.append(0)
    elif count<K:
        T01.append(0)
    else:
        T01.append(1)
    
            
point=0
for n in range(N-1):
    left=T01[n]
    right=T01[n+1]
    if left==right:
        point+=1

print(point)

Solution (Python):
n,k=map(int,input().split())
s="
ans=0
for i in range(1,n+1):
    if s[i]=="R" and s[i+1]=="R":
        ans+=1
    if s[i]=="L" and s[i-1]=="L":
        ans+=1
a=max(s.count("LR"),s.count("RL"))
if k>=a:
    ans=n-1
else:
    ans+=2*k
print(ans)

Solution (Python):
n,k=map(int,input().split())
s= input()
l=list(s)
li=s.split("R")
t=0
for i in li:
    if len(i)>=1:
        t+=1
if l[0]=="R" and l[n-1]=="R":
    t=t*2
elif l[0]=="L" and l[n-1]=="R":
    t=t*2-1
elif l[0]=="R" and l[n-1]=="L":
    t=t*2-1
else:
    t=t*2-2
ans=n-t-1
ans=min(ans+2*k,n-1)
print(ans)

Solution (Python):
import sys
import copy

def main():

	input = sys.stdin.readline

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

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

	cmin = min(LRcount,RLcount)
	if LRcount!=RLcount:
		ne = 1
	else:
		ne = 0

		
	if K<=cmin:
		count = count+K*2
	else:
		count = count+cmin*2
		if K>cmin:
			count = count+ne

	print(count)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  
def convert(ar, st, ed):
    for i in range(st, st + (ed-st+1)//2):
        ar[i], ar[ed-i+st] = not ar[ed-(i-st)], not ar[i]
    if (ed-st+1) % 2 == 1:
        ar[st+(ed-st+1)//2] = not ar[st+(ed-st+1)//2]

n, k = na()
ar = [ True if x == 'R' else False for x in ns()]
st = 1 
ed = 2

while ed < n-1 and k > 0:
    while st < n and not (ar[st] == False and (st == 0 or ar[st-1] != ar[st])):  
        st += 1
    
    ed = st+1
    while ed < n-1 and not(ar[ed] == False and (ed == n-1 or ar[ed+1] != ar[ed])):
        ed += 1
    if ed >= n-1:
        break
    
    
    convert(ar, st, ed)
    k-=1
    st = ed+2
    ed = st+1

st = 1
ed = 2
while k > 0 and ed < n-1:
    while st < n and not (ar[st] == True and (st == 0 or ar[st-1] != ar[st])):  
        st += 1
    
    ed = st+1
    while ed < n-1 and not(ar[ed] == True and (ed == n-1 or ar[ed+1] != ar[ed])):
        ed += 1
    if ed >= n-1:
        break
    
    
    convert(ar, st, ed)
    k -= 1
    st = ed+2
    ed = st+1

st = 0
ed = 1
while ed < n and k > 0:
    while st < n and not (ar[st] == False and (st == 0 or ar[st-1] != ar[st])):  
        st += 1
    
    ed = st+1
    while ed < n and not(ar[ed] == False and (ed == n-1 or ar[ed+1] != ar[ed])):
        ed += 1
    if ed >= n:
        break
    
    
    convert(ar, st, ed)
    k-=1
    st = ed+2
    ed = st+1

st = 0
ed = 1
while k > 0 and ed < n:
    while st < n and not (ar[st] == True and (st == 0 or ar[st-1] != ar[st])):  
        st += 1
    
    ed = st+1
    while ed < n and not(ar[ed] == True and (ed == n-1 or ar[ed+1] != ar[ed])):
        ed += 1
    if ed >= n:
        break
    
    
    convert(ar, st, ed)
    k -= 1
    st = ed+2
    ed = st+1


ans = 0
st = 0
cur = 0
for i in range(n):
    if ar[st] == ar[i]:
        cur += 1
    else:
        ans += cur-1
        cur = 1
        st = i
ans += cur-1 
print(ans)

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

count=sum(i==0 or S[i-1]!=S[i] for i in range(N))
print(N-max(count-2*K,1))

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

print(n - group_num)

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

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

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

ans = 0
flag = 0

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

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

if k > flag//2:
    ans = n-1
elif flag > 1:
    ans += 2*k
else:
    ans += 1

print(ans)

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

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

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

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

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

if cnt <= k*2:
    ans = n-1
else:
    ans = n-1-(cnt-k*2)

print(ans)

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

ans = 0
count = k
ans = 0
for i in range(1,n-1):
    if s[i+1] in s[:i] and s[i] != s[i+1] and count > 0:
        count -=1
        j = i
        while s[j] != s[i+1]:
            if s[j] == 'R':
                s = s[:j] + 'L' + s[j+1:]
                j -=1
            elif s[j] == 'L':
                s = s[:j] + 'R' + s[j+1:]
                j-=1

if count > 0 and s != 'L'*n and s != 'R'*n:
    ans += 1

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

print(ans)

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

if(s[0] == 'R' and s[1] == 'R'):
    happy = happy + 1
if(s[n-1] == 'L' and s[n-2] == 'L'):
    happy = happy + 1

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

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

Solution (Python):
N,K=map(int,input().split())
s=input()
ans=0
l1=[]
l2=[]
for i in range(N-1):
  if s[i:i+2]=='LR':
    l1.append(i)
  elif s[i:i+2]=='RL':
    l2.append(i)
  elif s[i]=='L' and s[i+1]=='L':
    ans+=1
  elif s[i]=='R' and s[i+1]=='R':
    ans+=1

while K>0 and len(s)-2>ans:
  ans+=2
  K-=1
if len(s)-2<=ans and K>=1:
  ans=len(s)-1
print(ans)

Solution (Python):
import re

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

LL = re.finditer('L+', S)
RR = re.finditer('R+', S)

happy = 0
next_happy = []

for l in LL:
    happy += len(l.group(0)) - 1
    if l.regs[0][0] == 0 or l.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

for r in RR:
    happy += len(r.group(0)) - 1
    if r.regs[0][0] == 0 or r.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

happy += sum(sorted(next_happy, reverse=True)[:K])

if happy >= N:
    happy = N - 1

print(happy)

Solution (Python):
tem = list(map(int,input().split()))
n = tem[0]
k = tem[1]
s = [i for i in input()]


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

tem_h = n - 1 - count

if((int(count/2)+count%2)>k):
    h = tem_h + 2*k
elif((int(count/2)+count%2)<=k):
    h = n-1

print(h)

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

def solve():
    global K, S
    concat_S = S[:]
    while 'LL' in concat_S:
        concat_S = concat_S.replace('LL', 'L')
    while 'RR' in concat_S:
        concat_S = concat_S.replace('RR', 'R')

    ans = len(S) - len(concat_S)
    if len(concat_S) >= 4:
        
        num_rm = min(K, (len(concat_S)-2)//2)
        K -= num_rm
        ans += num_rm * 2
        len_concat_S = len(S) - ans

        if K:
            if len_concat_S == 3:
                ans += 2
            elif len_concat_S == 2:
                ans += 1
        print(ans)
        return
    if K:
        if len(concat_S) == 3:
            ans += 2
        elif len(concat_S) == 2:
            ans += 1
        
    print(ans)
    return

if __name__ == "__main__":
    solve()

Solution (Python):
N,K = map(int,input().split())
S = input()
x = 0
for i in range(N-1):
    if S[i]==S[i+1]:
        x +=1

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

Solution (Python):
import numpy as np
N , K = map(int,input().split())
S = list(input())
s = np.ones(N)
for i , st in enumerate(S):
    if st == "R":
        s[i] = -1


s_res =s[1:N] - s[:N-1]

s_abs = np.abs(s_res)

non_happy = s_abs.sum()/2 - 2*K

if non_happy <0:
    non_happy =0

ans = (N-1) - non_happy
print(int(ans))

Solution (Python):
"""考え方
ブロックとそのブロック内の人数を調べることで、「現時点での幸福度」を調べる。
一回変換するごとに幸福度が2向上する。

"""

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


before_s = 'a'
cnt_str_cng = 0
ans = 0
for s in S:
  if s == before_s:
    ans += 1
  else:
    cnt_str_cng += 1
  before_s = s

if cnt_str_cng - 1 <= K:
  ans = N - 1
else:
  ans += K * 2

if ans >= N:
  ans = N - 1

print(ans)

Solution (Python):
import sys

N,K=map(int,sys.stdin.readline().split())
S=sys.stdin.readline().rstrip()

befores='N'
happiness=0
noCounter=0
for s in S:
  if befores==s:
    happiness+=1
  else:
    noCounter+=1
  befores=s

happiness+=min(K,noCounter)*2
print(min(happiness,N-1))

Solution (Python):
import copy

words = lambda t : list(map(t, input().split()))
n,k = words(int)
s = input()

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

t = list(copy.copy(s))
x = k
i = 0
while i < len(t) and t[i] == "L":
    i += 1
while x > 0 and i < len(t):
    if t[i] == "L":
        x -= 1
        while i < len(t) and t[i] == "L":
            t[i] = "R"
            i+=1
    else:
        i+= 1

if x > 0 and t[0] == "L":
    i = 0
    while i < len(t) and t[i] == "L":
        t[i] = "R"
        i += 1


def calcAns(t):
    ans = 0
    for i in range(1,len(t)-1):
        
        if t[i] == "L":
            if t[i-1] == "L":
                ans += 1
        elif t[i] == "R":
            if t[i+1] == "R":
                ans += 1
    if t[0] == "R" and t[1] == "R":
        ans += 1
    if t[-1] == "L" and t[-2] == "L":
        ans += 1
    return ans

ansR = calcAns(t)


t = list(copy.copy(s))
x = k
i = 0
while i < len(t) and t[-(i+1)] == "R":
    i += 1

while x > 0 and i < len(t):
    if t[-(i+1)] == "R":
        x -= 1
        while i < len(t) and t[-(i+1)] == "R":
            t[-(i+1)] = "L"
            i+=1
    else:
        i+= 1

if x > 0 and t[-1] == "R":
    i = 0
    while i < len(t) and t[-(i+1)] == "R":
        t[-(i+1)] = "L"
        i += 1


ansL = calcAns(t)


print(max(ansL,ansR))

Solution (Python):
import copy


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

lr = list(input())

count = 0
sum = 0
lr_org = copy.copy(lr)
if lr[0] == 'L':
        for i in range(n):
                if lr[i] == 'R':
                        lr[i] = 'L'
                        if lr_org[i-1] == 'L':
                                count += 1
                else:
                        if count >= k:
                                break
else:
        for i in range(n):
                if lr[i] == 'L':
                        lr[i] = 'R'
                        if lr_org[i-1] == 'R':
                                count += 1
                else:
                        if count >= k:
                                break
for i in range(n-1):
        if lr[i] == lr[i+1]:
                sum += 1
print(sum)

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

ans = 0
c = 0
for i in range(N):
    if S[i] == 'L':
        if i > 0 and S[i - 1] == 'L':
            ans += 1
    elif S[i] == 'R':
        if i < N - 1 and S[i + 1] == 'R':
            ans += 1
    if i < N - 1 and S[i] != S[i + 1]:
        c += 1

if c % 2 == 0:
    max_K = c // 2
    ans += min(K, max_K) * 2
else:
    max_K = (c - 1) // 2
    ans += min(K, max_K) * 2
    if K > max_K:
        ans += 1
print(ans)