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 = [int(i) for i in input().split()]
s = list(input())
c, cnt = 0, 0
cnt = 0
if s[0] == 'L':
  cnt += 1
if s[-1] == 'R':
  cnt += 1
for i in range(n-1):
  if s[i] == 'R' and s[i+1] == 'L':
    c += 1
if c > 0:
  if c - k >= 0:
    c -= k
  else:
    c = 0
    cnt = 1
print(n - 2*c - cnt)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
s = list(input())
c, r, l = 0, 0, 0
if s[0] == 'L':
  l = 1
if s[-1] == 'R':
  r = 1
for i in range(n-1):
  if s[i] == 'R' and s[i+1] == 'L':
    c += 1
if c - k >= 0:
  c -= k
else:
  c = 0
  r = 0
print(n - r - l - 2*c)

Submission status: Wrong Answer

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

cnt=0
lst=[]
for i in range(len(s)-1):
    if s[i]!=s[i+1]:
        cnt+=1
    else:
        lst.append(cnt)
        cnt=0

lst.append(cnt)

ans=n-sum(lst)-1

lst2=[]
for i in lst:
    for j in range(i//2):
        lst2.append(2)
    
    if i%2==1:
        lst2.append(1)

lst2.sort()
lst2.reverse()

if len(lst2)>k:
    ans+=sum(lst2[:k])
else:
    ans+=sum(lst2)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))

######################################################

N,K=mint()
S=input()
r1=0
r2=0
now=0
tmp='L'
for i in range(N):
    if i<N-1 and S[i]==S[i+1]:
        now+=1
    if tmp=='L' and S[i]=='R':
        if i==0:
            r1+=1
        else:
            r2+=1
    tmp=S[i]
if tmp=='R':
    r1+=1
    r2-=1
now+=2*min(K,r2)
K-=min(K,r2)
now+=min(K,r1)
print(now)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = S[0]
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
answer -= 1
print(answer, r_cnt)
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 >= K:
  print(answer+K*2)
else:
  print(answer+(r_cnt//2+1)*2-1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = S[0]
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
answer -= 1
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 >= K:
  print(answer+K*2)
else:
  print(answer+r_cnt*2-1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = S[0]
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
answer -= 1
print(answer, r_cnt)
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 >= K:
  print(answer+K*2)
else:
  print(answer+r_cnt*2-1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = ''
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 <= K:
  print(answer+r_cnt)
else:
  print(answer+K*2-1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def run():
    N, K = map(int, sysread().split())
    S = input()
    c = S[0]
    checkpoints = 0
    endpoints = 0
    for s in S:
        if c == 'L':
            if s == 'R':
                c = 'R'
                endpoints += 1
            else:
                continue
        elif c == 'R':
            if s == 'L':
                checkpoints += 1
                c = 'L'
            else:
                continue
    #print(checkpoints, endpoints)
    ans = N - 2 * checkpoints
    halfpoint = 0
    if S[0] == 'R':
        endpoints += 1
    else:
        halfpoint += 1

    if S[-1] == 'L':
        endpoints += 1
    else:
        pass
        halfpoint += 1

    ans -= halfpoint
    val = min([checkpoints, endpoints, K])
    checkpoints -= val
    endpoints -= val
    K -= val
    ans += 2 * val

    if K > 0:
        ans += min(halfpoint, K)

    print(ans)

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def run():
    N, K = map(int, sysread().split())
    S = input()
    c = S[0]
    checkpoints = 0
    endpoints = 0
    for s in S:
        if c == 'L':
            if s == 'R':
                c = 'R'
                endpoints += 1
            else:
                continue
        elif c == 'R':
            if s == 'L':
                checkpoints += 1
                c = 'L'
            else:
                continue
    #print(checkpoints, endpoints)
    ans = N - 2 * checkpoints
    halfpoint = 0
    if S[0] == 'R':
        endpoints += 1
    else:
        halfpoint += 1

    if S[-1] == 'L':
        endpoints += 1
    else:
        halfpoint += 1

    ans -= halfpoint
    val = min([checkpoints, endpoints, K])
    checkpoints -= val
    endpoints -= val
    K -= val
    ans += 2 * val

    if K > 0:
        ans += min(halfpoint, K)

    print(ans)

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
from sys import exit
n, k = map(int, input().split())
s = list(input())
if n == 1:
    print(0)
    exit()

L_record = []
R_record = []

if s[0] == "L":
    flg = "R"
else:
    flg = "L"

for i, char in enumerate(s):
    if char == "L" and flg == "R":
        L_record.append([i, 1])
        flg = "L"
    elif char == "L" and flg == "L":
        L_record[-1][1] += 1
    elif char == "R" and flg == "L":
        R_record.append([i, 1])
        flg = "R"
    elif char == "R" and flg == "R":
        R_record[-1][1] += 1

L_record.sort(key=lambda x: x[0])
R_record.sort(key=lambda x: x[0])
#print(L_record, R_record)

if L_record != []:
    if L_record[-1][0] + L_record[-1][1] == n:
        record = L_record.pop()
        L_record = [record] + L_record
if R_record != []:
    if R_record[-1][0] + R_record[-1][1] == n:
        record = R_record.pop()
        R_record = [record] + R_record


point = 0
for i, char in enumerate(s):
    #print(i, char)
    if char == "L" and i-1 >= 0:
        if s[i-1] == "L":
            point += 1
            #print(point)
    if char == "R" and i+1 < n:
        if s[i+1] == "R":
            point += 1
            #print(point)
#print(point)

#L -> R
point_l = point
for i in range(k):
    if L_record != []:
        idx, num = L_record.pop()
        if idx == 0:
            point_l += 1
        elif idx + num == n:
            point_l += 1
        else:
            point_l += 2

point_r = point
for i in range(k):
    if R_record != []:
        idx, num = R_record.pop()
        if idx == 0:
            point_r += 1
        elif idx + num == n:
            point_r += 1
        else:
            point_r += 2
print(max(point_r, point_l))

Submission status: Wrong Answer

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

L_record = []
R_record = []

if s[0] == "L":
    flg = "R"
else:
    flg = "L"

for i, char in enumerate(s):
    if char == "L" and flg == "R":
        L_record.append([i, 1])
        flg = "L"
    elif char == "L" and flg == "L":
        L_record[-1][1] += 1
    elif char == "R" and flg == "L":
        R_record.append([i, 1])
        flg = "R"
    elif char == "R" and flg == "R":
        R_record[-1][1] += 1

L_record.sort(key=lambda x: (x[0]))
R_record.sort(key=lambda x: (x[0]))
#print(L_record, R_record)

if L_record != []:
    if L_record[-1][0] + L_record[-1][1] == n:
        record = L_record.pop()
        L_record = [record] + L_record
if R_record != []:
    if R_record[-1][0] + R_record[-1][1] == n:
        record = R_record.pop()
        R_record = [record] + R_record


point = 0
for i, char in enumerate(s):
    #print(i, char)
    if char == "L" and i-1 >= 0:
        if s[i-1] == "L":
            point += 1
            #print(point)
    if char == "R" and i+1 < n:
        if s[i+1] == "R":
            point += 1
            #print(point)
#print(point)

#L -> R
point_l = point
for i in range(k):
    if L_record != []:
        idx, num = L_record.pop()
        if idx == 0:
            point_l += 1
        elif idx + num == n:
            point_l += 1
        else:
            point_l += 2

point_r = point
for i in range(k):
    if R_record != []:
        idx, num = R_record.pop()
        if idx == 0:
            point_r += 1
        elif idx + num == n:
            point_r += 1
        else:
            point_r += 2
print(max(point_r, point_l))

Submission status: Wrong Answer

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

for i in range(n-3):
    if k == 0:
        break
    if 'RLLR' == ''.join(s[i:i+4]):
        s[i:i+4] = 'RRRR'
        k -= 1
    elif 'LRRL' == ''.join(s[i:i+4]):
        s[i:i+4] = 'LLLL'
        k -= 1

for i in range(n-2):
    if k == 0:
        break
    if 'LRL' == ''.join(s[i:i+3]):
        s[i:i+3] = 'LLL'
        k -= 1
    elif 'RLR' == ''.join(s[i:i+3]):
        s[i:i+3] = 'RRR'
        k -= 1

ans, count, pre = 0, 0, s[0]
for i in range(1, n):
    if s[i] == 'R' and pre == 'R':
        count += 1
    else:
        ans += count
        count = 0
    pre = s[i]
ans += count
count, pre = 0, s[-1]
for i in range(n-2, -1, -1):
    if s[i] == 'L' and pre == 'L':
        count += 1
    else:
        ans += count
        count = 0
    pre = s[i]
ans += count
if k > 0:
    print(ans+1)
else:
    print(ans)

Submission status: Wrong Answer

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

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

ans = n - 2*rl - end

if end == 0:
    if k >= rl:
        ans += 2*rl
        ans -= 1
    else:
        ans += 2*k
elif end == 1:
    if k >= rl:
        ans += 2*rl
    else:
        ans += 2*k
elif end == 2:
    if k == rl:
        ans += 2*rl
        ans -= 1
    elif k >= rl+1:
        ans += 2*rl
        ans += 1
    else:
        ans += 2*k
print(ans)

Submission status: Wrong Answer

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

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

ans = n - 2*rl - end

if end == 0:
    if k >= rl:
        ans += 2*rl
        ans -= 1
    else:
        ans += 2*k
elif end == 1:
    if k == rl:
        ans += 2*rl
        ans -= 1
    elif k > rl:
        ans += 2*rl
    else:
        ans += 2*k
elif end == 2:
    if k == rl:
        ans += 2*rl
        ans -= 1
    elif k >= rl+1:
        ans += 2*rl
        ans += 1
    else:
        ans += 2*k
print(ans)

Submission status: Wrong Answer

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

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

ans = n - 2*rl - end

if end == 0:
    if k >= rl:
        ans += 2*rl
        ans -= 1
    else:
        ans += 2*k
elif end == 1:
    if k >= rl:
        ans += 2*rl
        ans -= 1
    else:
        ans += 2*k
elif end == 2:
    if k == rl:
        ans += 2*rl
        ans -= 1
    elif k >= rl+1:
        ans += 2*rl
        ans += 1
    else:
        ans += 2*k
print(ans)

Submission status: Wrong Answer

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

RL=0
LR=0

fo=0

if n<3:
  if n==1:
    print(0)
  else:
    print(1)
    
else:
  for i in range(n):
    if s[i]=='R' and i<n-1:
      if s[i+1]=='R':
        fo+=1
    if s[i]=='L' and 0<i:
      if s[i-1]=='L':
        fo+=1
        
    if i<n-1:
      if s[i]=='R' and s[i+1]=='L':
        RL+=1
      elif s[i]=='L' and s[i+1]=='R':
        LR+=1
        
  si=min(k,RL,LR)
  no=k-si
  LR_rest=LR-si
  RL_rest=RL-si
  if no==0:
    ans=fo+si*2
  else:
    ans=fo+si*2
    if s[0]=='L':
      if LR_rest>0:
        ans+=1
        LR_rest-=1
        no-=1
    if no>0:
      if s[-1]=='R':
        if LR_rest>0:
          ans+=1
  print(ans)

Submission status: Wrong Answer

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

RL=0
LR=0

fo=0

if n<3:
  if n==1:
    print(0)
  else:
    print(1)
    
else:
  for i in range(n):
    if s[i]=='R' and i<n-1:
      if s[i+1]=='R':
        fo+=1
    if s[i]=='L' and 0<i:
      if s[i-1]=='L':
        fo+=1
        
    if i<n-1:
      if s[i]=='R' and s[i+1]=='L':
        RL+=1
      elif s[i]=='L' and s[i+1]=='R':
        LR+=1
        
  si=min(k,RL,LR)
  no=k-si
  LR_rest=LR-si
  RL_rest=RL-si
  if no==0:
    ans=fo+si*2
  else:
    ans=fo+si*2
    if s[0]=='L':
      if LR_rest>0:
        ans+=1
        LR_rest-=1
    if s[-1]=='R':
      if LR_rest>0:
        ans+=1
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,k = list(map(int, input().split()))
s = input()
v = 0
l = [[s[0],1]]
for c in s[1:]:
    if l[-1][0]!=c:
        l.append([c,1])
    else:
        l[-1][1] += 1
vs = []
if len(l)==1:
    ans = n-1
else:
    vals = []
    l.insert(0, ("R",0))
    l.append(("L",0))
    for i in range(len(l)-1):
        if l[i][0]=="R" and l[i+1][0]=="L":
            if i==0 or i==len(l)-2:
                vals.append(1)
            else:
                vals.append(2)
    vals.sort()
    ans = n - sum(vals[:(len(vals)-k)])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,k = list(map(int, input().split()))
s = input()
v = 0
l = [[s[0],1]]
for c in s[1:]:
    if l[-1][0]!=c:
        l.append([c,1])
    else:
        l[-1][1] += 1
vs = []
if len(l)==1:
    ans = n-1
else:
    vals = []
    if l[0][0]=="L":
        vals.append(1)
    if l[-1][0]=="R":
        vals.append(1)
    for i in range(len(l)-1):
        if l[i][0]=="R" and l[i+1][0]=="L":
            vals.append(2)
    vals.sort()
    ans = n - sum(vals[:(len(vals)-k)])
print(ans)

Submission status: Wrong Answer

Solution code:
(str_len,cng_cnt) = input().split(" ")
str_input = list(input())


cng_cnt= int(cng_cnt)
tmp1=""
cnt=0
cnt2=0
for st in str_input:
    if(tmp1 == st ):
      cnt = cnt+1
    else:
      cnt2= cnt2 + 1
      
    tmp1=st

if (cng_cnt > cnt2 ):
	c_up = cng_cnt * 2 
else:
	c_up= (cnt2 * 2) + (cng_cnt - cnt2)
ans =cnt + c_up

print(ans)

Submission status: Wrong Answer

Solution code:
(str_len,cng_cnt) = input().split(" ")
str_input = list(input())


cng_cnt= int(cng_cnt)
tmp1=""
cnt=0
cnt2=0
for st in str_input:
    if(tmp1 == st ):
      cnt = cnt+1
    else:
      cnt2= cnt2 + 1
      
    tmp1=st

if (cng_cnt < cnt2 ):
	c_up = cng_cnt * 2 
else:
	c_up= (cnt2 * 2) + (cng_cnt - cnt2)
ans =cnt + c_up

print(ans)

Submission status: Wrong Answer

Solution code:
(str_len,cng_cnt) = input().split(" ")
str_input = list(input())


print(str_len)
print(cng_cnt)
cng_cnt= int(cng_cnt)
#print(asd)
tmp1=""
cnt=0
cnt2=0
for st in str_input:
    if(tmp1 == st ):
      cnt = cnt+1
    else:
      cnt2= cnt2 + 1
      
    tmp1=st

if (cng_cnt < cnt2 ):
	c_up = cng_cnt * 2 
else:
	c_up= (cnt2 * 2) + (cng_cnt - cnt2)
ans =cnt + c_up

print(ans)

Submission status: Wrong Answer

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    '''
    RL...LL 0
    RL...RR 0
    RL...RL 0

    RL...LR +2
    LR...RL +2

    RL      +1

    13 3
    LRRLRLRRLRLLR
    '''
    N, K = read_ints()
    S = ['R']
    S.extend(input())
    S.append('L')
    X, Y = 0, 0
    for i in range(N+1):
        if S[i:i+2] == ['R', 'L']:
            if i == 0 or i == N:
                Y += 1
            else:
                X += 1
    unhappy = Y+2*X
    happy = N-unhappy
    if X <= K:
        happy += 2*X
        K -= X
        if K > 0 and Y > 0:
            happy += min(K, Y)
    else:
        happy += 2*K
    return happy


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    '''
    RL...LL 0
    RL...RR 0
    RL...RL 0

    RL...LR +2
    LR...RL +2

    RL      +1

    13 3
    LRRLRLRRLRLLR
    '''
    N, K = read_ints()
    S = ['R']
    S.extend(input())
    S.append('L')
    X, Y = 0, 0
    if S[0:2] == ['R', 'L']:
        Y += 1
    if S[-2:] == ['R', 'L']:
        Y += 1
    for i in range(1, N):
        if S[i:i+2] == ['R', 'L']:
            X += 1
    unhappy = Y+2*X
    happy = N-unhappy
    if X <= K:
        happy += 2*X
        K -= X
        if K > 0 and Y > 0:
            happy += 1
    else:
        happy += 2*K
    return happy


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    '''
    RL...LL 0
    RL...RR 0
    RL...RL 0

    RL...LR +2
    LR...RL +2

    RL      +1

    13 3
    LRRLRLRRLRLLR
    '''
    N, K = read_ints()
    S = ['R']
    S.extend(input())
    S.append('L')
    X, Y = 0, 0
    if S[0:2] == ['R', 'L']:
        Y += 1
    if S[-2:] == ['R', 'L']:
        Y += 1
    for i in range(1, N):
        if S[i:i+2] == ['R', 'L']:
            X += 1
    unhappy = Y+2*X
    happy = N-unhappy
    if X <= K:
        happy += 2*X
        K -= X
        if K > 0 and Y > 0:
            happy += min(K, Y)
    else:
        happy += 2*K
    return happy


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
edge,inside,cnt,on = 0,0,0,0
if s[0]=='L':
  edge = 1
else:
  on = 1
if s[-1]=='L':
  edge = 1
temp = ''
for i in range(n):
  if temp=='L' and s[i]=='R':
    if on:
      inside += 1
    else:
      on = 1
  elif temp==s[i]:
    cnt += 1
  temp = s[i]
if inside>=k:
  print(min(k*2+cnt,n-1))
else:
  print(min(inside*2+edge+cnt,n-1))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
edge,inside,cnt,ix = 0,0,0,0
if s[0]=='R' or s[-1]=='R':
  edge += 1
temp = ''
#print(ix)
s += 'L'
for i in range(n+1):
  if i!=n:
    if temp=='R' and s[i]=='L':
      inside += 1
    elif temp==s[i]:
      cnt += 1
  temp = s[i]
print(inside,cnt,edge)
if inside>=k:
  print(min(k*2+cnt,n-1))
else:
  print(min(inside*2+edge+cnt,n-1))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
edge,inside,cnt,ix = 0,0,0,0
if s[0]=='R' or s[-1]=='R':
  edge += 1
temp = ''
#print(ix)
s += 'L'
for i in range(n+1):
  if i!=n:
    if temp=='R' and s[i]=='L':
      inside += 1
    elif temp==s[i]:
      cnt += 1
  temp = s[i]
#print(inside,cnt,edge)
if inside>=k:
  print(k*2+cnt)
else:
  print(inside*2+edge+cnt)

Submission status: Wrong Answer

Solution code:
import sys


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

lcnt=-1
rcnt=-1

rr=[0]*n
ll=[0]*n
sim=""
for si in s:
#    print(si)
    if si=="R" and sim=="R":
        rr[rcnt]+=1
    elif si=="R" and sim!="R":
        rcnt+=1
        rr[rcnt]=1
    if si=="L" and sim=="L":
        ll[lcnt]+=1
    elif si=="L" and sim!="L":
        lcnt+=1
        ll[lcnt]=1
    sim=si
        
#print(ll)
#print(rr)

ll=[i for i in ll if i!=0]
rr=[i for i in rr if i!=0]
#print(ll)
#print(rr)

maxlen=max(len(ll),len(rr))
if maxlen<=k:
    print(n-1)
    sys.exit()
    
lr=[[0]*2 for i in range(maxlen)]
if len(rr)>len(ll):
    for i in range(len(rr)):
        lr[i][1]=rr[i]
        if i <len(ll):
            lr[i][0]=ll[i]
else:
    for i in range(len(rr)):
        lr[i][1]=ll[i]
        if i <len(rr):
            lr[i][0]=rr[i]

#lr.sort(reverse=True)
#print(lr)
#print(lr)    
for i in range(k):
    lr[i][0]+=lr[i][1]
    lr[i][1]=0
#print(lr)
for i in range(maxlen-1):
    if lr[i][0]!=0 and lr[i][1]==0 :
        lr[i+1][0]+=lr[i][0]
        lr[i][0]=0
#print(lr)

icnt=0
for i in range(maxlen):
    icnt+=max(lr[i][0]-1,0)+max(lr[i][1]-1,0)
    
print(icnt)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
temp = ''
ans,cnt = 0,0
for l in s:
  if temp=='L' and l=='R':
    cnt += 1
  elif temp==l:
    ans += 1
  temp = l
print(min(min(cnt,k)*2+ans,n-1))

Submission status: Wrong Answer

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

rights = []
lefts = []

for i in range(N):
    if i == 0:
        if S[i] == "L":
            now = "L"
            start = i
        else:
            now = "R"
            start = i
    else:
        if S[i] != now:
            end = i
            if now == "R":
                rights.append((start, end))
            else:
                lefts.append((start, end))
            start = i
            now = S[i]
            if i == N-1:
                if now == "R":
                    rights.append((i, i+1))
                else:
                    lefts.append((i, i+1))

n_inner_left = 0
n_inner_right = 0
for left in lefts:
    if (0 not in left) and (N not in left):
        n_inner_left += 1
for right in rights:
    if (0 not in right) and (N not in right):
        n_inner_right += 1
# unhappy
total = 0
if S[0] == "L":
    total += 1
if S[N-1] == "R":
    total += 1
for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        total += 2
rest = K
if n_inner_left > n_inner_right:
    reduce_2 = min(K, n_inner_left)
    total -= 2*reduce_2
    rest -= reduce_2
    if S[0] == "L" and rest > 0:
        total -= 1
elif n_inner_left < n_inner_right:
    reduce_2 = min(K, n_inner_right)
    total -= 2*reduce_2
    rest -= reduce_2
    if S[N-1] == "R" and rest > 0:
        total -= 1
else:
    reduce_2 = min(K, n_inner_left)
    total -= 2*reduce_2
    rest -= reduce_2
    if rest > 0:
        if S[0] == "L" or S[N-1] == "R":
            total -= 1

print(N-total)

Submission status: Wrong Answer

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

rights = []
lefts = []

for i in range(N):
    if i == 0:
        if S[i] == "L":
            now = "L"
            start = i
        else:
            now = "R"
            start = i
    else:
        if S[i] != now:
            end = i
            if now == "R":
                rights.append((start, end))
            else:
                lefts.append((start, end))
            start = i
            now = S[i]
            if i == N-1:
                if now == "R":
                    rights.append((i, i+1))
                else:
                    lefts.append((i, i+1))

n_inner_left = 0
n_inner_right = 0
for left in lefts:
    if (0 not in left) and (N not in left):
        n_inner_left += 1
for right in rights:
    if (0 not in right) and (N not in right):
        n_inner_right += 1
# unhappy
total = 0
if S[0] == "L":
    total += 1
if S[N-1] == "R":
    total += 1
for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        total += 2
rest = K
reduce_2 = min(K, max(n_inner_left, n_inner_right))
total -= 2 * reduce_2
rest -= reduce_2
if rest > 0:
    if S[0] == "L" or S[N-1] == "R":
        total -= 1
print(N-total)

Submission status: Wrong Answer

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

rights = []
lefts = []

for i in range(N):
    if i == 0:
        if S[i] == "L":
            now = "L"
            start = i
        else:
            now = "R"
            start = i
    else:
        if S[i] != now:
            end = i
            if now == "R":
                rights.append((start, end))
            else:
                lefts.append((start, end))
            start = i
            now = S[i]
            if i == N-1:
                if now == "R":
                    rights.append((i, i+1))
                else:
                    lefts.append((i, i+1))

n_inner_left = 0
n_inner_right = 0
for left in lefts:
    if (0 not in left) and (N not in left):
        n_inner_left += 1
for right in rights:
    if (0 not in right) and (N not in right):
        n_inner_right += 1
# unhappy
total = 0
if S[0] == "L":
    total += 1
if S[N-1] == "R":
    total += 1
for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        total += 2
rest = K
reduce_2 = min(K, max(n_inner_left, n_inner_right))
total -= 2 * reduce_2
rest -= reduce_2
if rest > 0:
    total -= 1
print(N-total)

Submission status: Wrong Answer

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

rights = []
lefts = []

for i in range(N):
    if i == 0:
        if S[i] == "L":
            now = "L"
            start = i
        else:
            now = "R"
            start = i
    else:
        if S[i] != now:
            end = i
            if now == "R":
                rights.append((start, end))
            else:
                lefts.append((start, end))
            start = i
            now = S[i]
            if i == N-1:
                if now == "R":
                    rights.append((i, i+1))
                else:
                    lefts.append((i, i+1))

n_inner_left = 0
n_inner_right = 0
for left in lefts:
    if (0 not in left) and (N not in left):
        n_inner_left += 1
for right in rights:
    if (0 not in right) and (N not in right):
        n_inner_right += 1
# unhappy
total = 0
if S[0] == "L":
    total += 1
if S[N-1] == "R":
    total += 1
for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        total += 2

reduce_2 = min(K, max(n_inner_left, n_inner_right))
total -= 2 * reduce_2
print(N-total)

Submission status: Wrong Answer

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

s_=''
S_=[]

if len(set(S)) == 1:
    ans=0
else:
    for s in S:
        if s != s_:
            S_.append(s)
            s_=s

        else:
            S_[-1]+=s

    change_str_dict={'R':'L','L':'R'}
    change_str=change_str_dict[S_[1][0]]

    for m in range(1,min(2*M,len(S_)),2):
        S_[m]=change_str*len(S_[m])

    S_=''.join(S_)
    # print(S_)

    s_=S_[0]
    ans=0
    for s in S_[1:]:
        if s == s_:
            ans+=1
        else:
            s_=s

print(ans)

Submission status: Wrong Answer

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

unhappiness = 0
m1, m2 = 0, 0

if s[0] == 'L':
    unhappiness += 1
if s[-1] == 'R':
    unhappiness += 1
i = 0
while i < n and s[i] == 'L':
    i += 1
while i < n:
    h = i
    while i < n and s[i] == 'R':
        i += 1
    if i == n:
        break
    while i < n and s[i] == 'L':
        i += 1
    if h != 0 and i != n:
        m2 += 1
    elif h != 0 or i != n:
        m1 += 1
    unhappiness += 2

if  k <= m2:
    unhappiness -= 2 * k
else:
    unhappiness -= 2 * m2
    unhappiness -= m1
    if k - m2 - m1 > 0:
        unhappiness = 1
print(n-unhappiness)