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:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
ans = 0
B = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1
    elif l1 == l2:
        ans += 1

# Xかい+2
# Yかい+1できる
op = min(K, Y + X-1)  # op可能捜査回数

if X >= op:
    ans += 2*op
else:
    ans += 2*X
    op -= X
    if Y >= op:
        ans += op
    else:
        ans += Y
print(min(ans, N-1))

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
ans = 0
B = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1
    elif l1 == l2:
        ans += 1

# Xかい+2
# Yかい+1できる
op = min(K, Y + X-1)  # op可能捜査回数

if X >= op:
    ans += 2*op
else:
    ans += 2*X
    op -= X
    if Y >= op:
        ans += op
    else:
        ans += Y
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
ans = 0
B = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1
    elif l1 == l2:
        ans += 1

# Xかい+2
# Yかい+1できる
op = min(K, X-1)  # op可能捜査回数

if X >= op:
    ans += 2*op
else:
    ans += 2*X
    op -= X
    if Y >= op:
        ans += op
    else:
        ans += Y
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1

ans = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == l2:
        ans += 1

B = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "L" and l2 == "R":
        B += 1

op = min(B, K)
if X >= op:
    ans += 2*op
else:
    ans += 2*X
    op -= X
    assert op >= 0
    if Y >= op:
        ans += op
    else:
        ans += Y
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1

ans = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == l2:
        ans += 1

op = K
if X >= op:
    ans += 2*op
else:
    ans += 2*X
    op -= X
    if Y >= op:
        ans += op
    else:
        ans += Y
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1

ans = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == l2:
        ans += 1

B = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "L" and l2 == "R":
        B += 1

op = min(B, K)
if X >= op:
    ans += 2*op
else:
    ans += 2*X
    op -= X
    if Y >= op:
        ans += op
    else:
        ans += Y
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1

ans = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == l2:
        ans += 1

B = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "L" and l2 == "R":
        B += 1

op = min(B, K)
if X >= op:
    ans += 2*op
else:
    ans += 2*X
    B -= X
    if Y >= B:
        ans += B
    else:
        ans += Y
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1

ans = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == l2:
        ans += 1

if X >= K:
    ans += K*2
else:
    ans += X*2
    ans += min((K-X), Y)
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

Y = 0
if S[0] == "L":
    Y += 1
if S[-1] == "R":
    Y += 1
X = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == "R" and l2 == "L":
        X += 1

ans = 0
for l1, l2 in zip(S, S[1:]):
    if l1 == l2:
        ans += 1

if X >= K:
    ans += K*2
else:
    ans += X*2
    ans += (K-X)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k=map(int, input().split())
s=input()
dmL=0
dmR=0
cnt=0
maxnum=len(s)-1
for i in range(n):
  if s[i] == 'L' and dmL == 'L':
    cnt+=1
  if s[i] == 'R' and dmR == 'R':
    cnt+=1
  dmL=s[i]
  if i==n-2:
    dmR=0
  else:
    dmR=s[i]

print(min(maxnum,cnt+k*2))

Submission status: Wrong Answer

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

rl = 0
lr = 0

for i in range(n-1):
  if s[i] != s[i+1]:
    if s[i] == 'R':
      rl += 1
    else:
      lr += 1
ans = 0

for i in range(k):
  if rl == 0 or lr == 0:
    break
  rl -= 1
  lr -= 1
  
point = n - (2*rl) - lr

print(point)

Submission status: Wrong Answer

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

ans=0
for i in range(n-1):
    if s[i]==s[i+1]:
        ans+=1
count=0        
c='Q'
for i in range(n):
    if s[i]!=c:
        count+=1
        c=s[i]
    else:
        continue
if count==2:
    print(n-1)
else:
    count=count//2
    print(ans+min(count,k)*2)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
siawase = 0
 
LRchange_count = -1#初回必ず+1されるので、初期値を-1にしておく
LRittiFlg = True
temp_char = 'temp'
#temp_renzoku = 0
 
for s in S:
    #print(s,temp_char)
    if s != temp_char:
        LRchange_count += 1
    else:
        siawase += 1
    temp_char = s
    
if S[0] == S[-1]:
    LRittiFlg = True
else:
    LRittiFlg = False
    
if K+1 >= LRchange_count:
    print(len(S)-1)
# elif K-1 == LRchange_count:
#         if LRittiFlg:
#             print(K*2 + siawase)
#         else:
#             print(K*2 + siawase - 1)
else:
    print(max(K*2 + siawase,len(S)-1))

Submission status: Wrong Answer

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

LRchange_count = -1#初回必ず+1されるので、初期値を-1にしておく
LRittiFlg = True
temp_char = 'temp'
#temp_renzoku = 0

for s in S:
    #print(s,temp_char)
    if s != temp_char:
        LRchange_count += 1
    else:
        siawase += 1
    temp_char = s
    
if S[0] == S[-1]:
    LRittiFlg = True
else:
    LRittiFlg = False
    
if K+1 >= LRchange_count:
    print(len(S)-1)
# elif K-1 == LRchange_count:
#         if LRittiFlg:
#             print(K*2 + siawase)
#         else:
#             print(K*2 + siawase - 1)
else:
    print(K*2 + siawase)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc140/tasks/abc140_d

n, k = map(int, input().split())
str_n = input()
changed_str = [str_n]
base = {}
if str_n[0] == "R":
    base["f"] = "RL"
    base["l"] = "LR"
else:
    base["f"] = "LR"
    base["l"] = "RL"

for count in range(k): 
    ew_line = changed_str[count]
    first = ew_line.find(base["f"])
    last = ew_line.rfind(base["l"]) or -1
    change_count = last - first

    parts = {}   
    parts["head"] = ew_line[:first + 1]
    if last != -1:
        parts["body"] = ew_line[first + 1:last + 1][::-1]
        parts["foot"] = ew_line[last + 1:]
    else:
        parts["body"] = ew_line[first + 1:]
        parts["foot"] = ""
    
    parts["body"] = parts["body"].replace("R","C").replace("L","R").replace("C","L")

    new_line = "".join(parts.values())
    changed_str.append(new_line)

# print(changed_str[k])
count = 0
result_line = changed_str[k]
for i in range(len(changed_str[k]) - 1):
    if result_line[i] == result_line[i + 1]:
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
N,K = [int(zz) for zz in input().split()]
S = list(input())

L = []

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

L.append(cnt)

ans = sum(L)
p = len(L)
if p//3 >= K:
    ans += K*2
else:
    ans += p//3*2+p-1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
count_RL = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        count_RL += 1
count_edge = 0
if S[0] == 'L':
    count_edge += 1
if S[N-1] == 'R':
    count_edge += 1
count_K = 0
for i in range(K):
    if count_RL == 0:
        break
    count_RL -= 1
    count_K += 1
if (K - count_K) > 0:
    count = K - count_K
    if count_edge == 2:
        count_edge -= 1
not_happy = N - 2*count_RL - count_edge
print(not_happy)

Submission status: Wrong Answer

Solution code:
L1 = input().split(" ")
L2 = list(input())
L = 0
R = 0
S = 0

def change(lst):
    for i in range(len(lst)):
        if lst[i] == "R":
            lst[i] = "L"
        elif lst[i] == "L":
            lst[i] = "R"
    return lst

def replace(list, l, r):
    Ltr = list[:l] + change(list[l:r+1][::-1]) + list[r+1:]
    return Ltr

for i in range(int(L1[1])):
    for i in range(len(L2)):
        if L2[i] == "R":
            L = i
            break
    for i in range(len(L2)+1):
        if L2[-i] == "R":
            R = i
            break
    if R != L:
        L2 = replace(L2, L, len(L2)-R)
    else:
        break
for i in range(len(L2)):
    if L2[i] =="L" and i != 0 and L2[i-1] == "L":
        S += 1
    if L2[i] == "R" and i != len(L2)-1 and L2[i+1] == "R":
        S += 1  
print(S)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
count_RL = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        count_RL += 1
count_edge = 0
if S[0] == 'L':
    count_edge += 1
if S[N-1] == 'R':
    count_edge += 1
count_K = 0
for i in range(K):
    if count_RL == 0:
        break
    count_RL -= 1
    count_K += 1
if (K - count_K) > 0:
    count = K - count_K
    for i in range(count):
        if count_edge == 0:
            break
        count_edge -= 1
not_happy = N - 2*count_RL - count_edge
print(not_happy)

Submission status: Wrong Answer

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


def counthappy(s):
    happy = 0
    for i in range(0, len(s)):
        if i != 0 and s[i] == 'L':
            if s[i - 1] == 'L':
                happy += 1
        elif i != len(s) - 1 and s[i] == 'R':
            if s[i + 1] == 'R':
                happy += 1
    return happy


flag = 0
s_ = []
for i in range(0, N - 1):
    if S[i] != S[i + 1]:
        s_.append(S[flag:i + 1])
        flag = i + 1
        if flag == N - 1:
            s_.append(S[flag:N])

count = 0
for i in range(1, len(s_), 2):
    count += 1
    if s_[i][0] == 'L':
        s_[i] = s_[i].replace('L', 'R')
    elif s_[i][0] == 'R':
        s_[i] = s_[i].replace('R', 'L')

    if count == K:
        break

s_join = ''.join(s_)

happy = counthappy(s_join)
print(happy)

Submission status: Wrong Answer

Solution code:
L1 = input().split(" ")
L2 = input()
s = 0
n = 0

for i in range(len(L2)):
    if L2[i] == "R":
        if i != len(L2)-1 and L2[i+1] == "R":
            s += 1
        if i == len(L2)-1 or L2[i+1] == "L":
            n += 1
    if L2[i] == "L":
        if i != 0 and L2[i-1] == "L":
            s += 1

if n == 1 and L2[-1] == "R":
    n = 1/2
p = s + (min(n,int(L1[1]))*2)  
print(int(p))

Submission status: Wrong Answer

Solution code:
"""
Execution time over
"""

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


def counthappy(s):
    happy = 0
    for i in range(0, len(s)):
        if i != 0 and s[i] == 'L':
            if s[i - 1] == 'L':
                happy += 1
        elif i != len(s) - 1 and s[i] == 'R':
            if s[i + 1] == 'R':
                happy += 1
    return happy


def replace(s, k_):
    for l in range(0, N - 1):
        for r in range(l + 1, N):
            part_l = s[0:l]
            part = s[l:r+1]
            part_r = s[r+1:N][::-1]

            part = part.replace('L', '-')
            part = part.replace('R', 'L')
            part = part.replace('-', 'R')
            s_ = part_l + part + part_r

            happy = counthappy(s_)
            global answer
            if answer < happy:
                answer = happy
                if k_ < K:
                    replace(s_, k_ + 1)


answer = counthappy(S)
replace(S, 1)

print(answer)

Submission status: Wrong Answer

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


#count happy people
def count_happy(s):
    counter=False
    happy = 0
    for si in s:
        if si == "R" and counter:
            happy += 1
        elif si == "R":
            counter=True
        else:
            counter=False
    counter = False
    for si in s[::-1]:
        if si == "L" and counter:
            happy += 1
        elif si == "L":
            counter=True
        else:
            counter=False
    return happy

#group
happy = count_happy(s)

new_s = [s[0]]
last_char = s[0]
for si in s[1:]:
    if si == last_char:
        continue
    else:
        new_s.append(si)
        last_char = si

happy += min(len(new_s)//2, k)*2

if len(new_s)%2 == 0 and len(new_s)//2 == k:
    happy -= 1

print(happy)

Submission status: Wrong Answer

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

cnt=0
for i in range(1,n):
  if S[i-1]==S[i]:
    cnt+=1
print(min(n-1,cnt*2+k))

Submission status: Wrong Answer

Solution code:
# coding:utf-8

N, K = [int(i) for i in input().split()]
S = input()


def alterna(sword, base):
    """s文字列　l反転開始位置 r反転終了位置
    s文字列はLもしくはRを使用"""
    if base == 'L':
        searchword = 'R'
    else:
        searchword = 'L'
    l = sword.find(searchword)
    r = sword.rfind(searchword)
    alterstr = sword[l:r+1][::-1]

    alterstr = alterstr.replace('L', 'r')
    alterstr = alterstr.replace('R', 'l')
    alterstr = alterstr.upper()

    ans = (sword[0:l], alterstr, sword[r+1:])
    return ans


def countnum(s):
    "sのなかでs[i]が直前の文字と同じならスコア+1"
    score = 0
    for i in range(0, len(s)):
        if s[i] == 'L' and i > 0:
            if s[i-1] == s[i]:
                score += 1
        elif s[i] == 'R' and i < len(s)-1:
            if s[i+1] == s[i]:
                score += 1
    return score


answer = countnum(S)
bases = ('L', 'R')
for base in bases:
    midword = S
    headword, tailword = "", ""
    for _ in range(K):
        head, midword, tail = alterna(midword, base)
        headword += head
        tailword += tail
        happyword = headword + midword + tailword
        nownum = countnum(happyword)
        answer = max(answer, nownum)
print(answer)

Submission status: Wrong Answer

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

for i in range(N-1):
    if S[i]==S[i+1]:
        cnt+=1
print(cnt+K*2)

Submission status: Wrong Answer

Solution code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
flg = s[-1] == "R"
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg:
        print(cnt + (len(red_ls) - 1) * 2 + 1)
    else:
        print(cnt + len(red_ls) * 2)

Submission status: Wrong Answer

Solution code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
print(cnt)
flg = s[-1] == "R"
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg:
        print(cnt + (len(red_ls) - 1) * 2 + 1)
    else:
        print(cnt + len(red_ls) * 2)

Submission status: Wrong Answer

Solution code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i] and (s[i] != "L" or i != 0)])
s += "L"
red_ls = re.findall(r"RL", s)
flg = s[0] == "L"
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if not flg:
        print(cnt + (len(red_ls) - 1) * 2 + 1)
    else:
        print(cnt + len(red_ls) * 2)

Submission status: Wrong Answer

Solution code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i] and (s[i] != "L" or i != 0)])
s += "L"
red_ls = re.findall(r"RL", s)
flg = s[0] == "L"
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if not flg:
        print(cnt + (len(red_ls) - 1 * 2) + 1)
    else:
        print(cnt + len(red_ls) * 2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import re

N, K = tuple(map(int, sys.stdin.readline().rstrip().split()))
s = sys.stdin.readline().rstrip()

# print(N,K)
# print(s)


def rev_comp(s):
  tmp = s.translate(str.maketrans('LR', 'RL'))
  return tmp[::-1]

def score(s):
  sc = 0
  for i in range(len(s) - 1):
    sl = s[i:i+2]
    if sl == "RR" or sl == "LL":
      sc += 1
  return sc


lr = re.compile("LR.*RL")
rl = re.compile("RL.*LR")
lrl = re.compile("LRL")
rlr = re.compile("RLR")

r_l = re.compile("R+L+")
l_r = re.compile("L+R+")
ronly = re.compile("R+")
lonly = re.compile("L+")

flag = True

while K > 0 and flag:
  flag = False

  m = re.search(lrl, s)
  while m and K > 0:
    start = m.start()
    end = m.end()
    s = s[:start] + "LLL" + s[end:]

    flag = True
    K = K -1
    m = re.search(lrl, s)



  m = re.search(rlr, s)
  while m and K > 0:
    start = m.start()
    end = m.end()
    s = s[:start] + "RRR" + s[end:]
    flag = True
    K = K -1
    m = re.search(rlr, s)

  m = re.search(lr, s)
  while m and K > 0:
    start = m.start()
    end = m.end()
    print(start, end)
    
    s = s[:start + 1] + rev_comp(s[start + 1: end - 1]) + s[end - 1:]
    
    flag = True
    K = K -1
    m = re.search(lr, s)


  m = re.search(rl, s)
  while m and K > 0:
    start = m.start()
    end = m.end()
    print(start, end)
    
    s = s[:start + 1] + rev_comp(s[start + 1: end - 1]) + s[end - 1:]
    
    flag = True
    K = K -1
    m = re.search(rl, s)


flag = True
while K > 0 and flag:
  flag = False

  m = re.search(r_l, s)
  while m and K > 0:
    end = re.search(ronly, s).end()
    s = rev_comp(s[:end]) + s[end:]

    flag = True
    K = K -1
    m = re.search(r_l, s)
  

  m = re.search(l_r, s)
  while m and K > 0:
    end = re.search(lonly, s).end()
    s = rev_comp(s[:end]) + s[end: ]

    flag = True
    K = K -1
    m = re.search(r_l, s)

    
# print(s)

print(score(s))

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
x = 0
kohuku = 0
y = 0
if s[0] == "L" and s[-1] == "R":
  y = 1
for i in range(n-1):
  if s[i] == "R" and s[i+1] == "L":
    x += 1
  if s[i] == s[i+1]:
    kohuku += 1
while k != 0:
  if x != 0:
    kohuku += 2
    x -= 1
  else:
    if y == 1:
      kohuku += 1
      y = 0
  k -= 1
print(kohuku)

Submission status: Wrong Answer

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

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


NN=N
if RLcount > K:
    temp = (RLcount-K)
    NN = N-2*temp
if S[0]=="L":
    NN -= 1
if S[-1]=="R":
    NN -= 1
if RLcount <= K:
    NN = N+1

print(int(NN))

Submission status: Wrong Answer

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

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


NN=N
if RLcount > K:
    temp = (RLcount-K)
    NN = N-2*temp
if S[0]=="L":
    NN -= 1
if S[-1]=="R":
    NN -= 1
if RLcount <= K:
    NN = N-1

print(int(NN))

Submission status: Wrong Answer

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

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


temp = 0
if RLcount > K:
    temp = (RLcount-K)
    N -= 2*temp

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

print(N)

Submission status: Wrong Answer

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

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

rl_total = S.count("RL")
total += 2 * min(K, rl_total)
K -= min(K, rl_total)
if S[0] == 'L' and K > 0:
    total += 1
    K -= 1
if S[-1] == 'R' and K > 0:
    total += 1
print(total)

Submission status: Wrong Answer

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

cnt=0
for i in range(n-1):
  if s[i]!=s[i+1]:
    cnt+=1
happy=n-cnt-1
if cnt<=k:
  print(n-1)
else:
  print(happy+2*k)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
li=[]
seq=1
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        seq+=1
    else:
        li.append(seq)
        seq=1
        
li.append(seq)
length=len(li)
ans=sum(li)-length

for i in range(k):
    if length>=3:
        length+=-1
        ans+=2
    elif length==2:
        ans+=1
        length+=-1
    else:
        break


print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
li=[]
seq=1
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        seq+=1
    else:
        li.append(seq)
        seq=1
        
li.append(seq)
length=len(li)
ans=sum(li)-length

for i in range(k):
    if length>=3:
        length+=-1
        ans=+2
    elif length==2:
        ans+=1
        length+=-1
    else:
        break


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
str = list(input())
length = len(str)
a = list(str)
b = list(str)
max = -1
for i in range(k):
    for j in range(length):
        a[j] = str[j]

    for j in range(length):
        for m in range(j,length):
            a = list(str)
            for l in range(m-j+1):
                if str[j+l] == 'L':
                    a[m-l] = 'R'
                else:
                    a[m-l] = 'L'
            count = 0
            for l in range(length-1):
                if a[l] == a[l+1]:
                    count += 1
            if count > max:
                max = count
                for l in range(length):
                    b[l] = a[l]
    for j in range(length):
        str[j] = b[j]
print(max)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sc = 0
lr = 0
rl = 0

for i in range(n-1):
    c = s[i]
    nc = s[i+1]
    if c == nc:
        sc += 1
    elif c == 'L' and nc == 'R':
        lr += 1
    elif c == 'R' and nc == 'L':
        rl += 1

if rl > 1 and lr > 1:
    dc = max(lr, rl)
    oc = abs(lr-rl)
elif rl == 1 or lr == 1:
    dc = 0
    oc = 1
else:
    dc = 0
    oc = 0

dnum = min(k, dc)
onum = 0
if k - dc > 0 and oc == 1:
    onum += 1
print(onum + sc + dnum*2)

Submission status: Wrong Answer

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

sc = 0
lr = 0
rl = 0

for i in range(n-1):
    c = s[i]
    nc = s[i+1]

    if c == nc:
        sc += 1
    elif c == 'L' and nc == 'R':
        lr += 1
    elif c == 'R' and nc == 'L':
        rl += 1

if rl > 1 and lr > 1:
    dc = max(lr, rl)
    oc = abs(lr-rl)
elif rl == 1 or lr == 1:
    dc = 0
    oc = 1
else:
    dc = 0
    oc = 0

dnum = min(k, dc)
onum = 0
if k - dc > 0:
    onum += 1
print(onum + sc + dnum*2)

Submission status: Wrong Answer

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

sc = 0
lr = 0
rl = 0

for i in range(n-1):
    c = s[i]
    nc = s[i+1]

    if c == nc:
        sc += 1
    elif c == 'L' and nc == 'R':
        lr += 1
    elif c == 'R' and nc == 'L':
        rl += 1

dc = max(lr, rl)
oc = abs(lr-rl)

ans = sc
if k <= dc and dc != 1:
    ans += k*2

if k == dc+1 or dc == 1:
    ans += 1

if k > dc+1:
    ans += dc*2 + 1
print(ans)

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

unhappy = 0
if cnt + l + r <= K:
    unhappy = 1
elif cnt > K:
    unhappy = 2 * (cnt - K) + l + r
else:
    unhappy = l + r
print(N - unhappy)

Submission status: Wrong Answer

Solution code:
n,k  = map(int,input().split())
s = input()
res = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        res += 1

print (min(res+k*2,n))

Submission status: Wrong Answer

Solution code:
n,k  = map(int,input().split())
s = input()
res = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        res += 1

print (res+k*2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 与えられた文字列の幸福度は、LとRの入れ替わりの数だけで決まることに着目
# 幸福度は、最初の方は2ずつ上げることができる。
# 端数が出る場合は最後に1だけ上げられる

#        change   happiness
# LLLLL  0        4
# LLLRR  1        3 -> 4
# LLRRL  2        2 -> 4
# LLRLR  3        1 -> 3 -> 4


import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

prev = S[0]
change = 0
for ch in S[1:]:
    change += ch != prev
    prev = ch

happiness = N - 1 - change

# K回のうち、use回を使って、幸福度をuse * 2上昇させる
use = min(K, change // 2)
happiness += use * 2
K -= use

# まだねじれがあれば修正
if K > 0 and change > 0:
    happiness += 1

print(happiness)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K = MI()
    S = ST()
    before = "#"
    RLE = []
    for i,s in enumerate(S):
        if s != before:
            before = s
            RLE.append(1)                        
        else:
            RLE[-1]+=1
    ans = sum(RLE) - len(RLE) #すでに幸せ
    cnt = len(RLE)
    for i in range(K):
        if cnt >= 3:
            ans += 2
        elif  cnt == 2:
            ans += 1
        cnt -=1
        if not cnt:
            break
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K = MI()
    S = ST()
    before = "#"
    RLE = []
    for i,s in enumerate(S):
        if s != before:
            before = s
            RLE.append(1)                        
        else:
            RLE[-1]+=1
    ans = sum(RLE) - len(RLE) #すでに幸せ
    cnt = len(RLE)
    for i in range(K):
        if cnt >= 3:
            ans += 2
        elif 0 < cnt <= 2:
            ans += 1
        cnt -=1
    print(ans)
        
if __name__ == '__main__':
    main()