Problem:


Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7



Sample input:
6 1
LRLRRL


Sample output:
3

Submission status: Wrong Answer

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

# find the largest subsequence
packet = []
direction = [S[0]]
seq = 1
for i in range(1, N - 1):
    if S[i] == direction[-1]:
        seq += 1
    else:
        packet.append(seq)
        seq = 1
        direction.append(S[i])
if S[N - 1] == direction[-1]:
    seq += 1
    packet.append(seq)
else:
    packet.append(seq)
    seq = 1
    direction.append(S[N - 1])
    packet.append(seq)

#print(packet)


packet_len = len(packet)
for _ in range(K):
    if packet_len == 1 or packet_len == 2:
        print(sum(packet) - 1)
        exit()

# There is no need to choose the 'most invertible' index!!!

    else:
        val = packet[0] + packet[1] + packet[2]
        packet = [val] + packet[3:]
        packet_len -= 2

    #print(packet)

ans = 0
for v in packet:
    ans += v - 1
print(ans)

Submission status: Wrong Answer

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

    collisionList = []
    happy = 0

    for i in range(N-1):
        if S[i] == S[i + 1]:
            happy += 1
        else:
            collisionList.append(S[i]+S[i+1])

    if len(collisionList) <= 2:
        edge = len(collisionList)
        middle = 0
    else:
        middleList = list(filter(lambda x: x == 'LR', collisionList[1:-1]))
        middle = len(middleList)
        edge = 2
    
    if middle <= K:
        happy += middle * 2
        happy += min(edge, 2)
    else:
        happy += K * 2

    print(happy)

if __name__ == '__main__':
    func()

Submission status: Wrong Answer

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

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

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


if __name__ == '__main__':
    d()

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
ans = 0
LR, RL = 0, 0
for i in range(1, n):
    if s[i-1] + s[i] == "LR":
        LR += 1
    if s[i-1] + s[i] == "RL":
        RL += 1
    if min(LR, RL) > 0:
        LR -= 1
        RL -= 1
        ans += 2
        LR, RL = RL, LR
        k -= 1
        if not k:
            break
S = list(s)
for i in range(1, n):
    if S[i-1] == S[i]:
        ans += 1
for i in range(1, n):
    if k and S[i-1] + S[i] == "RL":
        k -= 1
        S[i-1] = "L"
        ans += 1
for i in range(1, n):
    if k and S[i-1] + S[i] == "LR":
        k -= 1
        S[i] = "R"
        ans += 1
print(ans)

Submission status: Wrong Answer

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

    collisionList = []
    happy = 0

    for i in range(N-1):
        if S[i] == S[i + 1]:
            happy += 1
        else:
            collisionList.append(S[i]+S[i+1])

    if len(collisionList) <= 2:
        edge = len(collisionList)
        middle = 0
    else:
        middleList = list(filter(lambda x: x == 'LR', collisionList[1:-1]))
        middle = len(middleList)
        edge = 2
    
    if middle <= K:
        happy += middle * 2
        K -= middle
        happy += min(K * edge, 2)
    else:
        happy += K * 2

    print(happy)

if __name__ == '__main__':
    func()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
if s[0]==s[n-1]:
  x=ct
  ct-=min(ct,k)
  ct*=2
  ct+=1
  print(n-ct)
else:
  x=ct
  ct-=min(ct,k)
  ct*=2
  if x<=k:
    ct+=1
  else:
    if s[0]=='L':
      ct+=2
  print(n-ct)

Submission status: Wrong Answer

Solution code:
def swap_list(S, l, r):
  result = list(S)
  count = r - l + 1
  for i in range(0, count):
    if S[r-i] == "R":
      result[l+i] = "L"
    elif S[r-i] == "L":
      result[l+i] = "R"
    
  return result 

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

current = 0
for i in range(0, K):
  l_RL_index = -1
  l_LR_index = -1
  r_RL_index = N-1
  r_LR_index = N-1
  start = current
  for j in range(start, N-1):  
    if S[j] == "R" and S[j+1] == "L":
      if l_LR_index != -1 and l_LR_index < j:
        r_RL_index = j
        break
      elif l_RL_index == -1:
        l_RL_index = j + 1
        j = j + 1 
    elif S[j] == "L" and S[j+1] == "R":
      if l_RL_index != -1 and l_RL_index < j:
        r_LR_index = j
        break
      elif l_LR_index == -1:
        l_LR_index = j + 1
        j = j + 1
  if r_RL_index < r_LR_index:
    S = swap_list(S, l_LR_index, r_RL_index)
    current = l_LR_index
  elif r_LR_index < r_RL_index:
    S = swap_list(S, l_RL_index, r_LR_index)
    current = l_RL_index
  else:
    if l_RL_index != -1:
      #print ("S = {0}, l_RL = {1}, r_LR = {2}".format(S, l_RL_index, r_LR_index))
      S = swap_list(S, l_RL_index, r_LR_index)
      current = l_RL_index
      #print("S_swap = {0}".format(S))
    elif l_LR_index != -1:
      #print ("S = {0}, l_LR = {1}, r_RL = {2}".format(S, l_LR_index, r_RL_index))
      S = swap_list(S, l_LR_index, r_RL_index)
      current = l_LR_index
      #print("S_swap = {0}".format(S))
  
  
result = 0
for i in range(0, N):
  if i == 0:
    if S[i] == "R" and S[i+1] == "R":
      result += 1
  elif i == N-1:
    if S[i] == "L" and S[i-1] == "L":
      result += 1
  else:
    if (S[i] == "R" and S[i+1] == "R") or (S[i] == "L" and S[i-1] == "L"):
      result += 1

print(result)

Submission status: Wrong Answer

Solution code:
import re
a = input().split(' ')
N = int(a[0])
K = int(a[1])
S = input()

LR = re.findall(r'LR', S)
RL = re.findall(r'RL', S)

num1 = len(LR)
num2 = len(RL)

if num1 < num2:
  num = num1
else:
  num = num2
  
if K < num:
  num = K
  
if (num1 == 0 and num2 != 0) or (num2 == 0 and num1 != 0):
  point = 1
else:
  point = num * 2
  
for i in range(N):
  if i != 0:
    if S[i] == S[i-1]:
      point = point + 1


print(point)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
ans = 0
LR, RL = 0, 0
for i in range(1, n):
    if s[i-1] + s[i] == "LR":
        LR += 1
    if s[i-1] + s[i] == "RL":
        RL += 1
    if min(LR, RL) > 0:
        LR -= 1
        RL -= 1
        ans += 2
        LR, RL = RL, LR
        k -= 1
        if not k:
            break
S = list(s)
for i in range(1, n):
    if S[i-1] == S[i]:
        ans += 1
    elif k:
        k -= 1
        if S[i-1] == "R":
            S[i-1] = "L"
        else:
            S[i-1] = "R"
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
def swap_list(S, l, r):
  result = list(S)
  count = r - l + 1
  for i in range(0, count):
    if S[r-i] == "R":
      result[l+i] = "L"
    elif S[r-i] == "L":
      result[l+i] = "R"
    
  return result 

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

current = 0
for i in range(0, K):
  l_RL_index = -1
  l_LR_index = -1
  r_RL_index = N-1
  r_LR_index = N-1
  for j in range(current, N-1):  
    if S[j] == "R" and S[j+1] == "L":
      if l_LR_index != -1 and l_LR_index < j:
        r_RL_index = j
        break
      elif l_RL_index == -1:
        l_RL_index = j + 1
        j = j + 1 
    elif S[j] == "L" and S[j+1] == "R":
      if l_RL_index != -1 and l_RL_index < j:
        r_LR_index = j
        break
      elif l_LR_index == -1:
        l_LR_index = j + 1
        j = j + 1
  if r_RL_index < r_LR_index:
    S = swap_list(S, l_LR_index, r_RL_index)
    current = l_LR_index
  elif r_LR_index < r_RL_index:
    S = swap_list(S, l_RL_index, r_LR_index)
    current = l_RL_index
  else:
    if l_RL_index != -1:
      #print ("S = {0}, l_RL = {1}, r_LR = {2}".format(S, l_RL_index, r_LR_index))
      S = swap_list(S, l_RL_index, r_LR_index)
      current = l_RL_index
      #print("S_swap = {0}".format(S))
    elif l_LR_index != -1:
      #print ("S = {0}, l_LR = {1}, r_RL = {2}".format(S, l_LR_index, r_RL_index))
      S = swap_list(S, l_LR_index, r_RL_index)
      current = l_LR_index
      #print("S_swap = {0}".format(S))
  
  
result = 0
for i in range(0, N):
  if i == 0:
    if S[i] == "R" and S[i+1] == "R":
      result += 1
  elif i == N-1:
    if S[i] == "L" and S[i-1] == "L":
      result += 1
  else:
    if (S[i] == "R" and S[i+1] == "R") or (S[i] == "L" and S[i-1] == "L"):
      result += 1

print(result)

Submission status: Wrong Answer

Solution code:
import re
a = input().split(' ')
N = int(a[0])
K = int(a[1])
S = input()

LR = re.findall(r'LR', S)
RL = re.findall(r'RL', S)

num1 = len(LR)
num2 = len(RL)

if num1 < num2:
  num = num1
else:
  num = num2
  
if K < num:
  num = K
  
if num == 0:
  point = 1
else:
  point = num * 2
  
for i in range(N):
  if i != 0:
    if S[i] == S[i-1]:
      point = point + 1


print(point)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
x=ct
ct-=min(ct,k)
ct*=2
if x>0 and x<=k and s[0]!=s[n-1]:
  ct+=1
if s[0]=='L':
  ct+=1
if s[n-1]=='R':
  ct+=1
if  x>0 and x<k:
  ct-=1
print(n-ct)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
x=ct
ct-=min(ct,k)
ct*=2
if x>0 and x<=k:
  ct+=1
if s[0]=='L':
  ct+=1
if s[n-1]=='R':
  ct+=1
if x<k:
  ct-=1
print(n-ct)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
x=ct
ct-=min(ct,k)
ct*=2
if x>0 and x<k:
  ct+=1
if s[0]=='L':
  ct+=1
if s[n-1]=='R':
  ct+=1
if x<k:
  ct-=1
print(n-ct)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n-1:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
                if count==n-1:break
            else:break
        k -=1
    else:count +=1

ans=0
for i in range(n-1):
    if s[i]==s[i+1]:
        ans +=1
print(max(ans,n-1))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n-1:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
                if count==n-1:break
            else:break
        k -=1
    else:count +=1

ans=0
for i in range(n-1):
    if s[i]==s[i+1]:
        ans +=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

pressed, cnts = press(S)

pressed = ["@"] + pressed + ["@"]

Xl, Yl = 0, 0
Xr, Yr = 0, 0
for i in range(1, len(pressed)-1):
    if pressed[i] == "L":
        if pressed[i-1] == "R" and pressed[i+1] == "R":
            Xl += 1
        else:
            Yl += 1
    elif pressed[i] == "R":
        if pressed[i-1] == "L" and pressed[i+1] == "L":
            Xr += 1
        else:
            Yl += 1

ans = sum([c-1 for c in cnts])

res1 = 2*min(Xl, K) + min(Yl, max(K-Xl, 0))
res2 = 2*min(Xr, K) + max(Yr, max(K-Xr, 0))
ans += max(res1, res2)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline
n,k = map(int,input().split())
s = input()
# print(n)
# print(k)
# 文字列sの幸福度をカウントする関数
def count_happy(s):
    count = 0
    for i in range(1,len(s)-1):
        if s[i]==s[i-1]:
            count += 1
    return count

# print(count_happy(s))
ans = count_happy(s) + 2*k
ans_final = min(n-1,ans)
print(ans_final)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
s = input()
print(n)
print(k)
# 文字列sの幸福度をカウントする関数
def count_happy(s):
    count = 0
    for i in range(1,len(s)-1):
        if s[i]==s[i-1]:
            count += 1
    return count

# print(count_happy(s))
ans = count_happy(s) + 2*k
ans_final = min(n-1,ans)
print(ans_final)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline
n,k = map(int,input().split())
s = input()
# 文字列sの幸福度をカウントする関数
def count_happy(s):
    count = 0
    for i in range(1,len(s)-1):
        if s[i]==s[i-1]:
            count += 1
    return count

# print(count_happy(s))
ans = count_happy(s) + 2*k
ans_final = min(n-1,ans)
print(ans_final)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
ct=0
for i in range(n-1):
  if s[i]=='R' and s[i+1]=='L':
    ct+=1
x=ct
ct-=min(ct,k)
ct*=2
if s[0]=='L':
  ct+=1
if s[n-1]=='R':
  ct+=1
if x<k:
  ct-=1
print(n-ct)

Submission status: Wrong Answer

Solution code:
#Face Produces Unhappiness

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

score = 0
for i in range(N):
    score += 1 if S[i-1] == S[i] else 0
ans = min(score+2*K, N-1)
print (ans)

Submission status: Wrong Answer

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

# 文字列sの幸福度をカウントする関数
def count_happy(s):
    count = 0
    for i in range(1,len(s)-1):
        if s[i]==s[i-1]:
            count += 1
    return count

# print(count_happy(s))
ans = count_happy(s) + 2*k
ans_final = min(n-1,ans)
print(ans_final)

Submission status: Wrong Answer

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

if n == 1:
  print(0)
  exit()

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

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

lr, k = max(0, lr - k), max(0, k - lr)
edge = max(0, edge - k)
happy = n - (lr * 2 + edge)
print(happy)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [1]+list(map(lambda x:-1 if x == "L" else 1, input()))+[-1]

happy = 0
for i in range(1, N+1):
  if S[i] == S[i+S[i]]:
    happy += 1
rl_inner = 0
for i in range(1, N):
  if S[i] == 1 and S[i+1] == -1:
    rl_inner += 1
rl_outer = (1 if S[1] == -1 else 0) + (1 if S[N] == 1 else 0)
if rl_inner+rl_outer > 1:
  happy += min(rl_inner, K)*2
  if rl_inner < K and rl_outer > 1:
    happy += 1
else:
  happy += 1
print(happy)

Submission status: Wrong Answer

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

RL = 0
rl = 0
for i in range(N):
    if i == N-1:
        continue
    if S[i] == "R" and  S[i+1] == "L":
        RL += 1
if S[0] == "L":
    rl += 1
if S[N-1] == "R":
    rl += 1

res = 0
for i in range(N):
    if S[i] == "L":
        if i == 0:
            continue
        if S[i-1] == "L":
            res += 1
    if S[i] == "R":
        if i == N-1:
            continue
        if S[i+1] == "R":
            res += 1
if RL > K:
    res += 2*K
else:
    res += RL
    res += rl//2
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from pprint import pprint

def solve(n, k, s):
    pre = ""
    count = 0
    ans = 0
    for s_i in s:
        if (pre == "L" and s_i == "R") or (pre == "R" and s_i == "L"):
            count += 1
        elif pre == s_i:
            # print(pre, s_i)
            ans += 1
        pre = s_i

    # print(count, ans)
    if count <= 2 and k <= 2:
        ans += k
    elif count <= 2:
        ans += count
    elif k < count -2:
        ans += 2 * k
    elif count -2 <= k <= count:
        ans += 2 * (count - 2) + (k - count + 2) 
    else:
        ans += 2 * (count - 2) + 2
    
    print(ans)

if __name__ == '__main__':
    n, k = map(int, sys.stdin.readline().strip().split(" "))
    s = list(sys.stdin.readline().strip())
    solve(n, k, s)

Submission status: Wrong Answer

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

for i in range(n-1):
    if s[i] == s[i+1]:
        count = count + 1
    else:
        RL = RL + 1

for n in range(k):
    if RL < 2:
        count = count + 1
        break
    else:
        count = count + 2
        RL = RL - 1

print(count)

Submission status: Wrong Answer

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

for i in range(n-1):
    if s[i] == s[i+1]:
        count = count + 1
    else:
        RL = RL + 1

for n in range(k):
    if RL < 2:
        count = count + 1
        break
    else:
        count = count + 2
        RL = RL - 2

print(count)

Submission status: Wrong Answer

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

ch_list=[]
for i in range(1,N):
  if S[i-1]!=S[i]:
    ch_list.append(i)
print(ch_list)
len_ch=len(ch_list)

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

Submission status: Wrong Answer

Solution code:
import sys
from pprint import pprint

def solve(n, k, s):
    pre = ""
    count = 0
    ans = 0
    for s_i in s:
        if (pre == "L" and s_i == "R") or (pre == "R" and s_i == "L"):
            count += 1
        elif pre == s_i:
            # print(pre, s_i)
            ans += 1
        pre = s_i

    # print(count, ans)
    if count <= 2 and k <= 2:
        ans += k
    elif k < count -2:
        ans += 2 * k
    elif count -2 <= k <= count:
        ans += 2 * (count - 2) + (k - count + 2) 
    else:
        ans += 2 * (count - 2) + 2
    
    print(ans)

if __name__ == '__main__':
    n, k = map(int, sys.stdin.readline().strip().split(" "))
    s = list(sys.stdin.readline().strip())
    solve(n, k, s)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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


#Rを最大化
ans = 0
res = 0
c = 0
s = S
while c < K:
    if L:
        ind,x = L.pop()
        c += 1
        s = s[:ind]+"R"*x+s[ind+x:]
    else:
        break
#評価
for i in range(N):
    if s[i] == "L":
        if i == 0:
            continue
        if s[i-1] == "L":
            res += 1
    if s[i] == "R":
        if i == N-1:
            continue
        if s[i+1] == "R":
            res += 1
if ans < res:
    ans = res
#Lを最大化
res = 0
c = 0
s = S
while c < K:
    if R:
        ind,x = R.pop()
        c += 1
        s = s[:ind]+"L"*x+s[ind+x:]
    else:
        break
#評価
for i in range(N):
    if s[i] == "L":
        if i == 0:
            continue
        if s[i-1] == "L":
            res += 1
    if s[i] == "R":
        if i == N-1:
            continue
        if s[i+1] == "R":
            res += 1
if ans < res:
    ans = res
print(ans)

Submission status: Wrong Answer

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

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

for k in range(K):
  i = 2 * k + 1
  if i >= len(l):
    break
  if S[l[i][0]] == "L":
    S[l[i][0] : l[i][1] + 1] = "R" * l[i][2]
  else:
    S[l[i][0] : l[i][1] + 1] = "L" * l[i][2]

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

print(ans)

Submission status: Wrong Answer

Solution code:
def swap_list(S, l, r):
  result = list(S)
  count = r - l + 1
  for i in range(0, count):
    if S[r-i] == "R":
      result[l+i] = "L"
    elif S[r-i] == "L":
      result[l+i] = "R"
    
  return result 

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

for i in range(0, K):
  l_RL_index = -1
  l_LR_index = -1
  r_RL_index = N-1
  r_LR_index = N-1
  for j in range(0, N-1):  
    if S[j] == "R" and S[j+1] == "L":
      if l_LR_index != -1 and l_LR_index < j:
        r_RL_index = j
        break
      elif l_RL_index == -1:
        l_RL_index = j + 1
        j = j + 1 
    elif S[j] == "L" and S[j+1] == "R":
      if l_RL_index != -1 and l_RL_index < j:
        r_LR_index = j
        break
      elif l_LR_index == -1:
        l_LR_index = j + 1
        j = j + 1
    
  if l_RL_index != -1:
    #print ("S = {0}, l_RL = {1}, r_LR = {2}".format(S, l_RL_index, r_LR_index))
    S = swap_list(S, l_RL_index, r_LR_index)
    #print("S_swap = {0}".format(S))
    current = r_LR_index + 1
    break
  elif l_LR_index != -1:
    #print ("S = {0}, l_RL = {1}, r_LR = {2}".format(S, l_LR_index, r_RL_index))
    S = swap_list(S, l_LR_index, r_RL_index)
    #print("S_swap = {0}".format(S))
    current = r_RL_index + 1
    break
  
result = 0
for i in range(0, N):
  if i == 0:
    if S[i] == "R" and S[i+1] == "R":
      result += 1
  elif i == N-1:
    if S[i] == "L" and S[i-1] == "L":
      result += 1
  else:
    if (S[i] == "R" and S[i+1] == "R") or (S[i] == "L" and S[i-1] == "L"):
      result += 1

print(result)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Ss = input()
 
num = Ss.count('RL')*2
if list(Ss)[-1]=='L':
  num += 1
if list(Ss)[0]=='L':
  num += 1
ans = N - max(1, num-2*K)
print(ans)

Submission status: Wrong Answer

Solution code:
def d():
    N, K = map(int,input().split())
    S = list(input())
    x, happiness = 0, 0
    if S[0] == 'R':
        x += 1
    if S[N-1] == 'L':
        x += 1

    for i in range(1, N):
        if S[i] == 'R':
            if S[i-1] == 'L':
                x += 1
            elif S[i-1] == 'R':
                happiness += 1

        if S[i] == 'L' and S[i-1] == 'L':
            happiness += 1

    if K >= x-1:
        happiness += (x - 2) * 2 + 1
    elif K < x-1:
        happiness += K * 2

    print(happiness)


if __name__ == '__main__':
    d()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

#Rを最大化
ans = 0
res = 0
c = 0
s = S
while c < K:
    if L:
        ind,x = L.pop()
        c += 1
        s = s[:ind]+"R"*x+s[ind+x:]
    else:
        break
#評価
for i in range(N):
    if s[i] == "L":
        if i == 0:
            continue
        if s[i-1] == "L":
            res += 1
    if s[i] == "R":
        if i == N-1:
            continue
        if s[i+1] == "R":
            res += 1
if ans < res:
    ans = res
#Lを最大化
res = 0
c = 0
s = S
while c < K:
    if R:
        ind,x = R.pop()
        c += 1
        s = s[:ind]+"L"*x+s[ind+x:]
    else:
        break
#評価
for i in range(N):
    if s[i] == "L":
        if i == 0:
            continue
        if s[i-1] == "L":
            res += 1
    if s[i] == "R":
        if i == N-1:
            continue
        if s[i+1] == "R":
            res += 1
if ans < res:
    ans = res
print(ans)

Submission status: Wrong Answer

Solution code:
def d():
    N, K = map(int,input().split())
    S = list(input())
    x, happiness = 0, 0
    if S[0] == 'R':
        x += 1
    if S[N-1] == 'L':
        x += 1

    for i in range(1, N):
        if S[i] == 'R':
            if S[i-1] == 'L':
                x += 1
            elif S[i-1] == 'R':
                happiness += 1

        if S[i] == 'L' and S[i-1] == 'L':
            happiness += 1

    if K >= x-1:
        happiness += (x - 1) * 2 + 1
    elif K < x-1:
        happiness += K * 2

    print(happiness)


if __name__ == '__main__':
    d()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Ss = input()
 
num = Ss.count('RL')*2
if list(Ss)[-1]=='L':
  if list(Ss)[0]=='L':
    num += 1
  num += 1

ans = N - max(1, num-2*K)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (counter*2 + RL_L + RL_R)

print(max(N-1, ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (RL-counter*2 + RL_L + RL_R)

print(max(N-1,ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
 
num = S.count('RL')
 
ans = N-max(1,(num-K)*2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (RL-counter*2 + RL_L + RL_R)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [1]+list(map(lambda x:-1 if x == "L" else 1, input()))+[-1]

happy = 0
for i in range(1, N+1):
  if S[i] == S[i+S[i]]:
    happy += 1
rl_inner = 0
for i in range(1, N):
  if S[i] == 1 and S[i+1] == -1:
    rl_inner += 1
rl_outer = (1 if S[1] == -1 else 0) + (1 if S[N] == 1 else 0)
happy += min(rl_inner, K)*2
if rl_inner < K and rl_outer > 1:
  happy += 1
print(happy)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (counter*2 + RL_L + RL_R)

print(max(1,ans))