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 = [l for l in input()]

# 変わり目の数をカウント
change_list = []
score = 0
for i,l in enumerate(s):
    if i != 0:
        if prev != l:
            change_list.append(i)
        else:
            score += 1
    prev = l

if len(change_list) >= 2:
    if len(change_list)-2 >= k:
        score += 2*k
    elif len(change_list)-1 == k:
        score += 2*(k-1) + 1
    else:
        score += 2*(len(change_list)-2) + 2
elif len(change_list) == 1:
    score += 1

print(score)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = [l for l in input()]

# 変わり目の数をカウント
change_list = []
score = 0
for i,l in enumerate(s):
    if i != 0:
        if prev != l:
            change_list.append(i)
        else:
            score += 1
    prev = l

if len(change_list)-2 >= k:
    score += 2*k
elif len(change_list)-1 == k:
    score += 2*(k-1) + 1
else:
    if len(change_list) == 1:
        score += 1
    else:
        score += 2*(len(change_list)-2) + 2

print(score)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S=input()
RL=S.count('RL')
LR=S.count('LR')
diff=RL+LR
happy=N-1-diff
if diff%2==0:
    print(happy+2*min(diff//2,K))
else:
    print(happy+2*K if (diff//2+1)>K else happy+2*diff-1)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S=input()
RL=S.count('RL')
LR=S.count('LR')
diff=RL+LR
happy=N-1-diff
if diff%2==0:
    print(happy+2*min(diff//2,K))
else:
    print(happy+2*K if diff>K else happy+2*diff-1)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S=input()
happy=s=0
if S[0]=='R':
    pre='R'
else:
    pre='L'
    s+=1
for i in S[1:]:
    if pre == i:
        happy+=1
    else:
        pre=i
        if i == 'L':
            s+=1
if pre == 'R':
    s+=1
if len(S)==2:
    if S[0]==S[1]:
        print(1)
    elif K>0:
        print(1)
elif S[0]=='L' and S[-1]=='R':
    s-=1
    if s > K:
        print(happy+2*K)
    else:
        print(happy+2*(s-1)+1)
else:
    s-=1
    print(happy+2*min(K,s))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S=input()
happy=s=0
if S[0]=='R':
    pre='R'
else:
    pre='L'
    s+=1
for i in S[1:]:
    if pre == i:
        happy+=1
    else:
        pre=i
        if i == 'L':
            s+=1
if pre == 'R':
    s+=1
if len(S)==1:
    print(0)
elif S[0]=='L' and S[-1]=='R':
    s-=1
    if s > K:
        print(happy+2*K)
    else:
        print(happy+2*(s-1)+1)
else:
    s-=1
    print(happy+2*min(K,s))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S=input()
happy=s=0
if S[0]=='R':
    pre='R'
else:
    pre='L'
    s+=1
for i in S[1:]:
    if pre == i:
        happy+=1
    else:
        pre=i
        if i == 'L':
            s+=1
if pre == 'R':
    s+=1
if S[0]=='L' and S[-1]=='R':
    s-=1
    if s > K:
        print(happy+2*K)
    else:
        print(happy+2*(s-1)+1)
else:
    s-=1
    print(happy+2*min(K,s))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(str, input()))
ans = 0
x = k
j = 1
w = n-2
while j<=w and x > 0:
    if s[j] != s[j-1]and s[w] != s[w+1]:
      if j==w:
        ans += 1
      else:
        ans += 2
      x -= 1
      j += 1
      w -= 1
    elif s[j] == s[j-1]:
      j += 1
      ans += 1
    elif s[w] == s[w+1]:
      w -= 1
      ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(str, input()))
ans = 0
x = k
j = 1
w = n-2
while j<=w and x > 0:
    if s[j] != s[j-1]and s[w] != s[w+1]:
      if j==w:
        ans += 1
      else:
        ans += 2
      x -= 1
    elif s[j] == s[j-1]:
      j += 1
      ans += 1
    elif s[w] == s[w+1]:
      w -= 1
      ans += 1
print(ans)

Submission status: Wrong Answer

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

RL = s.count("RL")
KADO = 0
if s[0] == "L":
    KADO += 1
if s[-1] == "R":
    KADO += 1

if k < RL:
    RL -= k
elif k > RL and KADO == 2:
    RL = 0
    KADO -= 1
else:
    RL = 0

print(n-(RL*2 + KADO))

Submission status: Wrong Answer

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

RL = s.count("RL")
KADO = 0
if s[0] == "L":
    KADO += 1
if s[-1] == "R":
    KADO += 1

if k < RL:
    RL -= k
elif k > RL and KADO == 2:
    KADO -= 1

print(len(s)-(RL*2 + KADO))

Submission status: Wrong Answer

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

happy_people=len(S)
unhappy_edge_place=0
unhappy_not_edge_place=0

if S[0]=='L':
    happy_people-=1
    unhappy_edge_place+=1
if S[len(S)-1]=='R':
    happy_people-=1
    unhappy_edge_place+=1
for i in range(len(S)-1):
    if S[i]=='R' and S[i+1]=='L':
        happy_people-=2
        unhappy_not_edge_place+=1

#print(happy_people,unhappy_edge_place,unhappy_not_edge_place)

ans=0

if (unhappy_not_edge_place-1)>=K:
    ans=happy_people+2*K

else:
    ans+=happy_people+2*max(unhappy_not_edge_place-1,0)
    if unhappy_not_edge_place>=1:
        ans+=1
    #if unhappy_not_edge_place<K:
        #ans+=min(unhappy_edge_place,1)

print(ans)

Submission status: Wrong Answer

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

happy_people=len(S)
unhappy_edge_place=0
unhappy_not_edge_place=0

if S[0]=='L':
    happy_people-=1
    unhappy_edge_place+=1
if S[len(S)-1]=='R':
    happy_people-=1
    unhappy_edge_place+=1
for i in range(len(S)-1):
    if S[i]=='R' and S[i+1]=='L':
        happy_people-=2
        unhappy_not_edge_place+=1

#print(happy_people,unhappy_edge_place,unhappy_not_edge_place)

ans=0

if (unhappy_not_edge_place-1)>=K:
    ans=happy_people+2*K

else:
    ans+=happy_people+2*max(unhappy_not_edge_place-1,0)
    if unhappy_not_edge_place>=1:
        ans+=1
    if unhappy_not_edge_place<K:
        ans+=min(unhappy_edge_place,1)

print(ans)

Submission status: Wrong Answer

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

happy_people=len(S)
unhappy_edge_place=0
unhappy_not_edge_place=0

if S[0]=='L':
    happy_people-=1
    unhappy_edge_place+=1
if S[len(S)-1]=='R':
    happy_people-=1
    unhappy_edge_place+=1
for i in range(len(S)-1):
    if S[i]=='R' and S[i+1]=='L':
        happy_people-=2
        unhappy_not_edge_place+=1

#print(happy_people,unhappy_edge_place,unhappy_not_edge_place)

ans=0

if (unhappy_not_edge_place-1)>=K:
    ans=happy_people+2*K

else:
    ans+=happy_people+2*max(unhappy_not_edge_place-1,0)+1

print(ans)

Submission status: Wrong Answer

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

def happy_now(s):
    number1=0
    number2=0
    number3=0
    if s[0]=='L':
        number1+=1
        number2+=1
    if s[len(s)-1]=='R':
        number1+=1
        number2+=1
    for i in range(len(s)-1):
        if s[i]=='R' and s[i+1]=='L':
            number1+=2
            number3+=1
    return [len(s)-number1,number2,number3]

happy_people=happy_now(S)[0]
unhappy_edge_place=happy_now(S)[1]
unhappy_not_edge_place=happy_now(S)[2]

#print(happy_people,unhappy_edge_place,unhappy_not_edge_place)

ans=0

if unhappy_not_edge_place-1>=K:
    ans=happy_people+2*K

else:
    ans=happy_people+2*max(unhappy_not_edge_place-1,0)
    if K-(unhappy_not_edge_place-1)>=3:
        ans+=1+unhappy_edge_place
    elif K-(unhappy_not_edge_place-1)==2:
        ans+=min(1+unhappy_edge_place,2)
    elif K-(unhappy_not_edge_place)==1:
        ans+=min(1+unhappy_edge_place,1)

print(min(ans,len(S)-1))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
RLcount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[i] == 'L' and s[i - 1] == 'R':
        RLcount += 1
#print("now HP =>{}".format(happyCount))
happyCount += min(k, RLcount) * 2
if k > RLcount:
    happyCount += 1
print(happyCount)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[pre] != s[i]:
        sepS.append(s[pre:i])
        pre = i
sepS.append(s[pre:n])
RLcount = len(sepS) - 2
happyCount += min(k, RLcount) * 2
if k > RLcount:
    k -= RLcount
    happyCount += min(2,k)
print(happyCount)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n, k = map(int, input().split())
s = input()
a = []
count = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        a.append(count)
        count = 1
    else:
        count += 1
a.append(count)
ans = 0
for i in a:
    ans += i - 1
b = []
if len(a) == 1:
    print(n - 1)
    exit()
for i in range(len(a)):
    if i == 0:
        b.append((a[i] + a[i + 1], a[i], 2))
    elif i == len(a) - 1:
        b.append((a[i - 1] + a[i], a[i], 2))
    else:
        b.append((a[i - 1] + a[i] + a[i + 1], a[i], 3))
b = sorted(b, key=itemgetter(1))
b = sorted(b, reverse=True, key=itemgetter(0))
if k < len(b):
    for i in range(k):
        if b[i][2] == 3:
            ans += 2
        else:
            ans += 1
    print(ans)
else:
    print(n - 1)

Submission status: Wrong Answer

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

if s[0] == 'L' and s[n-1] == 'L':
    s = 'R' + s
    cnt = 0
    for i in range(n):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    elif cnt == k+1:
        print(n-1)
    else:
        print(n-2*(cnt-1)-1+2*k)

elif s[0] == 'L' and s[n-1] == 'R':
    s = 'R' + s + 'L'
    cnt = 0
    for i in range(n+1):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    elif cnt == k+1:
        print(n-1)
    elif cnt == k+2:
        print(n-2)
    else:
        print(n-2*(cnt-2)-2+2*k)

elif s[0] == 'R' and s[n-1] == 'L':
    cnt = 0
    for i in range(n-1):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    else:
        print(n-2*cnt+2*k)

elif s[0] == 'R' and s[n-1] == 'R':
    s = s + 'R'
    cnt = 0
    for i in range(n):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    elif cnt == k+1:
        print(n-1)
    else:
        print(n-2*(cnt-1)-1+2*k)

Submission status: Wrong Answer

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

if s[0] == 'L' and s[n-1] == 'L':
    s = 'R' + s
    cnt = 0
    for i in range(n):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n)
    elif cnt == k+1:
        print(n-1)
    else:
        print(n-2*(cnt-1)-1+2*k)

elif s[0] == 'L' and s[n-1] == 'R':
    s = 'R' + s + 'L'
    cnt = 0
    for i in range(n+1):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n)
    elif cnt == k+1:
        print(n-1)
    elif cnt == k+2:
        print(n-2)
    else:
        print(n-2*(cnt-2)-2+2*k)

elif s[0] == 'R' and s[n-1] == 'L':
    cnt = 0
    for i in range(n-1):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n)
    else:
        print(n-2*cnt+2*k)

elif s[0] == 'R' and s[n-1] == 'R':
    s = s + 'R'
    cnt = 0
    for i in range(n):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n)
    elif cnt == k+1:
        print(n-1)
    else:
        print(n-2*(cnt-1)-1+2*k)

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
s = list(input())
left = 0
right = 0
if s[0] == "L":
    left = 1
if s[n-1] == "R":
    right = 1
count = 0
if n == 1:
    print(1)
    sys.exit()
for i in range(n-1):
    if s[i] == "R" and s[i+1] == "L":
        count += 1
if k < count:
    count -= k
    ans = n - count*2 - left - right
    print(ans)
elif k == count:
    if left == right == 1:
        print(n-2)
    else:
        print(n-1)
else:
    print(n-1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
l = 0
sumx = sum([S[i]==S[i+1] for i in range(N-1)])
for i in range(N-1):
    if K>0:
        if not S[i]==S[i+1]:
            l+=1
        if l==2:
            l=0
            K-=1
            sumx+=2
l=0
for i in range(N-1):
    if K>0:
        if not S[i]==S[i+1]:
            l+=1
        if l==1:
            K-=1
            l=0
            sumx+=1
        
print(sumx)

Submission status: Wrong Answer

Solution code:
l,k=map(int,input().split())
s=input()
ss=[]
tmp=s[0]
for i in range(1,l):
  if s[i]==tmp[-1]:
    tmp+=s[i]
  else:
    ss.append(tmp)
    tmp=s[i]
ss.append(tmp)
ll=len(ss)
if k>=ll//2:
  print(l-1)
else:
  s2=''
  cnt=0
  for i in range(ll):
    if i%2==0:
      s2+=ss[i]
    else:
      if cnt<k:
        cnt+=1
        if ss[i]=='L':
          s2+='R'*len(ss[i])
        else:
          s2+='L'*len(ss[i])
      else:
        s2+=ss[i]
  ans=0
  cnt=0
  for i in range(1,l):
    if s2[i-1]==s2[i]:
      cnt+=1
    else:
      ans+=cnt
      cnt=0
  ans+=cnt
  print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

#L,Rの境界の数だけで処理を済ませる
#⇒いちいち書いてあるとおりに実装しない。

#内側の不幸な人を(RLの人)を消せば幸福な人が2人増える (全部Rに変えていくイメージ)
# ⇒着目するのは RL でも LR　でもどっちでもいい。
#  LRに注目する場合はこのあとのコードのRとLに関連する部分が逆になるだけ
#端っこなら１人増える
#幸福な人の最大値は N - 1

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

#端以外の人のせいで不幸な人の数
count = 0
#端の人のせいで不幸な人の数
edge_count = 0

#端のせいで不幸な人
if S[0] == 'L':
    edge_count += 1
if S[N-1] == 'R':
    edge_count += 1 

#端以外のせいで不幸になっている人の数を数える (RLを探す)
for i in range(1,N-1):
    if S[i] == 'R':
        if S[i+1] == 'L':
            count += 1

#最初から幸せな人の数
happy = N - (count*2 + edge_count)

#操作回数KがRLの数より小さい
if count >= K:
    print(min(N-1, K * 2 + happy  ))
#操作回数K がRLの数より大きい
else:
    print(min(N-1, K * 2 + happy + (min(edge_count,K-count) )))

Submission status: Wrong Answer

Solution code:

#L,Rの境界の数だけで処理を済ませる
#⇒いちいち書いてあるとおりに実装しない。

#内側の不幸な人を(RLの人)を消せば幸福な人が2人増える (全部Rに変えていくイメージ)
# ⇒着目するのは RL でも LR　でもどっちでもいい。
#  LRに注目する場合はこのあとのコードのRとLに関連する部分が逆になるだけ
#端っこなら１人増える
#幸福な人の最大値は N - 1

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

#端以外の人のせいで不幸な人の数
count = 0
#端の人のせいで不幸な人の数
edge_count = 0

#端のせいで不幸な人
if S[0] == 'L':
    edge_count += 1
if S[N-1] == 'R':
    edge_count += 1 

#端以外のせいで不幸になっている人の数を数える (RLを探す)
for i in range(1,N-1):
    if S[i] == 'R':
        if S[i+1] == 'L':
            count += 1

#最初から幸せな人の数
happy = N - (count*2 + edge_count)

#操作回数KがRLの数より小さい
if count >= K:
    print(min(N-1, K * 2 + happy  ))
#操作回数K がRLの数より大きい
else:
    print(min(N-1, K * 2 + happy + (K-count) ))

Submission status: Wrong Answer

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

unhappy_0=s.count('RL')*2+1
print(n-max(unhappy_0-k*2,1))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
S = input()

cnt1 = 0
cnt2 = 0

for i in range(N-1):
    # edge
    if i == 0 and S[i] == 'L':
        cnt1 += 1
    if i == N-2 and S[i+1] == 'R':
        cnt1 += 1 

    # not edge
    if S[i] == S[i+1]:
        continue
    elif S[i] == 'L' and S[i+1] == 'R':
        continue
    elif S[i] == 'R' and S[i+1] == 'L':
        cnt2 += 1

if cnt2 >= K:
    print(max(1, N - (cnt2-K)*2 - cnt1))
else:
    print(max(1, N - max(0, (cnt1 - (K - cnt2)))))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
S = input()

cnt1 = 0
cnt2 = 0

for i in range(N-1):
    # edge
    if i == 0 and S[i] == 'L':
        cnt1 += 1
    if i == N-2 and S[i+1] == 'R':
        cnt1 += 1 

    # not edge
    if S[i] == S[i+1]:
        continue
    elif S[i] == 'L' and S[i+1] == 'R':
        continue
    elif S[i] == 'R' and S[i+1] == 'L':
        cnt2 += 1

print('cnt1:', cnt1)
print('cnt2:', cnt2)

if cnt2 >= K:
    print(max(1, N - (cnt2-K)*2 - cnt1))
else:
    print(max(1, N - max(0, (cnt1 - (K - cnt2)))))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
S = input()

cnt1 = 0
cnt2 = 0

for i in range(N-1):
    # edge
    if i == 0 and S[i] == 'L':
        cnt1 += 1
    if i == N-2 and S[i+1] == 'R':
        cnt1 += 1 
    # not edge
    if S[i] == S[i+1]:
        continue
    elif S[i] == 'L' and S[i+1] == 'R':
        continue
    elif S[i] == 'R' and S[i+1] == 'L':
        cnt2 += 1

if cnt2 >= K:
    print(N - (cnt2-K)*2 - cnt1)
else:
    print(N - max(0, (cnt1 - (K - cnt2))))

Submission status: Wrong Answer

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

S = SS[:]

key = SS[0]
if key == 'R':
    key2 = 'L'
else:
    key2 = 'R'

c = 0
for i in range(N):
    if SS[i] == key2:
        S[i] = key
    if key == 'R':
        if i != 0 and SS[i] == 'L' and SS[i-1] == 'R':
            c += 1
    else:
        if i != N-1 and SS[i] == 'R' and SS[i+1] == 'L':
            c += 1
    #print(i, c, S, SS)                                                                  

    if c >=  K:
        break
        
w = 0
for i in range(N):
    #print(i, w, S[i], S)                                                                
    if i == 0:
        if S[i] == 'L':
            continue
        else:
            if S[i+i] == 'R':
                w += 1
    elif i == N - 1:
        if S[i] == 'R':
            continue
        else:
            if S[i-1] == 'L':
                w += 1
    else:
        if S[i] == 'L':
            if S[i-1] == 'L':
                w += 1
            else:
                continue
        else:
            if S[i+1] == 'R':
                w += 1
            else:
                continue

print(w)

Submission status: Wrong Answer

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


c = 0
while(c < K):
    flag = 0
    s = -1
    e = -1
    for i in range(N):
        if S[i] == 'R' and flag == 0:
            s = i
            flag = 1
        if S[i] == 'L' and flag == 1:
            e = i-1

        if s > 0 and e > 0:
            break

    if s == -1:
        break

    if e == -1:
        e = len(S) - 1

    L = ''
    for i in range(s, e+1):
        L += 'L'

    S = S[:s]+L+S[e+1:]
    c += 1
    
w = 0
for i in range(N):
    #print(i, w, S[i], S)                                                                
    if i == 0:
        if S[i] == 'L':
            continue
        else:
            if S[i+i] == 'R':
                w += 1
    elif i == N - 1:
        if S[i] == 'R':
            continue
        else:
            if S[i-1] == 'L':
                w += 1
    else:
        if S[i] == 'L':
            if S[i-1] == 'L':
                w += 1
            else:
                continue
        else:
            if S[i+1] == 'R':
                w += 1
            else:
                continue

print(w)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


ans = 0
for i in range(len(S)):
    if S[i] == 'L':
        if i >= 1:
            if S[i-1] == 'L':
                ans += 1
    if S[i] == 'R':
        if i <= len(S)-2:
            if S[i+1] == 'R':
                ans +=1

c = 0
for i in range(len(S)-1):
    if S[i:i+2] == 'RL':
        ans += 2
        c += 1
        if c == K:
            print(ans)
            sys.exit()


if S[0] != S[-1]:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
import numpy as np
# from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

#### input
N, K = map(int, input().split())
S = input()
# a = list(map(int, input().split()))
# a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


# n = int(input())
# t = [int(input()) for i in range(n)]
if em: sys.stdin = open('/dev/tty')

#### functions
# @lru_cache(maxsize=10000)

#### process
# if em: embed()

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

num_R = len([s for s in S if s == "R"])

if num_R > len(S) / 2:
  S2 = S.split("R")
else:
  S2 = S.split("L")

edge     = len([s for s in [S2[0],S2[-1]] if s != ""])
non_edge = len([s for s in S2[1:-1] if s != ""])

# if em:embed()

if edge == 0 and non_edge == 0:
  print(score)
  exit()

score = score + 2 * min(K, non_edge)
K = K - min(K, non_edge)
score = score + K

print(score)

## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
import numpy as np
# from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

#### input
N, K = map(int, input().split())
S = input()
# a = list(map(int, input().split()))
# a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


# n = int(input())
# t = [int(input()) for i in range(n)]
if em: sys.stdin = open('/dev/tty')

#### functions
# @lru_cache(maxsize=10000)

#### process
# if em: embed()

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

num_R = len([s for s in S if s == "R"])

if num_R > len(S) / 2:
  S2 = S.split("R")
else:
  S2 = S.split("L")

edge     = len([s for s in [S2[0],S2[-1]] if s != ""])
non_edge = len([s for s in S2[1:-1] if s != ""])

if em:embed()

score = score + 2 * min(K, non_edge)
K = K - min(K, non_edge)
score = score + K

print(score)

## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
import numpy as np
# from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

#### input
N, K = map(int, input().split())
S = input()
# a = list(map(int, input().split()))
# a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


# n = int(input())
# t = [int(input()) for i in range(n)]
if em: sys.stdin = open('/dev/tty')

#### functions
# @lru_cache(maxsize=10000)

#### process
# if em: embed()

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

num_R = len([s for s in S if s == "R"])

if num_R > len(S) / 2:
  S2 = S.split("L")
else:
  S2 = S.split("R")

edge     = len([s for s in [S2[0],S2[-1]] if s != ""])
non_edge = len([s for s in S2[1:-1] if s != ""])

if em:embed()

score = score + 2 * min(K, non_edge)
K = K - min(K, non_edge)
score = score + K

print(score)

## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
import numpy as np
# from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

#### input
N, K = map(int, input().split())
S = input()
# a = list(map(int, input().split()))
# a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


# n = int(input())
# t = [int(input()) for i in range(n)]
if em: sys.stdin = open('/dev/tty')

#### functions
# @lru_cache(maxsize=10000)

#### process
# if em: embed()

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

Sr = S.split("L")

edge     = len([s for s in [Sr[0],Sr[-1]] if s != ""])
non_edge = len([s for s in Sr[1:-1] if s != ""])

if em:embed()

score = score + 2 * min(K, non_edge)
K = K - min(K, non_edge)
score = score + K

print(score)

## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Wrong Answer

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

if s[0] == 'R':
    left_happy = 1
else:
    left_happy = 0
if s[-1] == 'L':
    right_happy = 1
else:
    right_happy = 0
edge_happy = left_happy + right_happy

renzoku = 0
original_happy = 0
before = 0
unhappy = 0
for i in range(n):
    now = s[i]
    if before == now:
        renzoku += 1
    else:
        original_happy += renzoku
        renzoku = 0
    if before == 'R' and now == 'L':
        unhappy += 1
    before = now
original_happy += renzoku

if edge_happy == 0:
    if k <= unhappy:
        print(original_happy + k*2)
    else:
        print(original_happy + unhappy*2 + 1)
elif edge_happy == 1:
    if k <= unhappy:
        print(original_happy + k*2)
    else:
        print(print(original_happy + unhappy*2))
else:
    if k < unhappy:
        print(original_happy + k*2)
    else:
        print(original_happy + unhappy*2 - 1)

Submission status: Wrong Answer

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

def g(t):
    l = []
    b = "x"
    c = 0
    for i, t_i in enumerate(t):
        if b != t_i:
            l.append(c)
            c = 0
        c += 1
        b = t_i
    l.append(c)
    return l[1:]

l = g(S)
len_l = len(l)
score = sum(l)-len_l
if len_l%2:
    score += min(len_l//2, K)*2
else:
    score += min(len_l//2, K)*2
    if len_l//2 <= K:
        print(score)
        score -= 1

print(score)

Submission status: Wrong Answer

Solution code:
if __name__=='__main__':
    a = [int(i) for i in input().split(' ')]
    n = a[0]
    k = a[1]
    s = input()
    cnt = 0
    ans = 0
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            cnt += 1
        else:
            ans += 1
    if cnt == k:
        ans += k*2 -1
    if cnt > k:
        ans += k*2
    if cnt < k:
        ans = len(s)-1
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, S: str):
    Y = 0
    X = 0

    if S[0] == "L":
        Y += 1
    if S[-1] == "R":
        Y += 1

    for i in range(len(S)-1):
        if S[i:i+2] == "RL":
            X += 1

    H = len(S) - 2 * X - Y
    H += min(K,X) * 2
    if K-X > 0:
        H += min(K-X, Y)

    print(H)



# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, K, S)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

p = 0
while K > 0 and p < len(S):
	now = S[p]
	p += 1
	if p >= len(S):
		break
	while now == S[p]:
		p += 1
		if p >= len(S):
			break

	q = p + 1
	if q >= len(S):
		"""
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		"""
		break
	while S[p] == S[q]:
		q += 1
		if q >= len(S):
			break
	while p < q:
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		p += 1
		if p >= len(S):
			break
	K -= 1

ret = 0;
print(len(S))
for x in range(len(S)-1):
	print(x)
	if S[x] == S[x + 1]:
		ret += 1
print(ret)

Submission status: Wrong Answer

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

p = 0
while K > 0 and p < len(S):
	now = S[p]
	p += 1
	if p >= len(S):
		break
	while now == S[p]:
		p += 1
		if p >= len(S):
			break

	q = p + 1
	if q >= len(S):
		break
	while S[p] == S[q]:
		q += 1
		if q >= len(S):
			break
	while p < q:
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		p += 1
		if p >= len(S):
			break
	K -= 1

ret = 0;
for x in range(len(S)-1):
	if S[x] == S[x + 1]:
		ret += 1
print(ret)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    c = 0
    cl = 0
    mcl = 0
    cr = 0
    mcr = 0
    for i in range(n):
        if i != 0:
            if s[i] != s[i-1]:
                c += 1
                
        if s[i] == "R":
            cr += 1
            mcl += max((cl - 1), 0)
            cl = 0
        else:
            cl += 1
            mcr += max((cr - 1), 0)
            cr = 0

        if i == n - 1:
            mcl += max((cl - 1), 0)
            mcr += max((cr - 1), 0)

            

if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
happy = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    happy += 1
for i in range(K):
  happy += 1
  if happy == N-1:
    break
  elif happy == N-2:
    happy += 1
    break
  else:
    happy += 1
print(happy)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
happy_counter = 0
for count in range(1, n):
    if s[count] == s[count - 1]:
        happy_counter += 1
happy_counter += k * 2
if happy_counter > n:
    happy_counter = n

print(happy_counter)