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:
# -*- coding: utf-8 -*-
"""
Created on Fri Dec  6 13:30:45 2019

@author: USER
"""

N,K=map(int,input().split())
S=list(map(ord,input()))
a=0
b=0
c=0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        a=a+1
    else:
        if S[i]==82 and S[i+1]==76:
            b=b+1
        else:
            c=c+1
d=min(b,c)
if d<=K:
    E=a+2*d
    if S[0]==76 and max(b,c)-d>=1:
        if b!=0 and c!=0 or S[len(S)-2]!=76:
             E=E+1
    if S[len(S)-1]==82 and max(b,c)-d>=2:
         if b!=0 and c!=0 or S[1]!=82:
             E=E+1
if   d>K:
     E=a+2*K

    
print(E)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Dec  6 13:30:45 2019

@author: USER
"""

N,K=map(int,input().split())
S=list(map(ord,input()))
a=0
b=0
c=0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        a=a+1
    else:
        if S[i]==82 and S[i+1]==76:
            b=b+1
        else:
            c=c+1
d=min(b,c)
if d<=K:
    E=a+2*d
    if S[0]==76 and max(b,c)-d>=1:
        E=E+1
    if S[len(S)-1]==82 and max(b,c)-d>=2:
        E=E+1
if   d>K:
     E=a+2*K

    
print(E)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Dec  6 13:30:45 2019

@author: USER
"""

N,K=map(int,input().split())
S=list(map(ord,input()))
a=0
b=0
c=0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        a=a+1
    else:
        if S[i]==82 and S[i+1]==76:
            b=b+1
        else:
            c=c+1
d=min(b,c)
if d<=K:
    E=a+2*d
else:
    E=a+2*K
print(E)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = str(input())
S = 'R' + S + 'L' # N+2

M1 = 0 # +2できる場所
M2 = 0 # +1できる場所
for n in range(N+1):
  if S[n]+S[n+1] == 'RL':
    if n == 0 or n == N:
      M1 += 1
    else:
      M2 += 1
      
cur_h = N - (M2*2) - (M1*1)
if cur_h == N-1:
    print(cur_h)
    exit()


if K < M2:
  res = cur_h + 2*K
elif M2 <= K and K < M2+M1:
  res = cur_h + 2*M2 + 1*(K-M2)
else:
  res = cur_h + 2*M2 + 1*M1

if res == N-1:
  res -= 1
print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = str(input())
S = 'R' + S + 'L' # N+2

M1 = 0 # +2できる場所
M2 = 0 # +1できる場所
for n in range(N+1):
  if S[n]+S[n+1] == 'RL':
    if n == 0 or n == N:
      M1 += 1
    else:
      M2 += 1
      
cur_h = N - (M2*2) - (M1*1)
if cur_h == N-1:
    print(cur_h)
    exit()


if K < M2:
  res = cur_h + 2*K
elif M2 <= K and K < M2+M1:
  res = cur_h + 2*M2 + 1*(K-M2)
else:
  res = cur_h + 2*M2 + 1*M1

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = str(input())
S = 'R' + S + 'L' # N+2

M1 = 0 # +2できる場所
M2 = 0 # +1できる場所
for n in range(N+1):
  if S[n]+S[n+1] == 'RL':
    if n == 0 or n == N:
      M1 += 1
    else:
      M2 += 1
      
cur_h = N - (M2*2) - (M1*1)

if K < M2:
  res = cur_h + 2*K
elif M2 <= K and K < M2+M1:
  res = cur_h + 2*M2 + 1*(K-M2)
else:
  res = cur_h + 2*M2 + 1*M1

print(res)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()
L=[]
R=[]
if n==1:
    print(0)
else:
    c=1
    for i in range(n-1):
        if S[i+1]==S[i]:
            c+=1
        else:
            if S[i]=='L':
                L.append(c)
                c=1
            else:
                R.append(c)
                c=1
    if S[n-1]=='L':
        L.append(c)
    else:
        R.append(c)
    Lsum=sum(L)
    Rsum=sum(R)
    if S[0]=='L':
        L[0]-=1
    else:
        R[0]-=1
    if S[n-1]=='L':
        L[-1]-=1
    else:
        R[-1]-=1
    LS=sorted(L,reverse=True)
    RS=sorted(R,reverse=True)
    LC=len(L)
    RC=len(R)
    rL=Lsum+sum(RS[:k])-(max(0,LC-k))
    rR=Rsum+sum(LS[:k])-(max(0,RC-k))
    print(max(rL,rR))

Submission status: Wrong Answer

Solution code:
import sys
import copy
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
s = input().rstrip()
S = [string for string in s]

houkou = 'L' if S.count('L')>=S.count('R') else 'R'
#print(houkou)

for _ in range(K):
	# dp[i文字目][L0/R1] = 繋ぎのかず
	dp = [[0] * 2 for _ in range(N+1)]
	for i in range(N):
		for j in range(2):
			if (j==0 and S[i]=='L') or (j==1 and S[i]=='R'):
				dp[i+1][j] = dp[i][j] + 1

	max_len = 0
	l, r = -1, -1
	#print(dp)
	for idx,d in enumerate(dp):
		if houkou == 'L': check = 1
		elif houkou == 'R': check = 0
		if d[check] > max_len:
			max_len = max(d)
			l = idx-max(d)
			r = idx

	#print(l, r)

	#print('irekae')
	#print(S)
	for i in range(l,r):
		if S[i]=='L': S[i]='R'
		elif S[i]=='R': S[i]='L'
	#print('irekae owari')
	#print(S)

ans = 0
for i in range(len(S)-1):
	#print(S[i], S[i+1], S[i]==S[i+1])
	if S[i]==S[i+1]: ans+=1

#print(S)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()
L=[]
R=[]
if n==1:
    print(0)
else:
    c=1
    for i in range(n-1):
        if S[i+1]==S[i]:
            c+=1
        else:
            if S[i]=='L':
                L.append(c)
                c=1
            else:
                R.append(c)
                c=1
    if S[n-1]=='L':
        L.append(c)
    else:
        R.append(c)
    LS=sorted(L,reverse=True)
    RS=sorted(R,reverse=True)
    LC=len(L)
    RC=len(R)
    rL=sum(L)+sum(RS[:k])-(max(1,LC-k))
    rR=sum(R)+sum(LS[:k])-(max(1,RC-k))
    print(max(rL,rR))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()
L=[]
R=[]
if n==1:
    print(0)
else:
    c=1
    for i in range(n-1):
        if S[i+1]==S[i]:
            c+=1
        else:
            if S[i]=='L':
                L.append(c)
                c=1
            else:
                R.append(c)
                c=1
    if S[n-1]=='L':
        L.append(c)
    else:
        R.append(c)
LS=sorted(L,reverse=True)
RS=sorted(R,reverse=True)
LC=len(L)
RC=len(R)
rL=sum(L)+sum(RS[:k])-(max(1,LC-k))
rR=sum(R)+sum(LS[:k])-(max(1,RC-k))
print(max(rL,rR))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()
L=[]
R=[]
if n==1:
    print(0)
else:
    c=1
    for i in range(n-1):
        if S[i+1]==S[i]:
            c+=1
        else:
            if S[i]=='L':
                L.append(c)
                c=1
            else:
                R.append(c)
                c=1
    if S[n-1]=='L':
        L.append(c)
    else:
        R.append(c)
LS=sorted(L,reverse=True)
RS=sorted(R,reverse=True)
LC=len(L)
RC=len(R)
rL=sum(L)+sum(RS[:k])-(LC-k)
rR=sum(R)+sum(LS[:k])-(RC-k)
print(max(rL,rR))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()
L=[]
R=[]
if n==1:
    print(0)
else:
    c=1
    for i in range(n-1):
        if S[i+1]==S[i]:
            c+=1
        else:
            if S[i]=='L':
                L.append(c)
                c=1
            else:
                R.append(c)
                c=1
    if S[n-1]=='L':
        L.append(c)
    else:
        R.append(c)
#LS=sorted(L,reverse=True)
RS=sorted(R,reverse=True)
LC=len(L)
#RC=len(R)
rL=sum(L)+sum(RS[:k])-(LC-k)
#rR=sum(R)+sum(LS[:k])-(RC-k)
print(rL)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
segment = 0
now = '#'
ans = 0
for i in s:
    if i==now:
        segment+=1
        now = i
    else:
        ans+=1
for i in range(k):
    t = 0
    if segment > 2:
        t = 2
    elif segment == 2:
        t = 1
    segment, ans = segment-t, ans+t
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
segment = 0
now = 'w'
ans = 0
for i in range(len(s)):
    if s[i]!=now:
        segment+=1
        now = s[i]
    else:
        ans+=1
for i in range(k):
    t = 0
    if segment > 2:
        t = 2
    elif segment == 2:
        t = 1
    segment, ans = segment+t, ans+t
print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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

# 複数個の数値を、intの配列として取得
S = input()

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

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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

# 複数個の数値を、intの配列として取得
S = input()

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

if S[0]=='L':
    print(min(N-1,bck+K*2))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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

# 複数個の数値を、intの配列として取得
S = input()

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

if S[0]=='L':
    print(min(N-1,bck+K*2))

Submission status: Wrong Answer

Solution code:


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

Submission status: Wrong Answer

Solution code:
from collections import deque
N,K = list(map(int,input().split()))
S = list(input())
S = deque(S)
ans = 0
S_2 = [S.popleft()]
while len(S) > 0:
    if S[0] == S_2[-1]:
        ans += 1
        S.popleft()
    else:
        S_2.append(S.popleft())
cnt = 0

for i in range(1,len(S_2)-1):
    if S_2[i] != S_2[i-1] and S_2[i] != S_2[i+1]:
        ans += 2
        K -= 1
        S_2[i] = S_2[i-1]
        if K ==0:
            break
if len(S_2) == 1:
    print(ans) 
elif len(S_2) == 2:
    if S_2[0] == S_2[1] or K == 0:
        print(ans)
    elif K > 1 and S_2[0] != S_2[1] and S_2[-1] != S_2[-2]:
        print(ans + 2)
    else:
        print(ans + 1)
else:
    if S_2[0] != S_2[1] and S_2[-1] != S_2[-2] and K > 1:
        print(ans + 2)
    elif K == 0 or (S_2[0] == S_2[1] and S_2[-1] == S_2[-2]):
        print(ans)
    else:
        print(ans + 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,K = list(map(int,input().split()))
S = list(input())
S = deque(S)
ans = 0
S_2 = [S.popleft()]
while len(S) > 0:
    if S[0] == S_2[-1]:
        ans += 1
        S.popleft()
    else:
        S_2.append(S.popleft())
cnt = 0

if len(S_2) == 1:
    print(ans) 
elif len(S_2) == 2:
    if S_2[0] == S_2[1] or K == 0:
        print(ans)
    elif K > 1 and S_2[0] != S_2[1] and S_2[-1] != S_2[-2]:
        print(ans + 2)
    else:
        print(ans + 1)
else:
    if S_2[0] != S_2[1] and S_2[-1] != S_2[-2] and K > 1:
        print(ans + 2)
    elif K == 0 or (S_2[0] == S_2[1] and S_2[-1] == S_2[-2]):
        print(ans)
    else:
        print(ans + 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,K = list(map(int,input().split()))
S = list(input())
S = deque(S)
ans = 0
S_2 = [S.popleft()]
while len(S) > 0:
    if S[0] == S_2[-1]:
        ans += 1
        S.popleft()
    else:
        S_2.append(S.popleft())
cnt = 0

for i in range(1,len(S_2)-1):
    if S_2[i] != S_2[i-1] and S_2[i] != S_2[i+1]:
        ans += 2
        K -= 1
        S_2[i] = S_2[i-1]
        if K ==0:
            break
if K > 0:
    if len(S_2) == 1:
        print(ans)
    elif len(S_2) == 2 and S_2[0] != S_2[1]:
        print(ans + 1)
    elif len(S_2) == 2 and S_2[0] == S_2[1]:
        print(ans)
    else:
        if (S_2[0] != S_2[1] or S_2[-1] != S_2[-2]) and K == 1:
            print(ans + 1)
        elif K > 1 and (S_2[0] != S_2[1] and S_2[-1] != S_2[-2]):
            print(ans + 2)
        elif K > 1 and (S_2[0] != S_2[1] and S_2[-1] == S_2[-2]) or (S_2[0] == S_2[1] or S_2[-1] != S_2[-2]):
            print(ans + 1)
        else:
            print(ans)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(' '))
S = input()
previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
    non_search_s = 'L'
else:
    search_s = 'L'
    non_search_s = 'R'

flag  = False
i = 0
count_s = 0
new_S = ''
previous_s = S[0]
while K != 0 and i != N:
    if previous_s != S[i]:
        if previous_s == search_s:
            #new_S += non_search_s*count_s
            K -= 1
        #else:
            #new_S += non_search_s*count_s
        previous_s = S[i]
        #count_s = 1
        #count_s += 1
        #if i+1 == N:
        #    flag =True
            #new_S += non_search_s*count_s
        
    i += 1
if K > 0:
    print(N-1)
else:
    new_S += non_search_s*(i-1)
    if not flag:
        new_S += S[i-1:N]
    print(new_S)
    count_s = 0
    previous_s = new_S[0]
    for i in range(1,N):
        if previous_s == new_S[i]:
            count_s +=1
        else:
            previous_s = new_S[i]
    print(count_s)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(' '))
S = input()
previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
    non_search_s = 'L'
else:
    search_s = 'L'
    non_search_s = 'R'

continue_flag = False
idx = 0
lst_idx =  -5
new_S = ''
while K != 0 and idx != N:
    if S[idx] == search_s:
        if not continue_flag:
            continue_flag = True
        new_S += non_search_s
    else:
        if continue_flag:
            continue_flag = False
            K -= 1
            last_idx = idx
        new_S += S[idx]
    idx += 1
new_S += S[idx:N]
print(new_S)

count_s = 0
previous_s = new_S[0]
for i in range(1,N):
    if previous_s == new_S[i]:
        count_s +=1
    else:
        previous_s = new_S[i]
print(count_s)

Submission status: Wrong Answer

Solution code:
# 2019-11-18 00:38:19(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


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

    # R を反転させる
    unhappy_count = 0
    if s[0] == 'L':
        unhappy_count += 1
    if s[-1] == 'R':
        unhappy_count += 1

    two_unhappy = [c.start() for c in re.finditer('RL', s)]
    
    unhappy_count += 2 * len(two_unhappy)
    if k >= len(two_unhappy):
        if 0 in two_unhappy:
            unhappy_count -= 1 + 2 * (len(two_unhappy) - 1)
        else:
            unhappy_count -= 2 * len(two_unhappy)
        unhappy_count = max(0, unhappy_count - (k - len(two_unhappy)))
    else:
        unhappy_count -= 2 * k

    happy_count = n - unhappy_count
    print(happy_count)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019-11-18 00:38:19(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


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

    # R を反転させる
    unhappy_count = 0
    if s[0] == 'L':
        unhappy_count += 1
    if s[-1] == 'R':
        unhappy_count += 1

    two_unhappy = [c.start() for c in re.finditer('RL', s)]
    
    if k <= len(two_unhappy):
        unhappy_count += 2 * (len(two_unhappy) - k)
    else:
        unhappy_count = max(0, unhappy_count - (k - len(two_unhappy)))
    
    happy_count = n - unhappy_count

    print(happy_count)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
else:
    search_s = 'L'
continue_flag = False
search_list = []
for i in range(1,N):
    if S[i] == search_s and continue_flag:
        if i + 1 == N:
            search_list.append(i+1-previous_idx)
        continue
    elif S[i] == search_s:
        continue_flag = True
        previous_idx = i
        if i + 1 == N:
            search_list.append(i+1-previous_idx)
    elif continue_flag:
        search_list.append(i-previous_idx)
        continue_flag = False
#print(search_list)
search_list.sort(reverse=True)
for i in range(K):
    if len(search_list) == i:
        break
    search_list[i] = 0
len_remain_s = len(search_list) - K

if len_remain_s == 0:
    print(N-1)
else:
    print(N -len_remain_s-sum(search_list))

Submission status: Wrong Answer

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

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

happy = 0
ncc = 1 # number of connected components
for i in range(N):
    if S[i] == 'L':
        if i > 0 and S[i - 1] == 'L':
            happy += 1
    elif S[i] == 'R':
        if i < N - 1 and S[i + 1] == 'R':
            happy += 1
    if i > 0 and S[i] != S[i - 1]:
        ncc += 1
if ncc >= 3:
    p = min(K, ncc // 2)
    ncc -= 2 * p
    happy += 2 * p
    K -= p
if ncc == 2 and K > 0:
    happy += 1
print(happy)

Submission status: Wrong Answer

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

happy = 0
ncc = 1 # number of connected components
for i in range(N):
    if S[i] == 'L':
        if i > 0 and S[i - 1] == 'L':
            happy += 1
    elif S[i] == 'R':
        if i < N - 1 and S[i + 1] == 'R':
            happy += 1
    if i > 0 and S[i] != S[i - 1]:
        ncc += 1
if ncc >= 3:
    p = min(K, (ncc + 1) // 2)
    ncc -= 2 * p
    happy += 2 * p
    K -= p
if ncc == 2 and K > 0:
    happy += 1
print(happy)

Submission status: Wrong Answer

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

happy = 0
ncc = 1 # number of connected components
for i in range(N):
    if S[i] == 'L':
        if i > 0 and S[i - 1] == 'L':
            happy += 1
    elif S[i] == 'R':
        if i < N - 1 and S[i + 1] == 'R':
            happy += 1
    if i > 0 and S[i] != S[i - 1]:
        ncc += 1
p = min(K, (ncc - 2) // 2)
ncc -= 2 * p
happy += 2 * p
K -= p
p = min(K, ncc - 1)
ncc -= p
happy += p
K -= p
print(happy)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    S = input()
    s = S.split("L")
    s = [i for i in s if i != '']
    s.sort(reverse=True)
    cnt = 0
    if S.count("L") == len(S) or S.count("R") == len(S):
        print(len(S)-1)
    else:
        for i in range(k):
            if i < len(s):
                cnt += len(s[i])
                s.remove(s[i])
        for i in s:
            if len(i) >= 2:
                cnt += len(i) - 1
        r = S.split('R')
        r = [i for i in r if i != '']
        for i in r:
            if len(i) >= 2:
                cnt += len(i) - 1
            if len(i) == 1 and k > 0:
                cnt += 1
                k -= 1
        print(cnt)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
count = 0
for i in range(N):
    if S[i] == "L":
        if i == 0:
            count += 1
        else:
            if S[i-1] == "R":
                count += 1
    else:
        if i == N-1:
            count += 1
        else:
            if S[i+1] == "L":
                count += 1

if count / 2 <= K:
    if count % 2 == 0:
        print(N-1)

else:
    print(N - (count - 2*K))

Submission status: Wrong Answer

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

l = 0
r = 0
count = 0
score = 0
for i in range(n):
    if s[i] == 'R':
        if r == 1:
            score += 1
        l = 0
        r = 1
    else:
        if l == 1:
            score += 1
        l = 1
        r = 0
l = 0
r = 0
for i in range(n):
    if s[i] == 'L':
        if l == 1 and r == 1:
            count += 1
        l = 1
        r = 0
    else:
        if l == 1:
            r = 1
if count >= k:
    ans = score + k*2
else:
    ans = score + count*2 + 1
print(ans)

Submission status: Wrong Answer

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

l = 0
r = 0
count = 0
score = 0
for i in range(n):
    if s[i] == 'R':
        if r == 1:
            score += 1
        l = 0
        r = 1
    else:
        if l == 1:
            score += 1
        l = 1
        r = 0
l = r = 0
for i in range(n):
    if s[i] == 'L':
        if l == 1 and r == 1:
            count += 1
        l = 1
        r = 0
    else:
        if l == 1:
            r = 1
if count >= k:
    ans = score + k*2
else:
    ans = score + count*2 + 1
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = input()
memo = S[0]
count = 0
total = 0
cntDict= dict.fromkeys([int(i) for i in range(1,N+1)],0)


for s in S:
    if s == memo:
        count +=1
    else:
        cntDict[count] += 1
        count = 1
    memo = s
cntDict[count] += 1
changeCnt = sum(cntDict.values())

for k,v in cntDict.items():
    if v == 0 and k == 1:
        continue
    
    total += (k-1)*v
        
if changeCnt-2 <= K:
    total = N-1
else:
    total += 2*K
        

print(total)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = input()
memo = S[0]
count = 0
total = 0
cntDict= dict.fromkeys([int(i) for i in range(1,N+1)],0)


for s in S:
    if s == memo:
        count +=1
    else:
        cntDict[count] += 1
        count = 1
    memo = s
cntDict[count] += 1
changeCnt = sum(cntDict.values())

for k,v in sorted(cntDict.items(), key=lambda x: -x[0]):
    if v == 0 and k == 1:
        continue
    
    total += (k-1)*v
        
if changeCnt-2 <= K:
    total = N-1
else:
    total += 2*K
        

print(total)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = input()
memo = S[0]
count = 0
total = 0
cntDict= dict.fromkeys([int(i) for i in range(1,N+1)],0)
flag = True

for s in S:
    if s == memo:
        count +=1
    else:
        cntDict[count] += 1
        count = 1
    memo = s
cntDict[count] += 1
changeCnt = sum(cntDict.values())

for k,v in sorted(cntDict.items(), key=lambda x: -x[0]):
    if v == 0 and k == 1:
        continue
    
    total += (k-1)*v
        
if changeCnt-2 < K:
    total = N-1
else:
    total += 2*K
        

print(total)

Submission status: Wrong Answer

Solution code:
import sys

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

def rotate_lr(s, l, r):
	new_s = s

	if r - l == 1:
		print(l + r)
		sys.exit()

	for i in range(l + 1, r):
		if new_s[i] == 'L':
			new_s[i] = 'R'
		else:
			new_s[i] = 'L'
	return new_s

l = 0
r = n
for i in range(k):
	for si in range(l, n - 1):
		if s[si] != s[si + 1]:
			l = si
			break

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

	s = rotate_lr(s, l, r)

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

print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

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

kohuku = 0

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

if len(s) == 1:
    print(1)
    exit()

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

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

kohuku = 0

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

if len(s) == 1:
    print(1)
    exit()

num_2 = s.count("RL")

num_1 = 0
if s[0] == "R":
    num_1 += 1
elif s[-1] == "R":
    num_1 += 1
if k <= num_2:
    print(2*k+kohuku)
    exit()

if num_1 == 0:
    print(2*num_2+kohuku)

elif num_1 == 1:
    print(2*num_2+1+kohuku)

else:
    if k == num_2 +1:
        print(2*num_2+1+kohuku)
    else:
        print(2*num_2+2+kohuku)

Submission status: Wrong Answer

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

cnt = 0

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()
i=0
a=0
for i in range(n-1):
  if S[i]==S[i+1]:
    a=a+1
    i=i=1
  else:
    i=i+1
for j in range(k):
  if a<(n-3):
    a=a+2
    k=k+1
  elif a==(n-2):
    a=a+1
    k=k+1
  else:
    k=k+1
print(a)

Submission status: Wrong Answer

Solution code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N, K = LI()
S = str(input())
ans = int(0)

R = []
L = []
before = "R"
if S[0]=="L":
    R.append(0)
    before = "L"
cur = int(0)
for s in S:
    if before==s:
        cur +=1
    else:
        if s=="L":
            R.append(cur)
            before = "L"
            cur = int(1)
        else:
            L.append(cur)
            before = "R"
            cur = int(1)

if s == "L":
    L.append(cur)
else:
    R.append(cur)

#print(R,L)
Rlen = len(R)
Llen = len(L)
#ansC = []
#for i in range(Rlen-K):
#    cur = int(0)
#    for j in range(K+1):
#        cur += R[i+j]
#    for j in range(K):
#        cur += L[i+j]
#    ansC.append(cur)
#for i in range(Llen-K):
#    cur = int(0)
#    for j in range(K):
#       cur += R[i+j+1]
#    for j in range(K+1):
#        cur += L[i+j]
#    ansC.append(cur)
#print(ansC)

ans = min(N-1, 2*K+sum(R)+sum(L)-Rlen-Llen)
if S[0]=="L":
    ans +=1
print(ans)

Submission status: Wrong Answer

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

B = input()
C = []
for i in range(N-1):
    if B[i]!=B[i+1]:
        C.append(B[i])
C.append(B[N-1])

if len(C) % 2 == 1:
    print(min(M,len(C)//2)*2+N-len(C))
else:
    if M<len(C)//2:
        print(M*2+N-len(C))
    else:
        print(len(C)*2+N-len(C)-1)

Submission status: Wrong Answer

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

B = input()
C = []
for i in range(N-1):
    if B[i]!=B[i+1]:
        C.append(B[i])
C.append(B[N-1])

if len(C) % 2 == 1:
    print(min(M,len(C))*2+N-len(C))
else:
    if M<len(C)//2:
        print(M*2+N-len(C))
    else:
        print(len(C)*2+N-len(C)-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
s=list(input())
count=0
x=0
y=0
s=['R']+s+['L']

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

    else:
        if s[i]=='R' and s[i+1]=='L':
            if i==n:
                x+=1
            else:
                y+=1
if m<y:
    print(count+2*m)
elif m==y:
    print(count+y*2-1)
else:
    print(count+y*2-1+x)

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
s=list(input())
count=0
x=0
y=0
s=['R']+s+['L']

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

    else:
        if s[i]=='R' and s[i+1]=='L':
            if i==n:
                x+=1
            else:
                y+=1
if m<y:
    print(count+m*2)
elif m==y:
    print(count+y*2-1)
else:
    print(count+y*2-1+x)

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
s=list(input())
count=0
x=0
y=0
s=['R']+s+['L']

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

    else:
        if s[i]=='R' and s[i+1]=='L':
            if i==n:
                x+=1
            else:
                y+=1
if m<y:
    print(count+m*2+x)
else:
    print(count+y*2-1+x)

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
s=list(input())
count=0
x=0
s=['R']+s+['L']

for i in range(1,n+1):
    if s[i]=='L':
        if s[i-1]==s[i]:
            count+=1
    else:
        if s[i]==s[i+1]:
            count+=1
       
for i in range(1,n+1):
    if s[1]=='L':
        if s[i-1]=='R' and s[i]=='L':
            if i==1:
                x+=1
            else:
                count+=2
                m-=1
                if m==0:
                    print(count)
                    exit()
    else:
        if s[i]=='R' and s[i+1]=='L':
            if i==n:
                x+=1
            else:
                count+=2
                m-=1
                if m==0:
                    print(count)
                    exit()
if x==0:
    print(count)
else:
    print(count+1)