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



Sample input:
6 1
LRLRRL


Sample output:
3

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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:
  ans+=k*2
else:
  ans+=min(lrcnt,rlcnt)
  k-=min(lrcnt,rlcnt)//2
  if k>=2:
    ans+=lrch
    ans+=rlch
  else:
    ans+=1
  
print(ans)

Submission status: Wrong Answer

Solution code:
# run-length encoding, decoding
def rle_encode(data):
    encoding = ''
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding += str(count) + prev_char + ' '
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        encoding += str(count) + prev_char + ' '
        return encoding

N, K = [int(s) for s in input().split()]
S = input()
S_rle = rle_encode(S).split()
total = 0
for i, bunch in enumerate(S_rle):
  total += int(list(bunch)[0]) - 1

if len(S_rle) >= 2*K:
  print(total+2*K)
elif len(S_rle) >= 2*K-1:
  print(total+2*K-1)
else:
  print(N-1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()+'X'
A=[0]
l=0
r=0
for i in range(n):
  if S[i]=='L':
    l+=1
    if S[i+1]!='L':
      A.append(l)
      l=0
  if S[i]=='R':
    r-=1
    if S[i+1]!='R':
      A.append(r)
      r=0
A=sorted(A)
z=A.index(0)
L=sum(A[z+1:])-len(A[z+1:])-sum(A[:min(z,k)])+min(z,k)
R=sum(A[:z])+len(A[:z])-sum(A[-min(n-z,k):])-min(z,k)
if len(A[z+1:])==1:
  L-=1
if len(A[:z])==1:
  R+=1
print(max(L,-R))

Submission status: Wrong Answer

Solution code:
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)
elif coun -K <= foo:
    print(N-(coun-K))
else:
    print(N-2*(coun-K) +foo)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()+'X'
A=[0]
l=0
r=0
for i in range(n):
  if S[i]=='L':
    l+=1
    if S[i+1]!='L':
      A.append(l)
      l=0
  if S[i]=='R':
    r-=1
    if S[i+1]!='R':
      A.append(r)
      r=0
A=sorted(A)
z=A.index(0)
L=sum(A[z+1:])-len(A[z+1:])-sum(A[:min(z,k)])+min(z,k)
R=sum(A[:z])+len(A[:z])-sum(A[-min(n-z,k):])-min(z,k)
if len(A[z+1:])==min(z,k):
  L-=1
if len(A[:z])==min(z,k):
  R+=1
print(max(L,-R))

Submission status: Wrong Answer

Solution code:
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
print(ans,lrcnt,rlcnt,lrch,rlch)
     
if min(lrcnt,rlcnt)>=k:
  ans+=k*2
else:
  ans+=min(lrcnt,rlcnt)
  k-=min(lrcnt,rlcnt)//2
  if k>=2:
    ans+=lrch
    ans+=rlch
  else:
    ans+=1
  
print(ans)

Submission status: Wrong Answer

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

m = 0
ans = 0
buf = 0
flg = True
for i in range(N):
  if S[i] == ("L" if flg else "R"):
    buf += 1
  else:
    m = max( m, min(N-i, N-i-buf) )
    ans += max( 0, buf - 1 )
    buf = 1
    flg ^= 1
if S[N-1] == ("L" if flg else "R"):
  ans += buf - 1

for i in range(K):
  if i < m:
    ans += 2
  elif i >= m:
    ans += 1
  elif ans == N-1:
    break

print(ans)

Submission status: Wrong Answer

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

RL = []

l = 0
r = 0

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:
    K -= 1
    count -= 2
    ans += 2

if count <= 0:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
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
print(happiness)

Submission status: Wrong Answer

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

ret = 0
n_flip_count = 0

last_d = s[0]
for d in s[1:]:
    if last_d == d:
        ret += 1
    else:
        n_flip_count += 1
    last_d = d

if k <= n_flip_count // 2:
    ret += 2 * k

if n_flip_count // 2 < k and n_flip_count % 2 == 1:
    ret += 2 * (n_flip_count // 2) + 1

print(ret)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()+'X'
A=[0]
l=0
r=0
for i in range(n):
  if S[i]=='L':
    l+=1
    if S[i+1]!='L':
      A.append(l)
      l=0
  if S[i]=='R':
    r-=1
    if S[i+1]!='R':
      A.append(r)
      r=0
A=sorted(A)
z=A.index(0)
L=sum(A[z+1:])-len(A[z+1:])-sum(A[:min(z,k)])+min(z,k)
R=sum(A[:z])+len(A[:z])-sum(A[-min(n-z,k):])-min(z,k)
if len(A[z+1:])==1:
  L-=1
if len(A[z+1:])==1:
  R+=1
print(max(L,-R))

Submission status: Wrong Answer

Solution code:
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
#print(saisyo)

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

#print(nr)
#print(nl)

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

ans = saisyo

#rの時
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

rans = ans 
ans = saisyo

#lの時
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

lans = ans

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

Submission status: Wrong Answer

Solution code:
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
ans = ans + 2 * min(K,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
Slist = []
for k in range(N):
    Slist.append(S[k])
#LR数できまる.
#一番外はRとLをそれぞれ付けて考える
#Lの次にRならLに変えてRが続くならそれもLに返る
ans = 0
count = 0
for i in range(N-1):
    if count == K:
        break
    if S[i] == "L" and S[i+1] == "R":
        count += 1
        for p in range(i+1,N):
            if S[p] == "R":
                Slist[p] = "L"
            else:
                break

for j in range(N-1):
    if Slist[j] == Slist[j+1]:
        ans += 1

print(ans)

#print(Slist)

Submission status: Wrong Answer

Solution code:
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

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

print(output)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
Rs = []
now = S[0]
count = -1

for i in range(N):
    if S[i] == now:
        count += 1
    else:
        Rs.append(count)
        count = 0
        now = S[i]
Rs.append(count)
ans = 0
if K >= len(Rs) - 2:
    ans = N-1
elif K == len(Rs) - 3:
    ans = N-2
else:
    ans = sum(Rs) + 2 * K

print(ans)

Submission status: Wrong Answer

Solution code:
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': #Lで不幸せな人を見つけた
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': #Rで不幸せな人を見つけた
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
            print(LR, RL)
        elif LR > 0 or RL > 0:
            count += 1
            break
        
            
    print(count)

Submission status: Wrong Answer

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

S = input()
s_list = list(S)

#貪欲に変えてけばいい

ans = 0
now = s_list[0]
left = -1
set_c = 0


#change文字列


for i in range(1,N):
    if s_list[i]!=s_list[i-1]:
        if K ==0:
            continue
        if set_c == 0:
            set_c = 1
        else:

            if i != N-1 and s_list[i]==s_list[i+1]:
                continue
            elif i != N-1 and s_list[i]!=s_list[i+1]:
                set_c = 0
                ans += 2
                K = K -1
            else:
                # print('a')
                ans += 2


    else:
        if i == N-1 and set_c == 1:
            ans  += 2
        else:
            ans += 1
    # print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
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+1:
        foo  +=1
if K >= coun:
    print(N)
elif coun -K <= foo:
    print(N-foo)
else:
    print(N-2*(coun-K) +foo)

Submission status: Wrong Answer

Solution code:
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
print('change', change)
print('out', out)

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

Submission status: Wrong Answer

Solution code:
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': #Lで不幸せな人を見つけた
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': #Rで不幸せな人を見つけた
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
            print(LR, RL)
        elif LR > 0 or RL > 0:
            count += 1
        
            
    print(count)

Submission status: Wrong Answer

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

if len(SS)==1:
  print(N-1)
  exit()

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

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)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
Rs = []
now = S[0]
count = -1

for i in range(N):
    if S[i] == now:
        count += 1
    else:
        Rs.append(count)
        count = 0
        now = S[i]
ans = 0
if K >= len(Rs) - 2:
    ans = N-1
elif K == len(Rs) - 3:
    ans = N-2
else:
    ans = sum(Rs) + 2 * K

print(ans)

Submission status: Wrong Answer

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

ret = 0
n_flip_count = 0

last_d = s[0]
for d in s[1:]:
    if last_d == d:
        ret += 1
    else:
        n_flip_count += 1
    last_d = d

if k <= n_flip_count // 2:
    ret += 2 * k

if n_flip_count == 1:  # e.g., LLLLRRRR
    ret += 1

print(ret)

Submission status: Wrong Answer

Solution code:
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,sum(A)+2*K))

Submission status: Wrong Answer

Solution code:
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 -=0
    else:
        break

print(ans)

Submission status: Wrong Answer

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

same_L_cnt = 0

up_cnt = 0
same_cnt = 0
before = ""
buf = []


    
for i in range(N):
    now_S = S[i]
    if now_S == 'L':
        now_S = 1
    else:
        now_S = 0
    # 前回と同じか確認
    if now_S == before:
        # 同じならカウントアップ
        same_cnt += 1
    # L R R ..... R Lが出現したか確認する
    buf.append(now_S)
    #print('------ buffer ------')
    #pprint(buf)
    #print('--------------------')
    if buf[0] != 1:
        buf = []
    elif len(buf) == 2 and buf[1] == 1:
        buf = [1]
    elif len(buf) < 3:
        pass
    else:
        t = buf[1:len(buf)-2]
        if sum(t) != 0:
            buf = []
        elif buf[len(buf)-1] == 1:
            up_cnt += 1
            buf = [1]
        else:
            pass
    before = now_S
#print(up_cnt)
#print(same_cnt)
up_flg = 0
if sum(buf) == 1:
    up_flg = 1

pt = same_cnt
rev_cnt = min(K , up_cnt)
remain_cnt = K - rev_cnt
extra = 0
if up_flg and remain_cnt > 0:
    extra = 1
print(same_cnt + rev_cnt * 2 + extra)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

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

if len(SS)==1:
  print(N-1)
  exit()

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

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]
  
if SS[-1]!=SS[-2] and K>0:
  SS[-1]=SS[-2]
  
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)

Submission status: Wrong Answer

Solution code:
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)
elif coun -K <= foo:
    print(N-foo)
else:
    print(N-2*(coun-K) +foo)

Submission status: Wrong Answer

Solution code:
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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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) - k >=2:
  ans += k*2
if len(b)- k == 1:
  ans += (k*2-1)
if len(b)-k <=0:
  ans += ((len(b)-2)*2+2)
print(ans)

Submission status: Wrong Answer

Solution code:
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:
        omake+=1

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = str(input())
a = []
b = []

count = 1
for i in range(1, n):
    if s[i] == s[i-1]:
        count += 1
    else:
        if s[i-1] == "L":
            a.append(count)
        else:
            b.append(count)
        count = 1
if n == 1:
    i = 0
if s[i] == "L":
    a.append(count)
else:
    b.append(count)
count = 1

a.sort(reverse=True)
b.sort(reverse=True)
ans1 = sum(a) - len(a)
ans2 = sum(b) - len(b)
for i in range(min(2*k, len(b))):
    ans1 += b[i]
if len(b) < 2*k:
    ans1 += 2*k - len(b)
ans1 = min(n-1, ans1)
for i in range(min(2*k, len(a))):
    ans2 += a[i]
if len(a) < 2*k:
    ans2 += 2*k - len(a)
ans2 = min(n-1, ans2)
print(max(ans1, ans2))

Submission status: Wrong Answer

Solution code:
# run-length encoding, decoding
def rle_encode(data):
    encoding = ''
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding += str(count) + prev_char + ' '
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        encoding += str(count) + prev_char + ' '
        return encoding

N, K = [int(s) for s in input().split()]
S = input()
S_rle = rle_encode(S).split()
total = 0
for i, bunch in enumerate(S_rle):
  total += int(list(bunch)[0]) - 1

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

Submission status: Wrong Answer

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

res = 0
pre = None
add = {1: 0, 2: 0}
if S[0] != S[-1] and S[0] == "L":
    add[0] = 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 K > 0:
        res += add[1]
    print(res)

Submission status: Wrong Answer

Solution code:
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
ans -= 1
for nu in lis:
    ans += nu-1
print(ans)

Submission status: Wrong Answer

Solution code:
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-=1
print(score)

Submission status: Wrong Answer

Solution code:
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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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

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
else:
    if memo2 >= num+1 and lst[0] == "L" and num < k:
        ans += 1
    

print(ans)

Submission status: Wrong Answer

Solution code:
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
elif K <= len(count) - 1 :
    ret += 2 * len(count) - 3
else :
    ret += 2 * len(count) - 1
print(ret)

Submission status: Wrong Answer

Solution code:
n, k = [int(item) for item in input().split()]
s = input().rstrip()
cnt = [0] * (10**5 + 10)
prev = "#"
count = 0
ans = 0
for ch in s:
    if ch == prev:
        count += 1
    else:
        cnt[count] += 1
        if count != 0:
            ans += count
        count = 0
    prev = ch
cnt[count] += 1
cnt[0] -= 1
itr = 0
for i in range(10**5, -1, -1):
    if cnt[i] != 0:
        ans += min(k - itr, cnt[i]) * (i + 1)
        itr += cnt[i] 
    if itr >= k:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
S = list(input())
temp = S[0]
ind = 0
cs = [1]
for i in range(N - 1):
  if S[i] == S[i + 1]:
    cs[ind] += 1
  else:
    cs.append(1)
    ind += 1
    temp = S[i + 1]
#print(cs)
cscs = [cs[0]] + [0] * (len(cs))
for i in range(len(cs) - 1):
  cscs[i + 1] = cscs[i] + cs[i + 1]
cscs = [0] + cscs
css = [cs[0]] + [0] * (len(cs))
for i in range(len(cs) - 1):
  css[i + 1] = css[i] + cs[i + 1] - 1
css = [0] + css
#print(css)
#print(cscs)
res = 0
for i in range(len(cs) - 2 * M + 1):
  temp = css[-2] - css[i + 2 * M] + css[max(i - 1, 0)]
  #print(temp,css[-2],css[i + 2 * M],css[max(i - 1, 0)])
  res = max(res, cscs[i + 2 * M] - cscs[i] + temp)
  #print(res - 1, i, i + 2 * M)
print(res - 1)

Submission status: Wrong Answer

Solution code:
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': #Lで不幸せな人を見つけた
            LR += 1  
                    
        elif S[i] == 'R' and S[i+1] == 'L': #Rで不幸せな人を見つけた
            RL += 1
    
    for i in range(K):
        if LR > 0 and RL > 0:
            LR -= 1
            RL -= 1
            count += 2
        else:
            count += 1
            
    print(count)

Submission status: Wrong Answer

Solution code:
N,K=[int(s) for s in input().split()]
S=list(input())
a=S[0]
index=1
for i in range(K):
  while (index<N and S[index]==a) :
    index+=1
  while (index<N and S[index]!=a) :
    S[index]=a
    index+=1
#print(S,index)
###幸せな人を数える
count=0
for i in range(1,N-1):
  if S[i]=="L":
    if S[i-1]=="L":
      count+=1
  if S[i]=="R":
    if S[i+1]=="R":
      count+=1
if (S[0]=="R"and S[1]=="R"):
  count+=1
if (S[N-1]=="L"and S[N-2]=="L"):
  count+=1

print(count)

Submission status: Wrong Answer

Solution code:
import itertools
 
N, K = map(int, input().split())
S = input()
 
lis = [len(list(g)) for _, g in itertools.groupby(S)]
 
if len(lis) < 2*K + 1:
  print(N-1)
else:
  print(max(sum(lis[:2*K+1]), sum(lis[len(lis)-2*K-2:])))

Submission status: Wrong Answer

Solution code:
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)) <= k:
    print(n-1)
else:
    print(sum(cn)+k*2)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
ans = 0
if n == 1:
  print(ans)
else:
  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
      if ans == n-2:
        ans += 1
      else:
        pass
  print(ans)

Submission status: Wrong Answer

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

l = []
cnt = 0
for i in range(N):
  if S[i] == "R":
    cnt += 1
    if i == N - 1:
      l.append([i - cnt + 1, i, cnt])
  else:
    l.append([i - cnt, i - 1, cnt])
    cnt = 0
l.sort(key = lambda x:x[2], reverse = True)

for k in range(min(K, len(l))):
  S[l[k][0] : l[k][1] + 1] = "L" * l[k][2]

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

print(ans)

Submission status: Wrong Answer

Solution code:
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 -= 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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(ans+1)
    exit()

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

    #print(ans)

if k > 0 and t == 0:
    ans += n-q-1

print(ans)

Submission status: Wrong Answer

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

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

for i in range(K):
  A.insert(0,0)
  A.append(0)
  A[A.index(max(A))]+=A[A.index(max(A))-1]+A[A.index(max(A))+1]
  A[A.index(max(A))-1]=0
  A[A.index(max(A))+1]=0
  B.append(A.pop(A.index(max(A))))
  for j in range(A.count(0)):
    A.remove(0)

a = [i-1 for i in A]
b = [i-1 for i in B]
print(sum(a)+sum(b))

Submission status: Wrong Answer

Solution code:
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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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 i==n-2:
        omake+=1
if m<k:
    print(ans+2*min(m,k)+omake)
else:
    print(ans+2*min(m,k))