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 = str(input())
cnt = 0
l = 0
r = 0

for i in range(N-1):
    if i == 0 and S[i] == "L":
        l += 1
    if S[i] == "R" and S[i+1] == "L":
        cnt += 1
if S[N-1] == "R":
    r += 1
if cnt + l + r <= K:
    print(N-1)
elif cnt <= K:
    print(N+(K-cnt)-2)
else:
    print(N-2*(cnt-K)-1)

Submission status: Wrong Answer

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

last = ''
count = 0
face_count = 0
for i in range(len(s)):
    if i == 0: pass
    else:
        if s[i] == last:
            count += 1
        else:
            face_count += 0.5
    last = s[i]

if face_count >= k:
    print(count + 2*k)
elif face_count < k:
    print(n - k)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
    if P[N-1] == 'L':
        if K == count:
            print(count2 + (2*K) -1)
        elif K > count:
            print(count2 + (2 * count))
        else:
            print(count2 + (2*K))
    else:
        print(count2 + 2 * min(K, count))

else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
    if P[N-1] == 'R':
        if K == count:
            print(count2 + 2*K -1)
        elif K > count:
            print(count2 + 2*count)
        else:
            print(count2 + 2*K)
    else:
        print(count2 + 2 * min(K, count))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
    if P[N-1] == 'L':
        print(count2 + 2*min(K,count))
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
    if P[N-1] == 'R':
        print(count2 + 2*min(K,count))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
    if K == count:
        print(count2 + 2 * K - 1)
    elif K > count:
        print(count2 + 2 * count)
    else:
        print(count2 + 2 * K)
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
    if K == count:
        print(count2 + 2 * K - 1)
    elif K > count:
        print(count2 + 2 * count)
    else:
        print(count2 + 2 * K)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
    if P[N-1] == 'L':
        print(count2 + 2*min(K,count))
    else:
        if K == count:
            print(count2 + 2*K -1)
        elif K > count:
            print(count2 + 2*count)
        else:
            print(count2 + 2*K)
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
    if P[N-1] == 'R':
        print(count2 + 2*min(K,count))
    else:
        if K == count:
            print(count2 + 2*K -1)
        elif K > count:
            print(count2 + 2*count)
        else:
            print(count2 + 2*K)

Submission status: Wrong Answer

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

now = S[0]
change = 1
for i in range(1, len(S)):
    if now != S[i]:
        change += 1
    now = S[i]

if change == 1 or change == 2 or (change-2) <= K:
    print(N - 1)
else:
    print(N-change+2*K)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

"""解説
解説PDFを実装

考察問題。

与えられた文字列が
　L R L R R L

のとき、問題を単純化するため、左端にR、右端にLがあるものとする

(R) L R L R R L (L)

このとき、

・互いが向き合っている箇所（RL）を赤丸
・互いが反対方向を向いている箇所（LR）を青丸とする

赤丸と青丸を選んで操作をすれば、幸福な人を2人増やすことができる。ということに気づく必要がある

ただし、左端か右端を選んで操作をした場合、幸福な人は1人しか増やせない。

この性質を利用して、最大数を求める。


（入力例１）
i:  0  1 2 3 4 5 6  7
S: (R) L R L R R L (L) ... 幸福な人: 1人, RLの数: 3, LRの数: 2

1回目の操作で l=2, r=5 を選ぶと

i:  0  1 2 3 4 5 6  7
S: (R) L L L R L L (L) ... 幸福な人: 3人, RLの数: 2, LRの数: 1

もしもう一度操作できるなら、l=4, r=4 を選ぶと

i:  0  1 2 3 4 5 6  7
S: (R) L L L L L L (L) ... 幸福な人: 5人, RLの数: 1, LRの数: 0

もしこれ以上操作できたとしても、RLとLRの組は見つけることができないので、これ以上幸福な人を増やすことはできない

1回操作する毎に、RLの数とLRの数は1ずつ減る。なるべく左端または右端を選ばないように操作することで最大化することができる。

RLの数とLRの数は線形探索すればO(N)で数えることができる。
初期状態の幸福な人の数も線形探索でO(N)で調べることができる。

よってO(N)で答えを出せそうである。

"""


def solve():
    N, K = list(map(int, input().split()))
    S = input()
    S = "R" + S + "L"

    endRL = 0  # 端っこのRL
    RL, LR = 0, 0  # RLの数, LRの数
    happy_num = 0  # 幸福な人の数

    M = len(S)
    for i in range(1, M):
        if S[i-1]=="R" and S[i]=="L":
            if (i-1)==0 or i==(M-1):
                endRL += 1 if endRL == 0 else 0
            else:
                RL += 1
        elif S[i-1]=="L" and S[i]=="R":
            LR += 1

        # 初期状態の幸福な人の数を数える
        if (i-1)==0 or i==(M-1):
            continue
        if (S[i-1]=="L" and S[i]=="L") or (S[i-1]=="R" and S[i]=="R"):
            happy_num += 1

    # 幸福な人の数を可能な限り最大化する
    if RL >= LR:
        if K >= LR:
            happy_num += LR*2
            RL -= LR
            K -= LR
        else:
            happy_num += K*2
    else:
        if K >= RL:
            happy_num += RL*2
            LR -= RL
            K -= RL
            if K >= min(LR, endRL):
                happy_num += min(LR, endRL)
            else:
                happy_num += K
        else:
            happy_num += K*2

    print(happy_num)


"""別解
解説動画でやってること。

考察問題。

ランレングス法みたいな考え方をする。

LグループとRグループに分けて考える。

LグループとRグループの境目を選択して操作をすると、基本的に幸福な人の数を＋２できる

ただし、端っこの境目を選んだときのみ、＋１になる。


・入力例２を考える

## 初期状態
 L R R L R L R R L R L L R
   .         .         .     幸福な人：3人


## LグループとRグループの境目を選択して、操作をする

 L R R L R L R R L R L L R
  |___|

 L L L L R L R R L R L L R
   . . .     .         .     幸福な人：5人

## LグループとRグループの境目を選択して、操作をする

 L L L L R L R R L R L L R
        |_|

 L L L L L L R R L R L L R
   . . . . . .         .     幸福な人：7人

## LグループとRグループの境目を選択して、操作をする

 L L L L L L R R L R L L R
            |___|

 L L L L L L L L L R L L R
   . . . . . . . .     .     幸福な人：9人

## LグループとRグループの境目を選択して、操作をする

 L L L L L L L L L R L L R
                  |_|

 L L L L L L L L L L L L R
   . . . . . . . . . . .     幸福な人：11人

## LグループとRグループの境目を選択して、操作をする

 L L L L L L L L L L L L R
                        |_|

 L L L L L L L L L L L L L
   . . . . . . . . . . . .   幸福な人：12人


この考察からすると、初期状態の幸福な人の数を score とすると、

・K回操作して、初期状態の幸福な人に加えて 2*K 人を幸福にすることができたパターン = score + 2*K
・K回以下の操作で、すべての人を L(R) にすることができたパターン = N-1

のどちらかなので、最終的な幸福な人の数 ans は、

ans = min(score+2*K, N-1)

となる。

初期状態の幸福な人の数 score をO(N)で数えればよいだけなので、計算量はO(N)

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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

now = S[0]
change = 1
for i in range(1, len(S)):
    if now != S[i]:
        change += 1
    now = S[i]

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

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = list(map(int, input().split()))
    S = input()
    S = "R" + S + "L"

    endRL = 0  # 端っこのRL
    RL, LR = 0, 0  # RLの数, LRの数
    happy_num = 0  # 幸福な人の数

    M = len(S)
    for i in range(1, M):
        if S[i-1]=="R" and S[i]=="L":
            if (i-1)==0 or i==(M-1):
                endRL += 1
            else:
                RL += 1
        elif S[i-1]=="L" and S[i]=="R":
            LR += 1

        # 初期状態の幸福な人の数を数える
        if (i-1)==0 or i==(M-1):
            continue
        if (S[i-1]=="L" and S[i]=="L") or (S[i-1]=="R" and S[i]=="R"):
            happy_num += 1

    # 幸福な人の数を可能な限り最大化する
    if RL >= LR:
        if K >= LR:
            happy_num += LR*2
            RL -= LR
            K -= LR
        else:
            happy_num += K*2
    else:
        if K >= RL:
            happy_num += RL*2
            LR -= RL
            K -= RL
            if K >= min(LR, endRL):
                happy_num += min(LR, endRL)
            else:
                happy_num += K
        else:
            happy_num += K*2

    print(happy_num)

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

def get_ans():
    result = 0
    for i in range(1,n):
        if s[i] == s[i-1]:
            result += 1
    return result
ans = get_ans()
tmp = n - ans - 1

while k > 0 and tmp > 1:
    k -= 1
    tmp -= 2
    ans += 2
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

"""解説
解説PDFを実装

与えられた文字列が
　L R L R R L

のとき、問題を単純化するため、左端にR、右端にLがあるものとする

(R) L R L R R L (L)

このとき、

・互いが向き合っている箇所（RL）を赤丸
・互いが反対方向を向いている箇所（LR）を青丸とする

赤丸と青丸を選んで操作をすれば、幸福な人を2人増やすことができる。ということに気づく必要がある

ただし、左端か右端を選んで操作をした場合、幸福な人は1人しか増やせない。

この性質を利用して、最大数を求める。


（入力例１）
i:  0  1 2 3 4 5 6  7
S: (R) L R L R R L (L) ... 幸福な人: 1人, RLの数: 3, LRの数: 2

1回目の操作で l=2, r=5 を選ぶと

i:  0  1 2 3 4 5 6  7
S: (R) L L L R L L (L) ... 幸福な人: 3人, RLの数: 2, LRの数: 1

もしもう一度操作できるなら、l=4, r=4 を選ぶと

i:  0  1 2 3 4 5 6  7
S: (R) L L L L L L (L) ... 幸福な人: 5人, RLの数: 1, LRの数: 0

もしこれ以上操作できたとしても、RLとLRの組は見つけることができないので、これ以上幸福な人を増やすことはできない

1回操作する毎に、RLの数とLRの数は1ずつ減る。なるべく左端または右端を選ばないように操作することで最大化することができる。

RLの数とLRの数は線形探索すればO(N)で数えることができる。
初期状態の幸福な人の数も線形探索でO(N)で調べることができる。

よってO(N)で答えを出せそうである。

"""


def solve():
    N, K = list(map(int, input().split()))
    S = input()
    S = "R" + S + "L"

    endRL, endLR = 0, 0  # 端っこのRLとLRの数
    RL, LR = 0, 0  # RLの数, LRの数
    happy_num = 0  # 幸福な人の数

    M = len(S)
    for i in range(1, M):
        if S[i-1]=="R" and S[i]=="L":
            if (i-1)==0 or i==(M-1):
                endRL += 1
            else:
                RL += 1
        elif S[i-1]=="L" and S[i]=="R":
            if (i-1)==0 or i==(M-1):
                endLR += 1
            else:
                LR += 1

        # 初期状態の幸福な人の数を数える
        if (i-1)==0 or i==(M-1):
            continue
        if (S[i-1]=="L" and S[i]=="L") or (S[i-1]=="R" and S[i]=="R"):
            happy_num += 1

    # 幸福な人の数を可能な限り最大化する
    if RL >= LR:
        if K >= LR:
            happy_num += LR*2
            RL -= LR
            K -= LR
            if K >= min(RL, endLR):
                happy_num += min(RL, endLR)
            else:
                happy_num += K
        else:
            happy_num += K*2
    else:
        if K >= RL:
            happy_num += RL*2
            LR -= RL
            K -= RL
            if K >= min(LR, endRL):
                happy_num += min(LR, endRL)
            else:
                happy_num += K
        else:
            happy_num += K*2

    print(happy_num)



if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

s = input()

if s == 'LRL' or s == 'RLR' or s == 'LR' or s == 'RL' or s == 'L' or s == 'R':
    print(0)
    exit()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * max(count, 0) - 1)

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count, -1))

Submission status: Wrong Answer

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

s = input()

if s == 'LRL' or s == 'RLR' or s == 'LR' or s == 'RL' or s == 'L' or s == 'R':
    print(0)
    exit()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * max(count, 0) - 1 * (count <= 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count, -1))

Submission status: Wrong Answer

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

s = input()

if len(s) == 2 and s[0] != s[-1]:
    print(0)
    exit()

if s == 'LRL' or s == 'RLR':
    print(0)
    exit()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * max(count, 0) - 1 * (count <= 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count, -1))

Submission status: Wrong Answer

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

s = input()

if len(s) == 2 and s[0] != s[-1]:
    print(0)
    exit()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * max(count, 0) - 1 * (count <= 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count, -1))

Submission status: Wrong Answer

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

s = input()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * max(count, 0) - 1 * (count <= 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count, -1))

Submission status: Wrong Answer

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

s = input()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * count - 1 * (count <= 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count,-1))

Submission status: Wrong Answer

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

s = input()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * count - 1 * (count == 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - max(count,-1))

Submission status: Wrong Answer

Solution code:
nk = input().split(" ");
n = int(nk[0]);
k = int(nk[1]);
s = input();

hukou = 0;
for i in range(n-1) :
	if s[i] == 'R' and s[i+1] == 'L' :
		hukou += 2;

edge = 0;
if s[0] == 'L' :
	edge += 1;
if s[n-1] == 'R' :
	edge += 1;

while hukou > 0 and k > 0 :
	hukou -= 2;
	k -= 1;

while edge > 1 and k > 0 :
	edge -= 1;
	k -= 1;

ans = n-edge-hukou;
if n-edge-hukou <= n-1 :
	ans = n-1

print(ans);

Submission status: Wrong Answer

Solution code:
nk = input().split(" ");
n = int(nk[0]);
k = int(nk[1]);
s = input();

hukou = 0;
for i in range(n-1) :
	if s[i] == 'R' and s[i+1] == 'L' :
		hukou += 2;

edge = 0;
if s[0] == 'L' :
	edge += 1;
if s[n-1] == 'R' :
	edge += 1;

while hukou > 0 and k > 0 :
	hukou -= 2;
	k -= 1;

while edge > 1 and k > 0 :
	edge -= 1;
	k -= 1;

print(n-edge-hukou);

Submission status: Wrong Answer

Solution code:
nk = input().split(" ");
n = int(nk[0]);
k = int(nk[1]);
s = input();

hukou = 0;
for i in range(n-1) :
	if s[i] == 'R' and s[i+1] == 'L' :
		hukou += 2;

edge = 0;
if s[0] == 'L' :
	edge += 1;
if s[n-1] == 'R' :
	edge += 1;

while hukou > 0 and k > 0 :
	hukou -= 2;
	k -= 1;

while edge > 0 and k > 0 :
	edge -= 1;
	k -= 1;

print(n-edge-hukou);

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, sys.stdin.readline().split())
S = sys.stdin.readline()

score = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        score+=1
#print(S)
idx = []
for i in range(n):
    if S[i] == 'L':
        idx.append(i)
idx.append(-10)

s =-1
e =-1
ten = [0]
for i in range(len(idx)-1):
    if s ==-1:
        s = idx[i]
    if idx[i]+1 != idx[i+1]:
        e = idx[i]
        if s==0 and e ==n-1:
            pass
        elif s ==-1 or e==-1:
            pass
        elif s ==0 or e ==n-1:
            ten.append(1)
        else:
            ten.append(2)
        s =-1
ten = sorted(ten, reverse=True)
#print(score, ten)
print(score+ sum(ten[0:k]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# coding: UTF-8


#import scipy.optimize
import numpy as np

def turn(S,l,r):
    S[l-1:r] = [not(S[r-1-i]) for i in range(r-l+1)]
    return S

def AreYouHappy(S):
    Happy = 0
    N = len(S)
    if N==1:
        return Happy
    else:
        Happy += sum([(not(S[i]) and not(S[i+1])) for i in range(N-1)])
        Happy += sum([(S[i-1] and S[i]) for i in np.arange(1,N)])
        return Happy
    
##################################################################

if __name__ == "__main__":
    Input = [input() for i in range(2)]
    N,K = list(map(int,Input[0].split()))
    S = str(Input[1])
    S = [S[i]=='L' for i in range(N)]

    for Try in range(K):
        lLR = 0
        lRL = 0
        rLR = 0
        rRL = 0
        none = False
        for ind in range(N-1):
            if S[ind] and not(S[ind+1]):
                lLR = ind+2
            if not(S[ind]) and S[ind+1]:
                lRL = ind+2
            if lLR > 0 and lLR < ind+2 and not(S[ind]) and S[ind+1]:
                rLR = ind+1
                S = turn(S,lLR,rLR)
                break
            elif lRL > 0 and lRL < ind+2 and S[ind] and not(S[ind+1]):
                rRL = ind+1
                S = turn(S,lRL,rRL)
                break
            elif ind == N-2 and S[0] and rRL>0 :
                S = turn(S,1,rRL)
            elif ind == N-2 and not(S[-1]) and lLR>0:
                S = turn(S,lLR,N)
            elif ind == N-2:
                none = True
        if none:
            break
    Happy = AreYouHappy(S)
    
    print(Happy)

Submission status: Wrong Answer

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

for n in range(N-1):
    if S[n]==S[n+1]:
        total += 1
    if S[n]== 'R' and S[n+1]=='L':
        oppo += 1


if oppo > K:
        total += K*2
if oppo <= K and oppo != 0:
    if S[0] == S[N-1]:
        total += oppo*2
    elif oppo > 0:
        total += (oppo-1)*2+1
if oppo == 0:
    if total == N-1:
        total += 0
    if total != N-1:
        total += 1
print(total)

Submission status: Wrong Answer

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

x = 0
y = 0
if s[0] == 'L':
    y += 1
if s[-1] == 'R':
    y += 1

ans = 0
for i in range(n-1):
    ad = s[i:i+2]
    if ad == 'RL':
        x += 1
    if ad == 'LL' or ad == 'RR':
        ans += 1

while k > 0 and x > 0:
    ans += 2
    x -= 1
    k -= 1
while k > 0 and y > 1:
    ans += 1
    y -= 1
    k -= 1

print(ans)

Submission status: Wrong Answer

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

x = 0
y = 0
if s[0] == 'L':
    y += 1
if s[-1] == 'R':
    y += 1

ans = 0
for i in range(n-1):
    ad = s[i:i+2]
    if ad == 'RL':
        x += 1
    if ad == 'LL' or ad == 'RR':
        ans += 1

while k > 0 and x > 1:
    ans += 2
    x -= 1
    k -= 1
if k > 0 and x == 1:
    ans += 1
    k -= 1
while k > 0 and y > 1:
    ans += 1
    y -= 1
    k -= 1

print(ans)

Submission status: Wrong Answer

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

x = 0
y = 0
if s[0] == 'L':
    y += 1
if s[-1] == 'R':
    y += 1

ans = 0
for i in range(n-1):
    ad = s[i:i+2]
    if ad == 'RL':
        x += 1
    if ad == 'LL' or ad == 'RR':
        ans += 1

while k > 0 and x > 1:
    ans += 2
    x -= 1
    k -= 1
if k > 0 and x == 1:
    ans += 1
while k > 0 and y > 1:
    ans += 1
    y -= 1
    k -= 1

print(ans)

Submission status: Wrong Answer

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

for n in range(N-1):
    if S[n]==S[n+1]:
        total += 1
    if S[n]== 'R' and S[n+1]=='L':
        oppo += 1

if oppo > K:
        total += K*2
if oppo <= K and oppo != 0:
    if S[0] == S[N-1]:
        total += oppo*2
    elif oppo > 0:
        total += (oppo-1)*2+1
if oppo == 0:
        total += 1
print(total)

Submission status: Wrong Answer

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

x = 0
y = 0
if s[0] == 'L':
    y += 1
if s[-1] == 'R':
    y += 1

ans = 0
for i in range(n-1):
    ad = s[i:i+2]
    if i > 0 and ad == 'RL':
        x += 1
    if ad == 'LL' or ad == 'RR':
        ans += 1

while k > 0 and x > 0:
    ans += 2
    x -= 1
    k -= 1
while k > 0 and y > 1:
    ans += 1
    y -= 1
    k -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import deque
n, k = map(int, input().split())
s = input()

unhapiness = 0
div = deque([])
left = "R"
right = "L"
cntr = 0
s = s + "L"
for si in s:
    right = si
    if (left == "R") & (right == "L"):
        if 0 < cntr < n:
            unhapiness += 2
            div.append(cntr)
        else:
            unhapiness += 1
            div.appendleft(cntr)

    left = si
    cntr +=1

for i in range(k):
    if len(div) < 2:
        div.pop()
        unhapiness -= 1
        break
    p = div.pop()
    if 0 < p < n:
        unhapiness -= 2
    else:
        unhapiness -= 1

print(n-unhapiness)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

s = input()

if s[0] == 'L':
  count = s.count('LR')
else:
  count = s.count('RL')

count = count - m

if s[0] != s[-1]:
  print(n - 2 * count - 1 * (count == 0))

else:
  if count > 0:
    print(n - 2 * count - 1)
  else:
    print(n - 2 - count)

Submission status: Wrong Answer

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

for n in range(N-1):
    if S[n]==S[n+1]:
        total += 1
    if S[n]== 'R' and S[n+1]=='L':
        oppo += 1

if oppo > K:
        total += K*2
elif S[0] == S[N-1]:
        total += oppo*2
elif oppo > 0:
        total += (oppo-1)*2+1
elif oppo == 0:
        total += 1
print(total)

Submission status: Wrong Answer

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

i = 0
c = 0
cc = 0
while i < n:
	if s[i] == s[i - 1]:
		c += 1
	else:
		cc += 1

	i += 1

print(c + min(2 * k, cc))

Submission status: Wrong Answer

Solution code:
# -*- coding utf-8 -*-

MOD = 10 ** 9 + 7

N, K = map(int, input().split())
S = 'R' + input() + 'L'

ans = 0
for i in range(1, N + 1):
    j = i + 1 if S[i] == 'R' else i - 1
    if S[i] == S[j]:
        ans += 1

ans = min(ans + 2 * K, len(S) - 1)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	sls='R'+sl+'L'
	ss=0
	for cp, cn in zip(sls[:-1], sls[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'R':
			ss+=1
			
	for cp, cn in zip(sls[1:], sls[2:]):
		#print(cp,cn)
		if cp == cn and cp == 'L':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	sls='R'+sl+'L'
	ss=0
	for cp, cn in zip(sl[:-1], sl[1:]):
		if cp == cn and cp == 'R':
			ss+=1
			
	for cp, cn in zip(sl[1:], sl[2:]):
		if cp == cn and cp == 'L':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	sls='r'+sl+'l'
	ss=0
	for cp, cn in zip(sl[:-1], sl[1:]):
		if cp == cn and cp == 'R':
			ss+=1
			
	for cp, cn in zip(sl[1:], sl[2:]):
		if cp == cn and cp == 'L':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
if K > count:
    print(N-1)
elif K == count :
    print(count2 + 2 * (K-1))
else:
    print(count2 + 2 * K )

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
ans = 0
isOdd = False
isLastTurned = False
for i in range(0, n-1):
    if s[i] == s[i+1]:
        ans += 1
    elif k==0:
        isLastTurned = False
    else:
        isOdd = not isOdd
        if isOdd and k > 0:
            ans += 2
            k -= 1
            isLastTurned = True

if isLastTurned:
    ans -= 1
            
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
ans = 0
isOdd = False
isLastTurned = True
for i in range(0, n-1):
    if s[i] == s[i+1]:
        ans += 1
    elif k==0:
        isLastTurned = False
    else:
        isOdd = not isOdd
        if isOdd and k > 0:
            ans += 2
            k -= 1

if isLastTurned:
    ans -= 1
            
print(ans)

Submission status: Wrong Answer

Solution code:
import re

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

pattern = re.compile("RL")
X = len(pattern.findall(str))
result = n - X * 2
Y = 0
if str[0] == 'L':
    result -= 1
    Y += 1
if str[-1] == 'R':
    result -= 1
    Y += 1

if X + Y <= k:
    result = n
else:
    if X >= k:
        result += 2 * k
    else:
        result += 2 * X + (k-X)
print(result)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import deque
n, k = map(int, input().split())
s = input()

unhapiness = 0
div = deque([])
conv = deque([])
left = "R"
right = "L"
cntr = 0
s = s + "L"
for si in s:
    right = si
    if (left == "R") & (right == "L"):
        if 0 < cntr < n:
            unhapiness += 2
            div.append(cntr)
        else:
            unhapiness += 1
            div.appendleft(cntr)
    if (left == "L") & (right == "R"):
        conv.appendleft(cntr)

    left = si
    cntr +=1

for i in range(k):
    if len(conv) < 1:
        break
    p = div.pop()
    conv.pop()
    if 0 < p < n:
        unhapiness -= 2
    else:
        unhapiness -= 1

print(n-unhapiness)

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, sys.stdin.readline().split())
S = sys.stdin.readline()

score = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        score+=1
#print(S)
idx = []
for i in range(n):
    if S[i] == 'R':
        idx.append(i)
idx.append(-10)

s =-1
e =-1
ten = [0]
for i in range(len(idx)-1):
    if s ==-1:
        s = idx[i]
    if idx[i]+1 != idx[i+1]:
        e = idx[i]
        if s==0 and e ==n-1:
            pass
        elif s ==-1 or e==-1:
            pass
        elif s ==0 or e ==n-1:
            ten.append(1)
        else:
            ten.append(2)
        s =-1
ten = sorted(ten, reverse=True)
#print(score, ten)
print(score+ sum(ten[0:k]))

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, sys.stdin.readline().split())
S = sys.stdin.readline()

score = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        score+=1
#print(S)
idx = []
for i in range(n):
    if S[i] == 'R':
        idx.append(i)
idx.append(-10)

s =-1
e =-1
ten = []
for i in range(len(idx)-1):
    if s ==-1:
        s = idx[i]
    if idx[i]+1 != idx[i+1]:
        e = idx[i]
        if e-s == n-1:
            pass
        elif s ==-1 or e==-1:
            pass
        elif s ==0 or e ==n-1:
            ten.append(1)
        else:
            ten.append(2)
        s =-1
ten = sorted(ten, reverse=True)
#print(score, ten)
print(score+ sum(ten[0:k]))

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, sys.stdin.readline().split())
S = sys.stdin.readline()

score = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        score+=1
#print(S)
idx = []
for i in range(n):
    if S[i] == 'R':
        idx.append(i)
idx.append(-10)

s =-1
e =-1
ten = []
for i in range(len(idx)-1):
    if s ==-1:
        s = idx[i]
    if idx[i]+1 != idx[i+1]:
        e = idx[i]
        if e-s == n-1:
            pass
        elif s ==-1 and e==-1:
            pass
        elif s ==0 or e ==n-1:
            ten.append(1)
        else:
            ten.append(2)
        s =-1
ten = sorted(ten, reverse=True)
#print(score, ten)
print(score+ sum(ten[0:k]))

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, sys.stdin.readline().split())
S = sys.stdin.readline()

score = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        score+=1
#print(S)
idx = []
for i in range(n):
    if S[i] == 'R':
        idx.append(i)
idx.append(-10)

'''
def change(S, s, e):
    S = S[0:s] + 'L'*len(S[s:e+1]) + S[e+1:]
    return S
'''

s =-1
ten = []
for i in range(len(idx)-1):
    if s ==-1:
        s = idx[i]
    if idx[i]+1 != idx[i+1]:
        e = idx[i]
        #S = change(S, s, e)
        if e-s == n-1:
            pass
        elif s ==0 or e ==n-1:
            ten.append(1)
        else:
            ten.append(2)
        s =-1
ten = sorted(ten, reverse=True)
#print(score, ten)
print(score+ sum(ten[0:k]))