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

c = []
c_ap = c.append
cnt = 0

result = 0

for i in range(N - 1):
    if S[i] == S[i + 1]:
        result += 1
    if S[i] == "R":
        if S[i + 1] == "L":
            c_ap(cnt + 1)
            cnt = 0
        else:
            cnt += 1
            
            
if len(c) == 0 and "R" in S:
    result += 1
if len(c) >= K:
    result += K * 2
else:
    result = N - 1

print(result)

Solution (Python):
if __name__ == '__main__':
    N, K = map(int, input().split())
    S = str(input())
    count = 0
    
    LR = 0
    RL = 0
    
    for i in range(N-1):
        if S[i] == 'R' and S[i+1] == 'R':
            count += 1
        elif S[i+1] == 'L' and S[i] == 'L':
            count += 1
    
    for i in range(N-1):
        if S[i] == 'L' and S[i+1] == 'R': 
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': 
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
        else:
            if LR > 0 or RL > 0:
                count += 1
                break
            
    print(count)

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

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

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

if __name__ == "__main__":
    f_d()

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

count = []
pre = S[0]
c = 1
for t in S[1:] :
    if t == pre :
        c += 1
    else :
        count.append(c)
        pre = t
        c = 1
        
ret = sum(count)-len(count)
if K < len(count) // 2 :
    ret += 2 * K
else :
    ret = N - 1
print(ret)

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

length = len(S)
point=0

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

capacity = length-1
point += min(capacity, K*2)


print(point)

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)
from collections import *
from bisect import *
from copy import deepcopy
mod = 10**9+7

N, K = map(int, input().split())
T = [0 if x == 'L' else 1 for x in input()]
f = 0
cnt = 1
for i in range(N-1):
    if T[i] != T[i+1]:
        cnt += 1
if cnt//2 <= K:
    f = 1
ans1 = ans2 = N-cnt

S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 1:
    l += 1

while l < N-1 and S[l+1] == 0:
    l += 1
while 0 <= r and S[r] == 1:
    r -= 1

while 0 < r and S[r-1] == 0:
    r -= 1

while l < r and k < K:
    k += 1
    ans1 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1

S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 0:
    l += 1

while l < N-1 and S[l+1] == 1:
    l += 1
while 0 <= r and S[r] == 0:
    r -= 1

while 0 < r and S[r-1] == 1:
    r -= 1
while l < r and k < K:
    k += 1
    ans2 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
print(max(ans1, ans2, f*N-1))

Solution (Python):
def d_face_produces_unhappiness():
    N, K = [int(i) for i in input().split()]
    S = input()
    before_char = S[0]
    t = []
    v = 0
    for s in S + '*':
        if s != before_char:
            t.append(v)
            v = 1
        else:
            v += 1
        before_char = s
    t.sort(reverse=True)
    for i in range(min(K, len(t))):
        t[i] += 2
    return min(sum([e - 1 for e in t]), N - 1)

print(d_face_produces_unhappiness())

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os, pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
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 LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def perr(s): return print(s, file=sys.stderr)
N, K = LI()
S = input()

"""

答えは0以上N-1以下になる
まず何回ひっくり返せるかが重要
N==Kなら、全部ひっくり返して最大値となる
K==N-1なら、最大値から1箇所は矛盾する
LLRRLLなら
LLはRRになる
愚直回は、毎回どの区間をひっくり返すかを探索する
そうすると、区間の選び方だけでもかなり多い
好きな回数だけ選ぶと
まず塊の場所を特定する
6 1
LRLRRL
なら
てかどちらに統一するか問題もある
RRを
一番つながってるやつをひっくり返すのでいいか
Lにする場合とRにする場合を分けて考える
上のやつなら
Lにする場合
RRを返して
LRLLLL
で3
Rにする場合
LRRRRLになって3となる

9 2
RRRLRLRLL
の場合
Lの場合
LLLLRLRLL
になり、
LLLLLLRLL
5
Rの場合
111110101
111111101

長さをリストにすると
3, 1, 1, 1, 1, 2
最大にすると
5, 1, 1, 1, 1
6, 1, 1, 1

3, 1, 1, 1, 1, 2
3 1 1 4
7 1 1
RRRLRLLLL
LRLLLLLLL
だと6
RRRRRLRLL
RRRRRRRLR

LRLLLLRLL
LRLLLLLLL

RRRRLRRLL
RRRRRRLLR

RRRLLRLLL
RRRRRRLRR

RRRRRLRLR
RRRRRRLRR

-- RRRLRLRLL

RRRLRRRLR
LLLLRRRRR


長さをリストにすると
3, 1, 1, 1, 1, 2
3 3 1 2
3 6
2 + 5

RRRLLLRLL
RRRLLLLLL

まず、リストを作成

3 1 1 4
3 3 1 2
RRR LLL R LL
3 6
2 + 5
または


5 1 1 1


5 3
4 + 2

最小値の組み合わせをあわせる？
2個ずつ減っていく

LRLRRL
の場合
1 1 1 2 1
3 2 1
2+1 = 3
1 1 4 = 3

3 2 1
6
何回裏返せるかで決まってる

塊の数が
6子なら
6
4
26子なら
6
4
26子なら
6
4
26子なら
6
4
26子なら
6
4
26子なら
6
4
2
というふうに減っていく
4塊の場合はもう決まってる
3 3 1 2 = 5
3 1 1 4 = 5

-- RRRLRLRLL
3 1 1 1 1 2
6塊
まず9-6
ほんで3あまってる
答えは必ず3

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


len_l = len(l)
len_l -= 2 * K
if len_l <= 1:
    print(N - 1)
    exit()
elif len_l <= 2:
    print(N - 2)
else:
    ans = N - len_l
    print(ans)

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

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

for i in range(k):
  if ans<n-2:
    ans+=2
  elif ans==n-2:
    ans+=1
  

print(ans)

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

out = 0
prev_letter = ''
for letter in S:
    if letter == prev_letter:
        out += 1
    prev_letter = letter

change = N - out - 1

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

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

L = []

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



ans = 0

calc = sum(L) - len(L)

if len(L) <= 2*K + 1 :
    ans = sum(L)-1
else :
    su = sum(L[0:0+2*K+1])
    d = su
    c = calc + d - 1 - (d - (2*K+1))
    ans = max(ans,c)

    for i in range(2*K+1,N) :
        if i >= len(L) : break
        
        d += L[i]
        d -= L[i - (2*K+1)]
        c = calc + d - 1 - (d - (2*K+1))
        ans = max(ans, c)
        

print(ans)

Solution (Python):
n,k=[int(j) for j in input().split()]
s=input()
l=[1]
for i in range(1,n):
    if s[i]==s[i-1]:
        l[-1]+=1
    else:
        l.append(1)
if len(l)//2<=k:
    print(n-1)
    exit()
ans=sum(l)-len(l)
ans+=2*k
print(ans)

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

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

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

Solution (Python):
from collections import deque
N, K = [int(x) for x in input().split()]

def point(sl, num):
  p = ''
  ret = 0
  for i, s in enumerate(sl):
    if s == p:
      ret += num[i]
    else:
      ret += num[i] - 1
  return ret
  
S = input()
num = deque([])
sl = deque([])
c = 0
pre = S[0]
for s in S:
  if s == pre:
    c += 1
  else:
    sl.append(pre)
    num.append(c)
    pre = s
    c = 1
else:
  sl.append(pre)
  num.append(c)
  
if sl.count('R') > sl.count('L'):
  f = 'L'
  t = 'R'
else:
  f = 'R'
  t = 'L'

k = 0
sl2 = deque([])
num2 = deque([])
pre = sl[0]
c = 0
for i, s in enumerate(sl):
  if s == pre:
    c += num[i]
  else:
    if k < K:
      c += num[i]
      k += 1
    else:
      sl2.append(pre)
      num2.append(c)
      pre = s
      c = num[i]
else:
  sl2.append(pre)
  num2.append(c)
print(point(sl2, num2))

Solution (Python):
b = input().split()
n = int(b[0])
k = int(b[1])
r = 0
s=list(input())
for i in range(n-1):
    if(s[i] == s[i+1]):
        r += 1
t = n - r
if(k*2 < t):
    print(r + k*2)
else:
    print(n-1)

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

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


happy2 = min(K, (change // 2)) * 2
if change % 2 == 1 and K > change // 2:
  happy2 += 1
print(happy + happy2)

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

ans = 0
change = 0

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


for _ in range(k):
    if change >1:
        ans +=2
        change -=2
    elif change ==1:
        ans +=1
        change -=1
    else:
        break

print(ans)

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

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

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

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

for i,j in groupby(S):
  A.append(len(list(j))-1)

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

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

cnt = 0
ans = 0
for p, q in zip(s, s[1:]):
    if p != q:
        cnt += 1
    else:
        ans += 1
ans += min(k*2, cnt)
print(ans)

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

tmp = s[0]
count = 1
for i in range(1,n):
  if s[i] == tmp:
    count += 1
  else:
    tmp = s[i]
    ls.append(count)
    count = 1
ls.append(count)

length = len(ls)
for i in range(k):
  if length==1: break
  tmp = 0
  if length == 2:
    ls = [ls[0]+ls[1]]
    break
  else:
    tmp += ls.pop(0)
    tmp += ls.pop(0)
    ls[0] += tmp
    length -= 2
  
print(n-len(ls))

Solution (Python):
from sys import exit, stderr, stdin
input = stdin.readline

def gau(n):
    return (n-1)*(n-2)//2

def main():
    N,K = list(map(int,input().split()))
    S = str(input().rstrip())
    LS = len(S)
    SS = "R" + S + "L"
    ans = 0
    for i in range(1,LS+1):
        if SS[i] == "L" and SS[i-1] == "R":
            ans += 1
        elif SS[i] == "R" and SS[i+1] == "L":
            ans += 1
    print(min(N-1,N-ans+2*K))

if __name__ == "__main__":
    main()

Solution (Python):
n,m = map(int,input().split())
s = str(input())
L = []
R = []

if s == "L"*n or s == "R"*n:
    print(n-1)
    exit()
now = "E"
ss = 0
for i in range(n):
    if s[i] != now:
        if s[i] == "R" and ss:
            L.append(ss)
        elif s[i] == "L" and ss:
            R.append(ss)
        ss = 1
        now = s[i]
    else:
        ss += 1
    if i == n-1:
        if s[i] == "R" and ss:
            R.append(ss)
        elif s[i] == "L" and ss:
            L.append(ss)
lis = []
ri = 0
li = 0
if s[0] == "R":
    key = 0
else:
    key = 1
for i in range(len(R)+len(L)):
    if key == 0:
        lis.append(R[ri])
        ri += 1
        key = 1
    else:
        lis.append(L[li])
        li += 1
        key = 0


if len(lis) == 2:
    print(sum(lis)-1)
    exit()

m-=1
ans = 0

for i in range(3):
    ans += lis.pop(0)
for i in range(m):
    try:
        ans += lis.pop(0)
    except:
        continue
    try:
        ans += lis.pop(0)
    except:
        continue
ans -= 1
for nu in lis:
    ans += nu-1
print(ans)

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







koufuku = 0

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

max_koufuku = min(N-1, koufuku + 2*K)
print(max_koufuku)

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

if __name__ == '__main__':
    main()

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

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

    np = N - ans - 1

    for k in range(K):
        if np > 1:
            ans += 2
            np -= 2
        elif np == 1:
            ans += 1
            np -= 1

    print(ans)


if __name__ == '__main__':
    p_d()

Solution (Python):
import sys
input = sys.stdin.readline

MAX = 10**5+1

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

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

min_pare = max(pare - 2*k, 1)

print(n-min_pare)

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

w = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        w += 1
        
c = w-2*K
if c <= 0:
    print(N-1)
else:
    print(N-c-1)

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

R=0
c=0
f=2
for i in range(N):
    if S[i]=='R':
        R+=1
        if f==0:
            c+=1
        f=1
    else:
        if f==1:
            c+=1
        f=0

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

print(out)

Solution (Python):
def main():
    n, k = map(int, input().split())
    s = input()
    prev, seq = '0', 0
    a = []
    for c in s:
        if prev == c:
            seq += 1
        else:
            a.append(seq)
            seq = 1
        prev = c
    a.append(seq)
    a = a[1:]
    if len(a) // 2 <= k:
        print(n - 1)
        return
    b = [sum(a[:2 * k + 1])] + a[2 * k + 1:]
    ans = 0
    for m in b:
        ans += m - 1
    print(ans)


if __name__ == '__main__':
    main()

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

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

x = N - ans
if x%2 == 0:
    ans += min(2*K, x-1)
else:
    ans += min(2*K, x-1)

print(ans)

Solution (Python):
nums = list(map(int, input().split()))
N, K = nums[0], nums[1]
S = input()
before = S[0]
a = 0
for i in range(N-1):
    if S[i+1] != before:
        a += 1
    before = S[i+1]
b = max(0, a - 2 * K) + 1
print(N - b)

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

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

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

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

ans = 0
for i,j in zip(s,s[1:]):
    if i == j: ans += 1


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

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

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

repulsion_end = 0
if S[ 0]=='L': repulsion_end+=1
if S[-1]=='R': repulsion_end+=1

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

remove_repulsion = min(K, back2back, repulsion)
back2back -= remove_repulsion
K -= remove_repulsion
repulsion -= remove_repulsion

if K>0 and back2back>0:
    unhappy=1
elif K>0 and repulsion>0 and repulsion_end<2:
    repulsion_end += 1
    repulsion -=1
    unhappy=repulsion*2+repulsion_end
else:
    unhappy=repulsion*2+repulsion_end

print(N-unhappy)

Solution (Python):
n, k = [int(i) for i in input().split()]
s = input()
s_split = []
i = 0
while i < n:
    j = i
    while j < n and s[i] == s[j]:
        j += 1
    s_split.append(s[i:j])
    i = j
i_hp = sum([len(ss) - 1 for ss in s_split])
rs = [ss for i, ss in enumerate(s_split) if i != 0 and i != len(s_split) - 1 and ss[0] == 'R']
ls = [ss for i, ss in enumerate(s_split) if i != 0 and i != len(s_split) - 1 and ss[0] == 'L']
initial_good = s_split[0][0] == 'R'
final_good = s_split[-1][-1] == 'L'
max_flip = max(len(rs), len(ls))
if max_flip >= k:
    print(i_hp + 2 * k)
else:
    print(n - 1)

Solution (Python):
n, k = (int(i) for i in input().split())
S = input()
ans = 0
before = S[0]
l = []
c = 0
for s in S:
    if s != before:
        l.append(c)
        c = 1
    else:
        c += 1
    before = s
l.append(c)

ans = n-len(l)+k*2
if ans > n-1:
    ans = n-1
print(ans)

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

bf = s[0]
kyoukai = 0
sum = 0
for i in range(1,len(s)):
    if bf != s[i]:
        kyoukai += 1
    else:
        sum += 1
    bf = s[i]

sum += min(k*2,kyoukai)
print(sum)

Solution (Python):


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

S1 = list(S)


lim = 0
flag = False
for i in range(N):
    if S1[i] == "L":
        S1[i] = "R"
        flag = True
    elif flag :
        lim += 1
        flag = False

    if lim == K:
        break


S2 = list(S)
lim = 0
flag = False
for i in range(N):
    if S2[i] == "R":
        S2[i] = "L"
        flag = True
    elif flag:
        lim += 1
        flag = False

    if lim == K:
        break

anss =[0,0]
for i,s in enumerate([S1,S2]):
    Rcot = 0
    Lcot = 0
    for k in range(N - 1):  
        if s[k] == "R" and  s[k + 1] == "R":
            Rcot += 1
    for k in range(1,N ):  
        if s[k ] == "L" and  s[k -1] == "L":
            Rcot += 1
    anss[i] = Rcot + Lcot
print(max(anss))

Solution (Python):

n, k = map(int,input().split())
s = list(input())
cn = [0]*(n-1)
for i in range(n-1):
    if s[i] == s[i+1]:
        cn[i] = 1
if (n-1 - sum(cn)) <= 2*k:
    print(n-1)
else:
    print(sum(cn)+k*2)

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
for i in range(k):
  if ans < n-2:
    ans += 2
  elif ans == n-2:
    ans += 1
  else:
    pass
print(ans)

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

ans = 0
RL = 0
LR = 0

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

Solution (Python):
n1, n2 = [int(i) for i in input().split()]

s1 = input()

c1 = ""
r1 = 0

for i1 in range(n1):
    if i1 == 0:
        c1 = s1[0]
    elif i1 == n1 - 1:
        if c1 == s1[i1]:
            r1 += 1
        else:
            c1 = s1[i1]
    else:
        if c1 == s1[i1]:
            r1 += 1
        else:
            c1 = s1[i1]

r1 += (2 * n2)
if r1 >= n1:
    r1 = n1 - 1
print(r1)

Solution (Python):
N,K =list(map(int,input().split()))
S = input()
S2 = [(1 if S[i]+S[i+1]== 'LR' else -1) if S[i]!= S[i+1] else 0 for i in range(N-1)]
score = sum([1 if S[i]==S[i+1] else 0 for i in range(N-1)])
S3 = min(S2.count(1),S2.count(-1))
if S3 >= K:
    score += K*2
else:
    if S2.count(1) == S2.count(-1):
        score += S3*2
    else:
        score += S3*2+1
print(score)

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

diff = 0

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

print(n - (diff - k*2) -1 if (diff - k*2) >= 0 else n-1)

Solution (Python):
N, K = map(int, input().split())
S = input()
max_happy = N-1
init_happy = 0
for i in range(1, N-1):
  if S[i]=="L" and S[i-1]=="L":
    init_happy += 1
  elif S[i]=="R" and S[i+1]=="R":
    init_happy += 1

if N>1:
  if S[0]=="R" and S[1]=="R":
    init_happy += 1

  if S[-1]=="L" and S[-2]=="L":
    init_happy += 1

ans = min(max_happy, init_happy+K*2)

print(ans)

Solution (Python):
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    

n,k=map(int,input().split())
s=input()
l =[]
cnt=0

for i in range(len(s)-1):
	if s[i]==s[i+1]: 
		cnt+=1
		
d=len(s)-1-cnt
while d>1 and k>0:
	cnt+=2
	d-=2
	k-=1
if d==1 and k>0:
	cnt+=1
	d-=1
	
print(cnt)

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

r_area = 0
l_area = 0
flag = None
for i, s in enumerate(S):
    if s == "R":
        if flag == "R":
            continue
        flag = "R"
        r_area += 1
    if s == "L":
        if flag == "L":
            continue
        flag = "L"
        l_area += 1

small_area = min(r_area, l_area)
large_area = max(r_area, l_area)
if small_area <= K:
    print(N-1)
else:
    print(N-(small_area+large_area-2*K))

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

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

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

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

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

print(ans)

Solution (Python):
import sys
n=list(map(int,input().split()))
s=input()
a,b,c=[],s[0],0
if n[0]==1:
    print('0')
    sys.exit()
for i in range(n[0]-1):
    if b==s[i+1]:
        c+=1
    else:
        a.append(c)
        c=0
    b=s[i+1]
if c!=0:
    a.append(c)
if s[-2]==s[-1]:
    a[-1]+=1
else:
    a.append(1)
if n[1]>=len(a)//2:
    print(n[0]-1)
else:
    print(sum(a)+2*n[1]-1)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

N, K = inpl()
S = input().rstrip('\n')

elements, nums = get_sequence(S)
loss = len(nums)
loss -= K*2
if loss < 1:
    loss = 1
print(N-loss)

Solution (Python):
import sys
input=sys.stdin.readline

def runLength(s):
    a = []
    for c in s:
        if len(a)==0 or a[-1][0]!=c:
            a.append([c,1])
        else:
            a[-1][1] += 1
    return a

def main():
    N,K = map(int, input().split())
    S = input().strip()
    ans = 0
    for i,c in enumerate(S):
        if c == "L":
            if i > 0 and S[i-1] == "L":
                ans += 1
        if c == "R":
            if i < N-1 and S[i+1] == "R":
                ans += 1
    A = runLength(S)
    A.sort(key=lambda x:x[1], reverse=True)
    for i in range(K):
        if i < len(A):
            ans += 2
            if ans >= N:
                break
    if ans >= N:
        ans = N-1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

from itertools import groupby
N, K = map(int,input().split())
S = input()
L = []
for i in range(len(S)):
  L.append(S[i])
sum = 0
for key, value in groupby(L):
    sum += len(list(value))-1

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

Solution (Python):
N, K = map(int, input().split())
S = input()
kazulist = []
now = 1
for i in range(1, N):
    if S[i] == S[i-1]:
        now += 1
    else:
        kazulist.append(now)
        now = 1
kazulist.append(now)
lenkazu = len(kazulist)
sumkazu = sum(kazulist)
spl = max(lenkazu - 2 * K , 0)
if spl >= 1:
    print(sumkazu - spl)
else:
    print(N-1)

Solution (Python):
import sys
input = sys.stdin.readline

N, K = map(int,input().split())
s = input().split() 
s = list(s[0])
renketu=[]
cnt=1
for i in range(1,N):
    if s[i] != s[i-1]:
        renketu.append(cnt)
        cnt=1
    else:
        cnt+=1
renketu.append(cnt)
cnt=0
ans=0
for i in range(len(renketu)):
    if i%2==1:
        cnt+=1
    if cnt <= K:
        ans+=renketu[i]
    else:
        ans+=renketu[i]-1
print(ans-1)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

pre = ''
cnt = 0
f = False
K_cnt = 0

for i in range(N):
    k = S[i]
    if pre==k:
        cnt += 1
        
        if i==N-1:
            if K_cnt<K:
                if f:
                    cnt += 1
    else:
        if f:
            if K_cnt<K:
                cnt += 2
                K_cnt += 1
                f = False
        else:
            if not i==0:
                f = True
                if i==N-1:
                    if K_cnt<K:
                            cnt += 1
    pre = k
print(cnt)

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


score = 0
up1_num = 0
up2_num = 0

switch = []

prev = None
cnt =0
for i in range(N):
  if prev == None:
    prev = b[i]
    cnt = 1
    continue
    
  if prev==b[i]:
    cnt += 1
  else:
    score += (cnt-1)
    cnt = 1
    prev = b[i]
    switch.append(i)

if cnt != 0:
  score += (cnt-1)
  
  
if k <= len(switch)/2:
  score += 2*k
else:
  score += 2*(len(switch)//2)
  if len(switch)%2==1:
    score += 1
    
print(score)

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

Solution (Python):

n,k = map(int,input().split())
s = input()
l = [0 for i in range(len(s)+1)]
r = [0 for i in range(len(s)+1)]

count = 0
kukann = 1
for i in range(0,len(s)-1):
    if s[i] != s[i+1]:
        kukann += 1
    if s[i] == 'L' and s[i+1] == 'L':
        count += 1
    elif s[i] == 'R' and s[i+1] == 'R':
        count += 1
if ((kukann-2)//2) >= k:
    count += k*2
elif kukann % 2 == 0:
    if ((kukann-2)//2) >= 0:
        count += ((kukann-2)//2) * 2
    if ((kukann-2)//2) < k and kukann != 1:
        count += 1
else:
    count += min(k*2,(kukann-1))
print(count)

Solution (Python):
N, K = map(int, input().split())
S = input()
ls = []
l = 1
ans = 0
count = 0
pcount = 0
flag = False
for i in range(N):
  if S[i]=='L':
    if i!=0 and S[i]==S[i-1]:
      ans += 1
  else:
    if i!=N-1 and S[i]==S[i+1]:
      ans += 1
  
  if i!=N-1 and S[i]!=S[i+1]:
    count += 1

for i in range(K):
  if count>1:
    ans += 2
    count -= 2
    continue
  elif count==1:
    ans += 1
    break
print(ans)

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

S = str(input())
tmp = S[0]
num = []
num_tmp = 0
for i in range(N):
    if S[i] == tmp:
        num_tmp += 1
    else:
        tmp = S[i]
        num.append(num_tmp)
        num_tmp = 1
    if i == N-1:
        num.append(num_tmp)
ans = 0
for i in range(len(num)):
    ans += num[i] - 1
Kmax = len(num) // 2
if Kmax <= K:
    print(N-1)
else:
    print(2*K+ans)

Solution (Python):
from sys import stdin

n, k = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()

ans = 0
tmp = s[0]
ll = [0]
index = 0 
for i in range(len(s)):
    if tmp == s[i]:
        ll[index] += 1
    else:
        tmp = s[i]
        index += 1
        ll.append(1)



for i in range(len(ll)):
    ans += ll[i]-1

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

print(ans)

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

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


c = [1]
prev = s[0]
c_ind = 0

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

cur_len = len(c)
cur_sum = sum(c) - cur_len

for _ in range(k):
    if cur_len == 1:
        break
    elif cur_len == 2:
        cur_sum += 1
        break
    else:
        cur_sum += 2
        cur_len -= 2

print(cur_sum)

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

flag = True
for i in range(1, N):
    if K != 0:
        if flag:
            if S[i-1] != S[i]:
                flag = False
        if not flag:
            S[i] = 'L' if S[i] == 'R' else 'R'
            if i < N - 1:
                if S[i] == S[i+1]:
                    flag = True
                    K -= 1

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

print(ans)

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

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

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

Solution (Python):
import copy

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

lenLorR, LR, temp, happness = 0, S[0], 1, 0
for i in range(1, N):
    if LR == S[i]:  temp += 1
    else:
        lenLorR += 1
        happness += temp - 1
        LR, temp = S[i], 1
lenLorR += 1
happness += temp - 1

if lenLorR // 2 <= K:   print(N-1)
else:   print(happness + K * 2)